author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:55:17 +0200 | |
changeset 24417 | 03b083548da2 |
parent 24404 | 0681f4ab2d53 |
child 24597 | 435fc9fbc45e |
permissions | -rw-r--r-- |
30 | 1 |
" |
2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
17006 | 3 |
All Rights Reserved |
30 | 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 |
" |
|
5376
c2ef7d5e25ca
oops - was not able to reread my own storeString.
Claus Gittinger <cg@exept.de>
parents:
5292
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
c2ef7d5e25ca
oops - was not able to reread my own storeString.
Claus Gittinger <cg@exept.de>
parents:
5292
diff
changeset
|
13 |
|
17422
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
15 |
|
8246 | 16 |
AbstractTime subclass:#Timestamp |
22834 | 17 |
instanceVariableNames:'osTime additionalPicoseconds' |
19923 | 18 |
classVariableNames:'Epoch MaxOSTime MinOSTime TimeZoneInfo' |
17005 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Time' |
|
30 | 21 |
! |
22 |
||
11993 | 23 |
Object subclass:#TimestampBuilderAbstract |
17005 | 24 |
instanceVariableNames:'year month day hour minute second millisecond isUtcTime |
22873 | 25 |
hasTimezone yearAlreadyRead utcOffset picos' |
17005 | 26 |
classVariableNames:'' |
27 |
poolDictionaries:'' |
|
28 |
privateIn:Timestamp |
|
11993 | 29 |
! |
30 |
||
31 |
Timestamp::TimestampBuilderAbstract subclass:#TimestampISO8601Builder |
|
17005 | 32 |
instanceVariableNames:'stream' |
33 |
classVariableNames:'' |
|
34 |
poolDictionaries:'' |
|
35 |
privateIn:Timestamp |
|
11993 | 36 |
! |
37 |
||
8246 | 38 |
!Timestamp class methodsFor:'documentation'! |
30 | 39 |
|
82 | 40 |
copyright |
41 |
" |
|
42 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
17006 | 43 |
All Rights Reserved |
82 | 44 |
|
45 |
This software is furnished under a license and may be used |
|
46 |
only in accordance with the terms of that license and with the |
|
47 |
inclusion of the above copyright notice. This software may not |
|
48 |
be provided or otherwise made available to, or used by, any |
|
49 |
other person. No title to or ownership of the software is |
|
50 |
hereby transferred. |
|
51 |
" |
|
52 |
! |
|
53 |
||
30 | 54 |
documentation |
55 |
" |
|
22627 | 56 |
This class represents time values in milliseconds, starting some time in the past. |
22834 | 57 |
This base-time is called 'epoch' and always an UTC time. |
58 |
||
16945 | 59 |
When printing and accessing values like #hour, |
22627 | 60 |
the timestamp will be interpreted in the local timezone. |
16955 | 61 |
(as opposed to UtcTimestamp, which presents itself in UTC, |
17131 | 62 |
and as opposed to TZTimestamp, which remembers the timezone in which it was created). |
16939 | 63 |
|
22834 | 64 |
The internal representation, osTime, will typically start with 1970-01-01 00:00 UTC, |
16939 | 65 |
as used in the Unix operating system, but other systems may bias the time differently. |
16945 | 66 |
Actually, the implementation does not depend or even know which time/date |
16939 | 67 |
the OperatingSystem bases its time upon - it is simply keeping the value(s) |
68 |
as returned from the OS. |
|
69 |
For conversion, these values are given back to the OS, which will know |
|
22627 | 70 |
how to convert them. This has the advantage that timestamps on files |
16945 | 71 |
(such as last-access-time or last-modification-time) can be handled transparently - |
16939 | 72 |
especially when performing comparisons. |
73 |
||
16945 | 74 |
You should not interpret the osTime instance variable directly, |
16939 | 75 |
instead (if at all), ask the OS to convert. |
30 | 76 |
|
16945 | 77 |
The implementation of this class is not the same as in ST-80 |
7671 | 78 |
(which represents the time as seconds from Jan 1., 1901). |
30 | 79 |
|
22613 | 80 |
This class should not be confused with Time (which only represents the time within one day). |
22627 | 81 |
Time instances cannot be used to compare times across midnight, whereas instances of Timestamp can. |
82 |
Also, it should not be confused with TimeDuration, which represents a time-difference. |
|
83 |
||
84 |
Notice: |
|
85 |
this class was once called AbsoluteTime. Einstein told us in 1905, that talking |
|
86 |
about an absolute time is not a good idea (at least in our universe). So in 2004 the class |
|
87 |
has been renamed to Timestamp which makes us more compatible to other ST dialects (e.g. VW) |
|
88 |
AbsoluteTime was kept as an alias for backward compatibility and was finally removed in 2017. |
|
3479 | 89 |
|
16922 | 90 |
Also Note: |
21699 | 91 |
On UNIX, osTime can only hold dates between 1970-01-01T00:00:00Z and 2038-01-19T03:14:07Z |
22613 | 92 |
However, timestamp instances can hold negative osTime values (which are timestamps |
93 |
before 1.1.1970 and also osTimes greater than 4294967295 (2^32-1) for timestamps after 2038-01-19. |
|
22627 | 94 |
Thus, ST/X will have no problem when dealing with dates before the epoch or after 2038. |
17131 | 95 |
|
21699 | 96 |
For dates before 1582 (when calendars were changed from Julian to Gregorian), |
17131 | 97 |
the so called 'proleptic gregorian calendar' is used. This assumes leap years to continue in |
98 |
the past as if a gregorian calendar was used. Thus, 0000 is considered a leap year. |
|
16972 | 99 |
|
22834 | 100 |
ALso Note: |
101 |
because all timestamps keep the internal time value in UTC, they can be easily compared |
|
102 |
for being before/same/after another. Only when printing, a difference is made. |
|
103 |
The timezone is compensated out when a timestamp is created and recalculated in when printed. |
|
104 |
||
22627 | 105 |
News: |
106 |
The additional instance variable picoSeconds can be used to add more resolution. |
|
107 |
If non-nil, it holds additional picoseconds to be added to the millisecond osTime |
|
108 |
(i.e. the picoSeconds are an integer between 0 and 1000*1000*1000. |
|
22613 | 109 |
Although, not all OSs give us that detail when asking for the current time, |
22627 | 110 |
the picos can still be used in physical computations. |
111 |
Some OSs will provide microsecond resolution. |
|
22834 | 112 |
Notice: |
113 |
the picos are to be added to the millis, to get picos within the second. |
|
114 |
this is ugly, but makes all the rest backward compatible. |
|
22873 | 115 |
Also, most timestamps only require/have millisecond resolution, |
116 |
so the pico instvar is nil/0 and does not require an aditional largeInteger operation. |
|
22613 | 117 |
|
22867 | 118 |
The typical OS-time resolution is in the milli- or microsecond range. |
119 |
External logging hardware may generate timestamps in the micro- or nanosecond range. |
|
120 |
Picosecond resolution should be good enough for almost any application (at least for the near future). |
|
121 |
||
1306 | 122 |
[author:] |
17131 | 123 |
Claus Gittinger |
1306 | 124 |
|
1282 | 125 |
[See also:] |
17131 | 126 |
UtcTimestamp Time Date |
127 |
Delay ProcessorScheduler |
|
30 | 128 |
" |
129 |
! ! |
|
130 |
||
8246 | 131 |
!Timestamp class methodsFor:'initialization'! |
132 |
||
133 |
initialize |
|
17008 | 134 |
MinOSTime := OperatingSystem epochStartOSTime. |
135 |
MaxOSTime := OperatingSystem epochEndOSTime. |
|
17006 | 136 |
|
137 |
Epoch := UtcTimestamp basicNew setSeconds:0. |
|
21264 | 138 |
|
139 |
"Modified: / 19-01-2017 / 17:01:59 / stefan" |
|
8246 | 140 |
! ! |
141 |
||
142 |
!Timestamp class methodsFor:'instance creation'! |
|
30 | 143 |
|
4378 | 144 |
UTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
145 |
"return an instance of the receiver, given individual components, |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
146 |
interpreted in the UTC timezone." |
1501 | 147 |
|
16945 | 148 |
^ self basicNew |
17006 | 149 |
UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis |
30 | 150 |
|
241 | 151 |
" |
12819
a28ce068acd8
comment/format in: #UTCYear:month:day:hour:minute:second:millisecond:
Stefan Vogel <sv@exept.de>
parents:
12729
diff
changeset
|
152 |
Timestamp UTCYear:1970 month:1 day:1 hour:0 minute:0 second:0 millisecond:0 |
8246 | 153 |
Timestamp UTCYear:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:0 |
154 |
Timestamp UTCYear:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:100 |
|
155 |
Timestamp UTCYear:1999 month:7 day:1 hour:1 minute:0 second:0 millisecond:0 |
|
156 |
Timestamp UTCYear:2000 month:1 day:1 hour:1 minute:0 second:0 millisecond:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
157 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
158 |
UtcTimestamp UTCYear:2000 month:1 day:1 hour:1 minute:0 second:0 millisecond:0 |
241 | 159 |
" |
1228 | 160 |
|
4378 | 161 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
162 |
"Created: / 13.7.1999 / 12:34:37 / stefan" |
|
163 |
"Modified: / 13.7.1999 / 12:42:30 / stefan" |
|
164 |
! |
|
165 |
||
22873 | 166 |
UTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis additionalPicoseconds:picos |
167 |
"return an instance of the receiver, given individual components, |
|
168 |
interpreted in the UTC timezone." |
|
169 |
||
170 |
^ self basicNew |
|
171 |
UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis |
|
172 |
additionalPicoseconds:picos |
|
173 |
||
174 |
" |
|
175 |
Timestamp UTCYear:1970 month:1 day:1 hour:0 minute:0 second:0 millisecond:0 |
|
176 |
Timestamp UTCYear:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:0 |
|
177 |
Timestamp UTCYear:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:100 |
|
178 |
Timestamp UTCYear:1999 month:7 day:1 hour:1 minute:0 second:0 millisecond:0 |
|
179 |
Timestamp UTCYear:2000 month:1 day:1 hour:1 minute:0 second:0 millisecond:0 |
|
180 |
||
181 |
UtcTimestamp UTCYear:2000 month:1 day:1 hour:1 minute:0 second:0 millisecond:0 |
|
182 |
" |
|
183 |
||
184 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
|
185 |
"Created: / 13.7.1999 / 12:34:37 / stefan" |
|
186 |
"Modified: / 13.7.1999 / 12:42:30 / stefan" |
|
187 |
! |
|
188 |
||
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
189 |
decodeFromLiteralArray:anArray |
16986 | 190 |
"decode a Timestamp literalArray. |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
191 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
192 |
anArray may be: |
19897 | 193 |
#(Timestamp '200004182000.123') |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
194 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
195 |
or the deprecated old format, that is not portable between different architectures. |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
196 |
We parse this for backward compatibility (will be eventually removed). |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
197 |
|
19897 | 198 |
#(Timestamp #osTime: 12345678) |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
199 |
" |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
200 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
201 |
(anArray at:2) == #osTime: ifTrue:[ |
19897 | 202 |
^ self new osTime:(anArray at:3). |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
203 |
]. |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
204 |
|
16945 | 205 |
^ self |
19897 | 206 |
readGeneralizedFrom:(anArray at:2) |
207 |
onError:[ self conversionErrorSignal |
|
208 |
raiseWith:anArray errorString:' - Timestamp literal array decoding' ] |
|
8797 | 209 |
|
210 |
" |
|
211 |
Timestamp |
|
19897 | 212 |
decodeFromLiteralArray:#(Timestamp '20050323175226.014') |
16986 | 213 |
Timestamp |
19897 | 214 |
decodeFromLiteralArray:#(Timestamp '20050323175226.014-01') |
16986 | 215 |
Timestamp |
19897 | 216 |
decodeFromLiteralArray:#(Timestamp '20050323175226.014Z') |
8797 | 217 |
" |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
218 |
! |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
219 |
|
16945 | 220 |
epoch |
16986 | 221 |
"the epoch is based to 0 for 1970-01-01 00:00:00. |
222 |
However, we allow negative values to represent timestamps before that" |
|
16945 | 223 |
|
224 |
^ Epoch |
|
16928 | 225 |
! |
226 |
||
16932 | 227 |
fromDate:aDate |
228 |
"return an instance of the receiver, initialized from a time and a date |
|
229 |
object. |
|
230 |
See also `Timestamp now' and other protocol inherited |
|
231 |
from my superclass." |
|
232 |
||
16945 | 233 |
^ self |
17006 | 234 |
year:aDate year |
235 |
month:aDate month |
|
236 |
day:aDate day |
|
237 |
hour:0 |
|
238 |
minute:0 |
|
239 |
second:0 |
|
240 |
millisecond:0 |
|
16932 | 241 |
|
242 |
" |
|
16945 | 243 |
Timestamp fromDate:(Date today) andTime:(Time now) |
244 |
Timestamp fromDate:(Date today plusDays:1) andTime:(Time now) |
|
245 |
Timestamp now |
|
16932 | 246 |
" |
247 |
||
248 |
"Modified: / 8.9.1995 / 15:07:30 / claus" |
|
249 |
"Modified: / 19.4.1996 / 15:25:46 / cg" |
|
250 |
"Modified: / 13.7.1999 / 12:30:47 / stefan" |
|
251 |
! |
|
252 |
||
423 | 253 |
fromDate:aDate andTime:aTime |
254 |
"return an instance of the receiver, initialized from a time and a date |
|
1228 | 255 |
object. |
8246 | 256 |
See also `Timestamp now' and other protocol inherited |
1228 | 257 |
from my superclass." |
423 | 258 |
|
16950 | 259 |
^ self |
17006 | 260 |
year:aDate year |
261 |
month:aDate month |
|
262 |
day:aDate day |
|
263 |
hour:aTime hours |
|
264 |
minute:aTime minutes |
|
265 |
second:aTime seconds |
|
266 |
millisecond:aTime milliseconds |
|
16946 | 267 |
|
423 | 268 |
" |
16950 | 269 |
Timestamp fromDate:(Date today) andTime:(Time now) |
16946 | 270 |
Timestamp fromDate:(Date today) andTime:(Time nowWithMilliseconds) |
271 |
||
16950 | 272 |
Timestamp fromDate:(Date today plusDays:1) andTime:(Time now) |
273 |
Timestamp now |
|
423 | 274 |
" |
275 |
||
4378 | 276 |
"Modified: / 8.9.1995 / 15:07:30 / claus" |
277 |
"Modified: / 19.4.1996 / 15:25:46 / cg" |
|
278 |
"Modified: / 13.7.1999 / 12:30:47 / stefan" |
|
423 | 279 |
! |
280 |
||
22873 | 281 |
fromDate:aDate hour:hour minute:minute second:second |
282 |
"return an instance of the receiver, initialized from a time and a date object. |
|
283 |
See also `Timestamp now' and other protocol inherited from my superclass." |
|
284 |
||
285 |
^ self fromDate:aDate hour:hour minute:minute second:second millisecond:0 |
|
286 |
||
287 |
" |
|
288 |
Timestamp fromDate:(Date today) andTime:(Time now) |
|
289 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 |
|
290 |
" |
|
291 |
! |
|
292 |
||
293 |
fromDate:aDate hour:hour minute:minute second:second microsecond:micros |
|
294 |
"return an instance of the receiver, initialized from a time and a date object. |
|
295 |
See also `Timestamp now' and other protocol inherited from my superclass." |
|
296 |
||
297 |
^ (self |
|
298 |
year:aDate year |
|
299 |
month:aDate month |
|
300 |
day:aDate day |
|
301 |
hour:hour |
|
302 |
minute:minute |
|
303 |
second:second |
|
304 |
millisecond:0 |
|
305 |
) setMicrosecond:micros |
|
306 |
||
307 |
" |
|
308 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 microsecond:123456 |
|
309 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 microsecond:140 |
|
310 |
" |
|
311 |
! |
|
312 |
||
313 |
fromDate:aDate hour:hour minute:minute second:second millisecond:millis |
|
314 |
"return an instance of the receiver, initialized from a time and a date object. |
|
315 |
See also `Timestamp now' and other protocol inherited from my superclass." |
|
316 |
||
317 |
^ self |
|
318 |
year:aDate year |
|
319 |
month:aDate month |
|
320 |
day:aDate day |
|
321 |
hour:hour |
|
322 |
minute:minute |
|
323 |
second:second |
|
324 |
millisecond:millis |
|
325 |
||
326 |
" |
|
327 |
Timestamp fromDate:(Date today) andTime:(Time now) |
|
328 |
Timestamp fromDate:(Date today) andTime:(Time nowWithMilliseconds) |
|
329 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 millisecond:140 |
|
330 |
" |
|
331 |
! |
|
332 |
||
333 |
fromDate:aDate hour:hour minute:minute second:second nanosecond:nanos |
|
334 |
"return an instance of the receiver, initialized from a time and a date object. |
|
335 |
See also `Timestamp now' and other protocol inherited from my superclass." |
|
336 |
||
337 |
^ (self |
|
338 |
year:aDate year |
|
339 |
month:aDate month |
|
340 |
day:aDate day |
|
341 |
hour:hour |
|
342 |
minute:minute |
|
343 |
second:second |
|
344 |
millisecond:0 |
|
345 |
) setNanosecond:nanos |
|
346 |
||
347 |
" |
|
348 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 microsecond:123456 |
|
349 |
Timestamp fromDate:(Date today) hour:10 minute:5 second:30 microsecond:140 |
|
350 |
" |
|
351 |
! |
|
352 |
||
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
353 |
newDay:dayInYear year:year |
8246 | 354 |
"return a new Timestamp, given the year and the day-in-year (starting at 1). |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
355 |
Date protocol compatibility" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
356 |
|
16932 | 357 |
^ self fromDate:(Date newDay:dayInYear year:year) |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
358 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
359 |
" |
8246 | 360 |
Timestamp newDay:183 year:1996 |
16945 | 361 |
Timestamp newDay:1 year:1996 |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
362 |
" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
363 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
364 |
"Modified: 2.7.1996 / 09:39:30 / cg" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
365 |
! |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
366 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
367 |
secondsSince1970:secs |
22834 | 368 |
"set time from elapsed seconds since 1-1-1970, 00:00:00 (UTC). |
369 |
This is the format used in the UNIX world. |
|
370 |
Notice that the internal storage is always UTC based." |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
371 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
372 |
^ self basicNew setSeconds:secs. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
373 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
374 |
" |
22834 | 375 |
UtcTimestamp secondsSince1970:0 -> 1970-01-01 00:00:00Z |
376 |
Timestamp secondsSince1970:0 -> 1970-01-01 01:00:00 (local germany, ST) |
|
377 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
378 |
Timestamp secondsSince1970:3600 |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
379 |
Timestamp secondsSince1970:3600*24 |
22834 | 380 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
381 |
(Timestamp year:2010 month:7 day:1 hour:0 minute:0 second:0) |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
382 |
= |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
383 |
(Timestamp secondsSince1970:1277935200) |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
384 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
385 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
386 |
"Created: / 21-10-1996 / 17:48:30 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
387 |
"Modified: / 13-07-1999 / 12:31:22 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
388 |
"Modified: / 08-01-2011 / 16:06:28 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
389 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
390 |
|
23565 | 391 |
utcMillisecondsSince1970:millis |
16986 | 392 |
"set time from elapsed milliseconds since the epoch 1-1-1970, 00:00:00." |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
393 |
|
23565 | 394 |
^ self basicNew setMilliseconds:millis |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
395 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
396 |
"Created: / 08-01-2011 / 16:09:32 / cg" |
23565 | 397 |
"Modified (format): / 14-12-2018 / 19:45:12 / Claus Gittinger" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
398 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
399 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
400 |
utcNow |
16986 | 401 |
"return now as utc timestamp" |
402 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
403 |
^ UtcTimestamp now |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
404 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
405 |
" |
16986 | 406 |
Timestamp now |
407 |
Timestamp utcNow |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
408 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
409 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
410 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
411 |
utcSecondsSince1970:secs |
22834 | 412 |
"set time from the elapsed seconds since 1-1-1970, 00:00:00 UTC. |
413 |
This is the format used in the UNIX world. |
|
414 |
Notice that the internal storage is always UTC based." |
|
415 |
||
416 |
^ UtcTimestamp secondsSince1970:secs |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
417 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
418 |
" |
22834 | 419 |
UtcTimestamp secondsSince1970:0 -> 1970-01-01 00:00:00Z |
420 |
Timestamp secondsSince1970:0 -> 1970-01-01 01:00:00 (local germany, ST) |
|
421 |
||
422 |
UtcTimestamp secondsSince1970:3600 -> 1970-01-01 01:00:00Z |
|
423 |
Timestamp secondsSince1970:3600 -> 1970-01-01 02:00:00 (local) |
|
424 |
||
16945 | 425 |
Timestamp secondsSince1970:3600*24 |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
426 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
427 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
428 |
"Modified: / 13-07-1999 / 12:31:22 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
429 |
"Created: / 08-01-2011 / 16:05:09 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
430 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
431 |
|
19713 | 432 |
year:year month:month day:day |
433 |
"return a new Timestamp, given the year, month and day (starting at 1). |
|
434 |
Date protocol compatibility" |
|
435 |
||
436 |
^ self |
|
437 |
year:year |
|
438 |
month:month |
|
439 |
day:day |
|
440 |
hour:0 |
|
441 |
minute:0 |
|
442 |
second:0 |
|
443 |
millisecond:0 |
|
444 |
||
445 |
" |
|
446 |
Timestamp year:1996 month:1 day:1 |
|
447 |
" |
|
448 |
! |
|
449 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
450 |
year:y month:m day:d hour:h minute:min second:s |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
451 |
"return an instance of the receiver, given individual components. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
452 |
See also `Timestamp now' and other protocol inherited |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
453 |
from my superclass." |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
454 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
455 |
^ self year:y month:m day:d hour:h minute:min second:s millisecond:0 |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
456 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
457 |
" |
16945 | 458 |
Timestamp year:1970 month:1 day:1 hour:1 minute:0 second:0 |
459 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
460 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:100 |
16945 | 461 |
Timestamp year:2000 month:7 day:1 hour:1 minute:0 second:0 |
19771 | 462 |
Timestamp year:2080 month:7 day:1 hour:1 minute:0 second:0 |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
463 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
464 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
465 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
466 |
"Created: / 13.7.1999 / 12:06:39 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
467 |
"Modified: / 13.7.1999 / 12:27:47 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
468 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
469 |
|
22873 | 470 |
year:y month:m day:d hour:h minute:min second:s microsecond:micros |
471 |
"return an instance of the receiver, given individual components. |
|
472 |
See also `Timestamp now' and other protocol inherited |
|
473 |
from my superclass." |
|
474 |
||
475 |
^ (self year:y month:m day:d hour:h minute:min second:s) setMicrosecond:micros |
|
476 |
||
477 |
" |
|
478 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 microsecond:100 |
|
479 |
" |
|
480 |
! |
|
481 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
482 |
year:y month:m day:d hour:h minute:min second:s millisecond:millis |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
483 |
"return an instance of the receiver, given individual components. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
484 |
See also `Timestamp now' and other protocol inherited |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
485 |
from my superclass." |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
486 |
|
16945 | 487 |
^ self basicNew |
17006 | 488 |
year:y month:m day:d hour:h minute:min second:s millisecond:millis |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
489 |
" |
16945 | 490 |
Timestamp year:1970 month:1 day:1 hour:0 minute:0 second:0 |
491 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
492 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:100 |
16945 | 493 |
Timestamp year:2000 month:7 day:1 hour:1 minute:0 second:0 |
494 |
UtcTimestamp year:2000 month:7 day:1 hour:1 minute:0 second:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
495 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
496 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
497 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
498 |
"Created: / 13.7.1999 / 12:28:44 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
499 |
"Modified: / 13.7.1999 / 12:37:57 / stefan" |
22873 | 500 |
! |
501 |
||
502 |
year:y month:m day:d hour:h minute:min second:s millisecond:millis additionalPicoseconds:picos |
|
503 |
"return an instance of the receiver, given individual components. |
|
504 |
See also `Timestamp now' and other protocol inherited |
|
505 |
from my superclass." |
|
506 |
||
507 |
^ self basicNew |
|
508 |
year:y month:m day:d hour:h minute:min second:s |
|
509 |
millisecond:millis additionalPicoseconds:picos |
|
510 |
||
511 |
" |
|
512 |
Timestamp year:1970 month:1 day:1 hour:0 minute:0 second:0 |
|
513 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 |
|
514 |
Timestamp year:1991 month:1 day:2 hour:12 minute:30 second:0 millisecond:100 |
|
515 |
Timestamp year:2000 month:7 day:1 hour:1 minute:0 second:0 |
|
516 |
UtcTimestamp year:2000 month:7 day:1 hour:1 minute:0 second:0 |
|
517 |
" |
|
518 |
||
519 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
|
520 |
"Created: / 13.7.1999 / 12:28:44 / stefan" |
|
521 |
"Modified: / 13.7.1999 / 12:37:57 / stefan" |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
522 |
! ! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
523 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
524 |
!Timestamp class methodsFor:'Compatibility-Squeak'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
525 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
526 |
current |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
527 |
^ self now |
13604 | 528 |
! |
529 |
||
530 |
readFrom:aStringOrStream |
|
531 |
"Answer a new instance for the value given by aStringOrStream" |
|
532 |
||
16945 | 533 |
^ self |
19713 | 534 |
readFrom:aStringOrStream |
23564 | 535 |
onError:[ |
536 |
TimeConversionError raiseRequestWith:aStringOrStream errorString:' - timestamp' |
|
537 |
] |
|
13604 | 538 |
|
539 |
" |
|
540 |
self readFrom:'23-jun-2000 15:00' |
|
14969 | 541 |
self fromString:'23-jun-2000 15:00' |
13604 | 542 |
self readFrom:'23-jun-2000 ' |
543 |
" |
|
544 |
||
545 |
"Modified (comment): / 20-08-2011 / 16:52:10 / cg" |
|
23564 | 546 |
"Modified (format): / 14-12-2018 / 19:14:16 / Claus Gittinger" |
18886 | 547 |
! |
548 |
||
24160 | 549 |
year:year day:dayInYear |
550 |
"return a new Timestamp, given the year and the day-in-year (starting at 1). |
|
551 |
See also: Date today / Time now / Timestamp now. |
|
552 |
Squeak compatibility" |
|
553 |
||
554 |
^ self newDay:dayInYear year:year |
|
555 |
||
556 |
" |
|
557 |
self year:1970 day:1 |
|
558 |
self year:2000 day:1 |
|
559 |
" |
|
560 |
||
561 |
"Created: / 26-05-2019 / 11:59:20 / Claus Gittinger" |
|
562 |
! |
|
563 |
||
18886 | 564 |
year: year month: month day: day hour: hour minute: minute second: second millisecond: millisecond offset:timeDuration |
565 |
^ (UtcTimestamp year: year month: month day: day hour: hour minute: minute second: second millisecond: millisecond) |
|
566 |
utcOffset:timeDuration |
|
567 |
||
568 |
" |
|
569 |
Timestamp year:2015 month:11 day:9 hour:12 minute:0 second:0 millisecond:0 offset:(2 hours) |
|
570 |
" |
|
571 |
! |
|
572 |
||
573 |
year: year month: month day: day hour: hour minute: minute second: second offset:timeDuration |
|
574 |
^ (UtcTimestamp year: year month: month day: day hour: hour minute: minute second: second) |
|
575 |
utcOffset:timeDuration |
|
576 |
||
577 |
" |
|
578 |
Timestamp year:2015 month:11 day:9 hour:12 minute:0 second:0 offset:(2 hours) |
|
24163 | 579 |
Timestamp year:2015 month:11 day:9 hour:12 minute:0 second:0 offset:0 |
580 |
" |
|
581 |
||
582 |
"Modified (comment): / 26-05-2019 / 12:10:28 / Claus Gittinger" |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
583 |
! ! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
584 |
|
22837 | 585 |
!Timestamp class methodsFor:'class access'! |
586 |
||
587 |
timestampISO8601Builder |
|
588 |
"I hate private class overuse... |
|
589 |
... I need such ugly hacks whenever someone thinks that he/she must keep a secret... |
|
590 |
See Time for such its use" |
|
591 |
||
592 |
^ TimestampISO8601Builder |
|
593 |
! ! |
|
594 |
||
13700 | 595 |
!Timestamp class methodsFor:'format strings'! |
596 |
||
597 |
defaultFormatString |
|
16986 | 598 |
"a format string to present times in user interfaces. |
599 |
Do not use this to store/retrieve times (use ISO8601 for that). |
|
600 |
Although the string below looks like ISO8601, it does not include TZ information, |
|
601 |
so when read back, it will always be interpreted as a local timestamp." |
|
602 |
||
13700 | 603 |
^ '%(year)-%(month)-%(day) %h:%m:%s.%i' |
604 |
||
605 |
"Created: / 16-01-2011 / 11:23:36 / cg" |
|
606 |
! ! |
|
607 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
608 |
!Timestamp class methodsFor:'obsolete'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
609 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
610 |
day:d month:m year:y hour:h minutes:min seconds:s |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
611 |
<resource: #obsolete> |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
612 |
"This is obsolete. User #year:month:day:hour:minute:second:" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
613 |
|
16945 | 614 |
^ self |
17006 | 615 |
year:y month:m day:d hour:h |
616 |
minute:min second:s millisecond:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
617 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
618 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
619 |
"Modified: / 1.7.1996 / 15:22:26 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
620 |
"Modified: / 13.7.1999 / 12:29:56 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
621 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
622 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
623 |
day:d month:m year:y hour:h minutes:min seconds:s milliseconds:millis |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
624 |
<resource: #obsolete> |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
625 |
"This is obsolete. User #year:month:day:hour:minute:second:millisecond:" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
626 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
627 |
^ self year:y month:m day:d hour:h minute:min second:s millisecond:millis |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
628 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
629 |
" |
16945 | 630 |
Timestamp day:1 month:1 year:1970 hour:1 minute:0 second:0 |
631 |
Timestamp day:2 month:1 year:1991 hour:12 minute:30 second:0 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
632 |
Timestamp day:2 month:1 year:1991 hour:12 minute:30 second:0 millisecond:100 |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
633 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
634 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
635 |
"Created: / 1.7.1996 / 14:46:09 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
636 |
"Modified: / 1.7.1996 / 15:22:07 / cg" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
637 |
"Modified: / 13.7.1999 / 12:30:26 / stefan" |
19713 | 638 |
! |
639 |
||
640 |
newDay:day month:month year:year |
|
641 |
<resource: #obsolete> |
|
22873 | 642 |
|
19713 | 643 |
"return a new Timestamp, given the year, month and day (starting at 1). |
22873 | 644 |
Date protocol compatibility. |
645 |
Obsolete: use year:month:day:." |
|
19713 | 646 |
|
647 |
^ self |
|
22873 | 648 |
year:year |
649 |
month:month |
|
650 |
day:day |
|
651 |
hour:0 |
|
652 |
minute:0 |
|
653 |
second:0 |
|
654 |
millisecond:0 |
|
19713 | 655 |
|
656 |
" |
|
657 |
Timestamp newDay:1 month:1 year:1996 |
|
658 |
" |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
659 |
! ! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
660 |
|
22914 | 661 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
662 |
!Timestamp class methodsFor:'private'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
663 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
664 |
basicReadFrom:aStream |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
665 |
"return a new Timestamp, reading a printed representation from aStream. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
666 |
The string is interpreted as 24 hour format, as printed. |
16914 | 667 |
|
16945 | 668 |
Notice, that this is NOT the storeString format, which is iso8601 |
16914 | 669 |
This is more or less a heuristic attempt to read any reasonable format. |
16945 | 670 |
If the input starts with an integer > 31, |
16935 | 671 |
it is assumed to be a year and the rest is read in iso8601 format. |
16914 | 672 |
|
16945 | 673 |
KLUDGE: |
17071 | 674 |
us and non-us format have different ordering of day and month; |
675 |
The format read here is (non-us) dd-mm-yyyy hh:mm:ss.iii |
|
676 |
or (us-format, for Travis) mm/dd/yyyy hh:mm:ss.iii. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
677 |
On error, raise an exception" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
678 |
|
22873 | 679 |
|monthOrYear firstNumber secondNumber day month year hour min sec |
23564 | 680 |
millis usFormat possibeMonthName ch utcOffsetOrNil |
681 |
count mantissa fraction picos ts |
|
682 |
monthName| |
|
683 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
684 |
aStream skipSeparators. |
23564 | 685 |
aStream peek isLetter ifTrue:[ |
686 |
"/ US format, like 'July 21, 1983 01:15:00' ? |
|
687 |
monthName := aStream throughAnyForWhich:[:ch | ch isLetter]. |
|
688 |
month := Date indexOfMonth:monthName asLowercase. |
|
689 |
aStream skipSeparators. |
|
690 |
day := Integer readFrom:aStream onError:-1. |
|
691 |
aStream skipSeparators. |
|
692 |
aStream peek == $, ifTrue:[ |
|
693 |
aStream next |
|
694 |
]. |
|
695 |
] ifFalse:[ |
|
696 |
count := 0. |
|
697 |
monthOrYear := aStream throughAnyForWhich:[:ch | count := count+1. ch isDigit and:[count <= 4]]. |
|
698 |
firstNumber := Integer readFrom:monthOrYear onError:[TimeConversionError raiseErrorString:' - integer expected']. |
|
699 |
firstNumber > 31 ifTrue:[ |
|
700 |
"/ assume iso8601 format; |
|
701 |
^ self readIso8601FormatFrom:aStream yearAlreadyRead:firstNumber. |
|
702 |
]. |
|
703 |
aStream skipSeparators. |
|
704 |
||
705 |
"/ consider this a kludge |
|
706 |
usFormat := (aStream peek == $/ ). |
|
707 |
||
708 |
[(ch := aStream peekOrNil) notNil and:[ch isLetterOrDigit not]] whileTrue:[aStream next]. |
|
709 |
(ch notNil and:[ch isDigit]) ifTrue:[ |
|
710 |
secondNumber := Integer readFrom:aStream onError:-1. |
|
711 |
||
712 |
usFormat ifTrue:[ |
|
713 |
month := firstNumber. |
|
714 |
day := secondNumber. |
|
715 |
] ifFalse:[ |
|
716 |
month := secondNumber. |
|
717 |
day := firstNumber. |
|
718 |
]. |
|
17071 | 719 |
] ifFalse:[ |
23564 | 720 |
possibeMonthName := aStream throughAnyForWhich:[:ch | ch isLetter]. |
721 |
month := Date indexOfMonth:possibeMonthName asLowercase. |
|
17071 | 722 |
day := firstNumber. |
723 |
]. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
724 |
]. |
23564 | 725 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
726 |
(day between:1 and:31) ifFalse:[ TimeConversionError raiseErrorString:' - bad day' ]. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
727 |
(month between:1 and:12) ifFalse:[ TimeConversionError raiseErrorString:' - bad month' ]. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
728 |
|
16967 | 729 |
[(ch := aStream peekOrNil) notNil and:[ch isDigit not]] whileTrue:[aStream next]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
730 |
year := Integer readFrom:aStream onError:[ TimeConversionError raiseErrorString:' - bad year' ]. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
731 |
|
22873 | 732 |
picos := 0. |
733 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
734 |
aStream atEnd ifTrue:[ |
17071 | 735 |
hour := min := sec := millis := 0. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
736 |
] ifFalse:[ |
17071 | 737 |
[(ch := aStream peekOrNil) notNil and:[ch isDigit not]] whileTrue:[aStream next]. |
738 |
hour := Integer readFrom:aStream onError:-1. |
|
739 |
(hour between:0 and:24) ifFalse:[ TimeConversionError raiseErrorString:' - bad hour' ]. |
|
740 |
||
741 |
[(ch := aStream peekOrNil) notNil and:[ch isDigit not]] whileTrue:[aStream next]. |
|
742 |
min := Integer readFrom:aStream onError:-1. |
|
743 |
(min between:0 and:59) ifFalse:[ TimeConversionError raiseErrorString:' - bad minute' ]. |
|
744 |
||
745 |
aStream atEnd ifTrue:[ |
|
746 |
sec := millis := 0. |
|
747 |
] ifFalse:[ |
|
748 |
[(ch := aStream peekOrNil) notNil and:[ch isDigit not]] whileTrue:[aStream next]. |
|
749 |
sec := Integer readFrom:aStream onError:-1. |
|
750 |
(sec between:0 and:59) ifFalse:[ TimeConversionError raiseErrorString:' - bad second' ]. |
|
751 |
||
22873 | 752 |
(aStream peek == $. or:[aStream peek == $,]) ifTrue:[ |
17071 | 753 |
aStream next. |
22873 | 754 |
mantissa := Number readMantissaAndScaleFrom:aStream radix:10. |
755 |
fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)). |
|
756 |
(mantissa at:3) > 3 ifTrue:[ |
|
757 |
picos := fraction * (1000 * 1000 * 1000 * 1000). |
|
758 |
millis := picos // (1000 * 1000 * 1000). |
|
759 |
picos := picos \\ (1000 * 1000 * 1000). |
|
760 |
] ifFalse:[ |
|
761 |
millis := fraction * 1000. |
|
762 |
]. |
|
763 |
"/ millis := Integer readFrom:aStream onError:0. |
|
764 |
"/ millis >= 1000 ifTrue:[ TimeConversionError raiseErrorString:' - bad millisecond' ]. |
|
17071 | 765 |
] ifFalse:[ |
766 |
millis := 0. |
|
767 |
]. |
|
768 |
aStream atEnd ifFalse:[ |
|
769 |
utcOffsetOrNil := self utcOffsetFrom:aStream |
|
770 |
]. |
|
771 |
]. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
772 |
]. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
773 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
774 |
"special check - only 24:00:00 is allowed; |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
775 |
every time after that must wrap" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
776 |
hour == 24 ifTrue:[ |
17071 | 777 |
(min ~~ 0 or:[sec ~~ 0 or:[millis ~~ 0]]) ifTrue:[ TimeConversionError raiseErrorString:' - bad hour' ]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
778 |
]. |
22894 | 779 |
|
16967 | 780 |
utcOffsetOrNil notNil ifTrue:[ |
17071 | 781 |
utcOffsetOrNil = 0 ifTrue:[ |
782 |
"/ utc timestamp |
|
22894 | 783 |
ts := UtcTimestamp year:year month:month day:day hour:hour minute:min second:sec millisecond:millis |
784 |
] ifFalse:[ |
|
785 |
"/ tz timestamp |
|
786 |
ts := ((TZTimestamp basicNew |
|
787 |
setOSTimeFromUTCYear:year month:month day:day |
|
788 |
hour:hour minute:min second:sec millisecond:millis |
|
789 |
) utcOffset:utcOffsetOrNil |
|
790 |
) addSeconds:utcOffsetOrNil. |
|
17071 | 791 |
]. |
22894 | 792 |
] ifFalse:[ |
793 |
"/ a local timestamp |
|
794 |
ts := self year:year month:month day:day hour:hour minute:min second:sec millisecond:millis. |
|
16967 | 795 |
]. |
22873 | 796 |
picos ~~ 0 ifTrue:[ ts additionalPicoseconds:picos ]. |
797 |
^ ts |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
798 |
|
17081 | 799 |
"some ad hoc formats: |
16914 | 800 |
|
16945 | 801 |
Timestamp basicReadFrom:'20-2-1995 13:11:06' readStream |
802 |
Timestamp basicReadFrom:'20-2-1995 13:11:06.' readStream |
|
17081 | 803 |
Timestamp basicReadFrom:'20-feb-1995 13:11:06' readStream |
804 |
Timestamp basicReadFrom:'20-foo-1995 13:11:06' readStream |
|
16945 | 805 |
(Timestamp basicReadFrom:'10-9-1995 13:11:06' readStream) month - european |
16935 | 806 |
(Timestamp basicReadFrom:'10/9/1995 13:11:06' readStream) month - us |
16945 | 807 |
Timestamp basicReadFrom:'20-2-1995 13:11' readStream |
808 |
Timestamp basicReadFrom:'20-2-1995 13:11:06.100' readStream |
|
17081 | 809 |
Timestamp basicReadFrom:'20-2-1995 13:11:06.100 MESZ' readStream |
810 |
Timestamp basicReadFrom:'20-2-1995 13:11:06.100+0200' readStream |
|
16945 | 811 |
Timestamp basicReadFrom:'32-2-1995 13:11:06.100' readStream |
812 |
Timestamp basicReadFrom:'32-foo-1995 13:11:06.100' readStream |
|
813 |
Timestamp basicReadFrom:'20-13-1995 13:11:06.100' readStream |
|
814 |
Timestamp basicReadFrom:'20-12-1995 25:11:06.100' readStream |
|
815 |
Timestamp basicReadFrom:'20-12-1995 23:61:06.100' readStream |
|
816 |
Timestamp basicReadFrom:'20-12-1995 23:10:66.100' readStream |
|
817 |
Timestamp basicReadFrom:'20-12-1995 23:10:00.1000' readStream |
|
818 |
Timestamp basicReadFrom:'20-2-1995 24:01:00.100' readStream |
|
819 |
Timestamp basicReadFrom:'20-2-1995 24:00:01.100' readStream |
|
820 |
Timestamp basicReadFrom:'foo' readStream |
|
16914 | 821 |
|
822 |
any iso8601 format:. |
|
16945 | 823 |
Timestamp basicReadFrom:(Timestamp now printString readStream) |
824 |
Timestamp basicReadFrom:'1995-10-20 24:00:00.000' readStream |
|
825 |
Timestamp basicReadFrom:'1995-10-20 12:10:00.000' readStream |
|
17081 | 826 |
Timestamp basicReadFrom:'1995-10-20 12:10:00.000-0200' readStream |
16945 | 827 |
|
828 |
UtcTimestamp basicReadFrom:'1995-10-20 12:10:00.000' readStream |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
829 |
" |
22337 | 830 |
|
831 |
"Modified: / 09-11-2017 / 10:10:07 / cg" |
|
23564 | 832 |
"Modified: / 14-12-2018 / 19:22:17 / Claus Gittinger" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
833 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
834 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
835 |
readIso8601FormatFrom:aStringOrStream yearAlreadyRead:yearOrNil |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
836 |
"common helper for read methods. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
837 |
Return a new Timestamp, reading an iso8601 representation from aStream. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
838 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
839 |
If the time ends with a 'Z' it is the UTC (or zulu) time, |
16292 | 840 |
otherwise it is local time. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
841 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
842 |
Missing month/day values are replaced with 1; i.e. 1999T24:00 |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
843 |
is the same as 1999-01-01T24:00:00. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
844 |
Missing minute, second and ms values are replaced with 0; |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
845 |
i.e. 1999T12 is the same as 1999-01-01T12:00:00.000. |
16292 | 846 |
Partial hours, minutes, seconds are allowed at the end, |
16914 | 847 |
decimal separators are both $. and $, . |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
848 |
Of course, a 24 hour clock is used. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
849 |
On error, raise an exception. |
16914 | 850 |
Please use this format for all external representations - it's the standard." |
851 |
||
852 |
"/ changed to use the new reader |
|
16945 | 853 |
^ TimestampISO8601Builder |
17006 | 854 |
read:aStringOrStream withClass:self |
855 |
yearAlreadyReadAs:yearOrNil |
|
16914 | 856 |
|
857 |
"/ |str day month dayInWeek week year hour min sec tmpDay millis fraction isUtcTime peekChar ch| |
|
858 |
"/ |
|
859 |
"/ str := aStringOrStream readStream. |
|
860 |
"/ |
|
861 |
"/ month := day := 1. |
|
862 |
"/ hour := millis := 0. |
|
863 |
"/ isUtcTime := false. |
|
864 |
"/ |
|
865 |
"/ yearOrNil notNil ifTrue:[ |
|
866 |
"/ year := yearOrNil |
|
867 |
"/ ] ifFalse:[ |
|
868 |
"/ year := Integer readFrom:str onError:nil. |
|
869 |
"/ year isNil ifTrue:[ TimeConversionError raiseErrorString:' - bad year' ] |
|
870 |
"/ ]. |
|
871 |
"/ |
|
872 |
"/ str skipSeparators. |
|
873 |
"/ (((ch := str peek) == $-) or:[ch == $W]) ifTrue:[ |
|
874 |
"/ (ch == $-) ifTrue:[ str next ]. |
|
875 |
"/ str peek == $W ifTrue:[ |
|
876 |
"/ str next. |
|
877 |
"/ |
|
878 |
"/ "/ week follows |
|
879 |
"/ week := Integer readFrom:str onError:-1. |
|
880 |
"/ (week between:1 and:53) ifFalse:[ TimeConversionError raiseErrorString:' - bad week' ]. |
|
881 |
"/ |
|
882 |
"/ str skipSeparators. |
|
883 |
"/ str peek == $- ifTrue:[ |
|
884 |
"/ str next. |
|
885 |
"/ "/ day follows. |
|
886 |
"/ dayInWeek := Integer readFrom:str onError:-1. |
|
887 |
"/ (dayInWeek between:1 and:7) ifFalse:[ TimeConversionError raiseErrorString:' - bad day in week' ]. |
|
888 |
"/ ] ifFalse:[ |
|
889 |
"/ dayInWeek := 1. |
|
890 |
"/ ]. |
|
891 |
"/ tmpDay := Date newDayInWeek:dayInWeek week:week year:year. |
|
892 |
"/ day := tmpDay day. |
|
893 |
"/ month := tmpDay month. |
|
894 |
"/ year := tmpDay year. |
|
895 |
"/ ] ifFalse:[ |
|
896 |
"/ "/ month follows. |
|
897 |
"/ month := Integer readFrom:str onError:-1. |
|
898 |
"/ (month between:1 and:12) ifFalse:[ TimeConversionError raiseErrorString:' - bad month' ]. |
|
899 |
"/ |
|
900 |
"/ str skipSeparators. |
|
901 |
"/ str peek == $- ifTrue:[ |
|
902 |
"/ str next. |
|
903 |
"/ "/ day follows. |
|
904 |
"/ day := Integer readFrom:str onError:-1. |
|
905 |
"/ (day between:1 and:31) ifFalse:[ TimeConversionError raiseErrorString:' - bad day' ]. |
|
906 |
"/ ]. |
|
907 |
"/ ]. |
|
908 |
"/ ]. |
|
909 |
"/ |
|
910 |
"/ str skipSeparators. |
|
911 |
"/ str atEnd ifFalse:[ |
|
912 |
"/ "time follows" |
|
913 |
"/ |
|
914 |
"/ str peek == $T ifTrue:[ |
|
915 |
"/ "we treat the T as optional here" |
|
916 |
"/ str next. |
|
917 |
"/ str skipSeparators. |
|
918 |
"/ ]. |
|
919 |
"/ hour := Integer readFrom:str onError:-1. |
|
920 |
"/ (hour between:0 and:24) ifFalse:[ TimeConversionError raiseErrorString:' - bad hour' ]. |
|
921 |
"/ str skipSeparators. |
|
922 |
"/ str peekOrNil == $: ifTrue:[ |
|
923 |
"/ str next. |
|
924 |
"/ "/ minutes follow. |
|
925 |
"/ min := Integer readFrom:str onError:-1. |
|
926 |
"/ (min between:0 and:59) ifFalse:[ TimeConversionError raiseErrorString:' - bad minute' ]. |
|
927 |
"/ str skipSeparators. |
|
928 |
"/ str peekOrNil == $: ifTrue:[ |
|
929 |
"/ str next. |
|
930 |
"/ "/ seconds follow. |
|
931 |
"/ sec := Integer readFrom:str onError:-1. |
|
932 |
"/ (sec between:0 and:59) ifFalse:[ TimeConversionError raiseErrorString:' - bad seconds' ]. |
|
933 |
"/ str skipSeparators. |
|
934 |
"/ ]. |
|
935 |
"/ ]. |
|
936 |
"/ |
|
937 |
"/ peekChar := str peekOrNil. |
|
938 |
"/ (peekChar == $. or:[peekChar == $,]) ifTrue:[ |
|
939 |
"/ str next. |
|
940 |
"/ "/ decimals follow. |
|
941 |
"/ fraction := Number readMantissaFrom:str radix:10. |
|
942 |
"/ min isNil ifTrue:[ |
|
943 |
"/ min := 60 * fraction. |
|
944 |
"/ fraction := min fractionPart. |
|
945 |
"/ min := min truncated. |
|
946 |
"/ ]. |
|
947 |
"/ (sec isNil and:[fraction ~= 0])ifTrue:[ |
|
948 |
"/ sec := 60 * fraction. |
|
949 |
"/ fraction := sec fractionPart. |
|
950 |
"/ sec := sec truncated. |
|
951 |
"/ ]. |
|
952 |
"/ fraction ~= 0 ifTrue:[ |
|
953 |
"/ millis := (1000 * fraction) rounded. "/ mhmh - should it be truncated ? |
|
954 |
"/ ]. |
|
955 |
"/ ]. |
|
16945 | 956 |
"/ |
16914 | 957 |
"/ peekChar := str peekOrNil. |
958 |
"/ peekChar notNil ifTrue:[ |
|
959 |
"/ peekChar == $Z ifTrue:[ |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
960 |
"/ str next. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
961 |
"/ isUtcTime := true. |
16914 | 962 |
"/ ] ifFalse:[ |
963 |
"/ ((peekChar == $+) or:[peekChar == $- ]) ifTrue:[ |
|
964 |
"/ str next. |
|
965 |
"/ self halt. |
|
966 |
"/ isUtcTime := true. |
|
967 |
"/ ] |
|
968 |
"/ ] |
|
969 |
"/ ]. |
|
970 |
"/ ]. |
|
971 |
"/ |
|
972 |
"/ min isNil ifTrue:[min := 0]. |
|
973 |
"/ sec isNil ifTrue:[sec := 0]. |
|
974 |
"/ |
|
975 |
"/ "special check - only 24:00:00 is allowed; |
|
976 |
"/ every time after that must wrap" |
|
977 |
"/ hour == 24 ifTrue:[ |
|
978 |
"/ (min ~~ 0 or:[sec ~~ 0 or:[millis ~~ 0]]) ifTrue:[ TimeConversionError raiseErrorString:' - bad hour' ]. |
|
979 |
"/ ]. |
|
980 |
"/ |
|
981 |
"/ isUtcTime ifTrue:[ |
|
16945 | 982 |
"/ ^ self |
983 |
"/ UTCYear:year month:month day:day |
|
16914 | 984 |
"/ hour:hour minute:min second:sec millisecond:millis. |
985 |
"/ ] ifFalse:[ |
|
16945 | 986 |
"/ ^ self |
987 |
"/ year:year month:month day:day |
|
16914 | 988 |
"/ hour:hour minute:min second:sec millisecond:millis. |
989 |
"/ ] |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
990 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
991 |
" |
16945 | 992 |
Timestamp readIso8601FormatFrom:'1995-02-20T13:11:06.123' |
993 |
Timestamp readIso8601FormatFrom:'1995-02-20T13:11:06' |
|
994 |
Timestamp readIso8601FormatFrom:'1995-02T13:11:06' |
|
995 |
Timestamp readIso8601FormatFrom:'1995T13:11:06' |
|
996 |
Timestamp readIso8601FormatFrom:'1995T13:11' |
|
997 |
Timestamp readIso8601FormatFrom:'1995T13:11.5' |
|
998 |
Timestamp readIso8601FormatFrom:'1995T13:11,5' |
|
999 |
Timestamp readIso8601FormatFrom:'1995T13' |
|
1000 |
Timestamp readIso8601FormatFrom:'1995T13.25' |
|
1001 |
Timestamp readIso8601FormatFrom:'1995T13.333333' |
|
1002 |
Timestamp readIso8601FormatFrom:'1995' |
|
1003 |
||
1004 |
Timestamp readIso8601FormatFrom:'2014W40' -> 29.sep.2014 |
|
1005 |
Timestamp readIso8601FormatFrom:'2014W44-4' -> 30.oct.2014 |
|
1006 |
Timestamp readIso8601FormatFrom:'2014W1' -> 30.dec.2013 !!!!!! (this week starts in the previous year) |
|
1007 |
Timestamp readIso8601FormatFrom:'2014W1-1' -> same 30.dec.2013 !!!!!! (this week starts in the previous year) |
|
1008 |
Timestamp readIso8601FormatFrom:'2014W1-2' -> 31.dec.2013 !!!!!! (this week starts in the previous year) |
|
1009 |
Timestamp readIso8601FormatFrom:'2014W1-3' -> 1.jan.2014 !!!!!! (this week starts in the previous year) |
|
1010 |
||
1011 |
Timestamp readIso8601FormatFrom:'1995-02-20 13:11:06' |
|
1012 |
Timestamp readIso8601FormatFrom:'1995-02-20 13:11:06Z' |
|
1013 |
||
1014 |
Timestamp readIso8601FormatFrom:'1995-02-20 13:11' |
|
1015 |
Timestamp readIso8601FormatFrom:'1995-02-20 13' |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1016 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1017 |
24 is allowed with ISO, and is 00:00 of the next day: |
16945 | 1018 |
Timestamp readIso8601FormatFrom:'1995-02-20 24:00:00' |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1019 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1020 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1021 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1022 |
readIso8601FormatFrom:aStringOrStream yearAlreadyRead:yearOrNil onError:exceptionValue |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1023 |
"common helper for read methods." |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1024 |
|
21388 | 1025 |
^ [ |
1026 |
self readIso8601FormatFrom:aStringOrStream yearAlreadyRead:yearOrNil |
|
1027 |
] on:ConversionError do:exceptionValue. |
|
1028 |
||
1029 |
"Modified: / 09-02-2017 / 10:01:27 / stefan" |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1030 |
! ! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1031 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1032 |
!Timestamp class methodsFor:'reading'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1033 |
|
14713 | 1034 |
readFrom:aStringOrStream format:formatString language:languageOrNil onError:exceptionalValue |
1035 |
"return a new Timestamp, reading a printed representation from aStream using a formatString. |
|
1036 |
The formatString is similar to the one used when printing. |
|
16945 | 1037 |
On error, exceptionalValue is returned. |
16914 | 1038 |
If exceptionalValue is a one-arg block, an error message is passed as argument. |
1039 |
Format: |
|
18441 | 1040 |
%h hours, 00..23 (i.e. european) 0-padded to length 2 |
1041 |
%u hours, 00..12 (i.e. us) 0-padded to length 2 |
|
1042 |
%m minutes, 00..59 0-padded to length 2 |
|
1043 |
%s seconds, 00..59 0-padded to length 2 |
|
1044 |
%i milliseconds, 000..999 0-padded to length 3 |
|
19923 | 1045 |
%f fractional seconds any length, but only milliseconds kept |
22894 | 1046 |
%F fractional seconds any length, up to picoseconds kept |
18441 | 1047 |
%a am/pm |
22885 | 1048 |
%tz timezone |
18441 | 1049 |
|
1050 |
%d - day |
|
1051 |
%D - day |
|
1052 |
%(day) - day |
|
18447 | 1053 |
%(dayOfYear) - 1..365/366 |
18441 | 1054 |
|
1055 |
%(month) - month |
|
1056 |
||
1057 |
%(monthName) - monthName |
|
1058 |
||
1059 |
%(year) - year, full 4 digits |
|
1060 |
%Y - year, last 2 digits only, |
|
1061 |
0..71 map to 2000..2071; |
|
1062 |
72..99 map to 1972..1999; |
|
1063 |
%Y1900 - year, last 2 digits only, map to 1900..1999 |
|
1064 |
%Y2000 - year, last 2 digits only, map to 2000..2099 |
|
18445 | 1065 |
%Y1950 - year, last 2 digits only, map to 1950..2049 |
1066 |
%Y1980 - year, last 2 digits only, map to 1980..2079 |
|
22873 | 1067 |
%Y1970 - year, last 2 digits only, map to 1970..2069 |
14713 | 1068 |
|
16914 | 1069 |
an optional length after the % gives a field length; |
18441 | 1070 |
i.e. %2h%2m%2s parses '123557' as 12:35:37 |
1071 |
and '%4(year)%2(month)%2(day)' parses '20060620' as 2006-06-20 00:00:00 |
|
16914 | 1072 |
|
1073 |
Please consider using a standard format, such as iso8601. |
|
1074 |
" |
|
14713 | 1075 |
|
18447 | 1076 |
|day month year dayOfYear monthAndDay |
14713 | 1077 |
hour minute second millisecond |
16945 | 1078 |
utcOffset inStream formatStream error fChar format itemHandler |
23239 | 1079 |
len s fractionString fraction picos ts| |
14713 | 1080 |
|
16945 | 1081 |
error := [:msg | |
18441 | 1082 |
exceptionalValue isBlock ifTrue:[ |
18445 | 1083 |
^ exceptionalValue valueWithOptionalArgument:'format error' |
18441 | 1084 |
] ifFalse:[ |
1085 |
^ exceptionalValue value |
|
1086 |
]. |
|
1087 |
]. |
|
14713 | 1088 |
|
1089 |
itemHandler := [:format | |
|
18441 | 1090 |
|input| |
18445 | 1091 |
|
18441 | 1092 |
input := len isNil ifTrue:[ inStream ] ifFalse:[ inStream next: len ]. |
1093 |
||
1094 |
( #('d' 'D' 'day' ) includes:format ) ifTrue:[ |
|
1095 |
day := Integer readFrom:input onError:[ error value:'invalid day' ]. |
|
1096 |
||
1097 |
] ifFalse:[ ( format = 'month' ) ifTrue:[ |
|
1098 |
month := Integer readFrom:input onError:[ error value:'invalid month' ]. |
|
1099 |
||
1100 |
] ifFalse:[ ( format = 'year' or:[ format = 'y' ]) ifTrue:[ |
|
1101 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
|
1102 |
||
1103 |
] ifFalse:[ ( format = 'Y' ) ifTrue:[ |
|
1104 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
|
1105 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
18449 | 1106 |
(year < 70) ifTrue:[ |
1107 |
year := year + 2000 |
|
1108 |
] ifFalse:[ |
|
18441 | 1109 |
year := year + 1900 |
1110 |
] |
|
1111 |
||
1112 |
] ifFalse:[ (format = 'monthName') ifTrue:[ |
|
1113 |
s := input nextMatching:[:c | c isLetter] thenMatching:[:c | c isLetter]. |
|
1114 |
month := Date indexOfMonth:s asLowercase language:languageOrNil |
|
1115 |
||
18447 | 1116 |
] ifFalse:[ (format = 'dayOfYear') ifTrue:[ |
1117 |
dayOfYear := Integer readFrom:input onError:[ error value:'invalid day of year' ]. |
|
1118 |
||
18445 | 1119 |
] ifFalse:[ ( format sameAs: 'Y1900' ) ifTrue:[ |
18441 | 1120 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
1121 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
1122 |
year := year + 1900 |
|
1123 |
||
18445 | 1124 |
] ifFalse:[ ( format sameAs: 'Y1950' ) ifTrue:[ |
1125 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
|
1126 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
1127 |
(year between:0 and: 49) ifTrue:[ |
|
1128 |
year := year + 2000 |
|
1129 |
] ifFalse:[ |
|
1130 |
year := year + 1900 |
|
1131 |
] |
|
1132 |
||
1133 |
] ifFalse:[ ( format sameAs: 'Y1980' ) ifTrue:[ |
|
1134 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
|
1135 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
1136 |
(year between:0 and: 79) ifTrue:[ |
|
1137 |
year := year + 2000 |
|
1138 |
] ifFalse:[ |
|
1139 |
year := year + 1900 |
|
1140 |
] |
|
1141 |
||
22873 | 1142 |
] ifFalse:[ ( format sameAs: 'Y1970' ) ifTrue:[ |
1143 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
|
1144 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
1145 |
(year between:0 and: 69) ifTrue:[ |
|
1146 |
year := year + 2000 |
|
1147 |
] ifFalse:[ |
|
1148 |
year := year + 1900 |
|
1149 |
] |
|
1150 |
||
18445 | 1151 |
] ifFalse:[ ( format sameAs: 'Y2000' ) ifTrue:[ |
18441 | 1152 |
year := Integer readFrom:input onError:[ error value:'invalid year' ]. |
1153 |
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ]. |
|
1154 |
year := year + 2000 |
|
1155 |
||
1156 |
] ifFalse:[ ( format = 'h' or:[ format = 'H' ]) ifTrue:[ |
|
1157 |
hour := Integer readFrom:input onError:[ error value:'invalid hour' ]. |
|
1158 |
||
1159 |
] ifFalse:[ ( format = 'u' or:[ format = 'U']) ifTrue:[ |
|
1160 |
hour := Integer readFrom:input onError:[ error value:'invalid hour' ]. |
|
1161 |
||
1162 |
] ifFalse:[ ( format = 'm' or:[ format = 'M' ]) ifTrue:[ |
|
1163 |
minute := Integer readFrom:input onError:[ error value:'invalid minute' ]. |
|
1164 |
||
1165 |
] ifFalse:[ ( format = 's' or:[ format = 'S' ]) ifTrue:[ |
|
1166 |
second := Integer readFrom:input onError:[ error value:'invalid second' ]. |
|
1167 |
||
1168 |
] ifFalse:[ ( format = 'i' or:[ format = 'I' ]) ifTrue:[ |
|
1169 |
millisecond := Integer readFrom:input onError:[ error value:'invalid month' ]. |
|
1170 |
||
19923 | 1171 |
] ifFalse:[ ( format = 'f' or:[ format = 'F' ]) ifTrue:[ |
1172 |
fractionString := input upToMatching:[:ch | ch isDigit not]. |
|
1173 |
fraction := FixedPoint readFrom:'0.',fractionString. |
|
22894 | 1174 |
format = 'f' ifTrue:[ |
1175 |
millisecond := (fraction * 1000) truncated. |
|
1176 |
] ifFalse:[ |
|
1177 |
picos := (fraction * 1000*1000*1000*1000) truncated. |
|
1178 |
millisecond := picos // (1000*1000*1000). |
|
1179 |
picos := picos \\ (1000*1000*1000). |
|
1180 |
]. |
|
18441 | 1181 |
] ifFalse:[ ( format = 'tz' ) ifTrue:[ |
1182 |
utcOffset := self utcOffsetFrom:input. |
|
1183 |
utcOffset isNil ifTrue:[ error value:'invalid timezone' ] |
|
1184 |
] ifFalse:[ ( format = 'a' ) ifTrue:[ |
|
1185 |
s := (input next:2) asLowercase. |
|
1186 |
s = 'am' ifTrue:[ |
|
1187 |
(hour between:0 and:12) ifFalse:[ error value:'invalid hour' ] |
|
1188 |
] ifFalse:[ |
|
1189 |
s = 'pm' ifTrue:[ |
|
1190 |
(hour between:1 and:12) ifFalse:[ error value:'invalid hour' ]. |
|
1191 |
hour := hour + 12. |
|
1192 |
] ifFalse:[ |
|
1193 |
error value:'invalid am/pm' |
|
1194 |
] |
|
1195 |
] |
|
1196 |
||
1197 |
] ifFalse:[ |
|
1198 |
error value:'unhandled format:',format |
|
22873 | 1199 |
]]]]]]]]]]]]]]]]]]] |
18445 | 1200 |
]. |
14713 | 1201 |
|
1202 |
hour := 0. |
|
1203 |
minute := 0. |
|
1204 |
second := 0. |
|
1205 |
millisecond := 0. |
|
1206 |
||
1207 |
inStream := aStringOrStream readStream. |
|
1208 |
formatStream := formatString readStream. |
|
1209 |
||
1210 |
[formatStream atEnd] whileFalse:[ |
|
18441 | 1211 |
fChar := formatStream next. |
1212 |
fChar = Character space ifTrue:[ |
|
1213 |
inStream peek isSeparator ifFalse:[ error value: 'format error; space expcected' ]. |
|
1214 |
inStream skipSeparators. |
|
1215 |
] ifFalse:[ |
|
1216 |
fChar == $% ifTrue:[ |
|
1217 |
len := nil. |
|
1218 |
(formatStream peek isDigit) ifTrue:[ |
|
1219 |
len := Integer readFrom:formatStream onError:[ error value: 'format error; invalid length' ] |
|
1220 |
]. |
|
1221 |
(formatStream peek == $() ifTrue:[ |
|
1222 |
formatStream next. |
|
1223 |
format := formatStream upTo:$). |
|
1224 |
] ifFalse:[ |
|
1225 |
(formatStream peek == ${) ifTrue:[ |
|
1226 |
formatStream next. |
|
1227 |
format := formatStream upTo:$}. |
|
1228 |
] ifFalse:[ |
|
1229 |
(formatStream peek isLetter) ifTrue:[ |
|
1230 |
format := formatStream nextAlphaNumericWord. |
|
1231 |
] ifFalse:[ |
|
1232 |
error value:'unhandled format:',formatStream peek |
|
1233 |
] |
|
1234 |
] |
|
1235 |
]. |
|
1236 |
itemHandler value:format. |
|
1237 |
] ifFalse:[ |
|
1238 |
inStream peek = fChar ifFalse:[^ error value: 'format error; ',fChar,' expcected']. |
|
1239 |
inStream next. |
|
1240 |
] |
|
1241 |
]. |
|
14713 | 1242 |
]. |
1243 |
||
1244 |
year isNil ifTrue:[ |
|
23239 | 1245 |
year := Timestamp now year |
14713 | 1246 |
]. |
18447 | 1247 |
|
1248 |
dayOfYear notNil ifTrue:[ |
|
1249 |
monthAndDay := Date monthAndDayFromDayInYear:dayOfYear forYear:year. |
|
1250 |
month := (monthAndDay at:1). |
|
1251 |
day := (monthAndDay at:2). |
|
1252 |
]. |
|
22885 | 1253 |
|
1254 |
utcOffset notNil ifTrue:[ |
|
1255 |
utcOffset == 0 ifTrue:[ |
|
22894 | 1256 |
ts := (UtcTimestamp |
22885 | 1257 |
year:year month:month day:day |
1258 |
hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond) |
|
22894 | 1259 |
] ifFalse:[ |
1260 |
ts := ((TZTimestamp basicNew |
|
1261 |
setOSTimeFromUTCYear:year month:month day:day |
|
1262 |
hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond |
|
1263 |
) utcOffset:utcOffset |
|
1264 |
) addSeconds:utcOffset. |
|
1265 |
] |
|
1266 |
] ifFalse:[ |
|
1267 |
ts := (self |
|
22885 | 1268 |
year:year month:month day:day |
22894 | 1269 |
hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond) |
22885 | 1270 |
]. |
22894 | 1271 |
picos notNil ifTrue:[ |
1272 |
ts additionalPicoseconds:picos |
|
1273 |
]. |
|
1274 |
^ ts. |
|
14713 | 1275 |
|
1276 |
" |
|
16945 | 1277 |
Timestamp readFrom:'20-2-1995 13:11:06' format:'%day-%month-%year %h:%m:%s' language:nil onError:[self halt] |
1278 |
Timestamp readFrom:'20021995131106' format:'%2d%2month%4y%2h%2m%2s' language:nil onError:[self halt] |
|
18445 | 1279 |
Timestamp readFrom:'200295131106' format:'%2d%2month%2y%2h%2m%2s' language:nil onError:[self halt] |
1280 |
Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1900)%2h%2m%2s' language:nil onError:[self halt] |
|
1281 |
Timestamp readFrom:'200260131106' format:'%2d%2month%2(y2000)%2h%2m%2s' language:nil onError:[self halt] |
|
1282 |
Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1950)%2h%2m%2s' language:nil onError:[self halt] |
|
1283 |
Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1980)%2h%2m%2s' language:nil onError:[self halt] |
|
14713 | 1284 |
Timestamp readFrom:'March 7 2009 7:30pm EST' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt] |
16945 | 1285 |
Timestamp readFrom:'March 7 2009 7:30pm UTC' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt] |
18447 | 1286 |
Timestamp readFrom:'2015103' format:'%4y%3dayOfYear' onError:[self halt] |
19923 | 1287 |
|
1288 |
Timestamp readFrom:'20-2-1995 13:11:06.999' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self halt] |
|
1289 |
Timestamp readFrom:'20-2-1995 13:11:06.100' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self halt] |
|
1290 |
Timestamp readFrom:'20-2-1995 13:11:06.010' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self halt] |
|
1291 |
||
1292 |
Timestamp readFrom:'20-2-1995 13:11:06.1' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self halt] |
|
1293 |
Timestamp readFrom:'20-2-1995 13:11:06.01' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self halt] |
|
1294 |
Timestamp readFrom:'20-2-1995 13:11:06.001' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self halt] |
|
1295 |
Timestamp readFrom:'20-2-1995 13:11:06.12345' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self halt] |
|
14713 | 1296 |
" |
23239 | 1297 |
|
1298 |
"Modified: / 27-07-2018 / 11:57:59 / Stefan Vogel" |
|
14713 | 1299 |
! |
1300 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
1301 |
readFrom:aStringOrStream onError:exceptionBlock |
8246 | 1302 |
"return a new Timestamp, reading a printed representation from aStream. |
275 | 1303 |
The string is interpreted as 24 hour format, as printed. |
16945 | 1304 |
Notice, that this is not the storeString format and |
4898 | 1305 |
is different from the format expected by readFrom:. |
16914 | 1306 |
The format read here is either dd-mm-yyyy hh:mm:ss.iii |
1307 |
or iso8601 (if the first integer is >31). |
|
1308 |
||
1309 |
Please consider using a standard format, such as iso8601." |
|
275 | 1310 |
|
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1311 |
|stream newTime| |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1312 |
|
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1313 |
stream := aStringOrStream readStream. |
275 | 1314 |
|
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7058
diff
changeset
|
1315 |
Error handle:[:ex | |
17006 | 1316 |
newTime := super readFrom:stream onError:exceptionBlock |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3510
diff
changeset
|
1317 |
] do:[ |
17006 | 1318 |
newTime := self basicReadFrom:stream. |
275 | 1319 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3510
diff
changeset
|
1320 |
^ newTime |
275 | 1321 |
|
1322 |
" |
|
16945 | 1323 |
Timestamp readFrom:'20-2-1995 13:11:06' |
1324 |
Timestamp readFrom:'20-2-1995 13:11' |
|
1325 |
Timestamp readFrom:'20-2-2001 13:11' |
|
1326 |
Timestamp readFrom:'20-2-1995 13:11:06.100' |
|
1327 |
Timestamp readFrom:'32-2-1995 13:11:06.100' onError:'wrong' |
|
1328 |
Timestamp readFrom:'32-foo-1995 13:11:06.100' onError:'wrong' |
|
1329 |
Timestamp readFrom:'20-2-1995 24:01:00.100' onError:'wrong' |
|
1330 |
Timestamp readFrom:'20-2-1995 24:00:01.100' onError:'wrong' |
|
1331 |
Timestamp readFrom:'2002-08-02 24:00:01.100' onError:'wrong' |
|
1332 |
Timestamp readFrom:'foo' onError:'wrong' |
|
1333 |
Timestamp readFrom:(Timestamp now storeString) onError:'wrong' |
|
275 | 1334 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
1335 |
|
4378 | 1336 |
"Modified: / 8.10.1996 / 19:25:59 / cg" |
1337 |
"Modified: / 13.7.1999 / 12:31:14 / stefan" |
|
2311 | 1338 |
! |
1339 |
||
16945 | 1340 |
readGeneralizedFrom:aStringOrStream |
9620 | 1341 |
"return a new Timestamp, reading a printed representation from aStream. |
1342 |
The format read here is either |
|
19897 | 1343 |
yyyymmddHHMMSS.iii+uuuu, which is the ASN1 GeneralizedTime format. |
9620 | 1344 |
or: |
19897 | 1345 |
yyyy-mm-dd HH:MM:SS.iii +uuuu. |
12166 | 1346 |
The string is interpreted as 24 hour format, as printed. |
1347 |
||
1348 |
This format is used for BER specification of the ASN.1 GeneralizedTime as defined in X.208 Sec. 33, |
|
1349 |
so read this before changing anything here. |
|
1350 |
||
16914 | 1351 |
New applications should consider using a standard format, such as iso8601. |
1352 |
||
16945 | 1353 |
Notice, that this is not the storeString format and |
12166 | 1354 |
is different from the format expected by readFrom:. |
9620 | 1355 |
" |
16914 | 1356 |
|
16945 | 1357 |
^ self |
19897 | 1358 |
readGeneralizedFrom:aStringOrStream |
1359 |
short:false |
|
1360 |
onError:[ |
|
1361 |
self conversionErrorSignal raiseWith:aStringOrStream errorString:' - Timestamp GeneralizedTime format error' |
|
1362 |
]. |
|
9620 | 1363 |
|
1364 |
"Created: / 22-08-2006 / 16:05:55 / cg" |
|
1365 |
! |
|
1366 |
||
5292
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1367 |
readGeneralizedFrom:aStringOrStream onError:exceptionBlock |
8246 | 1368 |
"return a new Timestamp, reading a printed representation from aStream. |
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1369 |
The format read here is either |
17006 | 1370 |
yyyymmddHHMMSS.iii+uuuu, which is the ASN1 GeneralizedTime format. |
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1371 |
or: |
17006 | 1372 |
yyyy-mm-dd HH:MM:SS.iii +uuuu. |
12166 | 1373 |
The string is interpreted as 24 hour format, as printed. |
1374 |
||
1375 |
This format is used for BER specification of the ASN.1 GeneralizedTime as defined in X.208 Sec. 33, |
|
1376 |
so read this before changing anything here. |
|
1377 |
||
16914 | 1378 |
New applications should consider using a standard format, such as iso8601. |
1379 |
||
16945 | 1380 |
Notice, that this is not the storeString format and |
12166 | 1381 |
is different from the format expected by readFrom:. |
1382 |
" |
|
1383 |
||
1384 |
^ self readGeneralizedFrom:aStringOrStream short:false onError:exceptionBlock |
|
1385 |
||
1386 |
" |
|
1387 |
|s| |
|
1388 |
s := '' writeStream. |
|
1389 |
Timestamp now printGeneralizedOn:s. |
|
1390 |
Timestamp readGeneralizedFrom:s contents onError:[] |
|
1391 |
||
1392 |
Daylight saving time: |
|
1393 |
||
1394 |
Timestamp readGeneralizedFrom:'20000718120000Z' onError:[] |
|
1395 |
Timestamp readGeneralizedFrom:'20000718120000+0200' onError:[] |
|
1396 |
Timestamp readGeneralizedFrom:'20000718120000+0300' onError:[] |
|
1397 |
||
1398 |
No daylight saving time: |
|
1399 |
||
1400 |
Timestamp readGeneralizedFrom:'20000202120000Z' onError:[] |
|
1401 |
Timestamp readGeneralizedFrom:'20000202120000+0100' onError:[] |
|
1402 |
Timestamp readGeneralizedFrom:'20000202120000+0200' onError:[] |
|
1403 |
||
16945 | 1404 |
Timestamp readGeneralizedFrom:'19950220131106' onError:[] |
1405 |
Timestamp readGeneralizedFrom:'199502201311' onError:[] |
|
1406 |
Timestamp readGeneralizedFrom:'19950220131106.100' onError:[] |
|
1407 |
Timestamp readGeneralizedFrom:'19950232131106.100' onError:'wrong' |
|
1408 |
Timestamp readGeneralizedFrom:'19950fo2131106.100' onError:'wrong' |
|
1409 |
Timestamp readGeneralizedFrom:'foo' onError:'wrong' |
|
1410 |
||
1411 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00' onError:[] |
|
12166 | 1412 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100' onError:[] |
16945 | 1413 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100 +0100' onError:[] |
12166 | 1414 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 -0100' onError:[] |
16945 | 1415 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 +0000' onError:[] |
12166 | 1416 |
" |
1417 |
||
1418 |
"Modified: / 13-07-1999 / 12:31:14 / stefan" |
|
1419 |
"Modified: / 22-08-2006 / 12:30:11 / cg" |
|
1420 |
! |
|
1421 |
||
1422 |
readGeneralizedFrom:aStringOrStream short:shortFormat onError:exceptionBlock |
|
1423 |
"return a new Timestamp, reading a printed representation from aStream. |
|
1424 |
The long format read here is either |
|
18761 | 1425 |
yyyymmddHHMMSS.iii+uuuu, which is the ASN1 GeneralizedTime format. |
1426 |
or: |
|
1427 |
yyyy-mm-dd HH:MM:SS.iii +uuuu. |
|
12166 | 1428 |
The (not recommended) short forms are: |
18761 | 1429 |
yymmddHHMMSS.iii+uuuu, which is the ASN1 GeneralizedTime format. |
1430 |
or: |
|
1431 |
yy-mm-dd HH:MM:SS.iii +uuuu. |
|
12166 | 1432 |
The string is interpreted as 24 hour format, as printed. |
1433 |
||
1434 |
This format is used for BER specification of the ASN.1 GeneralizedTime and |
|
1435 |
UTCTime as defined in X.208 Sec. 33, so read this before changing anything here. |
|
1436 |
The short form is no longer recommended. |
|
1437 |
||
16914 | 1438 |
New applications should consider using a standard format, such as iso8601. |
1439 |
||
16945 | 1440 |
Notice, that this is not the storeString format and |
12166 | 1441 |
is different from the format expected by readFrom:. |
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1442 |
" |
5292
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1443 |
|
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1444 |
|
6624
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6047
diff
changeset
|
1445 |
^ [ |
22653 | 1446 |
|newTime str day month year hour min sec millis c fraction| |
18761 | 1447 |
|
1448 |
sec := millis := 0. |
|
1449 |
str := aStringOrStream readStream. |
|
1450 |
||
1451 |
shortFormat ifTrue:[ |
|
1452 |
year := Integer readFrom:(str next:2). |
|
1453 |
year < 50 ifTrue:[ |
|
1454 |
year := year + 2000. |
|
1455 |
] ifFalse:[ |
|
1456 |
year := year + 1900. |
|
1457 |
]. |
|
1458 |
] ifFalse:[ |
|
1459 |
year := Integer readFrom:(str next:4). |
|
1460 |
]. |
|
1461 |
str peek == $- ifTrue:[ str next]. |
|
1462 |
month := Integer readFrom:(str next:2). |
|
1463 |
(month between:1 and:12) ifFalse:[^ exceptionBlock value]. |
|
1464 |
str peek == $- ifTrue:[ str next]. |
|
1465 |
day := Integer readFrom:(str next:2). |
|
1466 |
(day between:1 and:31) ifFalse:[^ exceptionBlock value]. |
|
1467 |
||
1468 |
str skipSeparators. |
|
1469 |
hour:= Integer readFrom:(str next:2). |
|
1470 |
(hour between:0 and:24) ifFalse:[^ exceptionBlock value]. |
|
1471 |
str peek == $: ifTrue:[ str next]. |
|
1472 |
min:= Integer readFrom:(str next:2). |
|
1473 |
(min between:0 and:59) ifFalse:[^ exceptionBlock value]. |
|
1474 |
str atEnd ifFalse:[ |
|
1475 |
str peek == $: ifTrue:[ str next]. |
|
1476 |
sec := Integer readFrom:(str next:2). |
|
1477 |
(sec between:0 and:59) ifFalse:[^ exceptionBlock value]. |
|
1478 |
str atEnd ifFalse:[ |
|
1479 |
str peek == $. ifTrue:[ |
|
1480 |
str next. |
|
22653 | 1481 |
"/ the old code here was wrong in assuming that exactly 3 digits |
1482 |
"/ are coming; thus hh:mm:ss.1 was interpreted as 1ms (instead of 100) |
|
1483 |
"/ thus: count the zeros... |
|
1484 |
str peek isDigit ifTrue:[ |
|
1485 |
fraction := Number readMantissaFrom:str radix:10. |
|
1486 |
fraction isNil ifTrue:[^ exceptionBlock value]. |
|
1487 |
millis := (fraction * 1000) rounded. |
|
1488 |
] ifFalse:[ |
|
1489 |
millis := 0 |
|
1490 |
]. |
|
18761 | 1491 |
]. |
1492 |
str skipSeparators. |
|
1493 |
]. |
|
1494 |
]. |
|
1495 |
||
1496 |
str atEnd ifTrue:[ |
|
1497 |
"/ this is local time |
|
1498 |
newTime := self year:year month:month day:day |
|
1499 |
hour:hour minute:min second:sec millisecond:millis. |
|
1500 |
] ifFalse:[ |
|
1501 |
c := str next. |
|
1502 |
c ~~ $Z ifTrue:[ |
|
1503 |
|tzh tzmin| |
|
1504 |
tzh := Integer readFrom:(str next:2). |
|
1505 |
tzmin := Integer readFrom:(str next:2). |
|
1506 |
c == $+ ifTrue:[ |
|
1507 |
"the timezone is ahead of UTC or EAST from Greenwich: subtract hours and minutes" |
|
1508 |
hour := hour - tzh. |
|
1509 |
min := min - tzmin. |
|
1510 |
] ifFalse:[ |
|
1511 |
c == $- ifTrue:[ |
|
1512 |
"the timezone is behind of UTC or WEST from Greenwich: add hours and minutes" |
|
1513 |
hour := hour + tzh. |
|
1514 |
min := min + tzmin. |
|
1515 |
] ifFalse:exceptionBlock |
|
1516 |
]. |
|
1517 |
]. |
|
1518 |
"this is UTC time" |
|
1519 |
newTime := self UTCYear:year month:month day:day |
|
1520 |
hour:hour minute:min second:sec millisecond:millis. |
|
1521 |
]. |
|
1522 |
newTime |
|
6624
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6047
diff
changeset
|
1523 |
] on:Error do:exceptionBlock. |
5292
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1524 |
|
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1525 |
" |
5656
dbc52ea02716
Fix UTC Time w.r.t. Daylight saving time.
Stefan Vogel <sv@exept.de>
parents:
5572
diff
changeset
|
1526 |
|s| |
dbc52ea02716
Fix UTC Time w.r.t. Daylight saving time.
Stefan Vogel <sv@exept.de>
parents:
5572
diff
changeset
|
1527 |
s := '' writeStream. |
8246 | 1528 |
Timestamp now printGeneralizedOn:s. |
1529 |
Timestamp readGeneralizedFrom:s contents onError:[] |
|
5656
dbc52ea02716
Fix UTC Time w.r.t. Daylight saving time.
Stefan Vogel <sv@exept.de>
parents:
5572
diff
changeset
|
1530 |
|
16945 | 1531 |
Timestamp readGeneralizedFrom:'20080718120000+0300' short:false onError:[] |
1532 |
Timestamp readGeneralizedFrom:'080718120000+0300' short:true onError:[] |
|
1533 |
Timestamp readGeneralizedFrom:'990718120000+0300' short:true onError:[] |
|
12166 | 1534 |
|
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1535 |
Daylight saving time: |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1536 |
|
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1537 |
Timestamp readGeneralizedFrom:'20000718120000Z' onError:[] |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1538 |
Timestamp readGeneralizedFrom:'20000718120000+0200' onError:[] |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1539 |
Timestamp readGeneralizedFrom:'20000718120000+0300' onError:[] |
5656
dbc52ea02716
Fix UTC Time w.r.t. Daylight saving time.
Stefan Vogel <sv@exept.de>
parents:
5572
diff
changeset
|
1540 |
|
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1541 |
No daylight saving time: |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1542 |
|
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1543 |
Timestamp readGeneralizedFrom:'20000202120000Z' onError:[] |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1544 |
Timestamp readGeneralizedFrom:'20000202120000+0100' onError:[] |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
1545 |
Timestamp readGeneralizedFrom:'20000202120000+0200' onError:[] |
5656
dbc52ea02716
Fix UTC Time w.r.t. Daylight saving time.
Stefan Vogel <sv@exept.de>
parents:
5572
diff
changeset
|
1546 |
|
16945 | 1547 |
Timestamp readGeneralizedFrom:'19950220131106' onError:[] |
1548 |
Timestamp readGeneralizedFrom:'199502201311' onError:[] |
|
1549 |
Timestamp readGeneralizedFrom:'19950220131106.100' onError:[] |
|
1550 |
Timestamp readGeneralizedFrom:'19950232131106.100' onError:'wrong' |
|
1551 |
Timestamp readGeneralizedFrom:'19950fo2131106.100' onError:'wrong' |
|
1552 |
Timestamp readGeneralizedFrom:'foo' onError:'wrong' |
|
1553 |
||
1554 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00' onError:[] |
|
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1555 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100' onError:[] |
16945 | 1556 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100 +0100' onError:[] |
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1557 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 -0100' onError:[] |
16945 | 1558 |
Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 +0000' onError:[] |
5292
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1559 |
" |
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1560 |
|
9619
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1561 |
"Modified: / 13-07-1999 / 12:31:14 / stefan" |
6f1fc553757f
skip dashes and colons in readGeneralizedFrom
Claus Gittinger <cg@exept.de>
parents:
9449
diff
changeset
|
1562 |
"Modified: / 22-08-2006 / 12:30:11 / cg" |
5292
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1563 |
! |
a3936f79327b
Fix #readGeneralizedFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
5291
diff
changeset
|
1564 |
|
16914 | 1565 |
readISO8601From:aStringOrStream |
22840 | 1566 |
"obsoleted due to uc/lc confusion" |
1567 |
||
21388 | 1568 |
<resource: #obsolete> |
1569 |
||
1570 |
^ self readIso8601FormatFrom:aStringOrStream |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1571 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1572 |
"Created: / 16-06-2005 / 16:13:36 / masca" |
21388 | 1573 |
"Modified: / 09-02-2017 / 10:04:53 / stefan" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1574 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
1575 |
|
16914 | 1576 |
readISO8601From:aStringOrStream onError:exceptionValue |
22840 | 1577 |
"obsoleted due to uc/lc confusion" |
1578 |
||
21388 | 1579 |
<resource: #obsolete> |
1580 |
||
1581 |
^ self readIso8601FormatFrom:aStringOrStream onError:exceptionValue |
|
1582 |
||
1583 |
"Modified: / 09-02-2017 / 10:05:56 / stefan" |
|
16914 | 1584 |
! |
1585 |
||
6684
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1586 |
readIso8601FormatFrom:aStringOrStream |
8246 | 1587 |
"return a new Timestamp, reading an iso8601 UTC representation from aStream. |
6684
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1588 |
Missing month/day values are replaced with 1; i.e. 1999T24:00 |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1589 |
is the same as 1999-01-01T24:00:00. |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1590 |
Missing minute, second and ms values are replaced with 0; |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1591 |
i.e. 1999T12 is the same as 1999-01-01T12:00:00.000. |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1592 |
Of course, a 24 hour clock is used. |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1593 |
On error, raise an exception. |
16914 | 1594 |
|
1595 |
Please use this format for all external representations - it's the standard." |
|
1596 |
||
1597 |
"changed to use the new reader" |
|
1598 |
||
16945 | 1599 |
^ TimestampISO8601Builder read:aStringOrStream withClass:self |
16914 | 1600 |
"/ ^ self |
1601 |
"/ readIso8601FormatFrom:aStringOrStream yearAlreadyRead:nil |
|
6684
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1602 |
|
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1603 |
" |
16921 | 1604 |
Timestamp readIso8601FormatFrom:'20141106T123843.299Z' |
1605 |
Timestamp readIso8601FormatFrom:'20141106T123843.299' |
|
1606 |
||
16945 | 1607 |
Timestamp readIso8601FormatFrom:'1995-02-20T13:11:06' |
1608 |
Timestamp readIso8601FormatFrom:'1995-02T13:11:06' |
|
1609 |
Timestamp readIso8601FormatFrom:'1995T13:11:06' |
|
1610 |
Timestamp readIso8601FormatFrom:'1995T13:11' |
|
1611 |
Timestamp readIso8601FormatFrom:'1995T13' |
|
1612 |
Timestamp readIso8601FormatFrom:'1995' |
|
1613 |
||
1614 |
Timestamp readIso8601FormatFrom:'1995-02-20 13:11:06' |
|
1615 |
Timestamp readIso8601FormatFrom:'1995-02-20 13:11' |
|
1616 |
Timestamp readIso8601FormatFrom:'1995-02-20 13' |
|
1617 |
||
1618 |
Timestamp readIso8601FormatFrom:'20141106T122831.894Z' |
|
16921 | 1619 |
|
6684
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1620 |
24 is allowed with ISO, and is 00:00 of the next day: |
16945 | 1621 |
Timestamp readIso8601FormatFrom:'1995-02-20 24:00:00' |
6684
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1622 |
|
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1623 |
" |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1624 |
! |
1f4d15ab7c4c
default printString is now iso format.
Claus Gittinger <cg@exept.de>
parents:
6634
diff
changeset
|
1625 |
|
8432 | 1626 |
readIso8601FormatFrom:aStringOrStream onError:exceptionValue |
1627 |
"return a new Timestamp, reading an iso8601 UTC representation from aStream. |
|
1628 |
Missing month/day values are replaced with 1; i.e. 1999T24:00 |
|
1629 |
is the same as 1999-01-01T24:00:00. |
|
1630 |
Missing minute, second and ms values are replaced with 0; |
|
1631 |
i.e. 1999T12 is the same as 1999-01-01T12:00:00.000. |
|
1632 |
Of course, a 24 hour clock is used. |
|
1633 |
On error, raise an exception. |
|
16914 | 1634 |
|
1635 |
Please use this format for all external representations - it's the standard." |
|
1636 |
||
1637 |
"/ changed to use the new reader |
|
21388 | 1638 |
^ [ |
1639 |
TimestampISO8601Builder read:aStringOrStream withClass:self |
|
1640 |
] on:ConversionError do:exceptionValue. |
|
16914 | 1641 |
|
1642 |
"/ ^ self |
|
16945 | 1643 |
"/ readIso8601FormatFrom:aStringOrStream |
1644 |
"/ yearAlreadyRead:nil |
|
16914 | 1645 |
"/ onError:exceptionValue |
21388 | 1646 |
|
1647 |
"Modified: / 09-02-2017 / 10:02:03 / stefan" |
|
8432 | 1648 |
! |
1649 |
||
11847 | 1650 |
readRFC1123FormatFrom:rfc1123String onError:exceptionBlock |
16914 | 1651 |
"please use this only for http-requests. |
1652 |
All other programs should use iso8601, which is the standard for times and dates. |
|
1653 |
||
16945 | 1654 |
All HTTP/1.0 date/time stamps must be represented in Universal Time (UT), |
1655 |
also known as Greenwich Mean Time (GMT), without exception. |
|
1656 |
This is indicated in the first two formats by the inclusion of |
|
1657 |
'GMT' as the three-letter abbreviation for time zone, |
|
16914 | 1658 |
and should be assumed when reading the asctime format. |
1659 |
||
1660 |
HTTP-date = rfc1123-date | rfc850-date | asctime-date |
|
1661 |
||
1662 |
rfc1123-date = wkday ',' SP date1 SP time SP 'GMT' |
|
1663 |
rfc850-date = weekday ',' SP date2 SP time SP 'GMT' |
|
1664 |
asctime-date = wkday SP date3 SP time SP 4DIGIT |
|
1665 |
||
1666 |
date1 = 2DIGIT SP month SP 4DIGIT |
|
17081 | 1667 |
; day month year (e.g., 02 Jun 1982) |
16914 | 1668 |
date2 = 2DIGIT '-' month '-' 2DIGIT |
17081 | 1669 |
; day-month-year (e.g., 02-Jun-82) |
16914 | 1670 |
date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) |
17081 | 1671 |
; month day (e.g., Jun 2) |
16914 | 1672 |
|
1673 |
time = 2DIGIT ':' 2DIGIT ':' 2DIGIT |
|
17081 | 1674 |
; 00:00:00 - 23:59:59 |
16914 | 1675 |
|
1676 |
wkday = 'Mon' | 'Tue' | 'Wed' |
|
17081 | 1677 |
| 'Thu' | 'Fri' | 'Sat' | 'Sun' |
16914 | 1678 |
|
1679 |
weekday = 'Monday' | 'Tuesday' | 'Wednesday' |
|
17081 | 1680 |
| 'Thursday' | 'Friday' | 'Saturday' | 'Sunday' |
16914 | 1681 |
|
1682 |
month = 'Jan' | 'Feb' | 'Mar' | 'Apr' |
|
17081 | 1683 |
| 'May' | 'Jun' | 'Jul' | 'Aug' |
1684 |
| 'Sep' | 'Oct' | 'Nov' | 'Dec' |
|
16914 | 1685 |
|
1686 |
Mon, 17 Aug 2009 11:11:15 GMT |
|
1687 |
||
1688 |
however, occasionally, someone presents us with non-UTC strings which include a timezone; |
|
1689 |
thus, this also supports: |
|
17081 | 1690 |
Mon, 17 Aug 2009 11:11:15 +xxxx |
1691 |
Mon, 17 Aug 2009 11:11:15 -xxxx |
|
16914 | 1692 |
and: |
17081 | 1693 |
Mon, 17 Aug 2009 11:11:15 PST |
16914 | 1694 |
" |
14713 | 1695 |
|
1696 |
|parts indexModifier utcOffsetString utcOffset day year time monthName month| |
|
11847 | 1697 |
|
1698 |
rfc1123String isEmptyOrNil ifTrue:[^ exceptionBlock value]. |
|
1699 |
||
17081 | 1700 |
parts := rfc1123String asCollectionOfSubstringsSeparatedBy:Character space. |
11862 | 1701 |
parts size == 6 ifTrue:[ |
17081 | 1702 |
indexModifier := 0. |
11862 | 1703 |
] ifFalse:[ |
17081 | 1704 |
parts size == 5 ifTrue:[ |
1705 |
indexModifier := -1. |
|
1706 |
] ifFalse:[ |
|
1707 |
^ exceptionBlock value |
|
1708 |
]. |
|
11862 | 1709 |
]. |
11847 | 1710 |
|
24404 | 1711 |
(parts at:2 + indexModifier) first isDigit ifTrue:[ |
1712 |
day := Integer readFrom:(parts at:2 + indexModifier) onError:[^ exceptionBlock]. |
|
1713 |
year := Integer readFrom:(parts at:4 + indexModifier) onError:[^ exceptionBlock]. |
|
1714 |
monthName := parts at:3 + indexModifier. |
|
1715 |
time := Time readFrom:(parts at:5 + indexModifier) onError:[^ exceptionBlock]. |
|
1716 |
] ifFalse:[ |
|
1717 |
monthName := (parts at:2 + indexModifier). |
|
1718 |
day := Integer readFrom:(parts at:3 + indexModifier) onError:[^ exceptionBlock]. |
|
1719 |
year := Integer readFrom:(parts at:5 + indexModifier) onError:[^ exceptionBlock]. |
|
1720 |
time := Time readFrom:(parts at:4 + indexModifier) onError:[^ exceptionBlock]. |
|
1721 |
]. |
|
11847 | 1722 |
|
17081 | 1723 |
month := Date indexOfMonth:monthName language:#en. |
1724 |
month = 0 ifTrue:[^ exceptionBlock value]. |
|
1725 |
||
1726 |
utcOffsetString := parts at:6 + indexModifier ifAbsent:nil. |
|
1727 |
utcOffset := self utcOffsetFrom:utcOffsetString. |
|
1728 |
||
1729 |
(utcOffset isNil or:[utcOffset = 0]) ifTrue:[ |
|
1730 |
^ UtcTimestamp year:year month:month day:day hour:time hour minute:time minute second:time second millisecond:0. |
|
1731 |
]. |
|
22894 | 1732 |
^ ((TZTimestamp new |
1733 |
setOSTimeFromUTCYear:year month:month day:day |
|
1734 |
hour:time hour minute:time minute second:time second millisecond:0 |
|
1735 |
) utcOffset:utcOffset |
|
1736 |
) addSeconds:utcOffset. |
|
14713 | 1737 |
|
1738 |
" |
|
17081 | 1739 |
self readRFC1123FormatFrom:'Mon, 17 Aug 2009 11:11:15 GMT' onError:nil |
1740 |
self readRFC1123FormatFrom:'Mon, 17 Aug 2009 11:11:15 UTC' onError:nil |
|
16945 | 1741 |
self readRFC1123FormatFrom:'Mon, 17 Aug 2009 11:11:15 PST' onError:nil |
1742 |
self readRFC1123FormatFrom:'Mon, 17 Aug 2009 11:11:15 PDT' onError:nil |
|
1743 |
self readRFC1123FormatFrom:'Mon, 17 Aug 2009 11:11:15 +0100' onError:nil |
|
17081 | 1744 |
self readRFC1123FormatFrom:'17 Aug 2009 11:11:15 +0100' onError:nil |
24404 | 1745 |
self readRFC1123FormatFrom:'Thu Jul 4 15:04:40 2019 +0200' onError:nil |
14713 | 1746 |
" |
13085
ee5674e04bd6
changed: #readRFC1123FormatFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
12939
diff
changeset
|
1747 |
|
ee5674e04bd6
changed: #readRFC1123FormatFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
12939
diff
changeset
|
1748 |
"Modified: / 05-10-2010 / 16:05:32 / cg" |
14713 | 1749 |
! |
1750 |
||
16986 | 1751 |
timezoneInfo |
1752 |
"return a table containing timezone information. |
|
17006 | 1753 |
This may or may not be correct by the time you read this. |
16986 | 1754 |
It is recommended to add explicit information in the form of +hh:mm to a printed |
16990 | 1755 |
representation, instead of using names. |
1756 |
||
19624 | 1757 |
As this is searched for when reverse converting from utcOffset to TZName" |
1758 |
||
1759 |
"/ the table below can be customized via a setter, |
|
1760 |
"/ if an application knows better |
|
1761 |
"/ (reading a system timeZone table, or limiting the table to military/non-military names only, for example) |
|
1762 |
||
1763 |
TimeZoneInfo notNil ifTrue:[^ TimeZoneInfo]. |
|
1764 |
||
1765 |
"/ table is name, utcOffset(minutes), DST-flag, startDay, endDay |
|
16986 | 1766 |
^ #( |
19624 | 1767 |
'Z' 0 false "/ zulu |
1768 |
'UTC' 0 false |
|
1769 |
'GMT' 0 false |
|
17081 | 1770 |
|
1771 |
"/ US |
|
19624 | 1772 |
'HAST' -600 false "/ hawai standard |
1773 |
'AKST' -540 false "/ alaska standard |
|
1774 |
'YST' -540 false "/ yukon standard |
|
1775 |
'PST' -480 false "/ pacific standard |
|
1776 |
'PT' -480 false "/ pacific standard |
|
1777 |
'MST' -420 false "/ mountain standard |
|
1778 |
'CST' -360 false "/ central standard |
|
1779 |
'EST' -300 false "/ eastern standard |
|
1780 |
'AST' -240 false "/ atlantic standard |
|
1781 |
||
1782 |
'NST' -210 false "/ new foundland standard |
|
1783 |
'PMST' -180 false "/ pierre & miquelon |
|
1784 |
'WGT' -180 false "/ west greenland |
|
1785 |
'EGT' -60 false "/ east greenland |
|
17081 | 1786 |
|
1787 |
"/ europe |
|
19624 | 1788 |
'CET' 60 false "/ central european |
1789 |
'EET' 120 false "/ east european |
|
1790 |
'WET' 0 false "/ west european |
|
17081 | 1791 |
"/ conflict with india!! |
1792 |
"/ 'IST' 1 "/ irish standard time |
|
1793 |
"/ 'IST' 1 "/ israel standard time |
|
19624 | 1794 |
'AZOT' -60 false "/ azores standard |
1795 |
||
1796 |
'MSK' 240 false "/ moscow european |
|
1797 |
'MSD' 240 false |
|
1798 |
'BT' 240 false "/ baghdad |
|
17081 | 1799 |
|
1800 |
"/ pacific |
|
19624 | 1801 |
'NZST' 720 false "/ new zealand standard |
1802 |
'FJT' 720 false "/ fiji |
|
17081 | 1803 |
|
1804 |
"/ south america |
|
19624 | 1805 |
'ART' -180 false "/ argentina |
1806 |
'BOT' -240 false "/ bolivia |
|
1807 |
'BRT' -180 false "/ brasilia |
|
1808 |
'CLT' -240 false "/ chile |
|
1809 |
'ECT' -300 false "/ equador |
|
1810 |
'PET' -300 false "/ peru |
|
1811 |
'PYT' -240 false "/ paraguay |
|
1812 |
'UYT' -180 false "/ uruguay |
|
1813 |
'VET' -270 false "/ venezuela standard |
|
1814 |
'VST' -270 false "/ venezuela standard |
|
17081 | 1815 |
|
1816 |
"/ africa |
|
19624 | 1817 |
'CAT' 120 false "/ central africa |
1818 |
'EAT' 180 false "/ east africa |
|
1819 |
'SAST' 120 false "/ south africa standard |
|
1820 |
'WAT' 60 false "/ west africa |
|
1821 |
'WT' 0 false "/ west sahara standard |
|
1822 |
||
1823 |
'AST' 180 false "/ arabia |
|
1824 |
'IRT' 210 false "/ iran time |
|
1825 |
'AFT' 270 false "/ afghanistan time |
|
1826 |
||
1827 |
'HKT' 480 false "/ hongkong |
|
1828 |
'IST' 330 false "/ india standard |
|
1829 |
'ICT' 420 false "/ indochina |
|
1830 |
'CNST' 480 false "/ china standard |
|
1831 |
'JST' 540 false "/ japan standard |
|
1832 |
'KST' 540 false "/ korea standard |
|
1833 |
'SGT' 480 false "/ singapore |
|
1834 |
'MYT' 480 false "/ malaysia |
|
1835 |
'AWST' 480 false "/ australian west standard |
|
1836 |
'ACWST' 525 false "/ australian central western standard |
|
1837 |
'ACST' 570 false "/ australian central standard |
|
1838 |
'AEST' 600 false "/ australian east standard |
|
1839 |
'NFT' 690 false "/ norfolk island, australia |
|
1840 |
||
1841 |
'CHAST' 765 false "/ chatham island standard |
|
20429 | 1842 |
'WST' 780 false "/ west samoa - yes that's 13!! |
1843 |
'TOT' 780 false "/ tonga - yes that's 13!! |
|
1844 |
'TKT' 780 false "/ tokelau - yes that's 13!! |
|
1845 |
'LINT' 840 false "/ line islands - yes that's 14!! |
|
17081 | 1846 |
|
1847 |
"/ misc |
|
19624 | 1848 |
'IDLW' -720 false "/ international date line west |
1849 |
'IDLE' 720 false "/ international date line east |
|
1850 |
||
24404 | 1851 |
'MEZ' 60 false "/ mittel europäische Zeit / central european (german) |
19624 | 1852 |
'MESZ' 120 true "/ central european summer (german) |
1853 |
'WESZ' 60 true "/ west european summer (german) |
|
1854 |
||
1855 |
'WEZ' 0 false "/ west european (german) |
|
1856 |
||
1857 |
'HADT' -540 true "/ hawaii summer |
|
1858 |
'ADT' -180 true |
|
1859 |
'AKDT' -540 true "/ alaska summer |
|
1860 |
'YDT' -480 true "/ yukon summer |
|
1861 |
'PDT' -420 true "/ pacific daylight saving |
|
1862 |
'MDT' -360 true "/ mountain daylight saving |
|
1863 |
'CDT' -300 true "/ central daylight saving |
|
1864 |
'EDT' -240 true "/ eastern daylight saving |
|
1865 |
'CLST' -180 true "/ chile summer |
|
1866 |
'NDT' -150 true |
|
1867 |
'PMDT' -120 true |
|
1868 |
'BRST' -120 true "/ brasilia summer |
|
1869 |
'WGST' -120 true "/ west greenland summer |
|
1870 |
'EGST' 0 true "/ east greenland summer |
|
1871 |
'AZOST' 0 true "/ azores summer |
|
1872 |
'EEST' 180 true |
|
1873 |
'CEST' 120 true |
|
1874 |
'WAST' 120 true "/ west africa summer |
|
1875 |
"/ 'WST' 60 true "/ west sahara summer - conflict with west samoa |
|
1876 |
'WEST' 60 true |
|
1877 |
'BST' 60 true "/ british summer time |
|
1878 |
'IRST' 270 true "/ iran summer time |
|
1879 |
'AWDT' 540 true "/ australian west daylight saving |
|
1880 |
'ACDT' 630 true "/ australian central daylight saving |
|
1881 |
'AEDT' 660 true "/ australian east daylight saving |
|
1882 |
'CHADT' 825 true "/ chatham island daylight saving |
|
1883 |
'FJST' 780 true "/ fiji summer |
|
1884 |
'NZDT' 780 true "/ new zealand summer |
|
17081 | 1885 |
|
1886 |
"/ military |
|
19624 | 1887 |
'A' 60 false "/ alpha |
1888 |
'B' 120 false "/ bravo |
|
1889 |
'C' 180 false "/ charlie |
|
1890 |
'D' 240 false "/ delta |
|
1891 |
'E' 300 false "/ echo |
|
1892 |
'F' 360 false "/ foxtrot |
|
1893 |
'G' 420 false "/ golf |
|
1894 |
'H' 480 false "/ hotel |
|
1895 |
'I' 540 false "/ also called india - how misleading |
|
1896 |
'K' 600 false "/ kilo |
|
1897 |
'L' 660 false "/ lima - but not there |
|
1898 |
'M' 720 false "/ mike |
|
1899 |
'N' -60 false "/ november (but also in other months) |
|
1900 |
'O' -120 false "/ oscar |
|
1901 |
'P' -180 false "/ papa (not mama) |
|
1902 |
'Q' -240 false "/ quebec - really? |
|
1903 |
'R' -300 false "/ romeo and juliet |
|
1904 |
'S' -360 false "/ sierra |
|
1905 |
'T' -420 false "/ tango (& not rumba) |
|
1906 |
'U' -480 false "/ uniform |
|
1907 |
'V' -540 false "/ victor |
|
1908 |
'W' -600 false "/ whiskey (scotch?) |
|
1909 |
'X' -660 false "/ xray |
|
1910 |
'Y' -720 false "/ yankee |
|
1911 |
||
17081 | 1912 |
). |
16986 | 1913 |
! |
1914 |
||
1915 |
utcOffsetFrom:aStringOrStream |
|
1916 |
"return the utcOffset (in seconds) for a given time-zone name. |
|
17172 | 1917 |
Returns nil for invalid formats, 0 if no timezone offset is present. |
16986 | 1918 |
|
1919 |
Notice: this returns the negated value of what is read from a printed representation, |
|
1920 |
which means that the sign is the same as what the utcOffset instance variable |
|
17081 | 1921 |
in timeStamp and OperatingSystem-timeInfo will be. |
16986 | 1922 |
|
1923 |
If utcOffset is negative, the local timezone is east of Greenwich. |
|
1924 |
If utcOffset is positive, the local timezone is west of Greenwich. |
|
1925 |
||
1926 |
UtcOffset is what you have to ADD to correct the printed time to GMT. |
|
1927 |
I.e. for Germany, you'll get -3600 (+01h), for NewYork, you'll get +18000 (-05h)" |
|
1928 |
||
17081 | 1929 |
|ch offset stream| |
14713 | 1930 |
|
1931 |
stream := aStringOrStream readStream. |
|
1932 |
stream skipSeparators. |
|
1933 |
||
17079 | 1934 |
ch := stream peekOrNil. |
1935 |
ch isNil ifTrue:[^ 0]. |
|
17081 | 1936 |
|
17071 | 1937 |
ch isLetter ifTrue:[ |
19624 | 1938 |
|table tzName i minuteOffset| |
1939 |
||
17081 | 1940 |
table := self timezoneInfo. |
17071 | 1941 |
tzName := stream upToElementForWhich:[:ch | ch isLetter not]. |
1942 |
||
1943 |
i := table indexOf:tzName. |
|
1944 |
i == 0 ifTrue:[ |
|
1945 |
^ nil |
|
1946 |
]. |
|
19624 | 1947 |
minuteOffset := (table at:i+1). |
1948 |
offset := minuteOffset * 60 |
|
14713 | 1949 |
] ifFalse:[ |
17081 | 1950 |
|sign| |
1951 |
||
17071 | 1952 |
sign := 1. |
1953 |
ch == $- ifTrue:[ |
|
1954 |
sign := -1. |
|
1955 |
stream next. |
|
17081 | 1956 |
] ifFalse:[ch == $+ ifTrue:[ |
1957 |
stream next. |
|
17071 | 1958 |
] ifFalse:[ |
17081 | 1959 |
stream skipSeparators |
1960 |
]]. |
|
17172 | 1961 |
stream peekOrNil isDigit ifFalse:[^ nil]. |
17081 | 1962 |
|
17071 | 1963 |
offset := ((stream next:2) asNumber * 60 * 60). |
17081 | 1964 |
ch := stream peekOrNil. |
1965 |
ch notNil ifTrue:[ |
|
1966 |
ch == $: ifTrue:[ stream next ]. |
|
17071 | 1967 |
offset := offset + ((stream next:2) asNumber * 60). |
1968 |
]. |
|
1969 |
offset := offset * sign |
|
14713 | 1970 |
]. |
1971 |
||
16975 | 1972 |
"/ return what would be an utcOffset (not what is at the end of an iso string) |
1973 |
^ offset negated |
|
14713 | 1974 |
|
1975 |
" |
|
16945 | 1976 |
self utcOffsetFrom:'UTC' |
1977 |
self utcOffsetFrom:'PST' |
|
17006 | 1978 |
self utcOffsetFrom:'EST' |
1979 |
self utcOffsetFrom:'CET' |
|
16945 | 1980 |
self utcOffsetFrom:'+0130' |
16955 | 1981 |
self utcOffsetFrom:'+01:30' |
17081 | 1982 |
self utcOffsetFrom:'+1:30' |
16955 | 1983 |
self utcOffsetFrom:'+01' |
14713 | 1984 |
" |
1985 |
! |
|
1986 |
||
1987 |
utcOffsetFromString:aString |
|
16955 | 1988 |
<resource: #obsolete> |
14713 | 1989 |
"return the utcOffset (in seconds) for a given time-zone name. |
1990 |
Returns nil for invalid formats" |
|
1991 |
||
16955 | 1992 |
^ self utcOffsetFrom:aString |
14713 | 1993 |
|
1994 |
" |
|
17006 | 1995 |
self utcOffsetFromString:'UTC' |
1996 |
self utcOffsetFromString:'+01' |
|
14713 | 1997 |
" |
11993 | 1998 |
! ! |
1999 |
||
22914 | 2000 |
|
22834 | 2001 |
|
8246 | 2002 |
!Timestamp methodsFor:'accessing'! |
30 | 2003 |
|
2004 |
day |
|
92 | 2005 |
"return the day-in-month of the receiver (1..31). |
275 | 2006 |
For compatibility, use instances of Date for this." |
30 | 2007 |
|
16928 | 2008 |
^ self asDate day. |
54 | 2009 |
|
92 | 2010 |
" |
16945 | 2011 |
Timestamp now day |
92 | 2012 |
" |
1501 | 2013 |
|
2014 |
"Modified: 1.7.1996 / 15:23:02 / cg" |
|
2015 |
! |
|
2016 |
||
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2017 |
dayInWeek |
16986 | 2018 |
"return the week-day of the receiver - 1 for monday, 7 for sunday. |
2019 |
for pre-gregorian dates, it simply goes on assuming the current leapYear rules |
|
2020 |
WARNING: different from ANSIs dayOfWeek (which returns 1 for sunday, ... 7 for saturday)." |
|
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2021 |
|
16928 | 2022 |
^ self asDate dayInWeek |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2023 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2024 |
" |
16945 | 2025 |
Timestamp now dayInWeek |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2026 |
" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2027 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2028 |
"Modified: 2.7.1996 / 09:20:32 / cg" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2029 |
"Created: 2.7.1996 / 09:35:48 / cg" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2030 |
! |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2031 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2032 |
dayInYear |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2033 |
"return the year-day of the receiver - 1 for Jan, 1st." |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2034 |
|
16928 | 2035 |
^ self asDate dayInYear |
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2036 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2037 |
" |
16945 | 2038 |
Timestamp now dayInYear |
2039 |
(Timestamp newDay:184 year:1996) dayInYear |
|
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2040 |
" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2041 |
|
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2042 |
"Modified: 2.7.1996 / 10:21:02 / cg" |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2043 |
! |
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2044 |
|
13225 | 2045 |
dayOfMonth |
2046 |
"Answer the day of the month represented by me. |
|
2047 |
Same as day; for ST-80 compatibility." |
|
2048 |
||
2049 |
^ self asDate dayOfMonth |
|
2050 |
||
2051 |
" |
|
16945 | 2052 |
Timestamp now dayOfMonth |
2053 |
(Timestamp newDay:184 year:1996) dayOfMonth |
|
13225 | 2054 |
" |
2055 |
||
2056 |
"Created: / 20-01-2011 / 12:26:54 / cg" |
|
2057 |
! |
|
2058 |
||
16993 | 2059 |
dayOfWeek |
2060 |
"return the week-day of the receiver - 1 is sunday, 7 for saturday. |
|
17006 | 2061 |
WARNING: different from dayInWeek (which returns 1 for monday, ... 7 for sunday). |
16993 | 2062 |
" |
2063 |
||
2064 |
^ self asDate dayOfWeek |
|
2065 |
||
2066 |
" |
|
2067 |
Timestamp now dayOfWeek |
|
2068 |
" |
|
2069 |
! |
|
2070 |
||
13225 | 2071 |
dayOfWeekName |
2072 |
"return the week-day of the receiver as a string. |
|
2073 |
The returned string depends on the language setting. |
|
2074 |
Expect things like 'monday', 'tuesday' ..." |
|
2075 |
||
2076 |
^ self asDate dayOfWeekName |
|
2077 |
||
2078 |
" |
|
16945 | 2079 |
Timestamp now dayOfWeekName |
2080 |
(Timestamp newDay:184 year:1996) dayOfWeekName |
|
13225 | 2081 |
" |
2082 |
||
2083 |
"Created: / 20-01-2011 / 12:28:46 / cg" |
|
2084 |
! |
|
2085 |
||
22873 | 2086 |
exactMicroseconds |
2087 |
"return the exact microseconds within the stamp's second (0 .. 999.999...) as a fixedPoint number. |
|
2088 |
notice: |
|
2089 |
that is NOT the total number of microseconds, |
|
2090 |
but the fractional part (within the second) only. |
|
2091 |
A fixedPoint number holds the exact value, but prints itself rounded!!" |
|
2092 |
||
2093 |
|millis microsFromMillis| |
|
2094 |
||
2095 |
millis := (osTime \\ 1000). |
|
2096 |
microsFromMillis := millis * 1000. |
|
2097 |
additionalPicoseconds notNil ifTrue:[ |
|
2098 |
^ microsFromMillis + (FixedPoint numerator:additionalPicoseconds denominator:(1000*1000) scale:3) |
|
2099 |
]. |
|
2100 |
^ microsFromMillis. |
|
2101 |
||
2102 |
" |
|
2103 |
|ts| |
|
2104 |
||
2105 |
ts := Timestamp nowWithMicroseconds. |
|
2106 |
Transcript showCR:ts. |
|
2107 |
Transcript showCR:ts microseconds. |
|
2108 |
Transcript showCR:ts exactMicroseconds. |
|
2109 |
Transcript showCR:ts nanoseconds. |
|
2110 |
Transcript showCR:ts picoseconds. |
|
2111 |
" |
|
2112 |
! |
|
2113 |
||
2114 |
exactMilliseconds |
|
2115 |
"return the exact milliseconds within the stamp's second (0 .. 999.999...) as a fixedPoint number. |
|
2116 |
notice: |
|
2117 |
that is NOT the total number of microseconds, |
|
2118 |
but the fractional part (within the second) only. |
|
2119 |
A fixedPoint number holds the exact value, but prints itself rounded!!" |
|
2120 |
||
2121 |
|millis| |
|
2122 |
||
2123 |
millis := (osTime \\ 1000). |
|
2124 |
additionalPicoseconds notNil ifTrue:[ |
|
2125 |
^ millis + (FixedPoint numerator:additionalPicoseconds denominator:(1000*1000*1000) scale:3) |
|
2126 |
]. |
|
2127 |
^ millis. |
|
2128 |
||
2129 |
" |
|
2130 |
|ts| |
|
2131 |
||
2132 |
ts := Timestamp nowWithMicroseconds. |
|
2133 |
Transcript showCR:ts. |
|
2134 |
Transcript showCR:ts milliseconds. |
|
2135 |
Transcript showCR:ts exactMilliseconds. |
|
2136 |
Transcript showCR:ts microseconds. |
|
2137 |
Transcript showCR:ts nanoseconds. |
|
2138 |
Transcript showCR:ts picoseconds. |
|
2139 |
" |
|
2140 |
! |
|
2141 |
||
2142 |
exactMinutes |
|
2143 |
"return the exact minutes within the stamp's hour (00 .. 59.999...) as a fixedPoint number. |
|
2144 |
Notice: |
|
2145 |
that is NOT the total number of minutes, |
|
2146 |
but the fractional part (within the hour) only. |
|
2147 |
A fixedPoint number holds the exact value, but prints itself rounded!!" |
|
2148 |
||
2149 |
|minutes additionalSeconds| |
|
2150 |
||
2151 |
minutes := FixedPoint numerator:(osTime \\ (60*60*1000)) / 60 denominator:1000 scale:3. |
|
2152 |
additionalPicoseconds notNil ifTrue:[ |
|
2153 |
additionalSeconds := (FixedPoint numerator:additionalPicoseconds denominator:(1000*1000*1000*1000) scale:3). |
|
2154 |
minutes := minutes + (additionalSeconds / 60). |
|
2155 |
]. |
|
2156 |
^ minutes. |
|
2157 |
||
2158 |
" |
|
2159 |
|ts| |
|
2160 |
||
2161 |
ts := Timestamp nowWithMicroseconds. |
|
2162 |
Transcript showCR:ts. |
|
2163 |
Transcript showCR:ts minutes. |
|
2164 |
Transcript showCR:ts exactMinutes. |
|
2165 |
" |
|
2166 |
! |
|
2167 |
||
2168 |
exactNanoseconds |
|
2169 |
"return the exact nanoseconds within the stamp's second (0 .. 999.999...). |
|
2170 |
notice: |
|
2171 |
that is NOT the total number of nanoseconds, |
|
2172 |
but the fractional part (within the second) only. |
|
2173 |
A fixedPoint number holds the exact value, but prints itself rounded!!" |
|
2174 |
||
2175 |
|millis nanosFromMillis| |
|
2176 |
||
2177 |
millis := (osTime \\ 1000). |
|
2178 |
nanosFromMillis := millis * 1000 * 1000. |
|
2179 |
additionalPicoseconds notNil ifTrue:[ |
|
2180 |
^ nanosFromMillis + (FixedPoint numerator:additionalPicoseconds denominator:(1000) scale:3) |
|
2181 |
]. |
|
2182 |
^ nanosFromMillis. |
|
2183 |
||
2184 |
" |
|
2185 |
|ts| |
|
2186 |
||
2187 |
ts := Timestamp now + 100.3 nanoseconds. |
|
2188 |
Transcript showCR:ts. |
|
2189 |
Transcript showCR:ts milliseconds. |
|
2190 |
Transcript showCR:ts exactMilliseconds. |
|
2191 |
Transcript showCR:ts microseconds. |
|
2192 |
Transcript showCR:ts exactMicroseconds. |
|
2193 |
Transcript showCR:ts nanoseconds. |
|
2194 |
Transcript showCR:ts exactNanoseconds. |
|
2195 |
Transcript showCR:ts picoseconds. |
|
2196 |
" |
|
2197 |
! |
|
2198 |
||
2199 |
exactSeconds |
|
2200 |
"return the exact seconds within the stamp's minute (00 .. 59.999...) as a fixedPoint number. |
|
2201 |
Notice: |
|
2202 |
that is NOT the total number of seconds, |
|
2203 |
but the fractional part (within the minute) only. |
|
2204 |
A fixedPoint number holds the exact value, but prints itself rounded!!" |
|
2205 |
||
2206 |
|seconds additionalSeconds| |
|
2207 |
||
2208 |
seconds := FixedPoint numerator:(osTime \\ (60*1000)) denominator:1000 scale:3. |
|
2209 |
additionalPicoseconds notNil ifTrue:[ |
|
2210 |
additionalSeconds := (FixedPoint numerator:additionalPicoseconds denominator:(1000*1000*1000*1000) scale:3). |
|
2211 |
seconds := seconds + additionalSeconds |
|
2212 |
]. |
|
2213 |
^ seconds. |
|
2214 |
||
2215 |
" |
|
2216 |
|ts| |
|
2217 |
||
2218 |
ts := Timestamp fromDate:(Date today) hour:10 minute:30 second:20 millisecond:300. |
|
2219 |
Transcript showCR:ts. |
|
2220 |
Transcript showCR:ts seconds. |
|
2221 |
Transcript showCR:ts exactSeconds. |
|
2222 |
" |
|
2223 |
! |
|
2224 |
||
241 | 2225 |
hours |
2226 |
"return the hours (0..23)" |
|
2227 |
||
17006 | 2228 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
2229 |
^ self asTime hours. |
|
16928 | 2230 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2231 |
^ self timeInfo hours |
54 | 2232 |
|
92 | 2233 |
" |
16945 | 2234 |
Timestamp now hours |
241 | 2235 |
" |
2236 |
||
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2237 |
"Modified: 2.7.1996 / 09:20:32 / cg" |
1501 | 2238 |
! |
2239 |
||
22834 | 2240 |
microseconds |
22873 | 2241 |
"return the truncated microseconds within the stamp's second (0..999999). |
22834 | 2242 |
notice: that is NOT the total number of microseconds, |
2243 |
but the fractional part (within the second) only. |
|
2244 |
Use this only for printing." |
|
2245 |
||
22873 | 2246 |
|millis microsFromMillis| |
2247 |
||
2248 |
millis := (osTime \\ 1000). |
|
2249 |
microsFromMillis := millis * 1000. |
|
22834 | 2250 |
additionalPicoseconds notNil ifTrue:[ |
2251 |
^ microsFromMillis + (additionalPicoseconds // (1000*1000)) |
|
2252 |
]. |
|
2253 |
^ microsFromMillis. |
|
2254 |
||
2255 |
" |
|
22873 | 2256 |
-- (definitely millisecond resolution here) |
2257 |
Timestamp now |
|
2258 |
Timestamp now microseconds |
|
2259 |
||
2260 |
-- (but some OS's only deliver millisecond resolution also here) |
|
22834 | 2261 |
Timestamp nowWithMicroseconds microseconds |
2262 |
||
22873 | 2263 |
|
22834 | 2264 |
|t1 t2| |
2265 |
t1 := Timestamp nowWithMicroseconds microseconds. |
|
2266 |
t2 := Timestamp nowWithMicroseconds microseconds. |
|
2267 |
t2-t1 |
|
2268 |
||
2269 |
|t1 t2| |
|
2270 |
t1 := Timestamp now microseconds. |
|
2271 |
t2 := Timestamp nowWithMicroseconds microseconds. |
|
2272 |
t2-t1 |
|
2273 |
" |
|
2274 |
||
2275 |
"Created: 1.7.1996 / 15:15:02 / cg" |
|
2276 |
"Modified: 2.7.1996 / 09:21:41 / cg" |
|
2277 |
! |
|
2278 |
||
1501 | 2279 |
millisecond |
22873 | 2280 |
"return the truncated millisecond within the stamp's second (0..999). |
1501 | 2281 |
ST-80 Timestamp compatibility (I'd prefer the name #milliseconds)." |
2282 |
||
2283 |
^ self milliseconds |
|
2284 |
||
2285 |
"Created: 1.7.1996 / 15:14:50 / cg" |
|
2286 |
"Modified: 1.7.1996 / 15:15:24 / cg" |
|
2287 |
! |
|
2288 |
||
2289 |
milliseconds |
|
22873 | 2290 |
"return the truncated milliseconds within the stamp's second (0..999)" |
1501 | 2291 |
|
16928 | 2292 |
^ osTime \\ 1000. |
1501 | 2293 |
|
2294 |
" |
|
16945 | 2295 |
Timestamp now milliseconds |
1501 | 2296 |
" |
2297 |
||
2298 |
"Created: 1.7.1996 / 15:15:02 / cg" |
|
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2299 |
"Modified: 2.7.1996 / 09:21:41 / cg" |
1501 | 2300 |
! |
2301 |
||
241 | 2302 |
minutes |
2303 |
"return the minutes (0..59)" |
|
2304 |
||
17006 | 2305 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
2306 |
^ self asTime minutes. |
|
16928 | 2307 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2308 |
^ self timeInfo minutes |
241 | 2309 |
|
2310 |
" |
|
16945 | 2311 |
Timestamp now minutes |
92 | 2312 |
" |
241 | 2313 |
|
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2314 |
"Modified: 2.7.1996 / 09:20:49 / cg" |
241 | 2315 |
! |
2316 |
||
699 | 2317 |
month |
2318 |
"return the month of the receiver (1..12). |
|
2319 |
For compatibility, use instances of Date for this." |
|
2320 |
||
17006 | 2321 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
2322 |
^ self asDate month. |
|
16928 | 2323 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2324 |
^ self timeInfo month |
699 | 2325 |
|
2326 |
" |
|
8246 | 2327 |
Timestamp now month |
699 | 2328 |
" |
1501 | 2329 |
|
2330 |
"Modified: 1.7.1996 / 15:23:05 / cg" |
|
2331 |
! |
|
2332 |
||
18886 | 2333 |
monthIndex |
2334 |
"return the month of the receiver (1..12). |
|
2335 |
For compatibility" |
|
2336 |
||
2337 |
^ self asDate monthIndex. |
|
2338 |
||
2339 |
" |
|
2340 |
Timestamp now monthIndex |
|
2341 |
" |
|
2342 |
! |
|
2343 |
||
22834 | 2344 |
nanoseconds |
22873 | 2345 |
"return the truncated nanoseconds within the stamp's second (0..999999999). |
22834 | 2346 |
notice: that is NOT the total number of nanoseconds, |
2347 |
but the fractional part (within the second) only. |
|
2348 |
Use this only for printing." |
|
2349 |
||
2350 |
|nanosFromMillis| |
|
2351 |
||
2352 |
nanosFromMillis := (osTime \\ 1000) * (1000 * 1000). |
|
2353 |
additionalPicoseconds notNil ifTrue:[ |
|
2354 |
^ nanosFromMillis + (additionalPicoseconds // 1000) |
|
2355 |
]. |
|
2356 |
^ nanosFromMillis. |
|
2357 |
||
2358 |
" |
|
2359 |
Timestamp now nanoseconds |
|
2360 |
Timestamp nowWithMicroseconds nanoseconds |
|
2361 |
||
2362 |
|t1 t2| |
|
2363 |
t1 := Timestamp nowWithMicroseconds nanoseconds. |
|
2364 |
t2 := Timestamp nowWithMicroseconds nanoseconds. |
|
2365 |
t2-t1 |
|
2366 |
||
2367 |
|t1 t2| |
|
2368 |
t1 := Timestamp now nanoseconds. |
|
2369 |
t2 := Timestamp nowWithMicroseconds nanoseconds. |
|
2370 |
t2-t1 |
|
2371 |
" |
|
2372 |
||
2373 |
"Created: 1.7.1996 / 15:15:02 / cg" |
|
2374 |
"Modified: 2.7.1996 / 09:21:41 / cg" |
|
2375 |
! |
|
2376 |
||
8286 | 2377 |
osTime |
2378 |
"get the internal representation of the time. |
|
2379 |
Warning: do not depend on the value (unix vs. win32 - differences)" |
|
2380 |
||
2381 |
^ osTime |
|
2382 |
! |
|
2383 |
||
22613 | 2384 |
picoseconds |
22834 | 2385 |
"return the picoseconds within the stamp's second (0..999999999999). |
2386 |
notice: that is NOT the total number of picoseconds, |
|
2387 |
but the fractional part (within the second) only. |
|
2388 |
Use this only for printing." |
|
2389 |
||
2390 |
|picosFromMillis| |
|
2391 |
||
2392 |
picosFromMillis := (osTime \\ 1000) * (1000 * 1000 * 1000). |
|
2393 |
^ picosFromMillis + (additionalPicoseconds ? 0) |
|
22613 | 2394 |
|
2395 |
" |
|
2396 |
Timestamp now picoseconds |
|
22834 | 2397 |
Timestamp nowWithMicroseconds picoseconds |
22614 | 2398 |
" |
2399 |
! |
|
2400 |
||
241 | 2401 |
seconds |
22873 | 2402 |
"return the truncated seconds (0..59)" |
241 | 2403 |
|
17006 | 2404 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
22873 | 2405 |
^ self asTime seconds. |
16928 | 2406 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2407 |
^ self timeInfo seconds |
241 | 2408 |
|
2409 |
" |
|
16945 | 2410 |
Timestamp now seconds |
241 | 2411 |
" |
2412 |
||
1513
a4b4c4ce039c
added dayInYear & dayInWeek for Date compatibility
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
2413 |
"Modified: 2.7.1996 / 09:20:54 / cg" |
92 | 2414 |
! |
2415 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2416 |
timeZoneDeltaInMinutes |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2417 |
"answer the number of minutes between local time and utc time. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2418 |
Delta is positive if local time is ahead of utc, negative if behind utc." |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2419 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2420 |
^ self utcOffset negated // 60 |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2421 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2422 |
" |
16945 | 2423 |
Timestamp now timeZoneDeltaInMinutes |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2424 |
(Timestamp day:1 month:7 year:1995 hour:12 minutes:0 seconds:0) timeZoneDeltaInMinutes |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2425 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2426 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2427 |
"Modified: 20.12.1995 / 17:28:49 / stefan" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2428 |
"Modified: 1.7.1996 / 15:21:29 / cg" |
7734 | 2429 |
! |
2430 |
||
16986 | 2431 |
timeZoneName |
16990 | 2432 |
"find the first zone which is in this range. |
2433 |
This is a bit naive, but should be sufficient in most cases |
|
2434 |
(for a correct result, we'd have to care for start-end dates of summer time |
|
2435 |
of that particular year...)" |
|
16986 | 2436 |
|
19624 | 2437 |
|myUtcOffset iAmDST bestNonDST| |
16986 | 2438 |
|
2439 |
myUtcOffset := self utcOffset negated. |
|
19624 | 2440 |
iAmDST := self timeInfo dst ? false. |
2441 |
||
2442 |
bestNonDST := nil. |
|
2443 |
self class timezoneInfo inGroupsOf:3 do:[:name :offsetInMinutes :isDST| |
|
2444 |
|thisUtcOffset| |
|
2445 |
||
2446 |
thisUtcOffset := offsetInMinutes * 60. |
|
2447 |
thisUtcOffset = myUtcOffset ifTrue:[ |
|
2448 |
isDST == iAmDST ifTrue:[^ name ]. |
|
2449 |
]. |
|
2450 |
bestNonDST := name. |
|
16986 | 2451 |
]. |
19624 | 2452 |
bestNonDST notNil ifTrue:[^ bestNonDST]. |
2453 |
||
2454 |
"/ nothing found - construct a standard offset string |
|
16986 | 2455 |
^ ((self utcOffset > 0) ifTrue:['+'] ifFalse:['-']) |
2456 |
, ((myUtcOffset abs // 60) printStringLeftPaddedTo:2 with:$0) |
|
2457 |
, ((myUtcOffset abs \\ 60) printStringLeftPaddedTo:2 with:$0) |
|
2458 |
||
2459 |
" |
|
17006 | 2460 |
Timestamp now timeZoneName |
2461 |
UtcTimestamp now timeZoneName |
|
2462 |
||
19624 | 2463 |
(Timestamp now asTZTimestampInZone:'MEZ') timeZoneName - will find international name |
17006 | 2464 |
(Timestamp now asTZTimestampInZone:'EST') timeZoneName |
19624 | 2465 |
(Timestamp now asTZTimestampInZone:'IDLE') timeZoneName - sorry - will find 'NZST' |
2466 |
(Timestamp now asTZTimestampInZone:'BRST') timeZoneName - sorry: will find military 'O' |
|
2467 |
(Timestamp now asTZTimestampInZone:'MYT') timeZoneName - will find hongkong; same timezone |
|
16986 | 2468 |
" |
2469 |
! |
|
2470 |
||
799 | 2471 |
utcOffset |
8886 | 2472 |
"return the difference between UTC (Greenwich Mean Time) and the local time in seconds. |
2473 |
If daylight saving time applies to ourself, take that into account. |
|
8888
4171ed260947
Fix #utcSecondsSince1970 and #utcSecondsSince1901 (wrong direction of utcOffset)
Stefan Vogel <sv@exept.de>
parents:
8886
diff
changeset
|
2474 |
|
8886 | 2475 |
Add utcOffset to convert from local time to UTC time. |
8888
4171ed260947
Fix #utcSecondsSince1970 and #utcSecondsSince1901 (wrong direction of utcOffset)
Stefan Vogel <sv@exept.de>
parents:
8886
diff
changeset
|
2476 |
Subtract utcOffset to convert from UTC time to local time. |
8886 | 2477 |
|
22890 | 2478 |
If utcOffset is negative, the local timezone is east of Greenwich (Russia, Asia). |
2479 |
If utcOffset is positive, the local timezone is west of Greenwich (USA)." |
|
799 | 2480 |
|
17026 | 2481 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
22890 | 2482 |
"/ fake an info which the OS cannot give me |
2483 |
"/ we do not know about DST in the far future and in the long gone past. |
|
2484 |
"/ Take the utcOffset without DST |
|
2485 |
^ self utcOffsetWithoutDst. |
|
17026 | 2486 |
]. |
2487 |
||
17003 | 2488 |
^ (OperatingSystem computeTimeAndDateFrom:osTime) utcOffset |
799 | 2489 |
|
2490 |
" |
|
16945 | 2491 |
Timestamp now utcOffset |
17031 | 2492 |
(Timestamp year:1995 month:7 day:1 hour:12 minute:0 second:0) utcOffset |
2493 |
(Timestamp year:1995 month:1 day:1 hour:12 minute:0 second:0) utcOffset |
|
2494 |
(Timestamp year:1689 month:7 day:1 hour:12 minute:0 second:0) utcOffset |
|
2495 |
(Timestamp year:4096 month:7 day:1 hour:12 minute:0 second:0) utcOffset |
|
799 | 2496 |
" |
2497 |
||
2498 |
"Modified: 20.12.1995 / 17:28:49 / stefan" |
|
1501 | 2499 |
"Modified: 1.7.1996 / 15:21:29 / cg" |
799 | 2500 |
! |
2501 |
||
17031 | 2502 |
utcOffsetWithoutDst |
2503 |
"return the difference between UTC (Greenwich Mean Time) and the local time in seconds. |
|
2504 |
If daylight saving time applies to ourself, do not take that into account. |
|
2505 |
||
2506 |
Add utcOffset to convert from local time to UTC time. |
|
2507 |
Subtract utcOffset to convert from UTC time to local time. |
|
2508 |
||
2509 |
If utcOffset is negative, the local timezone is east of Greenwich. |
|
2510 |
If utcOffset is positive, the local timezone is west of Greenwich." |
|
2511 |
||
2512 |
|offset epochInfo| |
|
2513 |
||
2514 |
offset := 0. |
|
2515 |
[ |
|
17067 | 2516 |
"DST may be in winter in the southern hemisphere. If we are in DST, add some days" |
2517 |
epochInfo := OperatingSystem timeInfoFromSeconds:offset milliseconds:0 localTime:true. |
|
2518 |
offset := offset + (3600 * 24 * 90). "Add about 3 months" |
|
17031 | 2519 |
] doWhile:[epochInfo dst and:[offset < (3600 * 24 * 365) "avoid endless loop"]]. |
2520 |
||
2521 |
^ epochInfo utcOffset |
|
2522 |
||
2523 |
" |
|
2524 |
Timestamp now utcOffsetWithoutDst |
|
2525 |
" |
|
2526 |
! |
|
2527 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2528 |
weekInYear |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2529 |
"return the week number of the receiver - 1 for Jan, 1st." |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2530 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2531 |
^ Date weekInYearOf:self |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2532 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2533 |
" |
16945 | 2534 |
(Timestamp newDay:1 year:2000) weekInYear |
2535 |
(Timestamp newDay:2 year:2000) weekInYear |
|
2536 |
(Timestamp newDay:3 year:2000) weekInYear |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2537 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2538 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2539 |
|
16993 | 2540 |
weekday |
2541 |
"return the week-day of the receiver as a string. |
|
2542 |
The returned string depends on the current language setting. |
|
2543 |
Expect things like 'monday', 'tuesday' ... |
|
2544 |
For ST-80 compatibility" |
|
2545 |
||
2546 |
^ self asDate weekday |
|
2547 |
||
2548 |
" |
|
17006 | 2549 |
Timestamp now weekday |
16993 | 2550 |
|
2551 |
but maybe a different day there, in the south pacific: |
|
17006 | 2552 |
(Timestamp now asTZTimestampInZone:'LINT') weekday |
16993 | 2553 |
|
2554 |
but maybe a different day there, in alaska: |
|
17006 | 2555 |
(Timestamp now asTZTimestampInZone:'AKST') weekday |
16993 | 2556 |
" |
2557 |
! |
|
2558 |
||
699 | 2559 |
year |
2560 |
"return the year of the receiver i.e. 1992. |
|
2561 |
For compatibility, use instances of Date for this." |
|
241 | 2562 |
|
17006 | 2563 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
2564 |
^ self asDate year. |
|
16928 | 2565 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2566 |
^ self timeInfo year |
241 | 2567 |
|
2568 |
" |
|
8246 | 2569 |
Timestamp now year |
241 | 2570 |
" |
1501 | 2571 |
|
2572 |
"Modified: 1.7.1996 / 15:23:08 / cg" |
|
241 | 2573 |
! ! |
2574 |
||
8246 | 2575 |
!Timestamp methodsFor:'arithmetic'! |
30 | 2576 |
|
13199 | 2577 |
ceilingSecond |
2578 |
"return a timestamp which represents the next full second" |
|
2579 |
||
2580 |
|offs| |
|
2581 |
||
2582 |
offs := (self milliseconds / 1000) ceiling. |
|
2583 |
^ self class basicNew setSeconds:(self getSeconds + offs). |
|
2584 |
||
2585 |
" |
|
2586 |
|t1 t2| |
|
2587 |
||
2588 |
t1 := Timestamp now. |
|
2589 |
t2 := t1 ceilingSecond. |
|
2590 |
self halt |
|
2591 |
" |
|
2592 |
||
2593 |
"Created: / 08-01-2011 / 16:23:03 / cg" |
|
2594 |
! |
|
2595 |
||
2596 |
floorSecond |
|
2597 |
"return a timestamp truncated to the last full second" |
|
2598 |
||
2599 |
^ self class basicNew setSeconds:(self getSeconds). |
|
2600 |
||
2601 |
" |
|
2602 |
|t1 t2| |
|
2603 |
||
2604 |
t1 := Timestamp now. |
|
2605 |
t2 := t1 floorSecond. |
|
2606 |
self halt |
|
2607 |
" |
|
2608 |
||
2609 |
"Created: / 08-01-2011 / 16:20:55 / cg" |
|
2610 |
! |
|
2611 |
||
22940 | 2612 |
microsecondDeltaFrom:aTimestamp |
23776 | 2613 |
"return the delta in (truncated) microseconds between 2 timestamps. |
22940 | 2614 |
The argument is supposed to be BEFORE the receiver, |
2615 |
computes self - aTimestamp" |
|
2616 |
||
2617 |
^ self getMicroseconds - (aTimestamp getMicroseconds) |
|
2618 |
||
2619 |
" |
|
2620 |
|t1 t2| |
|
2621 |
||
2622 |
t1 := Timestamp now. |
|
2623 |
Delay waitForSeconds:0.5. |
|
2624 |
t2 := Timestamp now. |
|
2625 |
t2 microsecondDeltaFrom:t1 |
|
2626 |
" |
|
2627 |
||
2628 |
"Modified: / 10-07-2010 / 09:37:18 / cg" |
|
23776 | 2629 |
"Modified (comment): / 26-02-2019 / 14:02:16 / Claus Gittinger" |
22940 | 2630 |
! |
2631 |
||
8246 | 2632 |
millisecondDeltaFrom:aTimestamp |
23776 | 2633 |
"return the delta in (truncated) milliseconds between 2 timestamps. |
12939 | 2634 |
The argument is supposed to be BEFORE the receiver, |
23239 | 2635 |
computes self - aTimestamp" |
2636 |
||
2637 |
^ osTime - (aTimestamp getMilliseconds) |
|
2623
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2638 |
|
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2639 |
" |
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2640 |
|t1 t2| |
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2641 |
|
8246 | 2642 |
t1 := Timestamp now. |
2623
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2643 |
Delay waitForSeconds:0.5. |
8246 | 2644 |
t2 := Timestamp now. |
16945 | 2645 |
t2 millisecondDeltaFrom:t1 |
2623
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2646 |
" |
593ab389efd1
added #millisecondDeltaFrom:
Claus Gittinger <cg@exept.de>
parents:
2311
diff
changeset
|
2647 |
|
12939 | 2648 |
"Modified: / 10-07-2010 / 09:37:18 / cg" |
23239 | 2649 |
"Modified: / 27-07-2018 / 10:34:11 / Stefan Vogel" |
23776 | 2650 |
"Modified (comment): / 26-02-2019 / 14:02:13 / Claus Gittinger" |
9056 | 2651 |
! |
2652 |
||
13200 | 2653 |
roundedToSecond |
2654 |
"return a timestamp which represents the time rounded to the nearest full second" |
|
2655 |
||
2656 |
|offs| |
|
2657 |
||
2658 |
offs := (self milliseconds / 1000) rounded. |
|
2659 |
^ self class basicNew setSeconds:(self getSeconds + offs). |
|
2660 |
||
2661 |
" |
|
2662 |
|t1 t2| |
|
2663 |
||
2664 |
t1 := Timestamp now. |
|
2665 |
t2 := t1 roundedToSecond. |
|
2666 |
self halt |
|
2667 |
" |
|
2668 |
||
2669 |
"Created: / 08-01-2011 / 16:24:12 / cg" |
|
2670 |
! |
|
2671 |
||
9056 | 2672 |
secondDeltaFrom:aTimestamp |
23776 | 2673 |
"return the delta in (truncated) seconds between 2 timestamps. |
12939 | 2674 |
The argument is supposed to be BEFORE the receiver, |
23776 | 2675 |
computes self - aTimestamp" |
9056 | 2676 |
|
2677 |
^ self getSeconds - (aTimestamp getSeconds) |
|
2678 |
||
2679 |
" |
|
2680 |
|t1 t2| |
|
2681 |
||
2682 |
t1 := Timestamp now. |
|
2683 |
Delay waitForSeconds:5. |
|
2684 |
t2 := Timestamp now. |
|
16945 | 2685 |
t2 secondDeltaFrom:t1 |
9056 | 2686 |
" |
2687 |
||
12939 | 2688 |
"Modified: / 10-07-2010 / 09:37:24 / cg" |
23776 | 2689 |
"Modified (comment): / 26-02-2019 / 14:02:09 / Claus Gittinger" |
30 | 2690 |
! ! |
2691 |
||
8246 | 2692 |
!Timestamp methodsFor:'comparing'! |
699 | 2693 |
|
2694 |
hash |
|
22885 | 2695 |
"return an integer useful for hashing on time stamps" |
2696 |
||
2697 |
^ osTime "// 1000" - why ignore the millis? |
|
1520
da08f8e49e87
fixed comparing absoluteTimes
Claus Gittinger <cg@exept.de>
parents:
1513
diff
changeset
|
2698 |
|
da08f8e49e87
fixed comparing absoluteTimes
Claus Gittinger <cg@exept.de>
parents:
1513
diff
changeset
|
2699 |
"Modified: 3.7.1996 / 13:10:52 / cg" |
699 | 2700 |
! ! |
2701 |
||
8246 | 2702 |
!Timestamp methodsFor:'converting'! |
699 | 2703 |
|
2704 |
asDate |
|
2705 |
"return a Date object from the receiver. |
|
16932 | 2706 |
The returned date will only represent the day - not the timeOfDay. |
2707 |
Notice: if you convert a local timestamp, you will get the local date; |
|
2708 |
otherwise if you convert an utcTimestamp, you'll get the utc date." |
|
699 | 2709 |
|
17006 | 2710 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
17067 | 2711 |
|milliDelta dayDelta| |
2712 |
||
2713 |
"/ we do not know about DST in the far future and in the long gone past. |
|
2714 |
"/ Take the utcOffset without DST |
|
2715 |
milliDelta := osTime - (1000 * self utcOffsetWithoutDst). |
|
2716 |
dayDelta := milliDelta // (24 * 3600 * 1000). |
|
2717 |
^ Epoch asDate addDays:dayDelta. |
|
16928 | 2718 |
]. |
16945 | 2719 |
^ self timeInfo asDate |
699 | 2720 |
|
2721 |
" |
|
16945 | 2722 |
Timestamp now |
8246 | 2723 |
Timestamp now asDate |
16945 | 2724 |
(Timestamp now addTime:3600) asDate |
2725 |
(Timestamp now addTime:3600) asTime |
|
2726 |
Timestamp fromSeconds:(Timestamp now asSeconds + 3600) |
|
2727 |
(Timestamp fromSeconds:(Timestamp now asSeconds + 3600)) asDate |
|
699 | 2728 |
" |
2729 |
! |
|
2730 |
||
11003 | 2731 |
asLocalTimestamp |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2732 |
"represent myself as a timestamp in the local timezone" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2733 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2734 |
^ self |
11003 | 2735 |
|
2736 |
" |
|
2737 |
Timestamp now asUtcTimestamp |
|
2738 |
Timestamp now asUtcTimestamp asLocalTimestamp |
|
2739 |
" |
|
2740 |
! |
|
2741 |
||
16996 | 2742 |
asTZTimestamp |
2743 |
"return a timestamp which represents the very same time, |
|
2744 |
but will represent itself as a timestamp with the local utcOffset. |
|
2745 |
Use this to make sure that a local timestamp can be read back in another timezone" |
|
2746 |
||
2747 |
^ self asTZTimestamp:self utcOffset |
|
2748 |
||
2749 |
"see the different printStrings of: |
|
23003 | 2750 |
Timestamp now |
2751 |
and |
|
2752 |
Timestamp now asTZTimestamp |
|
16996 | 2753 |
and |
23003 | 2754 |
Timestamp now asUtcTimestamp |
2755 |
" |
|
2756 |
||
2757 |
"Modified (comment): / 24-05-2018 / 17:31:26 / Claus Gittinger" |
|
16996 | 2758 |
! |
2759 |
||
16967 | 2760 |
asTZTimestamp:utcOffset |
2761 |
"return a timestamp which represents the very same time, |
|
16986 | 2762 |
but will represent itself as a timestamp with the given utcOffset" |
16967 | 2763 |
|
2764 |
^ (TZTimestamp fromOSTime:osTime) utcOffset:utcOffset |
|
2765 |
||
2766 |
"what is the time now in NewYork? |
|
17006 | 2767 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') |
2768 |
Timestamp now asTZTimestampInZone:'EST' |
|
16975 | 2769 |
|
2770 |
what is the time now in Stuttgart? |
|
17006 | 2771 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'MEZ') |
2772 |
Timestamp now asTZTimestampInZone:'MEZ' |
|
16986 | 2773 |
" |
2774 |
! |
|
2775 |
||
2776 |
asTZTimestampInZone:timeZoneName |
|
2777 |
"return a timestamp which represents the very same time, |
|
2778 |
but will represent itself as a timestamp in the given timezone. |
|
2779 |
timeZoneName must be one of the standard names as listed in Timestamp >> timezoneInfo" |
|
2780 |
||
17081 | 2781 |
|utcOffset| |
2782 |
||
2783 |
utcOffset := self class utcOffsetFrom:timeZoneName. |
|
2784 |
utcOffset isNil ifTrue:[ |
|
17132 | 2785 |
^ TimeConversionError raiseRequestWith:self errorString:(' - Invalid/unknown timzone: %1' bindWith:timeZoneName) |
17081 | 2786 |
]. |
2787 |
||
2788 |
^ self asTZTimestamp:utcOffset |
|
16986 | 2789 |
|
2790 |
"what is the time now in NewYork? |
|
17133 | 2791 |
Timestamp now asTZTimestampInZone:'EST' |
16986 | 2792 |
|
2793 |
what is the time now in Stuttgart? |
|
17133 | 2794 |
Timestamp now asTZTimestampInZone:'MEZ' |
2795 |
||
2796 |
an error os raised for any unknown/invalid timezone: |
|
2797 |
Timestamp now asTZTimestampInZone:'BlaBla' |
|
16967 | 2798 |
" |
2799 |
! |
|
2800 |
||
699 | 2801 |
asTime |
2802 |
"return a Time object from the receiver. |
|
16936 | 2803 |
The returned time will only represent the timeOfDay - not the day, |
17003 | 2804 |
and does not include the milliseconds." |
699 | 2805 |
|
16932 | 2806 |
|secondDelta| |
2807 |
||
17006 | 2808 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
17067 | 2809 |
secondDelta := osTime // 1000. |
2810 |
"/ we do not know about DST in the far future and in the long gone past. |
|
2811 |
"/ Take the utcOffset without DST |
|
2812 |
secondDelta := (secondDelta - self utcOffsetWithoutDst) \\ (24*3600). |
|
2813 |
^ Epoch asTime addSeconds:secondDelta. |
|
16928 | 2814 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2815 |
^ self timeInfo asTime |
699 | 2816 |
|
2817 |
" |
|
16945 | 2818 |
Timestamp now |
8246 | 2819 |
Timestamp now asTime |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2820 |
UtcTimestamp now asTime |
16945 | 2821 |
(Timestamp now addTime:3600) asTime |
17003 | 2822 |
(Timestamp year:2014 month:7 day:1 hour:12 minute:0 second:0) asTime |
2823 |
(UtcTimestamp year:2014 month:7 day:1 hour:12 minute:0 second:0) asTime |
|
699 | 2824 |
" |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2825 |
! |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2826 |
|
17006 | 2827 |
asTimeWithMilliseconds |
2828 |
"return a Time object from the receiver. |
|
2829 |
The returned time will only represent the timeOfDay - not the day, |
|
2830 |
it will include the milliseconds." |
|
2831 |
||
2832 |
|milliSecondDelta| |
|
2833 |
||
2834 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
|
2835 |
milliSecondDelta := osTime - (self utcOffset * 1000). |
|
2836 |
^ self class epoch asTime addMilliseconds:milliSecondDelta. |
|
2837 |
]. |
|
2838 |
^ self timeInfo asTime |
|
2839 |
||
2840 |
" |
|
2841 |
Timestamp now |
|
2842 |
Timestamp now asTime |
|
2843 |
UtcTimestamp now asTime |
|
2844 |
(Timestamp now addTime:3600) asTime |
|
2845 |
(Timestamp year:2014 month:7 day:1 hour:12 minute:0 second:0) asTime |
|
2846 |
(UtcTimestamp year:2014 month:7 day:1 hour:12 minute:0 second:0) asTime |
|
2847 |
" |
|
2848 |
! |
|
2849 |
||
8246 | 2850 |
asTimestamp |
16928 | 2851 |
"return an Timestamp object from the receiver - that's the receiver." |
8246 | 2852 |
|
16945 | 2853 |
^ self |
8246 | 2854 |
! |
2855 |
||
18886 | 2856 |
asUTC |
2857 |
^ self asUtcTimestamp |
|
2858 |
||
2859 |
" |
|
2860 |
Timestamp now |
|
2861 |
Timestamp now asUTC |
|
2862 |
" |
|
2863 |
! |
|
2864 |
||
9402 | 2865 |
asUtcTimestamp |
16967 | 2866 |
"return a timestamp which represents the very same timestamp, |
2867 |
but will represent itself as a timestamp in the UTC timezone" |
|
16945 | 2868 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2869 |
^ UtcTimestamp fromOSTime:osTime |
9402 | 2870 |
|
2871 |
" |
|
11003 | 2872 |
Timestamp now asUtcTimestamp |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2873 |
Timestamp now asUtcTimestamp asLocalTimestamp |
9402 | 2874 |
" |
2875 |
! |
|
2876 |
||
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2877 |
literalArrayEncoding |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2878 |
"encode myself as an array, from which a copy of the receiver |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2879 |
can be reconstructed with #decodeAsLiteralArray. |
16945 | 2880 |
The encoding is: |
18761 | 2881 |
(#Timestamp YYYYMMDDhhmmss.iiiZ) |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2882 |
" |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2883 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2884 |
|s| |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2885 |
|
8223
56da5d8f8bc1
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8111
diff
changeset
|
2886 |
s := WriteStream on:(String new:18). |
16986 | 2887 |
self printGeneralizedOn:s isLocal:false. |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2888 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2889 |
^ Array |
18761 | 2890 |
with:self class name |
2891 |
with:s contents |
|
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2892 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2893 |
" |
9416 | 2894 |
Timestamp now literalArrayEncoding |
18761 | 2895 |
decodeAsLiteralArray |
16986 | 2896 |
UtcTimestamp now literalArrayEncoding |
18761 | 2897 |
decodeAsLiteralArray |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
2898 |
" |
7006 | 2899 |
! |
2900 |
||
24163 | 2901 |
utcOffset:secondsOrTimeDuration |
22890 | 2902 |
"answer a DateTime equivalent to the receiver but offset from UTC by offset. |
2903 |
If utcOffset is negative, the local timezone is east of Greenwich (Russia, Asia). |
|
24163 | 2904 |
If utcOffset is positive, the local timezone is west of Greenwich (USA). |
2905 |
If utcOffset is zero, you effectively get UTC time." |
|
18886 | 2906 |
|
2907 |
^ TZTimestamp new |
|
2908 |
osTime:osTime; |
|
24163 | 2909 |
utcOffset:(secondsOrTimeDuration asTimeDuration); |
23239 | 2910 |
yourself. |
18886 | 2911 |
|
2912 |
" |
|
2913 |
Timestamp now -- now as local time |
|
2914 |
Timestamp now asTZTimestamp -- now in your local timezone |
|
2915 |
Timestamp now asUtcTimestamp -- now in greenwich |
|
2916 |
UtcTimestamp now -- now in greenwich |
|
22894 | 2917 |
Timestamp now utcOffset:(-2 hours) -- now in East Europe |
18886 | 2918 |
Timestamp now utcOffset:(5 hours) -- now in Eastern time |
2919 |
Timestamp now asTZTimestampInZone:'EST' -- now in Eastern time |
|
2920 |
" |
|
23239 | 2921 |
|
2922 |
"Modified: / 27-07-2018 / 11:54:45 / Stefan Vogel" |
|
24163 | 2923 |
"Modified: / 26-05-2019 / 12:54:56 / Claus Gittinger" |
18886 | 2924 |
! |
2925 |
||
7006 | 2926 |
utcSecondsSince1901 |
2927 |
"return the number of seconds elapsed since Jan, 1st 1901" |
|
2928 |
||
16945 | 2929 |
" |
2930 |
secondsBetween1901and1970 := |
|
23559 | 2931 |
((Date year:1970 month:1 day:1) subtractDate:(Date year:1901 month:1 day:1)) |
2932 |
* (24 * 60 * 60) |
|
9430
919e0cb2257c
Fix for utc botch when retrieving seconds since 1901
Stefan Vogel <sv@exept.de>
parents:
9416
diff
changeset
|
2933 |
" |
7006 | 2934 |
|
16945 | 2935 |
^ self utcSecondsSince1970 + 2177452800. |
2936 |
||
2937 |
" |
|
2938 |
Timestamp now utcSecondsSince1901 |
|
7006 | 2939 |
" |
23559 | 2940 |
|
2941 |
"Modified (comment): / 10-12-2018 / 20:37:38 / Stefan Vogel" |
|
699 | 2942 |
! ! |
2943 |
||
23239 | 2944 |
!Timestamp methodsFor:'double dispatching'! |
2945 |
||
2946 |
differenceFromTimestamp:aTimestamp |
|
2947 |
|newMillis newPicos| |
|
2948 |
||
2949 |
newMillis := aTimestamp getMilliseconds - osTime. |
|
2950 |
newPicos := (aTimestamp additionalPicoseconds) - (additionalPicoseconds ? 0). |
|
2951 |
||
2952 |
^ TimeDuration basicNew |
|
2953 |
setMilliseconds:newMillis additionalPicoseconds:newPicos. |
|
2954 |
||
2955 |
"Created: / 27-07-2018 / 08:45:22 / Stefan Vogel" |
|
2956 |
"Modified (format): / 27-07-2018 / 10:50:05 / Stefan Vogel" |
|
2957 |
! ! |
|
2958 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2959 |
!Timestamp methodsFor:'initialization'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
2960 |
|
16996 | 2961 |
UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis |
2962 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
|
22885 | 2963 |
given y,m,d and h,m,s in my time. |
2964 |
All arguments MUST be integral (for now)" |
|
16996 | 2965 |
|
2966 |
self setOSTimeFromUTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis |
|
2967 |
! |
|
2968 |
||
22873 | 2969 |
UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis additionalPicoseconds:picos |
2970 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
|
22885 | 2971 |
given y,m,d and h,m,s in my time. |
2972 |
All arguments MUST be integral (for now)" |
|
22873 | 2973 |
|
2974 |
self setOSTimeFromUTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis. |
|
2975 |
additionalPicoseconds := picos. |
|
2976 |
! |
|
2977 |
||
16996 | 2978 |
setOSTimeFromUTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis |
2979 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
|
2980 |
given y,m,d and h,m,s in local time" |
|
2981 |
||
2982 |
Error handle:[:ex | |
|
19713 | 2983 |
"handler for timestamps before the epoch or after the OS representable time (2038 on current Unices). |
2984 |
Then, an out-of-os-range osTime is generated here manually." |
|
2985 |
||
2986 |
|deltaDays| |
|
2987 |
||
2988 |
deltaDays := self class epoch asDate subtractDate:(Date year:y month:m day:d). |
|
2989 |
"/ deltadays will be negative for dates before the epoch and positive if after. |
|
2990 |
||
2991 |
osTime := (h * 3600) + (min * 60) + s. |
|
2992 |
osTime := osTime - (deltaDays * 24 * 3600). |
|
2993 |
osTime := osTime * 1000. |
|
2994 |
osTime := osTime + millis. |
|
16996 | 2995 |
] do:[ |
19713 | 2996 |
osTime := OperatingSystem |
2997 |
computeOSTimeFromUTCYear:y month:m day:d |
|
2998 |
hour:h minute:min second:s |
|
2999 |
millisecond:millis |
|
16996 | 3000 |
] |
3001 |
! |
|
3002 |
||
16945 | 3003 |
setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis |
16986 | 3004 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
17006 | 3005 |
given y,m,d and h,m,s in local time. If the OS cannot do it, do it here." |
16986 | 3006 |
|
17026 | 3007 |
TimeConversionError handle:[:ex | |
19713 | 3008 |
"handler for timestamps before the epoch or after the OS representable time (2038 on current Unices). |
3009 |
Then, an out-of-os-range osTime is generated here manually." |
|
3010 |
||
3011 |
|deltaDays| |
|
3012 |
||
3013 |
deltaDays := self class epoch asDate subtractDate:(Date year:y month:m day:d). |
|
3014 |
"/ deltadays will be negative for dates before the epoch and positive if after. |
|
3015 |
||
3016 |
osTime := (h * 3600) + (min * 60) + s. |
|
3017 |
osTime := osTime + self utcOffset. |
|
3018 |
osTime := osTime - (deltaDays * 24 * 3600). |
|
3019 |
osTime := osTime * 1000. |
|
3020 |
osTime := osTime + millis. |
|
16928 | 3021 |
] do:[ |
19713 | 3022 |
osTime := OperatingSystem |
3023 |
computeOSTimeFromYear:y month:m day:d |
|
3024 |
hour:h minute:min second:s |
|
3025 |
millisecond:millis |
|
16996 | 3026 |
] |
3027 |
! |
|
3028 |
||
3029 |
year:y month:m day:d hour:h minute:min second:s millisecond:millis |
|
3030 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
|
22885 | 3031 |
given y,m,d and h,m,s in my time. |
3032 |
All arguments MUST be integral (for now)" |
|
16996 | 3033 |
|
3034 |
self setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis |
|
19652 | 3035 |
|
3036 |
" |
|
3037 |
self basicNew |
|
3038 |
year:2016 month:4 day:16 hour:17 minute:21 second:13 millisecond:726 |
|
3039 |
" |
|
22873 | 3040 |
! |
3041 |
||
3042 |
year:y month:m day:d hour:h minute:min second:s millisecond:millis additionalPicoseconds:picos |
|
3043 |
"private: ask the operating system to compute the internal osTime (based on the epoch), |
|
22885 | 3044 |
given y,m,d and h,m,s in my time. |
3045 |
All arguments MUST be integral (for now)" |
|
22873 | 3046 |
|
3047 |
self setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis. |
|
3048 |
additionalPicoseconds := picos |
|
3049 |
||
3050 |
" |
|
3051 |
self basicNew |
|
3052 |
year:2016 month:4 day:16 hour:17 minute:21 second:13 millisecond:726 |
|
3053 |
" |
|
11993 | 3054 |
! ! |
3055 |
||
13541
91828a57f378
added: #differenceFromTimestamp:
Claus Gittinger <cg@exept.de>
parents:
13236
diff
changeset
|
3056 |
|
8246 | 3057 |
!Timestamp methodsFor:'printing & storing'! |
30 | 3058 |
|
8111
67347b7e3e69
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
7817
diff
changeset
|
3059 |
addPrintBindingsTo:dict language:languageOrNil |
67347b7e3e69
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
7817
diff
changeset
|
3060 |
"private print support: add bindings for printing to aDictionary |
67347b7e3e69
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
7817
diff
changeset
|
3061 |
languageOrNil can only be #en or nil for the current language." |
5726 | 3062 |
|
16928 | 3063 |
|date| |
3064 |
||
3065 |
date := self asDate. |
|
3066 |
date addPrintBindingsTo:dict language:languageOrNil. |
|
17003 | 3067 |
super addPrintBindingsTo:dict language:languageOrNil. |
16928 | 3068 |
|
3069 |
date year == Date today year ifTrue:[ |
|
18447 | 3070 |
dict at:#yearOrTime put:('%h:%m' expandPlaceholdersWith:dict). |
6795
519894240d97
more printFormats: yearOrDate (as in unix); dayPadded and monthPadded
penk
parents:
6686
diff
changeset
|
3071 |
]. |
519894240d97
more printFormats: yearOrDate (as in unix); dayPadded and monthPadded
penk
parents:
6686
diff
changeset
|
3072 |
|
17003 | 3073 |
" |
18447 | 3074 |
|d| |
3075 |
d := Dictionary new. |
|
3076 |
Timestamp now addPrintBindingsTo:d language:nil. |
|
3077 |
d inspect. |
|
3078 |
" |
|
3079 |
||
3080 |
"used by: |
|
22331 | 3081 |
Timestamp now printStringFormat:'%y-%(mon)-%d' |
18447 | 3082 |
Timestamp now printStringFormat:'%(dayOfYear)' |
17003 | 3083 |
" |
22331 | 3084 |
|
3085 |
"Modified (comment): / 03-11-2017 / 10:54:30 / cg" |
|
4843 | 3086 |
! |
3087 |
||
16945 | 3088 |
printGeneralizedOn:aStream |
5290 | 3089 |
"append a representation of the receiver to aStream in a general format, |
18761 | 3090 |
top-down, without separators: 'yyyymmddHHMMSS.mmmZ' |
12166 | 3091 |
|
5290 | 3092 |
This format is used for the ASN.1 GeneralizedTime as defined in X.208 Sec. 33, |
3093 |
so read this before changing the output format." |
|
3094 |
||
12166 | 3095 |
^ self printGeneralizedOn:aStream isLocal:false short:false. |
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3096 |
! |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3097 |
|
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3098 |
printGeneralizedOn:aStream isLocal:isLocal |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3099 |
"append a representation of the receiver to aStream in a general format, |
11177 | 3100 |
top-down, without separators: 'yyyymmddHHMMSS.mmm+0100' |
12166 | 3101 |
|
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3102 |
This format is used for the ASN.1 GeneralizedTime as defined in X.208 Sec. 33, |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3103 |
so read this before changing the output format. |
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3104 |
|
18761 | 3105 |
If isLocal is true, represent as local time (Local 'yyyymmddHHMMSS.mmm+0100'), |
3106 |
otherwise as UTC time (UTC 'yyyymmddHHMMSS.mmmZ')." |
|
5379
2cf681402817
Make literal array encoding architecture independent.
Stefan Vogel <sv@exept.de>
parents:
5376
diff
changeset
|
3107 |
|
12166 | 3108 |
self printGeneralizedOn:aStream isLocal:isLocal short:false |
3109 |
||
3110 |
||
3111 |
"Modified: / 1.7.1996 / 15:20:59 / cg" |
|
3112 |
"Modified: / 17.1.2000 / 15:53:02 / stefan" |
|
3113 |
! |
|
3114 |
||
3115 |
printGeneralizedOn:aStream isLocal:isLocal short:shortFormat |
|
3116 |
"append a representation of the receiver to aStream in a general format, |
|
3117 |
top-down, without separators; |
|
18761 | 3118 |
long format: 'yyyymmddHHMMSS.mmm+0100' |
3119 |
short format: 'yymmddHHMMSS.mmm+0100' |
|
3120 |
Using the short format is strictly discouraged!! |
|
12166 | 3121 |
|
3122 |
This format is used for the ASN.1 GeneralizedTime and UTCTime |
|
3123 |
as defined in X.208 Sec. 33, so read this before changing the output format. |
|
18761 | 3124 |
|
3125 |
If isLocal is true, represent as local time (Local 'yyyymmddHHMMSS.mmm+0100'), |
|
3126 |
otherwise as UTC time (UTC 'yyyymmddHHMMSS.mmmZ')." |
|
12166 | 3127 |
|
5290 | 3128 |
|t off| |
3129 |
||
12819
a28ce068acd8
comment/format in: #UTCYear:month:day:hour:minute:second:millisecond:
Stefan Vogel <sv@exept.de>
parents:
12729
diff
changeset
|
3130 |
isLocal ifTrue:[ |
18761 | 3131 |
t := self timeInfo. |
3132 |
off := t utcOffset. |
|
12819
a28ce068acd8
comment/format in: #UTCYear:month:day:hour:minute:second:millisecond:
Stefan Vogel <sv@exept.de>
parents:
12729
diff
changeset
|
3133 |
] ifFalse:[ |
18761 | 3134 |
t := self asUtcTimestamp timeInfo. |
3135 |
off := 0. |
|
12819
a28ce068acd8
comment/format in: #UTCYear:month:day:hour:minute:second:millisecond:
Stefan Vogel <sv@exept.de>
parents:
12729
diff
changeset
|
3136 |
]. |
a28ce068acd8
comment/format in: #UTCYear:month:day:hour:minute:second:millisecond:
Stefan Vogel <sv@exept.de>
parents:
12729
diff
changeset
|
3137 |
|
12166 | 3138 |
shortFormat ifTrue:[ |
18761 | 3139 |
self assert:(t year between:1951 and:2049). |
3140 |
(t year \\ 100) printOn:aStream leftPaddedTo:2 with:$0. |
|
12166 | 3141 |
] ifFalse:[ |
18761 | 3142 |
t year printOn:aStream leftPaddedTo:4 with:$0. |
12166 | 3143 |
]. |
7512
07c56e8934f4
OS returns OSTimeInfo (instead of an Array)
Claus Gittinger <cg@exept.de>
parents:
7474
diff
changeset
|
3144 |
t month printOn:aStream leftPaddedTo:2 with:$0. |
07c56e8934f4
OS returns OSTimeInfo (instead of an Array)
Claus Gittinger <cg@exept.de>
parents:
7474
diff
changeset
|
3145 |
t day printOn:aStream leftPaddedTo:2 with:$0. |
16945 | 3146 |
t hours printOn:aStream leftPaddedTo:2 with:$0. |
7512
07c56e8934f4
OS returns OSTimeInfo (instead of an Array)
Claus Gittinger <cg@exept.de>
parents:
7474
diff
changeset
|
3147 |
t minutes printOn:aStream leftPaddedTo:2 with:$0. |
07c56e8934f4
OS returns OSTimeInfo (instead of an Array)
Claus Gittinger <cg@exept.de>
parents:
7474
diff
changeset
|
3148 |
t seconds printOn:aStream leftPaddedTo:2 with:$0. |
5290 | 3149 |
aStream nextPut:$.. |
7512
07c56e8934f4
OS returns OSTimeInfo (instead of an Array)
Claus Gittinger <cg@exept.de>
parents:
7474
diff
changeset
|
3150 |
t milliseconds printOn:aStream leftPaddedTo:3 with:$0. |
5290 | 3151 |
|
18761 | 3152 |
(self isUtcTimestamp or:[off == 0]) ifTrue:[ |
3153 |
aStream nextPut:$Z. |
|
3154 |
] ifFalse:[ |min| |
|
3155 |
off < 0 ifTrue:[ |
|
3156 |
aStream nextPut:$+. |
|
3157 |
off := off negated. |
|
3158 |
] ifFalse:[ |
|
3159 |
aStream nextPut:$-. |
|
3160 |
]. |
|
3161 |
min := off // 60. |
|
3162 |
min // 60 printOn:aStream leftPaddedTo:2 with:$0. |
|
3163 |
min \\ 60 printOn:aStream leftPaddedTo:2 with:$0. |
|
5290 | 3164 |
]. |
3165 |
||
3166 |
" |
|
16945 | 3167 |
Timestamp now printGeneralizedOn:Transcript. Transcript cr. |
3168 |
Timestamp now printGeneralizedOn:Transcript isLocal:true. Transcript cr. |
|
8246 | 3169 |
(Timestamp fromSeconds:0) printGeneralizedOn:Transcript. Transcript cr. |
16945 | 3170 |
Time now printOn:Transcript. Transcript cr. |
3171 |
Date today printOn:Transcript. Transcript cr. |
|
3172 |
||
3173 |
Timestamp now printGeneralizedOn:Transcript isLocal:false short:false. Transcript cr. |
|
18761 | 3174 |
Timestamp now printGeneralizedOn:Transcript isLocal:true short:false. Transcript cr. |
16945 | 3175 |
UtcTimestamp now printGeneralizedOn:Transcript isLocal:false short:false. Transcript cr. |
18761 | 3176 |
UtcTimestamp now printGeneralizedOn:Transcript isLocal:true short:false. Transcript cr. |
16945 | 3177 |
|
3178 |
Date today asTimestamp printGeneralizedOn:Transcript. Transcript cr. |
|
3179 |
Date today printOn:Transcript. Transcript cr. |
|
5290 | 3180 |
" |
3181 |
||
3182 |
"Modified: / 1.7.1996 / 15:20:59 / cg" |
|
3183 |
"Modified: / 17.1.2000 / 15:53:02 / stefan" |
|
3184 |
! |
|
3185 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3186 |
printISO8601 |
22840 | 3187 |
"obsoleted due to uc/lc and print vs. printString confusion" |
3188 |
||
16988 | 3189 |
<resource: #obsolete> |
23023 | 3190 |
"print in a format like 2014-10-17T17:08:44Z" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3191 |
|
16988 | 3192 |
^ self printStringIso8601 |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3193 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3194 |
"Created: / 16-06-2005 / 16:11:15 / masca" |
23023 | 3195 |
"Modified (comment): / 25-05-2018 / 12:00:52 / Claus Gittinger" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3196 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3197 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3198 |
printISO8601Compressed |
22840 | 3199 |
"obsoleted due to uc/lc and print vs. printString confusion" |
3200 |
||
16988 | 3201 |
<resource: #obsolete> |
23023 | 3202 |
"return a printString in a format like 20141017T170939Z" |
16988 | 3203 |
|
3204 |
^ self printStringIso8601Compressed |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3205 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3206 |
" |
16945 | 3207 |
Timestamp now printISO8601Compressed |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3208 |
" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3209 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3210 |
"Created: / 16-06-2005 / 16:11:31 / masca" |
23023 | 3211 |
"Modified (comment): / 25-05-2018 / 12:00:47 / Claus Gittinger" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3212 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3213 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3214 |
printISO8601CompressedOn: aStream |
22840 | 3215 |
"obsoleted due to uc/lc confusion" |
3216 |
||
16988 | 3217 |
<resource: #obsolete> |
23023 | 3218 |
"print in a format like 20141017T170939Z on aStream" |
16988 | 3219 |
|
3220 |
self printIso8601CompressedOn: aStream |
|
3221 |
||
3222 |
"Created: / 16-06-2005 / 16:11:50 / masca" |
|
23023 | 3223 |
"Modified (comment): / 25-05-2018 / 12:00:42 / Claus Gittinger" |
16988 | 3224 |
! |
3225 |
||
3226 |
printISO8601On: aStream |
|
22840 | 3227 |
"obsoleted due to uc/lc confusion" |
3228 |
||
16988 | 3229 |
<resource: #obsolete> |
23023 | 3230 |
"print in a format like 2014-10-17T17:08:44Z on aStream" |
16988 | 3231 |
|
3232 |
self printIso8601On: aStream |
|
3233 |
||
3234 |
"Created: / 16-06-2005 / 16:11:07 / masca" |
|
23023 | 3235 |
"Modified (comment): / 25-05-2018 / 12:00:37 / Claus Gittinger" |
16988 | 3236 |
! |
3237 |
||
3238 |
printIso8601CompressedOn: aStream |
|
23023 | 3239 |
"print in a format like 20141017T170939Z on aStream" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3240 |
|
23003 | 3241 |
self printIso8601FormatOn:aStream compressed:true timeSeparator:$T |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3242 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3243 |
"Created: / 16-06-2005 / 16:11:50 / masca" |
23003 | 3244 |
"Modified: / 24-05-2018 / 17:33:12 / Claus Gittinger" |
23023 | 3245 |
"Modified (comment): / 25-05-2018 / 12:00:30 / Claus Gittinger" |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3246 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3247 |
|
8479 | 3248 |
printIso8601FormatOn:aStream |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3249 |
"append the iso8601 representation of the receiver to aStream. |
8479 | 3250 |
This format looks like: |
23003 | 3251 |
1999-01-01T24:00:00 |
16945 | 3252 |
or, for zero hr:min:sec, |
23003 | 3253 |
1999-01-01 |
17049 | 3254 |
Of course, a 24 hour clock is used. |
3255 |
||
23023 | 3256 |
Timezone information (eg. Z or +0100) is added for TZ and Utc stamps, |
3257 |
otherwise the reader will read as local time." |
|
17049 | 3258 |
|
23003 | 3259 |
self printIso8601FormatOn:aStream compressed:false timeSeparator:$T |
17049 | 3260 |
|
3261 |
" |
|
3262 |
Timestamp now printIso8601FormatOn:Transcript. Transcript cr. |
|
3263 |
Timestamp readIso8601FormatFrom:(Timestamp now printStringIso8601Format). |
|
3264 |
||
3265 |
UtcTimestamp now printIso8601FormatOn:Transcript. Transcript cr. |
|
3266 |
UtcTimestamp readIso8601FormatFrom:(UtcTimestamp now printStringIso8601Format). |
|
3267 |
" |
|
23003 | 3268 |
|
3269 |
"Modified: / 24-05-2018 / 17:28:36 / Claus Gittinger" |
|
23023 | 3270 |
"Modified (comment): / 25-05-2018 / 12:00:23 / Claus Gittinger" |
17049 | 3271 |
! |
3272 |
||
23003 | 3273 |
printIso8601FormatOn:aStream compressed:compact timeSeparator:sepChar |
17049 | 3274 |
"append the iso8601 representation of the receiver to aStream. |
3275 |
This format looks like: |
|
22827 | 3276 |
1999-01-01T24:00:00 |
17049 | 3277 |
or, for zero hr:min:sec, |
22827 | 3278 |
1999-01-01 |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3279 |
Of course, a 24 hour clock is used. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3280 |
|
23023 | 3281 |
Timezone information (eg. Z or +0100) is added for TZ and Utc stamps, |
3282 |
otherwise the reader will read as local time." |
|
8479 | 3283 |
|
16975 | 3284 |
|asUTC asLocal| |
3285 |
||
16920 | 3286 |
self isUtcTimestamp ifTrue:[ |
22827 | 3287 |
asUTC := true. asLocal := false. |
16920 | 3288 |
] ifFalse:[ |
23239 | 3289 |
asUTC := false. |
3290 |
asLocal := self isLocalTimestamp. |
|
8479 | 3291 |
]. |
16920 | 3292 |
|
16975 | 3293 |
Timestamp::TimestampISO8601Builder |
23003 | 3294 |
print:self compact:compact |
22827 | 3295 |
asLocal:asLocal asUTC:asUTC withMilliseconds:true |
3296 |
timeSeparator:sepChar |
|
3297 |
on:aStream |
|
16975 | 3298 |
|
16920 | 3299 |
"/ |
3300 |
"/ |format| |
|
3301 |
"/ |
|
3302 |
"/ format := '%(year)-%(month)-%(day)T%h:%m:%s.%i'. |
|
3303 |
"/ self milliseconds = 0 ifTrue:[ |
|
3304 |
"/ format := '%(year)-%(month)-%(day)T%h:%m:%s'. |
|
3305 |
"/ self seconds = 0 ifTrue:[ |
|
3306 |
"/ format := '%(year)-%(month)-%(day)T%h:%m'. |
|
3307 |
"/ ((self hours = 0) and:[self minutes = 0]) ifTrue:[ |
|
3308 |
"/ format := '%(year)-%(month)-%(day)'. |
|
3309 |
"/ ] |
|
3310 |
"/ ] |
|
3311 |
"/ ]. |
|
16945 | 3312 |
"/ self printOn:aStream format:format |
8479 | 3313 |
|
3314 |
" |
|
22827 | 3315 |
Timestamp now printStringIso8601Format. |
3316 |
||
16945 | 3317 |
Timestamp now printIso8601FormatOn:Transcript. Transcript cr. |
8479 | 3318 |
Timestamp readIso8601FormatFrom:(Timestamp now printStringIso8601Format). |
16920 | 3319 |
|
16945 | 3320 |
UtcTimestamp now printIso8601FormatOn:Transcript. Transcript cr. |
16920 | 3321 |
UtcTimestamp readIso8601FormatFrom:(UtcTimestamp now printStringIso8601Format). |
8479 | 3322 |
" |
23003 | 3323 |
|
3324 |
"Created: / 24-05-2018 / 17:28:25 / Claus Gittinger" |
|
23023 | 3325 |
"Modified (comment): / 25-05-2018 / 12:00:14 / Claus Gittinger" |
23239 | 3326 |
"Modified: / 27-07-2018 / 11:57:21 / Stefan Vogel" |
23003 | 3327 |
! |
3328 |
||
3329 |
printIso8601FormatOn:aStream timeSeparator:sepChar |
|
3330 |
"append the iso8601 representation of the receiver to aStream. |
|
3331 |
This format looks like: |
|
3332 |
1999-01-01T24:00:00 |
|
3333 |
or, for zero hr:min:sec, |
|
3334 |
1999-01-01 |
|
3335 |
Of course, a 24 hour clock is used. |
|
3336 |
||
23023 | 3337 |
Timezone information (eg. Z or +0100) is added for TZ and Utc stamps, |
3338 |
otherwise, the reader will read as local time." |
|
23003 | 3339 |
|
3340 |
self printIso8601FormatOn:aStream compressed:false timeSeparator:sepChar |
|
3341 |
||
3342 |
" |
|
3343 |
Timestamp now printStringIso8601Format. |
|
3344 |
||
3345 |
Timestamp now printIso8601FormatOn:Transcript. Transcript cr. |
|
3346 |
Timestamp readIso8601FormatFrom:(Timestamp now printStringIso8601Format). |
|
3347 |
||
3348 |
UtcTimestamp now printIso8601FormatOn:Transcript. Transcript cr. |
|
3349 |
UtcTimestamp readIso8601FormatFrom:(UtcTimestamp now printStringIso8601Format). |
|
3350 |
" |
|
3351 |
||
3352 |
"Modified: / 24-05-2018 / 17:28:28 / Claus Gittinger" |
|
23023 | 3353 |
"Modified (comment): / 25-05-2018 / 11:59:57 / Claus Gittinger" |
8479 | 3354 |
! |
3355 |
||
16988 | 3356 |
printIso8601On: aStream |
23023 | 3357 |
"print in a format like 2014-10-17T17:08:44Z on aStream" |
16988 | 3358 |
|
3359 |
TimestampISO8601Builder print: self on: aStream |
|
3360 |
||
3361 |
"Created: / 16-06-2005 / 16:11:07 / masca" |
|
23023 | 3362 |
"Modified (comment): / 25-05-2018 / 11:59:22 / Claus Gittinger" |
16988 | 3363 |
! |
3364 |
||
241 | 3365 |
printOn:aStream |
275 | 3366 |
"append a user readable representation of the receiver to aStream. |
3367 |
The format is compatible with readFromString:, but not with readFrom:." |
|
3368 |
||
16975 | 3369 |
|asUTC asLocal| |
3370 |
||
3371 |
self isUtcTimestamp ifTrue:[ |
|
23239 | 3372 |
asUTC := true. asLocal := false. |
16975 | 3373 |
] ifFalse:[ |
23239 | 3374 |
asLocal := self isLocalTimestamp. |
16975 | 3375 |
]. |
3376 |
||
3377 |
Timestamp::TimestampISO8601Builder |
|
23239 | 3378 |
print:self compact:false |
3379 |
asLocal:asLocal asUTC:asUTC withMilliseconds:true |
|
3380 |
timeSeparator:(Character space) |
|
3381 |
on:aStream |
|
16975 | 3382 |
|
16955 | 3383 |
"/ self printOn:aStream format:'%(year)-%(mon)-%(day) %h:%m:%s.%i' |
16945 | 3384 |
"/ self printOn:aStream format:'%(Day)-%(mon)-%(year) %h:%m:%s.%i' |
3385 |
"/ self printOn:aStream format:'%(mon)/%(Day)/%(year) %h:%m:%s.%i' |
|
92 | 3386 |
|
3387 |
" |
|
16975 | 3388 |
|tHere tNewYork| |
3389 |
||
16945 | 3390 |
Timestamp now printOn:Transcript. Transcript cr. |
16955 | 3391 |
Timestamp now asUtcTimestamp printOn:Transcript. Transcript cr. |
16975 | 3392 |
(Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST')) printOn:Transcript. Transcript cr. |
3393 |
||
3394 |
tHere := Timestamp now. |
|
3395 |
tNewYork := tHere asTZTimestamp:(Timestamp utcOffsetFrom:'EST'). |
|
3396 |
tHere printOn:Transcript. Transcript cr. |
|
3397 |
tNewYork printOn:Transcript. Transcript cr. |
|
16955 | 3398 |
|
8246 | 3399 |
(Timestamp fromSeconds:0) printOn:Transcript. Transcript cr. |
16945 | 3400 |
Time now printOn:Transcript. Transcript cr. |
3401 |
Date today printOn:Transcript. Transcript cr. |
|
3402 |
||
3403 |
Time now asTimestamp printOn:Transcript. Transcript cr. |
|
3404 |
Timestamp now printOn:Transcript. Transcript cr. |
|
3405 |
||
3406 |
Date today asTimestamp printOn:Transcript. Transcript cr. |
|
3407 |
Date today printOn:Transcript. Transcript cr. |
|
92 | 3408 |
" |
1501 | 3409 |
|
23239 | 3410 |
"Modified: / 01-07-1996 / 15:20:59 / cg" |
3411 |
"Modified: / 27-07-2018 / 11:58:58 / Stefan Vogel" |
|
5719
cf09067dbd1d
can do millisecondDelta easier
Claus Gittinger <cg@exept.de>
parents:
5678
diff
changeset
|
3412 |
! |
30 | 3413 |
|
7474 | 3414 |
printRFC1123FormatOn:aStream |
3415 |
"append the RFC1123 representation of the receiver to aStream. |
|
3416 |
This format is used in HTTP requests and looks like: |
|
17006 | 3417 |
'Fri, 04 Jul 2003 15:56:11 GMT' |
7671 | 3418 |
(always GMT and all names in english)" |
3419 |
||
3420 |
"/ HTTP-date = rfc1123-date | rfc850-date | asctime-date |
|
3421 |
"/ |
|
3422 |
"/ rfc1123-date = wkday "," SP date1 SP time SP "GMT" |
|
3423 |
"/ rfc850-date = weekday "," SP date2 SP time SP "GMT" |
|
3424 |
"/ asctime-date = wkday SP date3 SP time SP 4DIGIT |
|
3425 |
"/ |
|
3426 |
"/ date1 = 2DIGIT SP month SP 4DIGIT |
|
3427 |
"/ ; day month year (e.g., 02 Jun 1982) |
|
3428 |
"/ date2 = 2DIGIT "-" month "-" 2DIGIT |
|
3429 |
"/ ; day-month-year (e.g., 02-Jun-82) |
|
3430 |
"/ date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) |
|
3431 |
"/ ; month day (e.g., Jun 2) |
|
3432 |
"/ |
|
3433 |
"/ time = 2DIGIT ":" 2DIGIT ":" 2DIGIT |
|
3434 |
"/ ; 00:00:00 - 23:59:59 |
|
3435 |
"/ |
|
3436 |
"/ wkday = "Mon" | "Tue" | "Wed" |
|
3437 |
"/ | "Thu" | "Fri" | "Sat" | "Sun" |
|
3438 |
"/ |
|
3439 |
"/ weekday = "Monday" | "Tuesday" | "Wednesday" |
|
3440 |
"/ | "Thursday" | "Friday" | "Saturday" | "Sunday" |
|
3441 |
"/ |
|
3442 |
"/ month = "Jan" | "Feb" | "Mar" | "Apr" |
|
3443 |
"/ | "May" | "Jun" | "Jul" | "Aug" |
|
3444 |
"/ | "Sep" | "Oct" | "Nov" | "Dec" |
|
7474 | 3445 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3446 |
|timeInfo| |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3447 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3448 |
timeInfo := self asUtcTimestamp timeInfo. |
7474 | 3449 |
|
7734 | 3450 |
aStream nextPutAll:(#('Mon' 'Tue' 'Wed' 'Thu' 'Fri' 'Sat' 'Sun') at:timeInfo dayInWeek); |
17006 | 3451 |
nextPutAll:', '. |
7734 | 3452 |
timeInfo day printOn:aStream leftPaddedTo:2 with:$0. |
7671 | 3453 |
aStream space. |
7734 | 3454 |
aStream nextPutAll:(#('Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec') at:timeInfo month). |
7671 | 3455 |
|
16986 | 3456 |
"/ the following is too slow for heavy use |
3457 |
"/ timeInfo printOn:aStream format:'%(ShortDayName), %(day) %(ShortMonthName) %y %h:%m:%s GMT' language:#en. |
|
7677 | 3458 |
|
3459 |
aStream nextPut:Character space. |
|
7734 | 3460 |
timeInfo year printOn:aStream. |
7677 | 3461 |
aStream nextPut:Character space. |
7734 | 3462 |
timeInfo hours printOn:aStream leftPaddedTo:2 with:$0. |
7677 | 3463 |
aStream nextPut:$:. |
7734 | 3464 |
timeInfo minutes printOn:aStream leftPaddedTo:2 with:$0. |
7677 | 3465 |
aStream nextPut:$:. |
7734 | 3466 |
timeInfo seconds printOn:aStream leftPaddedTo:2 with:$0. |
7677 | 3467 |
aStream nextPutAll:' GMT'. |
3468 |
||
7474 | 3469 |
" |
17006 | 3470 |
String streamContents:[:s| Timestamp now printRFC1123FormatOn:s] |
7474 | 3471 |
" |
3472 |
! |
|
3473 |
||
3474 |
printStringRFC1123Format |
|
3475 |
"return the RFC1123 representation of the receiver. |
|
3476 |
This format is used in HTTP requests and looks like: |
|
17006 | 3477 |
'Fri, 04 Jul 2003 15:56:11 GMT' |
7474 | 3478 |
(always GMT)" |
3479 |
||
3480 |
^ String streamContents:[:s | self printRFC1123FormatOn:s] |
|
3481 |
||
3482 |
" |
|
8246 | 3483 |
Timestamp now printStringRFC1123Format |
7474 | 3484 |
" |
3485 |
! |
|
3486 |
||
241 | 3487 |
storeOn:aStream |
275 | 3488 |
"store the receiver in a format suitable for reconstruction of the |
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
3489 |
receiver via readFrom: |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
3490 |
Use a OS/architecture independent format" |
275 | 3491 |
|
16945 | 3492 |
aStream nextPut:$(; |
17067 | 3493 |
nextPutAll:self storeStringClass name; |
3494 |
nextPutAll:' readIso8601FormatFrom:'''. |
|
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
3495 |
self printIso8601FormatOn:aStream. |
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
3496 |
aStream nextPutAll:''')'. |
54 | 3497 |
|
241 | 3498 |
" |
16945 | 3499 |
Timestamp now storeString |
30 | 3500 |
|
8822
10d99a9248f2
#storeOn: stores in ISO 8601 format.
Stefan Vogel <sv@exept.de>
parents:
8797
diff
changeset
|
3501 |
Object readFrom:(Timestamp now storeString) readStream |
8246 | 3502 |
Timestamp readFrom:(Timestamp now storeString) readStream |
241 | 3503 |
" |
17049 | 3504 |
! |
3505 |
||
3506 |
storeStringClass |
|
3507 |
^ self class |
|
30 | 3508 |
! ! |
699 | 3509 |
|
8246 | 3510 |
!Timestamp methodsFor:'private'! |
699 | 3511 |
|
22834 | 3512 |
additionalPicoseconds |
3513 |
"return the additional picoseconds (0..999999999). |
|
3514 |
These must alwyas be smaller than 1000*1000*1000 (i.e. 1ms), |
|
3515 |
to avoid overflow into the millis. |
|
3516 |
These are to be added to any milliseconds" |
|
3517 |
||
3518 |
^ additionalPicoseconds ? 0. |
|
3519 |
||
3520 |
" |
|
3521 |
Timestamp now picoseconds |
|
3522 |
Timestamp nowWithMicroseconds picoseconds |
|
3523 |
" |
|
3524 |
! |
|
3525 |
||
3526 |
additionalPicoseconds:anInteger |
|
3527 |
"set the additional picoseconds (0..999999999). |
|
3528 |
These must alwyas be smaller than 1000*1000*1000 (i.e. 1ms), |
|
3529 |
to avoid overflow into the millis." |
|
3530 |
||
22842 | 3531 |
self assert:(anInteger isInteger). |
22834 | 3532 |
self assert:(anInteger < (1000*1000*1000)). |
3533 |
additionalPicoseconds:= anInteger |
|
3534 |
||
3535 |
" |
|
3536 |
Timestamp now picoseconds |
|
3537 |
Timestamp now additionalPicoseconds |
|
3538 |
||
3539 |
Timestamp nowWithMicroseconds picoseconds |
|
3540 |
Timestamp nowWithMicroseconds additionalPicoseconds |
|
3541 |
" |
|
3542 |
! |
|
3543 |
||
16986 | 3544 |
computeTimeInfo |
3545 |
|d t info| |
|
3546 |
||
3547 |
"/ fake an info which the OS cannot give me |
|
3548 |
d := self asDate. |
|
3549 |
t := self asTime. |
|
3550 |
info := OperatingSystem timeInfoClass new. |
|
17006 | 3551 |
info year:d year month:d month day:d day |
17067 | 3552 |
hours:t hours minutes:t minutes seconds:t seconds milliseconds:self milliseconds |
3553 |
utcOffset:(self utcOffset) dst:false dayInYear:d dayInYear dayInWeek:d dayInWeek. |
|
16986 | 3554 |
^ info |
17026 | 3555 |
|
3556 |
" |
|
17067 | 3557 |
(Timestamp year:1592 month:7 day:1 hour:12 minute:0 second:0 millisecond:555) computeTimeInfo |
3558 |
(Timestamp year:4096 month:7 day:1 hour:12 minute:0 second:0 millisecond:555) computeTimeInfo |
|
17026 | 3559 |
" |
16986 | 3560 |
! |
3561 |
||
3562 |
computeUtcTimeInfo |
|
17026 | 3563 |
|utcTimestamp d t info| |
16986 | 3564 |
|
3565 |
"/ fake an info which the OS cannot give me |
|
17026 | 3566 |
utcTimestamp := self asUtcTimestamp. |
3567 |
d := utcTimestamp asDate. |
|
3568 |
t := utcTimestamp asTime. |
|
16986 | 3569 |
info := OperatingSystem timeInfoClass new. |
17006 | 3570 |
info year:d year month:d month day:d day |
17067 | 3571 |
hours:t hours minutes:t minutes seconds:t seconds milliseconds:self milliseconds |
3572 |
utcOffset:0 dst:false dayInYear:d dayInYear dayInWeek:d dayInWeek. |
|
16986 | 3573 |
^ info |
17026 | 3574 |
|
3575 |
" |
|
17067 | 3576 |
(Timestamp year:1950 month:7 day:1 hour:12 minute:0 second:0) timeInfo |
3577 |
(Timestamp year:1592 month:7 day:1 hour:12 minute:0 second:0) computeUtcTimeInfo |
|
3578 |
(Timestamp year:4096 month:7 day:1 hour:12 minute:0 second:0) computeUtcTimeInfo |
|
17026 | 3579 |
" |
16986 | 3580 |
! |
3581 |
||
1501 | 3582 |
fromOSTime:anUninterpretedOSTime |
16967 | 3583 |
"strictly private: set the milliseconds from an OS time (since the epoch). |
16943 | 3584 |
Notice: timestamps always have millisecond precision (in contrast to Time, where it is optional)" |
699 | 3585 |
|
1501 | 3586 |
osTime := anUninterpretedOSTime |
3587 |
||
3588 |
"Created: 1.7.1996 / 14:33:21 / cg" |
|
3589 |
! |
|
3590 |
||
16942 | 3591 |
fromOSTimeWithMilliseconds:anUninterpretedOSTime |
22834 | 3592 |
"strictly private: set the milliseconds from an OS time (since the epoch). |
3593 |
Notice: timestamps always have millisecond precision (in contrast to Time, where it is optional)" |
|
16942 | 3594 |
|
3595 |
osTime := anUninterpretedOSTime |
|
3596 |
! |
|
3597 |
||
22834 | 3598 |
fromOSTimeWithMilliseconds:anUninterpretedOSTime additionalPicoseconds:picos |
22627 | 3599 |
"strictly private: set the milliseconds and picoSeconds from an OS time (since the epoch)" |
3600 |
||
3601 |
osTime := anUninterpretedOSTime. |
|
22834 | 3602 |
additionalPicoseconds := picos |
22627 | 3603 |
|
3604 |
" |
|
3605 |
Timestamp nowWithMicroseconds |
|
3606 |
" |
|
3607 |
! |
|
3608 |
||
22940 | 3609 |
getMicroseconds |
3610 |
"strictly private: return the truncated microseconds (since the epoch) in utc" |
|
3611 |
||
3612 |
|millisAsMicros| |
|
3613 |
||
3614 |
millisAsMicros := osTime * 1000. |
|
3615 |
additionalPicoseconds notNil ifTrue:[ |
|
3616 |
^ millisAsMicros + (additionalPicoseconds // (1000 * 1000)). |
|
3617 |
]. |
|
3618 |
^ millisAsMicros |
|
3619 |
||
3620 |
"Created: 1.7.1996 / 14:33:56 / cg" |
|
3621 |
! |
|
3622 |
||
1501 | 3623 |
getMilliseconds |
22873 | 3624 |
"strictly private: return the truncated milliseconds (since the epoch) in utc" |
1501 | 3625 |
|
17006 | 3626 |
^ osTime |
1501 | 3627 |
|
3628 |
"Created: 1.7.1996 / 14:33:56 / cg" |
|
699 | 3629 |
! |
3630 |
||
3631 |
getSeconds |
|
22280 | 3632 |
"strictly private: return the (truncated) seconds (since the epoch) in utc" |
699 | 3633 |
|
17006 | 3634 |
^ osTime // 1000 |
22280 | 3635 |
|
3636 |
"Modified (comment): / 21-09-2017 / 18:50:23 / cg" |
|
699 | 3637 |
! |
3638 |
||
22873 | 3639 |
osTime:aTime |
3640 |
"set the internal representation of the time" |
|
3641 |
||
3642 |
osTime := aTime. |
|
3643 |
! |
|
3644 |
||
3645 |
setMicrosecond:aNumber |
|
3646 |
"change the sub-second fractional part only (leaves everything above seconds unchanged)" |
|
3647 |
||
3648 |
self |
|
23239 | 3649 |
setMilliseconds:(osTime // 1000) * 1000 "/ strip off any sub-second part |
22989 | 3650 |
additionalPicoseconds:(aNumber * 1000000) truncated. "/ set picos |
22873 | 3651 |
|
3652 |
" |
|
3653 |
Timestamp now setMicrosecond:15 - 15 microseconds after the current second's start |
|
3654 |
Timestamp now setMicrosecond:0.1 - 100 nanoseconds after the current second's start |
|
3655 |
" |
|
22989 | 3656 |
|
23239 | 3657 |
"Modified: / 27-07-2018 / 10:34:18 / Stefan Vogel" |
22873 | 3658 |
! |
3659 |
||
3660 |
setMillisecond:aNumber |
|
3661 |
"change the sub-second fractional part only (leaves everything above seconds unchanged)" |
|
3662 |
||
3663 |
self |
|
23239 | 3664 |
setMilliseconds:(osTime // 1000) * 1000 "/ strip off any sub-second part |
22989 | 3665 |
additionalPicoseconds:(aNumber * 1000000000) truncated. "/ set picos |
22873 | 3666 |
|
3667 |
" |
|
3668 |
Timestamp now setMillisecond:15 - 15 milliseconds after the current second's start |
|
3669 |
Timestamp now setMillisecond:0.05 - 50 microseconds after the current second's start |
|
3670 |
" |
|
22989 | 3671 |
|
23239 | 3672 |
"Modified: / 27-07-2018 / 10:34:42 / Stefan Vogel" |
22873 | 3673 |
! |
3674 |
||
1501 | 3675 |
setMilliseconds:millis |
16986 | 3676 |
"strictly private: set the milliseconds (since the epoch)" |
1501 | 3677 |
|
17006 | 3678 |
osTime := millis. |
1501 | 3679 |
|
3680 |
"Modified: 20.12.1995 / 11:46:36 / stefan" |
|
3681 |
"Created: 1.7.1996 / 14:34:24 / cg" |
|
3682 |
! |
|
3683 |
||
22873 | 3684 |
setMilliseconds:millis additionalPicoseconds:picos |
3685 |
"strictly private: set the milliseconds (since the epoch) and additional picos" |
|
3686 |
||
3687 |
|rest newMillis newPicos| |
|
3688 |
||
3689 |
millis isInteger ifTrue:[ |
|
3690 |
newMillis := millis. |
|
3691 |
newPicos := 0. |
|
3692 |
] ifFalse:[ |
|
3693 |
newMillis := millis truncated. |
|
3694 |
rest := millis - newMillis. |
|
22989 | 3695 |
newPicos := (rest * 1000000000) truncated. |
22873 | 3696 |
]. |
3697 |
||
3698 |
picos ~~ 0 ifTrue:[ |
|
3699 |
newPicos := newPicos + picos. |
|
22989 | 3700 |
newMillis := newMillis + (newPicos // 1000000000). |
3701 |
newPicos := (newPicos \\ 1000000000) truncated. |
|
22873 | 3702 |
]. |
3703 |
osTime := newMillis. |
|
3704 |
additionalPicoseconds := newPicos. |
|
22989 | 3705 |
|
3706 |
"Modified: / 22-05-2018 / 16:50:34 / Stefan Vogel" |
|
22873 | 3707 |
! |
3708 |
||
3709 |
setNanosecond:aNumber |
|
3710 |
"change the sub-second fractional part only (leaves everything above seconds unchanged)" |
|
3711 |
||
3712 |
self |
|
23239 | 3713 |
setMilliseconds:(osTime // 1000) * 1000 "/ strip off any sub-second part |
22873 | 3714 |
additionalPicoseconds:(aNumber * 1000) rounded. "/ set picos |
3715 |
||
3716 |
" |
|
3717 |
Timestamp now setNanosecond:15 - 15 nanoseconds after the current second's start |
|
3718 |
Timestamp now setNanosecond:0.1 - 10 picoseconds after the current second's start |
|
3719 |
" |
|
23239 | 3720 |
|
3721 |
"Modified: / 27-07-2018 / 10:34:52 / Stefan Vogel" |
|
22873 | 3722 |
! |
3723 |
||
699 | 3724 |
setSeconds:secs |
3725 |
"strictly private: set the seconds (since whatever)" |
|
3726 |
||
17006 | 3727 |
osTime := (secs * 1000). |
795 | 3728 |
|
3729 |
"Modified: 20.12.1995 / 11:46:36 / stefan" |
|
1501 | 3730 |
"Modified: 1.7.1996 / 14:34:10 / cg" |
5853 | 3731 |
! |
3732 |
||
16986 | 3733 |
timeInfo |
17006 | 3734 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
17067 | 3735 |
"/ fake an info which the OS cannot give me |
3736 |
^ self computeTimeInfo |
|
16986 | 3737 |
]. |
3738 |
||
3739 |
^ OperatingSystem computeTimeAndDateFrom:osTime |
|
17026 | 3740 |
|
3741 |
" |
|
17067 | 3742 |
(Timestamp year:1950 month:7 day:1 hour:12 minute:0 second:0) timeInfo |
3743 |
(UtcTimestamp year:1950 month:7 day:1 hour:12 minute:0 second:0) timeInfo |
|
17026 | 3744 |
" |
16986 | 3745 |
! |
3746 |
||
5853 | 3747 |
utcSecondsSince1970 |
3748 |
"return the UTC seconds since 1970" |
|
3749 |
||
9430
919e0cb2257c
Fix for utc botch when retrieving seconds since 1901
Stefan Vogel <sv@exept.de>
parents:
9416
diff
changeset
|
3750 |
^ self getSeconds |
5853 | 3751 |
|
3752 |
" |
|
8246 | 3753 |
Timestamp now utcSecondsSince1970 |
5853 | 3754 |
" |
17026 | 3755 |
! |
3756 |
||
3757 |
utcTimeInfo |
|
3758 |
(osTime between:MinOSTime and:MaxOSTime) ifFalse:[ |
|
17067 | 3759 |
"/ fake an info which the OS cannot give me |
3760 |
^ self computeUtcTimeInfo |
|
17026 | 3761 |
]. |
3762 |
||
3763 |
^ OperatingSystem computeUTCTimeAndDateFrom:osTime |
|
3764 |
||
3765 |
" |
|
17067 | 3766 |
(Timestamp year:1950 month:7 day:1 hour:12 minute:0 second:0) timeInfo |
3767 |
(Timestamp year:1950 month:7 day:1 hour:12 minute:0 second:0) utcTimeInfo |
|
17026 | 3768 |
" |
699 | 3769 |
! ! |
3770 |
||
22914 | 3771 |
!Timestamp methodsFor:'queries'! |
3772 |
||
3773 |
speciesForCompare |
|
3774 |
"all of my subclass instances can be compared, |
|
3775 |
because they all hold the UTC time internally" |
|
3776 |
||
3777 |
^ Timestamp |
|
3778 |
! ! |
|
3779 |
||
3780 |
||
3781 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3782 |
!Timestamp methodsFor:'testing'! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3783 |
|
16967 | 3784 |
isLocalTimestamp |
23003 | 3785 |
"return true, if I am a local timestamp (i.e. with no TZ info)" |
16986 | 3786 |
|
16967 | 3787 |
^ true |
23003 | 3788 |
|
3789 |
"Modified (comment): / 24-05-2018 / 17:29:32 / Claus Gittinger" |
|
3790 |
! |
|
3791 |
||
3792 |
isTZTimestamp |
|
3793 |
"return true, if I am a timestamp with TZ info" |
|
3794 |
||
3795 |
^ false |
|
3796 |
||
3797 |
"Created: / 24-05-2018 / 17:30:25 / Claus Gittinger" |
|
16967 | 3798 |
! |
3799 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3800 |
isTimestamp |
16986 | 3801 |
"return true, if I am a timestamp" |
3802 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3803 |
^ true |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3804 |
! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3805 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3806 |
isUtcTimestamp |
16986 | 3807 |
"return true, if I am a utc timestamp" |
3808 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3809 |
^ false |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3810 |
! ! |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3811 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3812 |
!Timestamp methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3813 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3814 |
acceptVisitor:aVisitor with:aParameter |
16718 | 3815 |
"dispatch for visitor pattern; send #visitTimestamp:with: to aVisitor." |
3816 |
||
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3817 |
^ aVisitor visitTimestamp:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3818 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8286
diff
changeset
|
3819 |
|
22873 | 3820 |
!Timestamp::TimestampBuilderAbstract class methodsFor:'documentation'! |
3821 |
||
3822 |
documentation |
|
3823 |
" |
|
3824 |
documentation to be added. |
|
3825 |
||
3826 |
[author:] |
|
3827 |
cg |
|
3828 |
||
3829 |
[instance variables:] |
|
3830 |
||
3831 |
[class variables:] |
|
3832 |
||
3833 |
[see also:] |
|
3834 |
||
3835 |
" |
|
3836 |
! |
|
3837 |
||
3838 |
examples |
|
3839 |
" |
|
3840 |
||
3841 |
more examples to be added: |
|
3842 |
[exBegin] |
|
3843 |
... add code fragment for |
|
3844 |
... executable example here ... |
|
3845 |
[exEnd] |
|
3846 |
" |
|
3847 |
! ! |
|
3848 |
||
11993 | 3849 |
!Timestamp::TimestampBuilderAbstract methodsFor:'error reporting'! |
3850 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3851 |
malformed:aString |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3852 |
TimeConversionError raiseErrorString:(' - ', aString) |
11993 | 3853 |
|
3854 |
"Created: / 15-06-2005 / 15:54:04 / masca" |
|
3855 |
! ! |
|
3856 |
||
3857 |
!Timestamp::TimestampBuilderAbstract methodsFor:'support'! |
|
3858 |
||
3859 |
addHoursAndMinutes: arrayWithHoursAndMinutes |
|
3860 |
"Add the given number of hours and minutes to the current timestamp state. If the time |
|
3861 |
is to be subtracted, both numbers in the array must be negated. When the are not the same |
|
3862 |
sign, the behavior may be strange. It's intended only for time zone corrections, where |
|
16977 | 3863 |
not more than 14 (in fact, 23) hours is added or subtracted (ie. date can be modified only |
11993 | 3864 |
one day forward or backward)." |
3865 |
||
3866 |
| hours minutes | |
|
16975 | 3867 |
|
11993 | 3868 |
hours := arrayWithHoursAndMinutes first. |
3869 |
minutes := arrayWithHoursAndMinutes second. |
|
3870 |
||
16975 | 3871 |
utcOffset := (((hours * 60) + minutes) * 60). |
3872 |
||
11993 | 3873 |
minutes isZero ifFalse: [ |
19915 | 3874 |
minute := minute + minutes. |
3875 |
minute >= 60 ifTrue: [ |
|
3876 |
hours := hours + 1. |
|
3877 |
minute := minute - 60. |
|
3878 |
]. |
|
3879 |
minute < 0 ifTrue: [ |
|
3880 |
hours := hours - 1. |
|
3881 |
minute := minute + 60. |
|
3882 |
] |
|
11993 | 3883 |
]. |
3884 |
||
3885 |
"Hours may get zero by time zone specification or by minutes modifications above." |
|
3886 |
hours isZero ifTrue: [^self]. |
|
3887 |
||
3888 |
"Add or subtract the hour and make date corrections if necessary." |
|
3889 |
hour := hour + hours. |
|
3890 |
hour < 0 ifTrue: [ |
|
19915 | 3891 |
"Oops, got to previous day, must adjust even the date." |
3892 |
hour := 24 - ((hour negated) \\ 24). |
|
3893 |
day := day - 1. |
|
3894 |
day <= 0 ifTrue: [ |
|
3895 |
"Go to previous month..." |
|
3896 |
month := month - 1. |
|
3897 |
month <= 0 ifTrue: [year := year - 1. month := 12]. |
|
3898 |
day := self lastDayInMonth: month |
|
3899 |
] |
|
11993 | 3900 |
]. |
3901 |
hour >= 24 ifTrue: [ |
|
19915 | 3902 |
hour := hour \\ 24. |
3903 |
day := day + 1. |
|
3904 |
day > (self lastDayInMonth: month) ifTrue: [ |
|
3905 |
month := month + 1. |
|
3906 |
month > 12 ifTrue: [year := year + 1. month := 1]. |
|
3907 |
day := 1 |
|
3908 |
] |
|
11993 | 3909 |
] |
3910 |
||
3911 |
"Created: / 15-06-2005 / 16:45:49 / masca" |
|
3912 |
"Modified: / 16-06-2005 / 15:04:45 / masca" |
|
3913 |
! |
|
3914 |
||
16836 | 3915 |
dateFromDayNumber: dayInYear |
11993 | 3916 |
"Set month and day from an absolute number of the day in the year. 1.1. is day number one." |
3917 |
||
16836 | 3918 |
|monthAndDay| |
3919 |
||
3920 |
(dayInYear between: 1 and: 365) ifFalse: [ |
|
17006 | 3921 |
(dayInYear = 366 and:[self leapYear: year]) |
3922 |
ifFalse: [self malformed: 'Bad day number: ' , dayInYear printString] |
|
11993 | 3923 |
]. |
3924 |
||
16836 | 3925 |
monthAndDay := Date monthAndDayFromDayInYear:dayInYear forYear:year. |
3926 |
month := monthAndDay at:1. |
|
3927 |
day := monthAndDay at:2 |
|
11993 | 3928 |
|
3929 |
"Created: / 15-06-2005 / 11:27:35 / masca" |
|
3930 |
"Modified: / 16-06-2005 / 12:31:37 / masca" |
|
3931 |
! |
|
3932 |
||
3933 |
dateFromWeek: weekInteger andWeekday: dayInteger |
|
3934 |
"Compute the month and day. Find the first day (weekday) in the year, maybe even |
|
3935 |
adjust the year. Both week and day are 1-based, the first week in a year is the one |
|
3936 |
with thursday (or the one containing 4.1.)." |
|
3937 |
||
16914 | 3938 |
|tmpDate| |
3939 |
||
11993 | 3940 |
"Check numbers. Year may be checked if it contains 53 weeks or 52 weeks only." |
3941 |
(dayInteger between: 1 and: 7) ifFalse: [self malformed: 'Bad weekday number: ' , dayInteger printString]. |
|
3942 |
(weekInteger between: 1 and: 53) ifFalse: [self malformed: 'Bad week number: ' , weekInteger printString]. |
|
3943 |
||
16914 | 3944 |
tmpDate := Date newDayInWeek:dayInteger week:weekInteger year:year. |
3945 |
day := tmpDate day. |
|
3946 |
month := tmpDate month. |
|
3947 |
year := tmpDate year. |
|
11993 | 3948 |
! |
3949 |
||
3950 |
isAllowedDay: anInteger |
|
3951 |
"Answer whether the given day is allowed in the current month." |
|
3952 |
||
3953 |
^anInteger between: 1 and: (self lastDayInMonth: month) |
|
3954 |
||
3955 |
"Created: / 15-06-2005 / 16:22:51 / masca" |
|
3956 |
! |
|
3957 |
||
3958 |
lastDayInMonth: anInteger |
|
3959 |
"Answer the number of the last day of the given month in the current year." |
|
3960 |
||
16836 | 3961 |
^ anInteger = 2 |
17006 | 3962 |
ifTrue: [(self leapYear: year) ifTrue: [29] ifFalse: [28]] |
3963 |
ifFalse: [#(31 28 31 30 31 30 31 31 30 31 30 31) at: month] |
|
11993 | 3964 |
|
3965 |
"Created: / 15-06-2005 / 17:12:31 / masca" |
|
3966 |
! |
|
3967 |
||
16836 | 3968 |
leapYear: anInteger |
3969 |
^ Date leapYear:anInteger. |
|
3970 |
! |
|
3971 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
3972 |
timestampWithClass:timestampClass |
11993 | 3973 |
"Answer the timestamp as it has been parsed." |
3974 |
"Notes: |
|
17016 | 3975 |
- When reading, the time has either been adjusted to UTZ/zulu or explicit offset time, |
3976 |
or is a local time stamp. |
|
3977 |
Attention: an explicit utcOffset in the input string has already been added into the hh:mm values." |
|
11993 | 3978 |
|
16975 | 3979 |
(timestampClass == UtcTimestamp) ifTrue:[ |
22873 | 3980 |
^ UtcTimestamp |
3981 |
UTCYear:year month:month day:day |
|
3982 |
hour:hour minute:minute second:second millisecond:millisecond additionalPicoseconds:picos |
|
16975 | 3983 |
]. |
3984 |
(timestampClass == TZTimestamp) ifTrue:[ |
|
22873 | 3985 |
"/ Attention: an explicit utcOffset in the input string has already been added into the hh:mm values." |
3986 |
^ ((TZTimestamp |
|
3987 |
UTCYear:year month:month day:day |
|
3988 |
hour:hour minute:minute second:second millisecond:millisecond additionalPicoseconds:picos |
|
3989 |
) utcOffset:utcOffset) |
|
16975 | 3990 |
]. |
3991 |
||
16989 | 3992 |
(isUtcTime or:[hasTimezone and:[utcOffset == 0]]) ifTrue:[ |
22873 | 3993 |
^ ((timestampClass == Timestamp) ifTrue:UtcTimestamp ifFalse:timestampClass) |
3994 |
UTCYear:year month:month day:day |
|
3995 |
hour:hour minute:minute second:second millisecond:millisecond additionalPicoseconds:picos |
|
17016 | 3996 |
]. |
3997 |
hasTimezone ifTrue:[ |
|
22873 | 3998 |
"/ Attention: an explicit utcOffset in the input string has already been added into the hh:mm values." |
3999 |
^ (((timestampClass == Timestamp) ifTrue:TZTimestamp ifFalse:timestampClass) |
|
4000 |
UTCYear:year month:month day:day |
|
4001 |
hour:hour minute:minute second:second millisecond:millisecond additionalPicoseconds:picos |
|
4002 |
) utcOffset:utcOffset |
|
17016 | 4003 |
]. |
17067 | 4004 |
"/ there was no timezone info, so make it a local timestamp again. |
22873 | 4005 |
^ timestampClass |
4006 |
year:year month:month day:day |
|
4007 |
hour:hour minute:minute second:second millisecond:millisecond additionalPicoseconds:picos |
|
16914 | 4008 |
! |
4009 |
||
4010 |
yearAlreadyReadAs:yearArg |
|
4011 |
"support for readers which may have already preread the year" |
|
4012 |
||
4013 |
year := yearArg. |
|
4014 |
yearAlreadyRead := true. |
|
11993 | 4015 |
! ! |
4016 |
||
4017 |
!Timestamp::TimestampISO8601Builder class methodsFor:'documentation'! |
|
4018 |
||
4019 |
documentation |
|
4020 |
" |
|
16945 | 4021 |
TimestampISO8601Builder is designed to read any (almost) format of ISO 8601 encoded timestamp. |
4022 |
Also, class methods can be used to print but the main reading job is done in instance protocol. |
|
16915 | 4023 |
It has been written because of insufficient abilities of Timestamp #readIso8601FormatFrom: method |
4024 |
(which was now changed to call this as well). |
|
4025 |
||
4026 |
It produces timestamps, i.e. when the string (or stream) contains only a time, an error will result |
|
11993 | 4027 |
(it may also pass in some cases but with the time undestood as date). It survives incomplete dates, |
4028 |
broken years, incomplete times and timezones. All times read with timezone difference are recomputed |
|
4029 |
to UTC before the timestamp is created (even passing across new year boundary is handled correctly). |
|
4030 |
Unknown offsets (usually local) are considered UTC - this may be wrong and more work is probably needed. |
|
4031 |
All data is checked for validity (including leap years, leap seconds,...) during reading and as soon as |
|
16945 | 4032 |
possible. |
16915 | 4033 |
For an example of what the builder can read, see the examples method and ISO 8601 itself. |
11993 | 4034 |
|
4035 |
[author:] |
|
17006 | 4036 |
Martin Dvorak (masca@volny.cz) |
11993 | 4037 |
|
4038 |
[instance variables:] |
|
17006 | 4039 |
stream A stream the builder operates on. Assigned on each call to instance method #read:, |
4040 |
so the builder instance can be reused (by at most one thread). |
|
4041 |
year Current timestamp year. No default value, date must be present. |
|
4042 |
month Current timestamp month. May change during parsing. Defaults to 1. |
|
4043 |
day Current timestamp day. Defaults to 1. |
|
4044 |
hour Current timestamp hour. Defaults to 0. |
|
4045 |
minute Current timestamp minute. Defaults to 0. |
|
4046 |
second Current timestamp second. Defaults to 0. |
|
4047 |
millisecond Current timestamp millisecond. Defaults to 0. |
|
11993 | 4048 |
|
4049 |
[see also:] |
|
17006 | 4050 |
Timestamp |
11993 | 4051 |
" |
4052 |
! |
|
4053 |
||
4054 |
examples |
|
4055 |
" |
|
21999 | 4056 |
See the unit tests in stx/goodies:regression >> RegressionTests::timeAndDateTest |
11993 | 4057 |
It covers the main features this builder has. |
4058 |
||
4059 |
Just to introduce some coding examples, try: |
|
21999 | 4060 |
Timestamp readISO8601From: (TimestampISO8601Builder print: Timestamp now). |
4061 |
UtcTimestamp readISO8601From: (TimestampISO8601Builder print: UtcTimestamp now). |
|
4062 |
Timestamp readISO8601From: (TimestampISO8601Builder print: UtcTimestamp now). |
|
4063 |
Timestamp readISO8601From: (TimestampISO8601Builder print: TZTimestamp now). |
|
16945 | 4064 |
|
4065 |
Timestamp readISO8601From:'fooBar' onError:[ Timestamp now ]. |
|
11993 | 4066 |
" |
4067 |
! |
|
4068 |
||
4069 |
history |
|
4070 |
"Created: / 16-06-2005 / 16:28:38 / masca" |
|
4071 |
! ! |
|
4072 |
||
4073 |
!Timestamp::TimestampISO8601Builder class methodsFor:'printing'! |
|
4074 |
||
4075 |
print: aTimestamp |
|
16919 | 4076 |
"Returns the printString of the given timestamp in general ISO8601 format, |
4077 |
such as '2014-11-06T11:48:09Z'. |
|
4078 |
The time is printed as UTC time" |
|
11993 | 4079 |
|
4080 |
| stream | |
|
16919 | 4081 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4082 |
stream := '' writeStream. |
11993 | 4083 |
self print: aTimestamp on: stream. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4084 |
^ stream contents |
11993 | 4085 |
|
16919 | 4086 |
" |
4087 |
self print:(Timestamp now) |
|
4088 |
" |
|
4089 |
||
11993 | 4090 |
"Created: / 15-06-2005 / 17:52:29 / masca" |
4091 |
! |
|
4092 |
||
16921 | 4093 |
print:aTimestamp asLocal:asLocal on:aStream |
16919 | 4094 |
"Print the given timestamp in general ISO8601 format, |
4095 |
such as '2014-11-06T11:48:09Z'. |
|
4096 |
The time is always printed as UTC time" |
|
11993 | 4097 |
|
16967 | 4098 |
self print:aTimestamp compact:false asLocal:asLocal asUTC:asLocal not withMilliseconds:true on:aStream |
16919 | 4099 |
|
4100 |
" |
|
4101 |
self print:(Timestamp now) on:Transcript |
|
4102 |
" |
|
11993 | 4103 |
|
4104 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
|
4105 |
! |
|
4106 |
||
16919 | 4107 |
print:aTimestamp on:aStream |
4108 |
"Print the given timestamp in general ISO8601 format, |
|
4109 |
such as '2014-11-06T11:48:09Z'. |
|
4110 |
The time is printed as UTC time" |
|
4111 |
||
22834 | 4112 |
self |
4113 |
print:aTimestamp |
|
4114 |
compact:false asLocal:false asUTC:true |
|
4115 |
withMilliseconds:true timeSeparator:$T timeOnly:false |
|
4116 |
on:aStream |
|
16919 | 4117 |
|
4118 |
" |
|
22834 | 4119 |
self print:(Timestamp now) on:Transcript. |
4120 |
Transcript cr. |
|
4121 |
self print:(Time now) on:Transcript. |
|
4122 |
Transcript cr. |
|
4123 |
self print:(Time nowWithMilliseconds) on:Transcript. |
|
4124 |
Transcript cr. |
|
16919 | 4125 |
" |
4126 |
||
4127 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
|
4128 |
! |
|
4129 |
||
4130 |
printAsLocalTime: aTimestamp on: aStream |
|
4131 |
"Print the given timestamp in general ISO8601 format, |
|
16955 | 4132 |
such as '2014-11-06T11:48:09+01'. |
16919 | 4133 |
The time is printed as local time" |
4134 |
||
22834 | 4135 |
self |
4136 |
print:aTimestamp compact:false asLocal:true asUTC:false |
|
4137 |
withMilliseconds:true timeSeparator:$T timeOnly:false |
|
4138 |
on:aStream |
|
16919 | 4139 |
|
4140 |
" |
|
17006 | 4141 |
self printAsLocalTime:(Timestamp now) on:Transcript |
16919 | 4142 |
" |
4143 |
! |
|
4144 |
||
11993 | 4145 |
printCompressed: aTimestamp |
16919 | 4146 |
"Return a printString in compressed format such as '20141106T114636Z'. |
4147 |
(for example, for timestamp interchange with mobile devices). |
|
4148 |
The time is printed as UTC time" |
|
11993 | 4149 |
|
4150 |
| stream | |
|
16919 | 4151 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4152 |
stream := '' writeStream. |
11993 | 4153 |
self printCompressed: aTimestamp on: stream. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4154 |
^ stream contents |
11993 | 4155 |
|
16919 | 4156 |
" |
4157 |
self printCompressed:(Timestamp now) |
|
4158 |
" |
|
4159 |
||
11993 | 4160 |
"Created: / 15-06-2005 / 17:52:52 / masca" |
4161 |
! |
|
4162 |
||
22834 | 4163 |
printCompressed:aTimestamp asLocal:asLocal on:aStream |
4164 |
"generates a compressed string representation, |
|
4165 |
(optionally as localtime) such as '20141106T114636Z'" |
|
4166 |
||
4167 |
self |
|
4168 |
print:aTimestamp |
|
4169 |
compact:true asLocal:asLocal asUTC:asLocal not |
|
4170 |
withMilliseconds:true timeSeparator:$T timeOnly:false |
|
4171 |
on:aStream |
|
4172 |
||
4173 |
" |
|
4174 |
self printCompressed:(Timestamp now) on:Transcript |
|
4175 |
" |
|
4176 |
||
4177 |
"Created: / 15-06-2005 / 17:54:17 / masca" |
|
4178 |
! |
|
4179 |
||
4180 |
printCompressed:aTimestamp on:aStream |
|
4181 |
"generates a compressed string representation, such as '20141106T114636Z'. |
|
4182 |
The time is printed as UTC time" |
|
4183 |
||
4184 |
self |
|
4185 |
print:aTimestamp |
|
4186 |
compact:true asLocal:false asUTC:true |
|
4187 |
withMilliseconds:true timeSeparator:$T timeOnly:false |
|
4188 |
on:aStream |
|
16919 | 4189 |
|
4190 |
" |
|
4191 |
self printCompressed:(Timestamp now) on:Transcript |
|
4192 |
" |
|
4193 |
||
4194 |
"Created: / 15-06-2005 / 17:54:17 / masca" |
|
4195 |
! |
|
4196 |
||
22834 | 4197 |
printCompressedAsLocalTime:aTimestamp on:aStream |
16919 | 4198 |
"generates a compressed string representation, such as '20141106T114636Z'. |
22834 | 4199 |
The time is printed as local time" |
4200 |
||
4201 |
self |
|
4202 |
print:aTimestamp |
|
4203 |
compact:true asLocal:true asUTC:false |
|
4204 |
withMilliseconds:true timeSeparator:$T timeOnly:false |
|
4205 |
on:aStream |
|
16919 | 4206 |
|
4207 |
" |
|
4208 |
self printCompressed:(Timestamp now) on:Transcript |
|
4209 |
" |
|
4210 |
||
4211 |
"Created: / 15-06-2005 / 17:54:17 / masca" |
|
4212 |
! |
|
4213 |
||
22834 | 4214 |
printTime:aTimeOrTimestamp on:aStream |
4215 |
"Print the given time in general ISO8601 format, |
|
4216 |
such as 'T11:48:09Z'. |
|
4217 |
The time is printed as UTC time. |
|
4218 |
No date is printed." |
|
4219 |
||
4220 |
self |
|
4221 |
print:aTimeOrTimestamp |
|
4222 |
compact:false asLocal:false asUTC:true |
|
4223 |
withMilliseconds:true timeSeparator:$T timeOnly:true |
|
4224 |
on:aStream |
|
16919 | 4225 |
|
4226 |
" |
|
22834 | 4227 |
self print:(Time nowWithMilliseconds) on:Transcript. |
4228 |
Transcript cr. |
|
4229 |
self printTime:(Time nowWithMilliseconds) on:Transcript. |
|
4230 |
Transcript cr. |
|
16919 | 4231 |
" |
11993 | 4232 |
|
22834 | 4233 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
16919 | 4234 |
! |
4235 |
||
16967 | 4236 |
printTimeZone:tzOffsetArg on: aStream |
16919 | 4237 |
"Print the timezone delta" |
4238 |
||
4239 |
|tzOffset tzHours tzMinutes| |
|
4240 |
||
16967 | 4241 |
tzOffset := tzOffsetArg. |
17020 | 4242 |
tzOffset == 0 ifTrue:[ |
17067 | 4243 |
aStream nextPutAll:'+00'. |
17020 | 4244 |
] ifFalse:[ |
17067 | 4245 |
tzOffset := tzOffset // 60. "/ convert from seconds to minutes |
4246 |
tzOffset < 0 ifTrue:[ |
|
4247 |
tzOffset := tzOffset negated. |
|
4248 |
aStream nextPut:$+ |
|
4249 |
] ifFalse:[ |
|
4250 |
aStream nextPut:$-. |
|
4251 |
]. |
|
4252 |
||
4253 |
tzHours := tzOffset // 60. |
|
4254 |
tzMinutes := tzOffset \\ 60. |
|
4255 |
(tzHours ~= 0 or:[tzMinutes ~= 0]) ifTrue:[ |
|
4256 |
aStream |
|
4257 |
nextPutAll:(tzHours printStringRadix: 10 size: 2 fill: $0). |
|
4258 |
tzMinutes ~= 0 ifTrue:[ |
|
4259 |
aStream |
|
4260 |
nextPut: $:; |
|
4261 |
nextPutAll: (tzMinutes printStringRadix: 10 size: 2 fill: $0). |
|
4262 |
]. |
|
4263 |
]. |
|
16919 | 4264 |
]. |
4265 |
||
4266 |
" |
|
4267 |
self print:(Timestamp now) asLocal:true on:Transcript. |
|
4268 |
Transcript cr. |
|
4269 |
||
4270 |
self print:(Timestamp now) asLocal:false on:Transcript. |
|
4271 |
Transcript cr. |
|
4272 |
||
4273 |
self printCompressed:(Timestamp now) asLocal:true on:Transcript. |
|
4274 |
Transcript cr. |
|
4275 |
||
4276 |
self printCompressed:(Timestamp now) asLocal:false on:Transcript. |
|
16945 | 4277 |
Transcript cr. |
16919 | 4278 |
" |
11993 | 4279 |
! ! |
4280 |
||
16921 | 4281 |
!Timestamp::TimestampISO8601Builder class methodsFor:'printing - basic'! |
4282 |
||
22926 | 4283 |
print:aTimeOrTimestamp compact:compact asLocal:asLocal asUTC:asUTC |
4284 |
subSecondDigits:numDigits |
|
4285 |
suppressZeroSubSecondDigits:suppressZeroSubSecondDigits |
|
4286 |
timeSeparator:tSep timeOnly:timeOnly on:aStream |
|
4287 |
||
4288 |
"Print the given timestamp in general ISO8601 format, |
|
4289 |
such as '2014-11-06T11:48:09Z'. |
|
4290 |
compact: if true, the compact format (without separating dashes and colons is generated) |
|
4291 |
asLocal: if true, generates a localtime string (without any timezone info) |
|
4292 |
asUTC: if true, generates a utc string |
|
4293 |
if both are false: |
|
4294 |
generate a string depending on the type of timestamp: |
|
4295 |
if local: generate a local timezone string |
|
4296 |
if utc: generate a utc string |
|
4297 |
otherwise it is a timestamp from another timezone (TZTimestamp), then print in its timezone |
|
4298 |
numDigits: nr of post-second fractional part (i.e. 3 for millis, 6 for micros, 0 for none, #variable for as-required); |
|
4299 |
suppressZeroSubSecondDigits: to suppress zeros (i.e. the old behavior). |
|
4300 |
if timeOnly is true, only the time is printed." |
|
4301 |
||
4302 |
|aTimestamp timeInfo picos picosString | |
|
4303 |
||
4304 |
aTimestamp := aTimeOrTimestamp asTimestamp. |
|
4305 |
||
4306 |
asLocal ifTrue:[ |
|
4307 |
"/ force local |
|
4308 |
timeInfo := aTimestamp asLocalTimestamp timeInfo. |
|
4309 |
] ifFalse:[ |
|
4310 |
asUTC ifTrue:[ |
|
4311 |
"/ force utc |
|
4312 |
timeInfo := aTimestamp asUtcTimestamp timeInfo. |
|
4313 |
] ifFalse:[ |
|
4314 |
"/ in the timestamps own format |
|
4315 |
timeInfo := aTimestamp timeInfo. |
|
4316 |
] |
|
4317 |
]. |
|
4318 |
||
4319 |
timeOnly ifFalse:[ |
|
4320 |
timeInfo year printOn:aStream leftPaddedTo:4 with:$0. |
|
4321 |
compact ifFalse:[ aStream nextPut: $- ]. |
|
4322 |
timeInfo month printOn:aStream leftPaddedTo:2 with:$0. |
|
4323 |
compact ifFalse:[ aStream nextPut: $- ]. |
|
4324 |
timeInfo day printOn:aStream leftPaddedTo:2 with:$0. |
|
23028 | 4325 |
aStream nextPut:tSep. |
22926 | 4326 |
]. |
4327 |
timeInfo hours printOn:aStream leftPaddedTo:2 with:$0. |
|
4328 |
compact ifFalse:[ aStream nextPut: $:]. |
|
4329 |
timeInfo minutes printOn:aStream leftPaddedTo:2 with:$0. |
|
4330 |
||
4331 |
"always print the seconds, even if 0. |
|
4332 |
According to http://www.w3.org/TR/xmlschema11-2/#dateTime this is mandatory" |
|
4333 |
||
4334 |
compact ifFalse:[ aStream nextPut: $:]. |
|
4335 |
timeInfo seconds printOn:aStream leftPaddedTo:2 with:$0. |
|
4336 |
||
22946 | 4337 |
(numDigits == 3 and:[suppressZeroSubSecondDigits not]) ifTrue:[ |
22926 | 4338 |
"/ special case, because it is so common |
4339 |
aStream nextPut: $.. |
|
4340 |
aTimestamp milliseconds printOn:aStream leftPaddedTo:3 with:$0. |
|
4341 |
] ifFalse:[ |
|
4342 |
numDigits ~~ 0 ifTrue:[ |
|
4343 |
picos := aTimestamp picoseconds. |
|
4344 |
(suppressZeroSubSecondDigits and:[picos = 0]) ifFalse:[ |
|
4345 |
"/ not suppressed |
|
4346 |
picosString := picos printStringLeftPaddedTo:12 with:$0. |
|
4347 |
numDigits == #variable ifTrue:[ |
|
4348 |
picosString := picosString withoutTrailing:$0. |
|
4349 |
] ifFalse:[ |
|
4350 |
numDigits > 12 ifTrue:[ |
|
4351 |
picosString := picosString paddedTo:numDigits with:$0 |
|
4352 |
] ifFalse:[ |
|
4353 |
picosString := picosString copyTo:numDigits. |
|
4354 |
]. |
|
4355 |
]. |
|
4356 |
aStream nextPut: $.. |
|
4357 |
aStream nextPutAll:picosString. |
|
4358 |
] |
|
4359 |
]. |
|
4360 |
]. |
|
4361 |
||
4362 |
asUTC ifTrue:[ |
|
4363 |
aStream nextPut: $Z |
|
4364 |
] ifFalse:[ |
|
4365 |
asLocal ifFalse:[ |
|
4366 |
self printTimeZone:aTimestamp utcOffset on:aStream. |
|
4367 |
]. |
|
4368 |
]. |
|
4369 |
||
4370 |
" |
|
4371 |
Transcript cr. self |
|
4372 |
print:(Timestamp nowWithMicroseconds) |
|
4373 |
compact:false asLocal:false asUTC:true |
|
4374 |
subSecondDigits:3 |
|
4375 |
suppressZeroSubSecondDigits:false |
|
4376 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4377 |
||
4378 |
Transcript cr. self |
|
4379 |
print:(Timestamp nowWithMicroseconds) |
|
4380 |
compact:false asLocal:false asUTC:true |
|
4381 |
subSecondDigits:6 |
|
4382 |
suppressZeroSubSecondDigits:false |
|
4383 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4384 |
||
4385 |
Transcript cr. self |
|
4386 |
print:(Timestamp nowWithMicroseconds) |
|
4387 |
compact:false asLocal:false asUTC:true |
|
4388 |
subSecondDigits:#variable |
|
4389 |
suppressZeroSubSecondDigits:false |
|
4390 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4391 |
||
4392 |
Transcript cr. self |
|
4393 |
print:(Timestamp now) |
|
4394 |
compact:false asLocal:false asUTC:true |
|
4395 |
subSecondDigits:#variable |
|
4396 |
suppressZeroSubSecondDigits:false |
|
4397 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4398 |
||
4399 |
Transcript cr. self |
|
4400 |
print:(Timestamp now) |
|
4401 |
compact:false asLocal:false asUTC:true |
|
4402 |
subSecondDigits:1 |
|
4403 |
suppressZeroSubSecondDigits:false |
|
4404 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4405 |
||
4406 |
Transcript cr. self |
|
4407 |
print:(Timestamp now) |
|
4408 |
compact:false asLocal:false asUTC:true |
|
4409 |
subSecondDigits:0 |
|
4410 |
suppressZeroSubSecondDigits:false |
|
4411 |
timeSeparator:$T timeOnly:false on:Transcript |
|
4412 |
" |
|
4413 |
||
4414 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
|
23028 | 4415 |
"Modified: / 26-05-2018 / 13:43:00 / Claus Gittinger" |
22926 | 4416 |
! |
4417 |
||
16967 | 4418 |
print: aTimestamp compact:compact asLocal:asLocal asUTC:asUTC withMilliseconds:withMillis on: aStream |
16921 | 4419 |
"Print the given timestamp in general ISO8601 format, |
4420 |
such as '2014-11-06T11:48:09Z'. |
|
23028 | 4421 |
compact: if true, the compact format (without separating dashes and colons is generated) |
4422 |
asLocal: if true, generates a localtime string (with the machine's current timezone setting) |
|
4423 |
asUTC: if true, generates a utc string |
|
4424 |
if both are false: |
|
4425 |
generate a string depending on the type of timestamp: |
|
4426 |
if local: generate a local timezone string |
|
4427 |
if utc: generate a utc string |
|
4428 |
otherwise it is a timestamp from another timezone (TZTimestamp), then print in its timezone |
|
4429 |
withMilliseconds: if false, no milliseconds are generated" |
|
17006 | 4430 |
|
4431 |
self |
|
23028 | 4432 |
print:aTimestamp |
4433 |
compact:compact asLocal:asLocal asUTC:asUTC |
|
4434 |
withMilliseconds:withMillis |
|
4435 |
timeSeparator:$T timeOnly:false |
|
4436 |
on:aStream |
|
16975 | 4437 |
|
4438 |
" |
|
4439 |
self print:(Timestamp now) on:Transcript |
|
4440 |
self printAsLocalTime:(Timestamp now) on:Transcript |
|
4441 |
self printAsLocalTime:(Timestamp now asTZTimestamp:-7200) on:Transcript |
|
4442 |
" |
|
23028 | 4443 |
|
4444 |
"Modified: / 26-05-2018 / 13:43:39 / Claus Gittinger" |
|
16975 | 4445 |
! |
4446 |
||
4447 |
print:aTimestamp compact:compact asLocal:asLocal asUTC:asUTC withMilliseconds:withMillis timeSeparator:tSep on:aStream |
|
4448 |
"Print the given timestamp in general ISO8601 format, |
|
4449 |
such as '2014-11-06T11:48:09Z'. |
|
17422
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4450 |
compact: if true, the compact format (without separating dashes and colons is generated) |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4451 |
asLocal: if true, generates a localtime string (without any timezone info) |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4452 |
asUTC: if true, generates a utc string |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4453 |
if both are false: |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4454 |
generate a string depending on the type of timestamp: |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4455 |
if local: generate a local timezone string |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4456 |
if utc: generate a utc string |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4457 |
otherwise it is a timestamp from another timezone (TZTimestamp), then print in its timezone |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4458 |
withMilliseconds: if false, no milliseconds are generated" |
0a01cd7cf5f2
class: Timestamp::TimestampISO8601Builder
Stefan Vogel <sv@exept.de>
parents:
17172
diff
changeset
|
4459 |
|
22834 | 4460 |
self |
4461 |
print:aTimestamp |
|
4462 |
compact:compact asLocal:asLocal asUTC:asUTC |
|
4463 |
withMilliseconds:withMillis timeSeparator:tSep timeOnly:false |
|
4464 |
on:aStream |
|
4465 |
||
4466 |
" |
|
4467 |
self print:(Timestamp now) on:Transcript |
|
4468 |
self printAsLocalTime:(Timestamp now) on:Transcript |
|
4469 |
self printAsLocalTime:(Timestamp now asTZTimestamp:-7200) on:Transcript |
|
4470 |
" |
|
4471 |
||
4472 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
|
4473 |
! |
|
4474 |
||
22926 | 4475 |
print:aTimeOrTimestamp compact:compact asLocal:asLocal asUTC:asUTC |
4476 |
withMilliseconds:withMillis timeSeparator:tSep timeOnly:timeOnly on:aStream |
|
4477 |
||
22834 | 4478 |
"Print the given timestamp in general ISO8601 format, |
4479 |
such as '2014-11-06T11:48:09Z'. |
|
4480 |
compact: if true, the compact format (without separating dashes and colons is generated) |
|
4481 |
asLocal: if true, generates a localtime string (without any timezone info) |
|
4482 |
asUTC: if true, generates a utc string |
|
4483 |
if both are false: |
|
4484 |
generate a string depending on the type of timestamp: |
|
4485 |
if local: generate a local timezone string |
|
4486 |
if utc: generate a utc string |
|
4487 |
otherwise it is a timestamp from another timezone (TZTimestamp), then print in its timezone |
|
4488 |
withMilliseconds: if false, no milliseconds are generated. |
|
22926 | 4489 |
if timeOnly is true, only the time is printed. |
4490 |
Warning: this has a feature (a bug) of implicitly suppressing fractional seconds, if the millis are zero. |
|
4491 |
this is strange, but for backward compatibility, left as is |
|
4492 |
(in case some printout depends on it)" |
|
4493 |
||
4494 |
|nDigits| |
|
4495 |
||
4496 |
nDigits := withMillis ifTrue:[3] ifFalse:[0]. |
|
4497 |
self |
|
4498 |
print:aTimeOrTimestamp |
|
4499 |
compact:compact asLocal:asLocal asUTC:asUTC |
|
4500 |
subSecondDigits:nDigits |
|
4501 |
suppressZeroSubSecondDigits:true |
|
4502 |
timeSeparator:tSep timeOnly:timeOnly on:aStream |
|
4503 |
||
16921 | 4504 |
|
4505 |
" |
|
4506 |
self print:(Timestamp now) on:Transcript |
|
22834 | 4507 |
self print:(Time now) on:Transcript |
16967 | 4508 |
self printAsLocalTime:(Timestamp now) on:Transcript |
4509 |
self printAsLocalTime:(Timestamp now asTZTimestamp:-7200) on:Transcript |
|
16921 | 4510 |
" |
4511 |
||
4512 |
"Created: / 15-06-2005 / 17:56:51 / masca" |
|
4513 |
! ! |
|
4514 |
||
16914 | 4515 |
!Timestamp::TimestampISO8601Builder class methodsFor:'public parsing'! |
4516 |
||
4517 |
read: stringOrStream withClass:timestampClass |
|
4518 |
^ self new read:stringOrStream withClass:timestampClass |
|
4519 |
||
4520 |
"Created: / 15-06-2005 / 17:52:03 / masca" |
|
4521 |
! |
|
4522 |
||
4523 |
read: stringOrStream withClass:timestampClass yearAlreadyReadAs:yearArg |
|
4524 |
"support for readers which may have already preread the year" |
|
4525 |
||
16945 | 4526 |
^ self new |
17006 | 4527 |
yearAlreadyReadAs:yearArg; |
4528 |
read:stringOrStream withClass:timestampClass |
|
16914 | 4529 |
|
4530 |
"Created: / 15-06-2005 / 17:52:03 / masca" |
|
4531 |
! ! |
|
4532 |
||
11993 | 4533 |
!Timestamp::TimestampISO8601Builder methodsFor:'private-reading'! |
4534 |
||
4535 |
nextDigit |
|
4536 |
| char | |
|
16939 | 4537 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4538 |
char := stream peekOrNil. |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4539 |
char isNil ifTrue: [^-1]. |
11993 | 4540 |
|
17067 | 4541 |
char isDigit ifTrue: [ |
4542 |
stream next. |
|
4543 |
^ char codePoint - $0 codePoint |
|
4544 |
]. |
|
4545 |
^ -1 |
|
11993 | 4546 |
|
4547 |
"Created: / 14-06-2005 / 11:48:52 / masca" |
|
4548 |
! |
|
4549 |
||
4550 |
nextDigitOrError |
|
4551 |
||
4552 |
| digit | |
|
4553 |
digit := self nextDigit. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4554 |
^ digit < 0 |
17006 | 4555 |
ifTrue: [self malformed: 'No digit found'] |
4556 |
ifFalse: [digit] |
|
11993 | 4557 |
|
4558 |
"Created: / 15-06-2005 / 10:57:00 / masca" |
|
4559 |
"Modified: / 15-06-2005 / 17:22:52 / masca" |
|
4560 |
! |
|
4561 |
||
4562 |
nextDigits: anInteger |
|
4563 |
||
4564 |
| char number | |
|
4565 |
number := 0. |
|
4566 |
anInteger timesRepeat: [ |
|
17006 | 4567 |
char := stream peekOrNil. |
4568 |
char ifNil: [self malformed: 'Stream does not contain all ' , anInteger printString , ' digits']. |
|
4569 |
char isDigit |
|
4570 |
ifTrue: [ |
|
4571 |
stream next. |
|
4572 |
number := number * 10 + char codePoint - $0 codePoint] |
|
4573 |
ifFalse: [self malformed: 'Requested ' , anInteger printString , ' digits not found'] |
|
11993 | 4574 |
]. |
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4575 |
^ number |
11993 | 4576 |
|
4577 |
"Created: / 14-06-2005 / 11:57:22 / masca" |
|
4578 |
"Modified: / 15-06-2005 / 15:54:29 / masca" |
|
4579 |
! ! |
|
4580 |
||
16914 | 4581 |
!Timestamp::TimestampISO8601Builder methodsFor:'public processing'! |
11993 | 4582 |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4583 |
read:stringOrStream withClass:timestampClass |
11993 | 4584 |
| peek | |
4585 |
||
4586 |
stream := stringOrStream readStream. |
|
4587 |
||
4588 |
month := day := 1. |
|
4589 |
hour := minute := second := millisecond := 0. |
|
16975 | 4590 |
isUtcTime := hasTimezone := false. |
4591 |
utcOffset := 0. |
|
11993 | 4592 |
|
16936 | 4593 |
yearAlreadyRead ~~ true ifTrue:[ |
17465 | 4594 |
"Read the year. This will read and swallow up to four year digits." |
4595 |
self readYear. |
|
16923 | 4596 |
]. |
11993 | 4597 |
|
4598 |
"Check if date has been read, ie. T or space necountered. If yes, read the time. |
|
4599 |
There is possible inconsistency - a dash may be read followed by T, which is not |
|
4600 |
valid. But don't mind that, timestamps will be well-formatted in most cases." |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4601 |
peek := stream peekOrNil. |
11993 | 4602 |
peek ifNil: [ |
17465 | 4603 |
"End of stream, only year has been read." |
4604 |
^ self timestampWithClass:timestampClass]. |
|
16955 | 4605 |
peek == $- ifTrue: [ |
17465 | 4606 |
"Skip the dash after year, if present." |
4607 |
stream next. |
|
4608 |
peek := stream peekOrNil]. |
|
11993 | 4609 |
peek := peek asUppercase. |
4610 |
||
17465 | 4611 |
(peek == $T or: [peek == Character space]) |
4612 |
ifTrue: [ |
|
4613 |
"Got time signature. Skip the signature, read time and answer the timestamp." |
|
4614 |
stream next. |
|
4615 |
peek == Character space ifTrue:[stream skipSeparators]. |
|
4616 |
self readTime. |
|
4617 |
self readTimezone. |
|
4618 |
^ self timestampWithClass:timestampClass |
|
4619 |
] |
|
4620 |
ifFalse: [ |
|
4621 |
"Date not read completely yet, expecting month/day or week/day or day" |
|
4622 |
peek == $W |
|
4623 |
ifTrue: [ |
|
4624 |
"Parse week number and (possibly) day number." |
|
4625 |
stream next. |
|
4626 |
self readWeekNumber] |
|
4627 |
ifFalse: [ |
|
4628 |
"Got digit, read month number followed by day or day number." |
|
4629 |
self readMonthOrDay] |
|
4630 |
]. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4631 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4632 |
peek := stream peekOrNil. |
11993 | 4633 |
peek ifNil: [ |
22873 | 4634 |
"End of stream, only date has been read." |
17465 | 4635 |
^ self timestampWithClass:timestampClass]. |
16955 | 4636 |
|
4637 |
(peek asUppercase == $T or: [peek == Character space]) |
|
17465 | 4638 |
ifTrue: [ |
4639 |
"Got time signature, expecting time follows. Otherwise only date was in the stream." |
|
4640 |
stream next. |
|
4641 |
peek == Character space ifTrue:[stream skipSeparators]. |
|
4642 |
self readTime. |
|
4643 |
self readTimezone |
|
4644 |
]. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4645 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4646 |
^ self timestampWithClass:timestampClass |
11993 | 4647 |
|
4648 |
"Created: / 14-06-2005 / 11:45:04 / masca" |
|
4649 |
"Modified: / 16-06-2005 / 10:15:35 / masca" |
|
4650 |
! ! |
|
4651 |
||
4652 |
!Timestamp::TimestampISO8601Builder methodsFor:'reading'! |
|
4653 |
||
16914 | 4654 |
readFraction |
4655 |
"Read an arbitrary number of digits representing a fraction." |
|
4656 |
||
16939 | 4657 |
^ Fraction readDecimalFractionFrom:stream onError:[self malformed: 'Missing digits after fraction separator']. |
22873 | 4658 |
|
4659 |
" |
|
4660 |
(Fraction readDecimalFractionFrom:'12345' readStream onError:nil) |
|
4661 |
" |
|
16914 | 4662 |
! |
4663 |
||
11993 | 4664 |
readMilliseconds |
22873 | 4665 |
"Read an arbitrary number of digits representing the fractional part |
4666 |
(used to be milliseconds, but now we can represent anything down to pico seconds" |
|
4667 |
||
4668 |
|fraction ms| |
|
4669 |
||
4670 |
fraction := self readFraction. "/ 0 .. 0.99999... |
|
4671 |
ms := (fraction * 1000). "/ 0 .. 999.999999 |
|
4672 |
millisecond := (ms // 1). "/ 0 .. 999 |
|
4673 |
picos := (ms \\ 1) * (1000 * 1000 * 1000). |
|
11993 | 4674 |
|
4675 |
"Created: / 15-06-2005 / 15:25:45 / masca" |
|
4676 |
! |
|
4677 |
||
4678 |
readMonthOrDay |
|
4679 |
"Read month number, optionally followed by day, or absolute day number (three digit)." |
|
4680 |
||
4681 |
| dayDigit1 dayDigit2 | |
|
4682 |
month := self nextDigits: 2. |
|
4683 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4684 |
stream peekOrNil = $- |
17006 | 4685 |
ifTrue: [ |
4686 |
"Got dash. Day number must follow." |
|
4687 |
stream next. |
|
4688 |
day := self nextDigits: 2. |
|
4689 |
(self isAllowedDay: day) ifFalse: [self malformed: 'Bad day: ' , day printString]. |
|
4690 |
^self]. |
|
11993 | 4691 |
|
4692 |
dayDigit1 := self nextDigit. |
|
4693 |
dayDigit1 < 0 ifTrue: [ |
|
17006 | 4694 |
"No more digits than month, leave day unspecified." |
4695 |
(month between: 1 and: 12) ifFalse: [self malformed: 'Bad month: ' , month printString]. |
|
4696 |
^self]. |
|
11993 | 4697 |
|
4698 |
dayDigit2 := self nextDigit. |
|
4699 |
dayDigit2 < 0 |
|
17006 | 4700 |
ifTrue: [ |
4701 |
"Read only three digits, this is absolute day number in a year." |
|
4702 |
self dateFromDayNumber: month * 10 + dayDigit1] |
|
4703 |
ifFalse: [ |
|
4704 |
"Read four digits. So there's month and day." |
|
4705 |
(month between: 1 and: 12) ifFalse: [self malformed: 'Bad month: ' , month printString]. |
|
4706 |
day := dayDigit1 * 10 + dayDigit2. |
|
4707 |
(self isAllowedDay: day) ifFalse: [self malformed: 'Bad day: ' , day printString]] |
|
11993 | 4708 |
|
4709 |
"Created: / 15-06-2005 / 11:12:02 / masca" |
|
4710 |
"Modified: / 16-06-2005 / 11:47:34 / masca" |
|
4711 |
! |
|
4712 |
||
4713 |
readTime |
|
22873 | 4714 |
"Date already read, don't mind it. |
4715 |
Read only the time value." |
|
11993 | 4716 |
|
16914 | 4717 |
| peek f | |
11993 | 4718 |
|
4719 |
hour := self nextDigits: 2. |
|
4720 |
(hour between: 0 and: 24) ifFalse: [self malformed: 'Bad hour: ' , hour printString]. |
|
4721 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4722 |
peek := stream peekOrNil. |
16914 | 4723 |
peek isNil ifTrue: [^self]. |
4724 |
(peek == $:) ifTrue: [ |
|
20817 | 4725 |
"/ read minutes |
4726 |
stream next. |
|
4727 |
minute := self nextDigits: 2. |
|
16914 | 4728 |
] ifFalse: [ |
20817 | 4729 |
peek isDigit ifTrue: [ |
4730 |
"/ read minutes |
|
4731 |
minute := self nextDigits: 2. |
|
4732 |
] ifFalse:[ |
|
4733 |
(peek == $. or:[peek == $,]) ifTrue:[ |
|
4734 |
stream next. |
|
4735 |
minute := self readFraction * 60. |
|
4736 |
] ifFalse:[ |
|
4737 |
^ self. |
|
4738 |
]. |
|
4739 |
] |
|
16914 | 4740 |
]. |
4741 |
||
4742 |
minute isInteger ifFalse:[ |
|
20817 | 4743 |
f := minute. |
4744 |
minute := f truncated. |
|
4745 |
second := (f - minute) * 60. |
|
4746 |
second isInteger ifFalse:[ |
|
4747 |
f := second. |
|
4748 |
second := f truncated. |
|
4749 |
millisecond := (f - second) * 1000. |
|
4750 |
millisecond := millisecond rounded. |
|
4751 |
]. |
|
16914 | 4752 |
]. |
11993 | 4753 |
(minute between: 0 and: 59) ifFalse: [self malformed: 'Bad minute: ' , minute printString]. |
4754 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4755 |
peek := stream peekOrNil. |
16914 | 4756 |
peek isNil ifTrue: [^self]. |
4757 |
(peek == $:) ifTrue: [ |
|
20817 | 4758 |
"/ read seconds |
4759 |
stream next. |
|
4760 |
second := self nextDigits: 2. |
|
16914 | 4761 |
] ifFalse: [ |
20817 | 4762 |
peek isDigit ifTrue: [ |
4763 |
"/ read seconds |
|
4764 |
second := self nextDigits: 2. |
|
4765 |
] ifFalse:[ |
|
4766 |
(peek == $. or:[peek == $,]) ifTrue:[ |
|
4767 |
stream next. |
|
4768 |
second := self readFraction * 60. |
|
4769 |
] ifFalse:[ |
|
4770 |
^ self. |
|
4771 |
]. |
|
4772 |
] |
|
16914 | 4773 |
]. |
4774 |
||
4775 |
second isInteger ifFalse:[ |
|
20817 | 4776 |
f := second. |
4777 |
second := f truncated. |
|
4778 |
millisecond := (f - second) * 1000. |
|
4779 |
millisecond := millisecond rounded. |
|
16914 | 4780 |
]. |
11993 | 4781 |
(second between: 0 and: 59) ifFalse: [ |
20817 | 4782 |
"Seconds are usually in this range, do a special check for leap seconds." |
4783 |
second <= 61 |
|
4784 |
ifTrue: [ |
|
4785 |
"Leap seconds can occur only on midnight on 31.12. or 30.6. Don't check year |
|
4786 |
as it's not deterministic." |
|
4787 |
(minute = 59 and: [hour = 23 and: [(month = 12 and: [day = 31]) or: [month = 6 and: [day = 30]]]]) |
|
4788 |
ifFalse: [self malformed: 'Bad leap second']] |
|
4789 |
ifFalse: [self malformed: 'Bad second: ' , second printString] |
|
11993 | 4790 |
]. |
4791 |
||
4792 |
"Hour, minute and second read. Read appendices." |
|
16914 | 4793 |
((peek := stream peekOrNil) == $. or:[peek == $,]) |
20817 | 4794 |
ifTrue: [ |
4795 |
"Read dot. Skip it and read milliseconds." |
|
4796 |
stream next. |
|
4797 |
self readMilliseconds]. |
|
11993 | 4798 |
|
4799 |
hour = 24 ifTrue: [ |
|
20817 | 4800 |
(minute = 0 and: [second = 0 and: [millisecond = 0]]) |
4801 |
ifTrue: [ |
|
4802 |
"On 24 hour, advance to the next day." |
|
4803 |
"hour := 0. |
|
4804 |
self addMinutes: 1440"] |
|
4805 |
ifFalse: [self malformed: 'Bad 24 hour (minutes, seconds and millis not 0)'] |
|
11993 | 4806 |
] |
4807 |
||
4808 |
"Created: / 14-06-2005 / 17:27:00 / masca" |
|
4809 |
"Modified: / 30-06-2005 / 11:34:38 / masca" |
|
4810 |
! |
|
4811 |
||
4812 |
readTimezone |
|
4813 |
"Read time zone information. There are three possibilities of what can occur. |
|
22873 | 4814 |
If there is nothing more to read, the offset is unknown - this is treated as |
4815 |
Zulu time as this may not be true." |
|
11993 | 4816 |
|
19916 | 4817 |
| peek tzOffset | |
16975 | 4818 |
|
11993 | 4819 |
peek := stream peek. |
4820 |
peek ifNil: [^self]. |
|
4821 |
peek := peek asUppercase. |
|
4822 |
||
4823 |
"If the time is in Zulu, don't modify the timestamp. This makes the machine |
|
22873 | 4824 |
run in Zulu time zone, maybe some corrections would be nice." |
19916 | 4825 |
peek == $Z ifTrue: [ |
4826 |
"Time read, skip Zulu signature and exit." |
|
4827 |
isUtcTime := true. |
|
4828 |
stream next. |
|
4829 |
^ self |
|
4830 |
]. |
|
4831 |
||
4832 |
(peek == $+ or:[peek == $-]) ifTrue: [ |
|
4833 |
"Read a plus/minus, expect a negative/positive time zone difference." |
|
4834 |
hasTimezone := true. |
|
4835 |
stream next. |
|
4836 |
tzOffset := self readTimezoneOffset. |
|
19917 | 4837 |
peek == $+ ifTrue:[ |
19916 | 4838 |
tzOffset := tzOffset collect: [:e | e negated]. |
4839 |
]. |
|
4840 |
self addHoursAndMinutes: tzOffset. |
|
4841 |
^ self |
|
4842 |
]. |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4843 |
|
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4844 |
"This is local time" |
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4845 |
isUtcTime := false. |
16975 | 4846 |
hasTimezone := false. |
11993 | 4847 |
|
4848 |
"Created: / 16-06-2005 / 09:54:21 / masca" |
|
4849 |
! |
|
4850 |
||
4851 |
readTimezoneOffset |
|
4852 |
"Read time zone offset as a number minutes. Generally, there should be hours only |
|
22873 | 4853 |
but as the format introduces minutes in offsets, we must accept them. |
4854 |
(actually: there are countries with half-hour offsets!!)" |
|
11993 | 4855 |
|
4856 |
| hours digit | |
|
4857 |
||
4858 |
"Read hours." |
|
4859 |
hours := self nextDigits: 2. |
|
4860 |
(hours between: 0 and: 12) ifFalse: [self malformed: 'Bad offset hour: ' , hours printString]. |
|
4861 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4862 |
stream peekOrNil = $: |
22873 | 4863 |
ifTrue: [ |
4864 |
"Colon read, minutes must follow." |
|
4865 |
stream next. |
|
4866 |
digit := self nextDigits: 2. |
|
4867 |
(digit between: 0 and: 59) ifFalse: [self malformed: 'Bad offset minute: ' , digit printString]. |
|
4868 |
^Array with: hours with: digit]. |
|
11993 | 4869 |
|
4870 |
"Read next digit and check whether minutes follow. If not, return only with hours. If yes, |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4871 |
check boundaries." |
11993 | 4872 |
digit := self nextDigit. |
4873 |
digit < 0 ifTrue: [^Array with: hours with: 0]. |
|
4874 |
digit >= 6 ifTrue: [self malformed: 'Bad offset minute: ' , (digit * 10) printString]. |
|
4875 |
||
4876 |
"Read the last digit of offset, it must be present." |
|
4877 |
^Array with: hours with: digit * 10 + self nextDigitOrError |
|
4878 |
||
4879 |
"Created: / 15-06-2005 / 15:35:41 / masca" |
|
4880 |
"Modified: / 15-06-2005 / 17:45:58 / masca" |
|
4881 |
! |
|
4882 |
||
4883 |
readWeekNumber |
|
22873 | 4884 |
"Read week number. It is always two digits long." |
11993 | 4885 |
|
16914 | 4886 |
| week dayInWeek digit | |
4887 |
||
11993 | 4888 |
week := self nextDigits: 2. |
4889 |
||
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4890 |
stream peekOrNil = $- |
22873 | 4891 |
ifTrue: [ |
4892 |
"Got dash, day number must follow." |
|
4893 |
stream next. |
|
4894 |
digit := self nextDigit. |
|
4895 |
digit < 0 ifTrue: [self malformed: 'Bad weekday number']. |
|
4896 |
digit > 7 ifTrue: [self malformed: 'Bad weekday number']. |
|
4897 |
self dateFromWeek: week andWeekday: digit. |
|
4898 |
^self]. |
|
11993 | 4899 |
|
4900 |
"Read day number that follows the week. If the number is not given, consider it monday." |
|
16914 | 4901 |
dayInWeek := self nextDigit. |
4902 |
dayInWeek <= 0 ifTrue: [dayInWeek := 1]. |
|
4903 |
dayInWeek > 7 ifTrue: [self malformed: 'Bad weekday number']. |
|
4904 |
||
4905 |
self dateFromWeek: week andWeekday: dayInWeek |
|
11993 | 4906 |
|
4907 |
"Created: / 14-06-2005 / 12:06:47 / masca" |
|
4908 |
"Modified: / 15-06-2005 / 15:53:34 / masca" |
|
4909 |
! |
|
4910 |
||
4911 |
readYear |
|
4912 |
"Read YYYY or :Y (broken decade) from the stream. Also handles correctly YY- and YYY-." |
|
4913 |
||
4914 |
| read peek | |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4915 |
stream peekOrNil = $: |
17465 | 4916 |
ifTrue: [ |
4917 |
"Broken two digit year > 1999 follows." |
|
4918 |
stream next. |
|
4919 |
year := self nextDigitOrError + 2000. |
|
4920 |
^self]. |
|
11993 | 4921 |
|
4922 |
"Expecting two-, three- or four-digit year" |
|
4923 |
"Read the first two digits. They must be there." |
|
4924 |
read := self nextDigits: 2. |
|
4925 |
||
4926 |
"Check if there's a dash, this can help us deciding whether the year ends." |
|
13236
74872163b47b
Support subclass UtcTimestamp -
Stefan Vogel <sv@exept.de>
parents:
13225
diff
changeset
|
4927 |
peek := stream peekOrNil. |
11993 | 4928 |
peek ifNil: [^self]. |
4929 |
||
17067 | 4930 |
((peek == $-) or:[peek == $W]) |
17465 | 4931 |
ifTrue: [ |
4932 |
"OK, got two digits. These are expected to be the year after 1970." |
|
4933 |
year := read < 70 |
|
4934 |
ifTrue: [read + 2000] |
|
4935 |
ifFalse: [read + 1900] |
|
4936 |
] ifFalse: [ |
|
4937 |
"Read the next digit for the case of three-digit year after 1900 (ie. year > 1999)." |
|
4938 |
read := read * 10 + self nextDigitOrError. |
|
4939 |
peek := stream peekOrNil. |
|
4940 |
(peek isNil or: [peek == $-]) |
|
4941 |
ifTrue: [ |
|
4942 |
"Read three digit year, return it." |
|
4943 |
year := read + 1900 |
|
4944 |
] ifFalse: [ |
|
4945 |
"Read the fourth digit of the year. These can be month digits but the |
|
4946 |
two-digit year format is deprecated anyway." |
|
4947 |
read := read * 10 + self nextDigitOrError. |
|
4948 |
year := read. |
|
4949 |
] |
|
4950 |
] |
|
11993 | 4951 |
|
4952 |
"Created: / 14-06-2005 / 12:01:11 / masca" |
|
4953 |
"Modified: / 15-06-2005 / 17:31:56 / masca" |
|
4954 |
! ! |
|
4955 |
||
8246 | 4956 |
!Timestamp class methodsFor:'documentation'! |
699 | 4957 |
|
4958 |
version |
|
18761 | 4959 |
^ '$Header$' |
12164
b87f20d77369
added: #readShortGeneralizedFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
11994
diff
changeset
|
4960 |
! |
b87f20d77369
added: #readShortGeneralizedFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
11994
diff
changeset
|
4961 |
|
b87f20d77369
added: #readShortGeneralizedFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
11994
diff
changeset
|
4962 |
version_CVS |
18761 | 4963 |
^ '$Header$' |
699 | 4964 |
! ! |
8246 | 4965 |
|
14713 | 4966 |
|
8246 | 4967 |
Timestamp initialize! |