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