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