TZTimestamp.st
author Claus Gittinger <cg@exept.de>
Fri, 07 Nov 2014 18:49:14 +0100
changeset 16966 7d549c75105f
parent 16954 e13618117ebc
child 16976 34cb1703d013
permissions -rw-r--r--
class: TZTimestamp added: #asLocalTimestamp #asTZTimestamp: #isLocalTimestamp #timeInfo comment/format in: #documentation #printISO8601On:

"
 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') negated ) 

    [author:]
        Claus Gittinger

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

!TZTimestamp methodsFor:'accessing'!

isLocalTimestamp
    ^ false
!

timeInfo
    "fake it"

    |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
! !

!TZTimestamp class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.2 2014-11-07 17:49:14 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.2 2014-11-07 17:49:14 cg Exp $'
! !