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