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