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