author | Jan Vrany <jan.vrany@labware.com> |
Mon, 29 Nov 2021 23:00:04 +0000 | |
branch | jv |
changeset 25437 | 622824e701ea |
parent 25423 | bcfde4da086a |
permissions | -rw-r--r-- |
16954 | 1 |
" |
2 |
COPYRIGHT (c) 2014 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
17423 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
16954 | 16 |
Timestamp subclass:#TZTimestamp |
17 |
instanceVariableNames:'utcOffset' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Magnitude-Time' |
|
21 |
! |
|
22 |
||
23 |
!TZTimestamp class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2014 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
This class represents time values in milliSeconds starting some time in the past, |
|
42 |
which were created in another (explicit) timezone. |
|
43 |
Internally, they keep the milliseconds based on UTC time (just like the other timestamps), |
|
44 |
so the time values can be compared easily. |
|
17134 | 45 |
However, when printed, the original timezone information is taken into account. |
16954 | 46 |
|
47 |
Also Note: |
|
48 |
On UNIX, osTime can only hold dates between 1970-01-01T00:00:00Z and 2038-01-19T00:00:00Z |
|
17134 | 49 |
However, timestamp instances can hold negative osTime values (which are timestamps |
16954 | 50 |
before 1.1.1970 and greater than 4294967295 (2^32-1) for timestamps after 2038-01-19. |
51 |
||
16966 | 52 |
[example:] |
53 |
the current time as local time: |
|
54 |
Transcript showCR:Timestamp now |
|
55 |
||
56 |
the current time as utc time: |
|
17134 | 57 |
Transcript showCR:UtcTimestamp now. |
16966 | 58 |
same: |
17134 | 59 |
Transcript showCR:Timestamp now asUtcTimestamp. |
16966 | 60 |
|
61 |
the current time in NewYork: |
|
17134 | 62 |
Transcript showCR:( Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') ) |
63 |
same |
|
64 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'EST' ) |
|
65 |
same |
|
66 |
Transcript showCR:( Timestamp now asTZTimestamp:(5*3600) ) |
|
67 |
||
68 |
what is the current time in Berlin: |
|
69 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'MEZ' ) |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
70 |
|
17134 | 71 |
and the date: |
72 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'MEZ' ) asDate |
|
73 |
and the date in Tokio: |
|
74 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'JST' ) asDate |
|
75 |
and the date in Hawai: |
|
76 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'HAST' ) asDate |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
77 |
and the date in NewYork: |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
78 |
Transcript showCR:( Timestamp now asTZTimestampInZone:'EST' ) asDate |
16966 | 79 |
|
16954 | 80 |
[author:] |
81 |
Claus Gittinger |
|
82 |
||
83 |
[See also:] |
|
84 |
Timestamp UtcTimestamp Time Date |
|
85 |
" |
|
86 |
! ! |
|
87 |
||
17423 | 88 |
!TZTimestamp class methodsFor:'instance creation'! |
89 |
||
90 |
fromDate:aDate andTime:aTime utcOffset:utcOffsetSeconds |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
91 |
"return an instance of the receiver, |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
92 |
initialized from a time and a date object. |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
93 |
Both date and time are assumed to be the utcOffset's local time. |
17423 | 94 |
See also `Timestamp now' and other protocol inherited |
95 |
from my superclass." |
|
96 |
||
97 |
^ (self |
|
98 |
UTCYear:aDate year |
|
99 |
month:aDate month |
|
100 |
day:aDate day |
|
101 |
hour:aTime hours |
|
102 |
minute:aTime minutes |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
103 |
second:(aTime seconds + utcOffsetSeconds) |
17423 | 104 |
millisecond:aTime milliseconds) |
105 |
utcOffset:utcOffsetSeconds |
|
106 |
||
107 |
" |
|
108 |
Timestamp fromDate:(Date today) andTime:(Time now) |
|
109 |
Timestamp fromDate:(Date today) andTime:(Time nowWithMilliseconds) |
|
110 |
||
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
111 |
TZTimestamp fromDate:(Date today) andTime:(Time now) utcOffset:3600 |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
112 |
|
17423 | 113 |
Timestamp fromDate:(Date today plusDays:1) andTime:(Time now) |
114 |
Timestamp now |
|
115 |
" |
|
116 |
! ! |
|
117 |
||
16954 | 118 |
!TZTimestamp methodsFor:'accessing'! |
119 |
||
16966 | 120 |
isLocalTimestamp |
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
121 |
"return true, if I am a local timestamp (i.e. with no TZ info)" |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
122 |
|
16966 | 123 |
^ false |
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
124 |
|
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
125 |
"Modified (comment): / 24-05-2018 / 17:29:26 / Claus Gittinger" |
16966 | 126 |
! |
127 |
||
128 |
timeInfo |
|
16984 | 129 |
"fake it. Convert to utc, ask OS for the info, then convert back. |
130 |
This is returns wrong info for weekday and we have to compensate again. |
|
131 |
Also, it (currently) only works for timestamps after the epoch" |
|
16966 | 132 |
|
133 |
|ti| |
|
134 |
||
135 |
"/ utcOffset negative: east of GMT |
|
18453 | 136 |
ti := OperatingSystem computeUTCTimeAndDateFrom:(osTime - ((utcOffset ? 0) * 1000)). |
137 |
ti utcOffset:(utcOffset ? 0). |
|
16966 | 138 |
^ ti |
139 |
! |
|
140 |
||
16954 | 141 |
utcOffset |
142 |
"return the difference between UTC (Greenwich Mean Time) and the local time in seconds. |
|
143 |
If daylight saving time applies to ourself, take that into account. |
|
144 |
||
145 |
Add utcOffset to convert from local time to UTC time. |
|
146 |
Subtract utcOffset to convert from UTC time to local time. |
|
147 |
||
148 |
If utcOffset is negative, the local timezone is east of Greenwich. |
|
149 |
If utcOffset is positive, the local timezone is west of Greenwich." |
|
150 |
||
18453 | 151 |
^ utcOffset ? 0 |
16954 | 152 |
! |
153 |
||
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
154 |
utcOffset:secondsOrTimeDuration |
16954 | 155 |
"set the difference between UTC (Greenwich Mean Time) and the local time in seconds. |
156 |
If daylight saving time applies to ourself, take that into account. |
|
157 |
||
158 |
Add utcOffset to convert from local time to UTC time. |
|
159 |
Subtract utcOffset to convert from UTC time to local time. |
|
160 |
||
161 |
If utcOffset is negative, the local timezone is east of Greenwich. |
|
162 |
If utcOffset is positive, the local timezone is west of Greenwich." |
|
163 |
||
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
164 |
utcOffset := secondsOrTimeDuration asInteger. |
17103 | 165 |
|
166 |
"Modified: / 20-11-2014 / 13:05:48 / sr" |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
167 |
"Modified (format): / 26-05-2019 / 12:09:21 / Claus Gittinger" |
16954 | 168 |
! ! |
169 |
||
16966 | 170 |
!TZTimestamp methodsFor:'converting'! |
171 |
||
172 |
asLocalTimestamp |
|
173 |
"return a local timestamp, representing the same time as the receiver" |
|
174 |
||
175 |
^ Timestamp fromOSTime:osTime. |
|
176 |
! |
|
177 |
||
178 |
asTZTimestamp:utcOffsetArg |
|
179 |
"return a timestamp in a given timezone, representing the same time as the receiver" |
|
180 |
||
181 |
utcOffset = utcOffsetArg ifTrue:[ |
|
182 |
^ self. |
|
183 |
]. |
|
184 |
^ super asTZTimestamp:utcOffsetArg |
|
16976 | 185 |
|
186 |
"what is the time now in NewYork? |
|
187 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'EST') |
|
188 |
||
189 |
what is the time now in Stuttgart? |
|
190 |
Timestamp now asTZTimestamp:(Timestamp utcOffsetFrom:'MEZ') |
|
191 |
" |
|
16966 | 192 |
! ! |
193 |
||
17019 | 194 |
!TZTimestamp methodsFor:'private'! |
195 |
||
196 |
speciesNew |
|
197 |
^ self species basicNew |
|
18450 | 198 |
utcOffset:(utcOffset ? 0); |
17019 | 199 |
yourself |
17050 | 200 |
! |
201 |
||
202 |
storeStringClass |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
203 |
"my storeString will ask Timestamp to read the instance" |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
204 |
|
17050 | 205 |
^ Timestamp |
17019 | 206 |
! ! |
207 |
||
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
208 |
!TZTimestamp methodsFor:'testing'! |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
209 |
|
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
210 |
isTZTimestamp |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
211 |
"return true, if I am a timestamp with TZ info" |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
212 |
|
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
213 |
^ true |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
214 |
|
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
215 |
"Created: / 24-05-2018 / 17:30:38 / Claus Gittinger" |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
216 |
! ! |
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
217 |
|
16954 | 218 |
!TZTimestamp class methodsFor:'documentation'! |
219 |
||
220 |
version |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
221 |
^ '$Header$' |
16954 | 222 |
! |
223 |
||
224 |
version_CVS |
|
25423
bcfde4da086a
Cherry-picked `AbstractTime.st`, `Time`, `TimeDuration`, `Timestamp` `TZTimestamp` and `UtcTimestamp`
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
225 |
^ '$Header$' |
16954 | 226 |
! ! |
227 |