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