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