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