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