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