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