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