16954
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2014 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
17423
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
16954
|
16 |
Timestamp subclass:#TZTimestamp
|
|
17 |
instanceVariableNames:'utcOffset'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Magnitude-Time'
|
|
21 |
!
|
|
22 |
|
|
23 |
!TZTimestamp class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2014 by eXept Software AG
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
This class represents time values in milliSeconds starting some time in the past,
|
|
42 |
which were created in another (explicit) timezone.
|
|
43 |
Internally, they keep the milliseconds based on UTC time (just like the other timestamps),
|
|
44 |
so the time values can be compared easily.
|
17134
|
45 |
However, when printed, the original timezone information is taken into account.
|
16954
|
46 |
|
|
47 |
Also Note:
|
|
48 |
On UNIX, osTime can only hold dates between 1970-01-01T00:00:00Z and 2038-01-19T00:00:00Z
|
17134
|
49 |
However, timestamp instances can hold negative osTime values (which are timestamps
|
16954
|
50 |
before 1.1.1970 and greater than 4294967295 (2^32-1) for timestamps after 2038-01-19.
|
|
51 |
|
16966
|
52 |
[example:]
|
|
53 |
the current time as local time:
|
|
54 |
Transcript showCR:Timestamp now
|
|
55 |
|
|
56 |
the current time as utc time:
|
17134
|
57 |
Transcript showCR:UtcTimestamp now.
|
16966
|
58 |
same:
|
17134
|
59 |
Transcript showCR:Timestamp now asUtcTimestamp.
|
16966
|
60 |
|
|
61 |
the current time in NewYork:
|
17134
|
62 |
Transcript showCR:( Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') )
|
|
63 |
same
|
|
64 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'EST' )
|
|
65 |
same
|
|
66 |
Transcript showCR:( Timestamp now asTZTimestamp:(5*3600) )
|
|
67 |
|
|
68 |
what is the current time in Berlin:
|
|
69 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'MEZ' )
|
|
70 |
and the date:
|
|
71 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'MEZ' ) asDate
|
|
72 |
and the date in Tokio:
|
|
73 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'JST' ) asDate
|
|
74 |
and the date in Hawai:
|
|
75 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'HAST' ) asDate
|
16966
|
76 |
|
16954
|
77 |
[author:]
|
|
78 |
Claus Gittinger
|
|
79 |
|
|
80 |
[See also:]
|
|
81 |
Timestamp UtcTimestamp Time Date
|
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
17423
|
85 |
!TZTimestamp class methodsFor:'instance creation'!
|
|
86 |
|
|
87 |
fromDate:aDate andTime:aTime utcOffset:utcOffsetSeconds
|
|
88 |
"return an instance of the receiver, initialized from a time and a date
|
|
89 |
object.
|
|
90 |
See also `Timestamp now' and other protocol inherited
|
|
91 |
from my superclass."
|
|
92 |
|
|
93 |
^ (self
|
|
94 |
UTCYear:aDate year
|
|
95 |
month:aDate month
|
|
96 |
day:aDate day
|
|
97 |
hour:aTime hours
|
|
98 |
minute:aTime minutes
|
|
99 |
second:aTime seconds + utcOffsetSeconds
|
|
100 |
millisecond:aTime milliseconds)
|
|
101 |
utcOffset:utcOffsetSeconds
|
|
102 |
|
|
103 |
"
|
|
104 |
Timestamp fromDate:(Date today) andTime:(Time now)
|
|
105 |
Timestamp fromDate:(Date today) andTime:(Time nowWithMilliseconds)
|
|
106 |
|
|
107 |
Timestamp fromDate:(Date today plusDays:1) andTime:(Time now)
|
|
108 |
Timestamp now
|
|
109 |
"
|
|
110 |
! !
|
|
111 |
|
16954
|
112 |
!TZTimestamp methodsFor:'accessing'!
|
|
113 |
|
16966
|
114 |
isLocalTimestamp
|
|
115 |
^ false
|
|
116 |
!
|
|
117 |
|
|
118 |
timeInfo
|
16984
|
119 |
"fake it. Convert to utc, ask OS for the info, then convert back.
|
|
120 |
This is returns wrong info for weekday and we have to compensate again.
|
|
121 |
Also, it (currently) only works for timestamps after the epoch"
|
16966
|
122 |
|
|
123 |
|ti|
|
|
124 |
|
|
125 |
"/ utcOffset negative: east of GMT
|
18453
|
126 |
ti := OperatingSystem computeUTCTimeAndDateFrom:(osTime - ((utcOffset ? 0) * 1000)).
|
|
127 |
ti utcOffset:(utcOffset ? 0).
|
16966
|
128 |
^ ti
|
|
129 |
!
|
|
130 |
|
16954
|
131 |
utcOffset
|
|
132 |
"return the difference between UTC (Greenwich Mean Time) and the local time in seconds.
|
|
133 |
If daylight saving time applies to ourself, take that into account.
|
|
134 |
|
|
135 |
Add utcOffset to convert from local time to UTC time.
|
|
136 |
Subtract utcOffset to convert from UTC time to local time.
|
|
137 |
|
|
138 |
If utcOffset is negative, the local timezone is east of Greenwich.
|
|
139 |
If utcOffset is positive, the local timezone is west of Greenwich."
|
|
140 |
|
18453
|
141 |
^ utcOffset ? 0
|
16954
|
142 |
!
|
|
143 |
|
|
144 |
utcOffset:seconds
|
|
145 |
"set the difference between UTC (Greenwich Mean Time) and the local time in seconds.
|
|
146 |
If daylight saving time applies to ourself, take that into account.
|
|
147 |
|
|
148 |
Add utcOffset to convert from local time to UTC time.
|
|
149 |
Subtract utcOffset to convert from UTC time to local time.
|
|
150 |
|
|
151 |
If utcOffset is negative, the local timezone is east of Greenwich.
|
|
152 |
If utcOffset is positive, the local timezone is west of Greenwich."
|
|
153 |
|
17103
|
154 |
utcOffset := seconds asInteger.
|
|
155 |
|
|
156 |
"Modified: / 20-11-2014 / 13:05:48 / sr"
|
16954
|
157 |
! !
|
|
158 |
|
16966
|
159 |
!TZTimestamp methodsFor:'converting'!
|
|
160 |
|
|
161 |
asLocalTimestamp
|
|
162 |
"return a local timestamp, representing the same time as the receiver"
|
|
163 |
|
|
164 |
^ Timestamp fromOSTime:osTime.
|
|
165 |
!
|
|
166 |
|
|
167 |
asTZTimestamp:utcOffsetArg
|
|
168 |
"return a timestamp in a given timezone, representing the same time as the receiver"
|
|
169 |
|
|
170 |
utcOffset = utcOffsetArg ifTrue:[
|
|
171 |
^ self.
|
|
172 |
].
|
|
173 |
^ super asTZTimestamp:utcOffsetArg
|
16976
|
174 |
|
|
175 |
"what is the time now in NewYork?
|
|
176 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST')
|
|
177 |
|
|
178 |
what is the time now in Stuttgart?
|
|
179 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'MEZ')
|
|
180 |
"
|
16966
|
181 |
! !
|
|
182 |
|
17019
|
183 |
!TZTimestamp methodsFor:'private'!
|
|
184 |
|
|
185 |
speciesNew
|
|
186 |
^ self species basicNew
|
18450
|
187 |
utcOffset:(utcOffset ? 0);
|
17019
|
188 |
yourself
|
17050
|
189 |
!
|
|
190 |
|
|
191 |
storeStringClass
|
|
192 |
^ Timestamp
|
17019
|
193 |
! !
|
|
194 |
|
16954
|
195 |
!TZTimestamp class methodsFor:'documentation'!
|
|
196 |
|
|
197 |
version
|
18453
|
198 |
^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.12 2015-06-06 12:42:27 cg Exp $'
|
16954
|
199 |
!
|
|
200 |
|
|
201 |
version_CVS
|
18453
|
202 |
^ '$Header: /cvs/stx/stx/libbasic/TZTimestamp.st,v 1.12 2015-06-06 12:42:27 cg Exp $'
|
16954
|
203 |
! !
|
|
204 |
|