TZTimestamp.st
author ca
Thu, 13 Nov 2014 13:55:02 +0100
changeset 17050 f421040c58ae
parent 17019 15fb22ffb5fe
child 17103 d390437c201f
permissions -rw-r--r--
class: TZTimestamp added: #storeStringClass storeString: save as Timestamp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2014 by eXept Software AG
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic' }"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Timestamp subclass:#TZTimestamp
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'utcOffset'
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'Magnitude-Time'
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!TZTimestamp class methodsFor:'documentation'!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
copyright
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 COPYRIGHT (c) 2014 by eXept Software AG
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
              All Rights Reserved
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 This software is furnished under a license and may be used
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 hereby transferred.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
documentation
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
    This class represents time values in milliSeconds starting some time in the past,
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    which were created in another (explicit) timezone.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    Internally, they keep the milliseconds based on UTC time (just like the other timestamps),
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    so the time values can be compared easily.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    However, wehn printed, the original timezone information is taken into account.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    Also Note:
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
        On UNIX, osTime can only hold dates between 1970-01-01T00:00:00Z and 2038-01-19T00:00:00Z
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
        However, timestamp instances can now hold negative osTime values (which are timestamps
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
        before 1.1.1970 and greater than 4294967295 (2^32-1) for timestamps after 2038-01-19.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    50
    [example:]
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    51
      the current time as local time:
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    52
        Transcript showCR:Timestamp now
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    53
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    54
      the current time as utc time:       
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    55
        Transcript showCR:UtcTimestamp now
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    56
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    57
      same:
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    58
        Transcript showCR:Timestamp now asUtcTimestamp
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    59
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    60
      the current time in NewYork:         
16976
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
    61
        Transcript showCR:( Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST')) 
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    62
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    [author:]
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
        Claus Gittinger
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    [See also:]
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
        Timestamp UtcTimestamp Time Date
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
"
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
! !
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
!TZTimestamp methodsFor:'accessing'!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    73
isLocalTimestamp
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    74
    ^ false
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    75
!
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    76
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    77
timeInfo
16984
46430aef9bec class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16980
diff changeset
    78
    "fake it. Convert to utc, ask OS for the info, then convert back.
46430aef9bec class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16980
diff changeset
    79
     This is returns wrong info for weekday and we have to compensate again.
46430aef9bec class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16980
diff changeset
    80
     Also, it (currently) only works for timestamps after the epoch"
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    81
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    82
    |ti|
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    83
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    84
    "/ utcOffset negative: east of GMT
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    85
    ti := OperatingSystem computeUTCTimeAndDateFrom:(osTime - (utcOffset * 1000)).
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    86
    ti utcOffset:utcOffset.
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    87
    ^ ti
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    88
!
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
    89
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
utcOffset
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    "return the difference between UTC (Greenwich Mean Time) and the local time in seconds.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
     If daylight saving time applies to ourself, take that into account.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
     Add utcOffset to convert from local time to UTC time.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
     Subtract utcOffset to convert from UTC time to local time.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
     If utcOffset is negative, the local timezone is east of Greenwich.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
     If utcOffset is positive, the local timezone is west of Greenwich."
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    ^  utcOffset
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
utcOffset:seconds
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    "set the difference between UTC (Greenwich Mean Time) and the local time in seconds.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
     If daylight saving time applies to ourself, take that into account.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
     Add utcOffset to convert from local time to UTC time.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
     Subtract utcOffset to convert from UTC time to local time.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
     If utcOffset is negative, the local timezone is east of Greenwich.
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
     If utcOffset is positive, the local timezone is west of Greenwich."
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    utcOffset := seconds
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
! !
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   116
!TZTimestamp methodsFor:'converting'!
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   117
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   118
asLocalTimestamp
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   119
    "return a local timestamp, representing the same time as the receiver"
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   120
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   121
    ^ Timestamp fromOSTime:osTime.
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   122
!
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   123
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   124
asTZTimestamp:utcOffsetArg
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   125
    "return a timestamp in a given timezone, representing the same time as the receiver"
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   126
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   127
    utcOffset = utcOffsetArg ifTrue:[
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   128
        ^ self.
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   129
    ].
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   130
    ^ super asTZTimestamp:utcOffsetArg
16976
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   131
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   132
    "what is the time now in NewYork?
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   133
     Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') 
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   134
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   135
     what is the time now in Stuttgart?
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   136
     Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'MEZ')  
34cb1703d013 class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16966
diff changeset
   137
    "
16966
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   138
! !
7d549c75105f class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16954
diff changeset
   139
17019
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   140
!TZTimestamp methodsFor:'private'!
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   141
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   142
speciesNew
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   143
    ^ self species basicNew 
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   144
        utcOffset:utcOffset;
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   145
        yourself
17050
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   146
!
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   147
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   148
storeStringClass
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   149
    ^ Timestamp
17019
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   150
! !
15fb22ffb5fe class: TZTimestamp
Claus Gittinger <cg@exept.de>
parents: 16984
diff changeset
   151
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
!TZTimestamp class methodsFor:'documentation'!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
version
17050
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   155
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.7 2014-11-13 12:55:02 ca Exp $'
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
!
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
version_CVS
17050
f421040c58ae class: TZTimestamp
ca
parents: 17019
diff changeset
   159
    ^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.7 2014-11-13 12:55:02 ca Exp $'
16954
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
! !
e13618117ebc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161