TZTimestamp.st
author Claus Gittinger <cg@exept.de>
Mon, 10 Nov 2014 17:56:51 +0100
changeset 17019 15fb22ffb5fe
parent 16984 46430aef9bec
child 17050 f421040c58ae
permissions -rw-r--r--
class: TZTimestamp added: #speciesNew

"
 COPYRIGHT (c) 2014 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Timestamp subclass:#TZTimestamp
	instanceVariableNames:'utcOffset'
	classVariableNames:''
	poolDictionaries:''
	category:'Magnitude-Time'
!

!TZTimestamp class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2014 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    This class represents time values in milliSeconds starting some time in the past,
    which were created in another (explicit) timezone.
    Internally, they keep the milliseconds based on UTC time (just like the other timestamps),
    so the time values can be compared easily.
    However, wehn printed, the original timezone information is taken into account.

    Also Note:
        On UNIX, osTime can only hold dates between 1970-01-01T00:00:00Z and 2038-01-19T00:00:00Z
        However, timestamp instances can now hold negative osTime values (which are timestamps
        before 1.1.1970 and greater than 4294967295 (2^32-1) for timestamps after 2038-01-19.

    [example:]
      the current time as local time:
        Transcript showCR:Timestamp now

      the current time as utc time:       
        Transcript showCR:UtcTimestamp now

      same:
        Transcript showCR:Timestamp now asUtcTimestamp

      the current time in NewYork:         
        Transcript showCR:( Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST')) 

    [author:]
        Claus Gittinger

    [See also:]
        Timestamp UtcTimestamp Time Date
"
! !

!TZTimestamp methodsFor:'accessing'!

isLocalTimestamp
    ^ false
!

timeInfo
    "fake it. Convert to utc, ask OS for the info, then convert back.
     This is returns wrong info for weekday and we have to compensate again.
     Also, it (currently) only works for timestamps after the epoch"

    |ti|

    "/ utcOffset negative: east of GMT
    ti := OperatingSystem computeUTCTimeAndDateFrom:(osTime - (utcOffset * 1000)).
    ti utcOffset:utcOffset.
    ^ ti
!

utcOffset
    "return the difference between UTC (Greenwich Mean Time) and the local time in seconds.
     If daylight saving time applies to ourself, take that into account.

     Add utcOffset to convert from local time to UTC time.
     Subtract utcOffset to convert from UTC time to local time.

     If utcOffset is negative, the local timezone is east of Greenwich.
     If utcOffset is positive, the local timezone is west of Greenwich."

    ^  utcOffset
!

utcOffset:seconds
    "set the difference between UTC (Greenwich Mean Time) and the local time in seconds.
     If daylight saving time applies to ourself, take that into account.

     Add utcOffset to convert from local time to UTC time.
     Subtract utcOffset to convert from UTC time to local time.

     If utcOffset is negative, the local timezone is east of Greenwich.
     If utcOffset is positive, the local timezone is west of Greenwich."

    utcOffset := seconds
! !

!TZTimestamp methodsFor:'converting'!

asLocalTimestamp
    "return a local timestamp, representing the same time as the receiver"

    ^ Timestamp fromOSTime:osTime.
!

asTZTimestamp:utcOffsetArg
    "return a timestamp in a given timezone, representing the same time as the receiver"

    utcOffset = utcOffsetArg ifTrue:[
        ^ self.
    ].
    ^ super asTZTimestamp:utcOffsetArg

    "what is the time now in NewYork?
     Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') 

     what is the time now in Stuttgart?
     Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'MEZ')  
    "
! !

!TZTimestamp methodsFor:'private'!

speciesNew
    ^ self species basicNew 
        utcOffset:utcOffset;
        yourself
! !

!TZTimestamp class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.6 2014-11-10 16:56:51 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.6 2014-11-10 16:56:51 cg Exp $'
! !