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