author | Claus Gittinger <cg@exept.de> |
Tue, 15 Aug 2000 16:34:33 +0200 | |
changeset 5523 | 86ad12f4afc9 |
parent 5515 | 4adb4b3a7475 |
child 5548 | eead51e4752e |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 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 |
||
5515 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
54 | 15 |
Magnitude subclass:#Date |
972 | 16 |
instanceVariableNames:'dateEncoding' |
17 |
classVariableNames:'DayNames MonthNames DayAbbrevs MonthAbbrevs EnvironmentChange' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-General' |
|
1 | 20 |
! |
21 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
22 |
!Date class methodsFor:'documentation'! |
54 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
54 | 38 |
documentation |
39 |
" |
|
92 | 40 |
Instances of Date represent dates as year, month and day encoded in the |
41 |
(private & hidden) instance dateEncoding. The value found there is |
|
42 |
year*100*100 + month*100 + day (which makes magnitude-like comparison of |
|
43 |
dates easy, but is not guaranteed for future versions). |
|
1228 | 44 |
Do not depend on the internal representation. |
54 | 45 |
|
92 | 46 |
The old representation used days since 1st Jan. 1901 internally - |
47 |
with the new implementation, it is possible to reasonably represent almost |
|
48 |
any Date. (which insurance companies will like, since they can now |
|
49 |
represent even very old peoples birthday :-) |
|
68 | 50 |
Notice: no correction for pre-Julian dates is done. |
54 | 51 |
|
92 | 52 |
The printed representation of dates is controlled by resource definitions - |
53 |
thus national variants are already supported (see file 'resources/Date.rs'). |
|
54 |
||
55 |
Compatibility notice: |
|
1228 | 56 |
due to some historic reasons, there are some methods found twice |
57 |
with different names in this class. The old ST/X methods will vanish in |
|
58 |
one of the next releases, and kept for a while to support existing |
|
59 |
applications (the info on how these methods should be named came |
|
60 |
somewhat late from the testers ..). |
|
92 | 61 |
|
1228 | 62 |
Please do not use methods marked as obsolete in their comment. |
92 | 63 |
|
64 |
Most useful methods: |
|
65 |
||
1228 | 66 |
Date today |
67 |
(Date today) addDays: |
|
68 |
(Date today) subtractDays: |
|
1295 | 69 |
|
70 |
[author:] |
|
71 |
Claus Gittinger |
|
72 |
||
73 |
[see also:] |
|
74 |
Time AbstractTime |
|
75 |
Filename |
|
76 |
OperatingSystem |
|
54 | 77 |
" |
78 |
! ! |
|
79 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
80 |
!Date class methodsFor:'instance creation'! |
699 | 81 |
|
82 |
fromDays:dayCount |
|
83 |
"return a new Date, given the day-number starting with 0 at 1.Jan 1901; |
|
84 |
(i.e. 'Date fromDays:0' returns 1st Jan. 1901). |
|
85 |
Date asDays is the reverse operation. |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
86 |
Added for GNU/ST-80 compatibility" |
699 | 87 |
|
88 |
|yr rest d| |
|
89 |
||
90 |
"approx. year" |
|
91 |
yr := (dayCount // 366) + 1901. |
|
92 |
rest := dayCount - (self yearAsDays:yr) + 1. "+1 for ST-80 compatibility" |
|
93 |
d := self daysInYear:yr. |
|
94 |
(rest > d) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
95 |
"adjust" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
96 |
yr := yr + 1. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
97 |
rest := rest - d. |
699 | 98 |
]. |
99 |
||
1501 | 100 |
^ self newDay:rest year:yr |
699 | 101 |
|
102 |
" |
|
103 |
Date fromDays:0 -> 1 jan 1901 |
|
104 |
Date fromDays:365 -> 1 jan 1902 |
|
105 |
Date fromDays:730 -> 1 jan 1903 |
|
106 |
Date fromDays:1095 -> 1 jan 1903 |
|
107 |
Date fromDays:1460 ->31 dec 1904 since 1904 was a leap year |
|
108 |
" |
|
109 |
||
1501 | 110 |
"Modified: 1.7.1996 / 14:24:25 / cg" |
699 | 111 |
! |
112 |
||
113 |
newDay:day month:month year:year |
|
114 |
"return a new Date, given the day, month and year. |
|
115 |
For your convenience, month may be either an integer |
|
116 |
or the months name as a string. |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
117 |
|
699 | 118 |
Year may be the actual year (such as 1890, 2001) or the number |
119 |
of years since 1900 (which is rubbish ST-80 compatibility: |
|
120 |
it will be totally useless in a few years ...). |
|
121 |
You better not use this short-year feature in your programs." |
|
122 |
||
123 |
|monthIndex ok yr| |
|
124 |
||
125 |
yr := year. |
|
126 |
yr < 100 ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
127 |
yr := yr + 1900. |
699 | 128 |
]. |
129 |
||
130 |
month isInteger ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
131 |
monthIndex := month |
699 | 132 |
] ifFalse:[ |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
133 |
monthIndex := self indexOfMonth:month |
699 | 134 |
]. |
135 |
(monthIndex == 2 and:[day == 29]) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
136 |
ok := self leapYear:yr |
699 | 137 |
] ifFalse:[ |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
138 |
ok := day <= (self daysInMonth:month forYear:yr) |
699 | 139 |
]. |
140 |
((day > 0) and:[ok]) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
141 |
^ self basicNew dateEncoding:(((yr * 100) + monthIndex) * 100) + day |
699 | 142 |
]. |
143 |
||
144 |
"this error is triggered if you try to create a date from an |
|
145 |
invalid year/month/day combination; |
|
146 |
Such as 29-feb-year, where year is no leap year |
|
147 |
" |
|
148 |
self error:'invalid date' |
|
149 |
||
150 |
" |
|
151 |
Date newDay:8 month:'may' year:1993 |
|
152 |
Date newDay:8 month:5 year:1994 |
|
153 |
Date newDay:29 month:'feb' year:1994 |
|
154 |
Date newDay:29 month:'feb' year:1993 |
|
155 |
Date newDay:28 month:'feb' year:5 |
|
156 |
Date newDay:28 month:'feb' year:95 |
|
157 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
158 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
159 |
"Modified: 19.4.1996 / 15:28:15 / cg" |
699 | 160 |
! |
161 |
||
162 |
newDay:dayInYear year:year |
|
163 |
"return a new Date, given the year and the day-in-year (starting at 1). |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
164 |
See also: Date today / Time now / AbsoluteTime now. |
699 | 165 |
ST-80 compatibility" |
166 |
||
167 |
|monthAndDay| |
|
168 |
||
169 |
(dayInYear between:1 and:365) ifFalse:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
170 |
((dayInYear == 366) and:[self leapYear:year]) ifFalse:[ |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
171 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
172 |
this error is triggered, when you try to create a |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
173 |
day from an invalid day-in-year; |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
174 |
for example, 366 in a non-leap year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
175 |
I dont know, if ST-80 wraps to the next year(s) in this case. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
176 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
177 |
^ self error:'invalid date' |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
178 |
] |
699 | 179 |
]. |
180 |
monthAndDay := self monthAndDayFromDayInYear:dayInYear forYear:year. |
|
1501 | 181 |
^ self newDay:(monthAndDay at:2) month:(monthAndDay at:1) year:year |
699 | 182 |
|
183 |
" |
|
184 |
Date newDay:150 year:1994 |
|
185 |
Date newDay:1 year:1994 |
|
186 |
Date newDay:1 year:1901 |
|
187 |
Date newDay:1 year:1902 |
|
188 |
Date newDay:365 year:1992 |
|
189 |
Date newDay:366 year:1992 |
|
190 |
Date newDay:365 year:1994 |
|
191 |
Date newDay:366 year:1994 |
|
192 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
193 |
|
1501 | 194 |
"Modified: 1.7.1996 / 14:22:24 / cg" |
699 | 195 |
! |
196 |
||
197 |
readFrom:aStringOrStream onError:exceptionBlock |
|
198 |
"return a new Date, reading a printed representation from aStream. |
|
199 |
Notice, that this is not the storeString format and |
|
200 |
is different from the format expected by readFrom:. |
|
201 |
BUG: |
|
202 |
This method assumes american format (i.e. month-day-year) instead |
|
203 |
of the german/french and other day-month-year. |
|
4998 | 204 |
There ought to be a internationalized variant of this." |
699 | 205 |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
206 |
|newDate| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
207 |
|
972 | 208 |
ErrorSignal handle:[:ex | |
209 |
^ exceptionBlock value |
|
699 | 210 |
] do:[ |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
211 |
|str month day year| |
972 | 212 |
str := aStringOrStream readStream. |
699 | 213 |
|
972 | 214 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
215 |
(str peek isDigit) ifTrue:[ |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
216 |
day := Integer readFrom:str |
972 | 217 |
]. |
218 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
219 |
(str peek isLetter) ifTrue:[ |
|
220 |
month := str nextAlphaNumericWord. |
|
221 |
day isNil ifTrue:[ |
|
222 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
223 |
day := Integer readFrom:str. |
972 | 224 |
] |
225 |
] ifFalse:[ |
|
226 |
month := self nameOfMonth:day. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
227 |
day := Integer readFrom:str |
972 | 228 |
]. |
229 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
230 |
year := Integer readFrom:str. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
231 |
|
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
232 |
newDate := self newDay:day month:month year:year |
699 | 233 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
234 |
^ newDate |
699 | 235 |
|
236 |
" |
|
237 |
Date readFromString:'31 December 1992' |
|
4997 | 238 |
Date readFrom:'19:11:1999' |
239 |
Date readFromString:'December, 5 1992' |
|
240 |
Date readFromString:'3-jan-95' |
|
241 |
Date readFromString:'12/31/1992' |
|
242 |
Date readFromString:'15.4.1992' -> german; leads to an error |
|
243 |
Date readFromString:'10.4.1992' -> german; leads to a wrong date |
|
244 |
Date readFromString:'10.4.1992' onError:['wrong date'] |
|
245 |
Date readFromString:'32.4.1992' onError:['wrong date'] |
|
246 |
Date readFromString:'fooBar' onError:['wrong date'] |
|
247 |
Date readFromString:'10.4' onError:['wrong date'] |
|
248 |
Date readFromString:'10041999' onError:['wrong date'] |
|
249 |
" |
|
250 |
||
251 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
252 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
253 |
! |
|
254 |
||
255 |
readFrom:aStringOrStream printFormat:aFormatArray |
|
256 |
"return a new Date, reading a printed representation from aStream. |
|
257 |
1 day position (1, 2 or 3) |
|
258 |
2 month position (1..3) |
|
259 |
3 year position (1..3) |
|
260 |
" |
|
261 |
^ self readFrom:aStringOrStream |
|
262 |
printFormat:aFormatArray |
|
263 |
onError:[self error:'expected: ' , self name] |
|
264 |
||
265 |
" |
|
266 |
Date readFrom:'19:11:1999' printFormat:#( 1 2 3 ) |
|
267 |
Date readFrom:'19-nov-1999' printFormat:#( 1 2 3 ) |
|
268 |
Date readFrom:'19:11:1999' printFormat:#( 2 1 3 ) -> exception: wrong month |
|
269 |
Date readFrom:'5:12:1999' printFormat:#( 2 1 3 ) |
|
270 |
Date readFrom:'may-12-1999' printFormat:#( 2 1 3 ) |
|
271 |
Date readFrom:'1999 may 12' printFormat:#( 3 2 1 ) |
|
272 |
" |
|
273 |
||
274 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
275 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
276 |
! |
|
277 |
||
278 |
readFrom:aStringOrStream printFormat:aFormatArray onError:exceptionBlock |
|
279 |
"return a new Date, reading a printed representation from aStream. |
|
280 |
1 day position (1, 2 or 3) |
|
281 |
2 month position (1..3) |
|
282 |
3 year position (1..3) |
|
283 |
" |
|
284 |
|newDate| |
|
285 |
||
286 |
ErrorSignal handle:[:ex | |
|
287 |
^ exceptionBlock value |
|
288 |
] do:[ |
|
289 |
|str arg| |
|
290 |
||
291 |
str := aStringOrStream readStream. |
|
292 |
arg := Array new:3. |
|
293 |
||
294 |
1 to:3 do:[:i||v| |
|
295 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
296 |
||
297 |
v := (str peek isDigit) ifTrue:[Integer readFrom:str] |
|
298 |
ifFalse:[str nextAlphaNumericWord]. |
|
299 |
arg at:i put:v |
|
300 |
]. |
|
301 |
newDate := self newDay:(arg at:(aFormatArray at:1)) |
|
302 |
month:(arg at:(aFormatArray at:2)) |
|
303 |
year:(arg at:(aFormatArray at:3)) |
|
304 |
]. |
|
305 |
^ newDate |
|
306 |
||
307 |
" |
|
308 |
Date readFromString:'31 December 1992' |
|
309 |
Date readFrom:'19:11:1999' |
|
699 | 310 |
Date readFromString:'December, 5 1992' |
311 |
Date readFromString:'3-jan-95' |
|
312 |
Date readFromString:'12/31/1992' |
|
313 |
Date readFromString:'15.4.1992' -> german; leads to an error |
|
314 |
Date readFromString:'10.4.1992' -> german; leads to a wrong date |
|
315 |
Date readFromString:'10.4.1992' onError:['wrong date'] |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
316 |
Date readFromString:'32.4.1992' onError:['wrong date'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
317 |
Date readFromString:'fooBar' onError:['wrong date'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
318 |
Date readFromString:'10.4' onError:['wrong date'] |
4421 | 319 |
Date readFromString:'10041999' onError:['wrong date'] |
320 |
" |
|
321 |
||
322 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
323 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
324 |
! |
|
325 |
||
326 |
readMMDDYYYYFrom:aStringOrStream onError:exceptionBlock |
|
327 |
"return a new Date, reading a printed representation from aStream. |
|
328 |
Notice, that this is not the storeString format and |
|
4998 | 329 |
is different from the format expected by readFrom:" |
4421 | 330 |
|
331 |
|newDate| |
|
332 |
||
333 |
ErrorSignal handle:[:ex | |
|
334 |
^ exceptionBlock value |
|
335 |
] do:[ |
|
336 |
|str month day year| |
|
337 |
||
338 |
str := aStringOrStream readStream. |
|
339 |
month := str next:2. |
|
340 |
month := Integer readFrom:month. |
|
341 |
day := str next:2. |
|
342 |
day := Integer readFrom:day. |
|
343 |
year := str next:4. |
|
344 |
year := Integer readFrom:year. |
|
345 |
||
346 |
newDate := self newDay:day month:month year:year |
|
347 |
]. |
|
348 |
^ newDate |
|
349 |
||
350 |
" |
|
351 |
Date readMMDDYYYYFrom:'10041999' onError:['wrong date'] |
|
699 | 352 |
" |
353 |
||
354 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
355 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
699 | 356 |
! |
357 |
||
358 |
today |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
359 |
"return a date, representing today. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
360 |
See also: Time now / AbsoluteTime now." |
699 | 361 |
|
1501 | 362 |
^ self fromOSTime:(OperatingSystem getOSTime) |
699 | 363 |
|
364 |
" |
|
365 |
Date today |
|
366 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
367 |
|
1501 | 368 |
"Modified: 1.7.1996 / 15:20:16 / cg" |
699 | 369 |
! ! |
370 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
371 |
!Date class methodsFor:'general queries'! |
699 | 372 |
|
373 |
abbreviatedNameOfDay:dayIndex |
|
374 |
"given a day index (1..7), return the abbreviated name |
|
375 |
of the day" |
|
376 |
||
377 |
EnvironmentChange ifTrue:[ |
|
378 |
self initNames |
|
379 |
]. |
|
380 |
^ DayAbbrevs at:dayIndex |
|
381 |
||
382 |
" |
|
383 |
Date abbreviatedNameOfDay:4 |
|
384 |
" |
|
385 |
! |
|
386 |
||
387 |
abbreviatedNameOfMonth:monthIndex |
|
388 |
"given a month index (1..12), return the abbreviated name |
|
389 |
of the month" |
|
390 |
||
391 |
EnvironmentChange ifTrue:[ |
|
392 |
self initNames |
|
393 |
]. |
|
394 |
^ MonthAbbrevs at:monthIndex |
|
395 |
||
396 |
" |
|
397 |
Date abbreviatedNameOfMonth:11 |
|
398 |
Date abbreviatedNameOfMonth:12 |
|
399 |
" |
|
400 |
! |
|
401 |
||
402 |
dateAndTimeNow |
|
403 |
"return an array containing the date and time of now" |
|
404 |
||
405 |
^ Time dateAndTimeNow |
|
406 |
||
407 |
" |
|
408 |
Date dateAndTimeNow |
|
409 |
" |
|
410 |
! |
|
411 |
||
412 |
dayOfWeek:dayName |
|
413 |
"given the name of a day (either string or symbol), |
|
414 |
return the day-index (1 for monday; 7 for sunday). |
|
415 |
Return 0 for invalid day name" |
|
416 |
||
417 |
EnvironmentChange ifTrue:[ |
|
418 |
self initNames |
|
419 |
]. |
|
420 |
^ DayNames indexOf:dayName |
|
421 |
||
422 |
" |
|
423 |
Date dayOfWeek:'wednesday' |
|
424 |
" |
|
425 |
! |
|
426 |
||
427 |
daysInMonth:month forYear:yearInteger |
|
428 |
"given the name of a month and a year, return the number |
|
429 |
of days this month has (modified GNU). |
|
430 |
return 0 if the month name was invalid. |
|
431 |
For your convenience, month maybe an integer or name-string." |
|
432 |
||
433 |
|monthIndex "{ Class: SmallInteger }"| |
|
434 |
||
435 |
month isInteger ifTrue:[ |
|
436 |
monthIndex := month |
|
437 |
] ifFalse:[ |
|
438 |
monthIndex := self indexOfMonth:month |
|
439 |
]. |
|
440 |
||
441 |
^ self daysInMonthIndex:monthIndex forYear:yearInteger |
|
442 |
||
443 |
" |
|
444 |
Date daysInMonth:2 forYear:1980 |
|
445 |
Date daysInMonth:2 forYear:1981 |
|
446 |
Date daysInMonth:'feb' forYear:1981 |
|
447 |
" |
|
448 |
! |
|
449 |
||
450 |
daysInYear:yearInteger |
|
451 |
"return the number of days in a year" |
|
452 |
||
453 |
(self leapYear:yearInteger) ifTrue:[^ 366]. |
|
454 |
^ 365 |
|
455 |
||
456 |
" |
|
457 |
Date daysInYear:1900 |
|
458 |
Date daysInYear:1901 |
|
459 |
Date daysInYear:1904 |
|
460 |
Date daysInYear:1980 |
|
461 |
Date daysInYear:1981 |
|
462 |
" |
|
463 |
! |
|
464 |
||
465 |
daysUntilMonth:month forYear:yearInteger |
|
466 |
"given the name of a month and a year, return the number |
|
467 |
of days from 1st january to last of prev month of that year. |
|
468 |
Return 0 if the month name/index is invalid or is january. |
|
469 |
For your convenience, month maybe an integer or name-string." |
|
470 |
||
471 |
|monthIndex "{ Class: SmallInteger }" |
|
472 |
sumDays "{ Class: SmallInteger }" | |
|
473 |
||
474 |
month isInteger ifTrue:[ |
|
475 |
monthIndex := month |
|
476 |
] ifFalse:[ |
|
477 |
monthIndex := self indexOfMonth:month |
|
478 |
]. |
|
479 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
480 |
||
481 |
sumDays := 0. |
|
482 |
1 to:monthIndex-1 do:[:m | |
|
483 |
sumDays := sumDays + (self daysInMonthIndex:m forYear:yearInteger) |
|
484 |
]. |
|
485 |
^ sumDays |
|
486 |
||
487 |
" |
|
488 |
Date daysUntilMonth:'feb' forYear:1993 |
|
489 |
Date daysUntilMonth:'jan' forYear:1993 |
|
490 |
" |
|
491 |
! |
|
492 |
||
493 |
indexOfMonth:aMonthString |
|
494 |
"given the name of a month (either string or symbol), |
|
495 |
return the month-index (1 for jan; 12 for december). |
|
496 |
The given string may be a full or abbreviated name, |
|
497 |
case is ignored. |
|
498 |
Return 0 for invalid month name." |
|
499 |
||
500 |
|idx name| |
|
501 |
||
502 |
EnvironmentChange ifTrue:[ |
|
503 |
self initNames |
|
504 |
]. |
|
505 |
name := aMonthString asLowercase. |
|
506 |
idx := MonthAbbrevs indexOf:name. |
|
507 |
idx ~~ 0 ifTrue:[^ idx]. |
|
508 |
idx := MonthNames indexOf:name. |
|
509 |
idx ~~ 0 ifTrue:[^ idx]. |
|
510 |
||
511 |
name at:1 put:(name at:1) asUppercase. |
|
512 |
idx := MonthAbbrevs indexOf:name. |
|
513 |
idx ~~ 0 ifTrue:[^ idx]. |
|
514 |
idx := MonthNames indexOf:name. |
|
515 |
idx ~~ 0 ifTrue:[^ idx]. |
|
516 |
||
517 |
^ idx |
|
518 |
||
519 |
" |
|
520 |
Date indexOfMonth:'jan' |
|
521 |
Date indexOfMonth:'Jan' |
|
522 |
Date indexOfMonth:'December' |
|
523 |
" |
|
524 |
! |
|
525 |
||
526 |
isLeapYear:yearInteger |
|
527 |
"Return true, if a year is a leap year. |
|
528 |
Obsolete: |
|
529 |
Please use the ST-80 compatible #leapYear for new programs, |
|
530 |
since this method will vanish." |
|
531 |
||
532 |
^ self leapYear:yearInteger |
|
533 |
! |
|
534 |
||
535 |
leapYear:yearInteger |
|
536 |
"return true, if yearInteger is a leap year." |
|
537 |
||
538 |
|y "{ Class: SmallInteger }"| |
|
539 |
||
540 |
y := yearInteger. |
|
541 |
(y \\ 4 == 0) ifTrue:[ |
|
542 |
(y \\ 100 ~~ 0) ifTrue:[^ true]. |
|
543 |
(y \\ 400 == 0) ifTrue:[^ true] |
|
544 |
]. |
|
545 |
^ false |
|
546 |
||
547 |
" |
|
548 |
Date leapYear:1992 |
|
549 |
Date leapYear:1994 |
|
550 |
Date leapYear:1900 |
|
551 |
Date leapYear:2000 |
|
552 |
" |
|
553 |
! |
|
554 |
||
555 |
monthAndDayFromDayInYear:aDayInYear forYear:yearInteger |
|
556 |
"given a day-in-year (1..365) return an Array containing the |
|
557 |
month index and the day-in-month. Return nil if the argument is invalid." |
|
558 |
||
559 |
|restDays daysInMonth| |
|
560 |
||
561 |
restDays := aDayInYear. |
|
562 |
restDays < 1 ifTrue:[^ nil]. |
|
563 |
||
564 |
1 to:12 do:[:m | |
|
565 |
daysInMonth := self daysInMonthIndex:m forYear:yearInteger. |
|
566 |
restDays <= daysInMonth ifTrue:[ |
|
567 |
^ Array with:m with:restDays |
|
568 |
]. |
|
569 |
restDays := restDays - daysInMonth |
|
570 |
]. |
|
571 |
restDays > daysInMonth ifTrue:[^ nil]. |
|
572 |
^ Array with:12 with:restDays |
|
573 |
||
574 |
" |
|
575 |
Date monthAndDayFromDayInYear:66 forYear:1980 |
|
576 |
Date monthAndDayFromDayInYear:66 forYear:1981 |
|
577 |
" |
|
578 |
! |
|
579 |
||
580 |
nameOfDay:dayIndex |
|
581 |
"given a day index (1..7), return the name of the day" |
|
582 |
||
583 |
EnvironmentChange ifTrue:[ |
|
584 |
self initNames |
|
585 |
]. |
|
586 |
^ DayNames at:dayIndex |
|
587 |
||
588 |
" |
|
589 |
Date nameOfDay:4 |
|
590 |
" |
|
591 |
! |
|
592 |
||
593 |
nameOfMonth:monthIndex |
|
594 |
"given a month index (1..12), return the name of the month" |
|
595 |
||
596 |
EnvironmentChange ifTrue:[ |
|
597 |
self initNames |
|
598 |
]. |
|
599 |
^ MonthNames at:monthIndex |
|
600 |
||
601 |
" |
|
602 |
Date nameOfMonth:11 |
|
603 |
Date nameOfMonth:12 |
|
604 |
Date nameOfMonth:4 |
|
605 |
" |
|
606 |
! |
|
607 |
||
608 |
yearAsDays: yearInteger |
|
609 |
"Returns the number of days since Jan 1, 1901. (GNU) |
|
610 |
to the first Jan of the year, yearInteger. |
|
611 |
For 1901 this is zero, for 1902 its 365. |
|
612 |
Defined for years >= 1901" |
|
613 |
||
614 |
|y "{ Class: SmallInteger }"| |
|
615 |
||
616 |
y := yearInteger - 1900. |
|
617 |
y := y - 1. |
|
618 |
^ (y * 365) |
|
619 |
+ (y // 4) |
|
620 |
- (y // 100) |
|
621 |
+ ((y + 300) // 400) |
|
622 |
||
623 |
" |
|
624 |
Date yearAsDays:1901 |
|
625 |
Date yearAsDays:1902 |
|
626 |
Date yearAsDays:1903 |
|
627 |
Date yearAsDays:1904 |
|
628 |
Date yearAsDays:1905 |
|
629 |
Date yearAsDays:1994 |
|
630 |
(Date yearAsDays:2001) - (Date yearAsDays:2000) |
|
631 |
" |
|
632 |
! ! |
|
633 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
634 |
!Date class methodsFor:'handling language changes'! |
699 | 635 |
|
636 |
initialize |
|
637 |
"check for case where Resource-classes are absent" |
|
638 |
ResourcePack isNil ifTrue:[ |
|
639 |
self initNames |
|
640 |
] ifFalse:[ |
|
641 |
Smalltalk addDependent:self. |
|
642 |
EnvironmentChange := true |
|
643 |
] |
|
644 |
! |
|
645 |
||
1468 | 646 |
update:something with:aParameter from:changedObject |
699 | 647 |
((something == #Language) or:[something == #LanguageTerritory]) ifTrue:[ |
1468 | 648 |
"just remember change for next access" |
649 |
EnvironmentChange := true |
|
699 | 650 |
] |
1468 | 651 |
|
652 |
"Created: 15.6.1996 / 15:19:25 / cg" |
|
699 | 653 |
! ! |
654 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
655 |
!Date class methodsFor:'obsolete instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
656 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
657 |
day:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
658 |
"return a new Date, given the day, month and year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
659 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
660 |
use newDay:month:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
661 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
662 |
^ self newDay:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
663 |
! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
664 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
665 |
day:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
666 |
"return a new Date, given the year and the day-in-year (starting at 1). |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
667 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
668 |
use newDay:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
669 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
670 |
^ self newDay:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
671 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
672 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
673 |
!Date class methodsFor:'private'! |
1 | 674 |
|
699 | 675 |
daysInMonthIndex: monthIndex forYear: yearInteger |
676 |
"return the number of days in month monthIndex of |
|
677 |
year yearInteger (modified GNU). |
|
678 |
Return 0 for invalid month index. |
|
679 |
This is the internal version of daysInMonth:forYear:" |
|
680 |
||
681 |
|days| |
|
682 |
||
683 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
684 |
||
685 |
days := #(31 28 31 "Jan Feb Mar" |
|
686 |
30 31 30 "Apr May Jun" |
|
687 |
31 31 30 "Jul Aug Sep" |
|
688 |
31 30 31 "Oct Nov Dec" |
|
689 |
) at: monthIndex. |
|
690 |
||
691 |
(monthIndex == 2) ifTrue:[ |
|
692 |
(self leapYear:yearInteger) ifTrue:[ |
|
693 |
^ days + 1 |
|
694 |
] |
|
695 |
]. |
|
696 |
^ days |
|
697 |
||
698 |
" |
|
699 |
Date daysInMonthIndex:2 forYear:1994 |
|
700 |
Date daysInMonthIndex:2 forYear:1980 |
|
701 |
Date daysInMonthIndex:2 forYear:1981 |
|
702 |
" |
|
703 |
! |
|
704 |
||
1 | 705 |
initNames |
706 |
"read the language specific names" |
|
707 |
||
10 | 708 |
|resources| |
709 |
||
54 | 710 |
DayNames := #('monday' |
155 | 711 |
'tuesday' |
712 |
'wednesday' |
|
713 |
'thursday' |
|
714 |
'friday' |
|
715 |
'saturday' |
|
716 |
'sunday'). |
|
1 | 717 |
|
54 | 718 |
DayAbbrevs := #('mon' |
155 | 719 |
'tue' |
720 |
'wed' |
|
721 |
'thu' |
|
722 |
'fri' |
|
723 |
'sat' |
|
724 |
'sun'). |
|
1 | 725 |
|
54 | 726 |
MonthNames := #('january' |
155 | 727 |
'february' |
728 |
'march' |
|
729 |
'april' |
|
730 |
'may' |
|
731 |
'june' |
|
732 |
'july' |
|
733 |
'august' |
|
734 |
'september' |
|
735 |
'october' |
|
736 |
'november' |
|
737 |
'december'). |
|
1 | 738 |
|
54 | 739 |
MonthAbbrevs := #('jan' |
155 | 740 |
'feb' |
741 |
'mar' |
|
742 |
'apr' |
|
743 |
'may' |
|
744 |
'jun' |
|
745 |
'jul' |
|
746 |
'aug' |
|
747 |
'sep' |
|
748 |
'oct' |
|
749 |
'nov' |
|
750 |
'dec'). |
|
54 | 751 |
|
752 |
"check for case where Resource-classes are absent" |
|
753 |
ResourcePack notNil ifTrue:[ |
|
155 | 754 |
resources := ResourcePack for:self. |
54 | 755 |
|
155 | 756 |
DayNames := resources array:DayNames. |
757 |
DayAbbrevs := resources array:DayAbbrevs. |
|
758 |
MonthNames := resources array:MonthNames. |
|
759 |
MonthAbbrevs := resources array:MonthAbbrevs. |
|
54 | 760 |
]. |
1 | 761 |
|
29 | 762 |
EnvironmentChange := false |
10 | 763 |
|
764 |
"Date initNames" |
|
54 | 765 |
! ! |
766 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
767 |
!Date class methodsFor:'private encoding'! |
54 | 768 |
|
769 |
encodeYear:y month:m day:d |
|
92 | 770 |
"the internal encoding is stricktly private, |
771 |
and should not be used outside." |
|
772 |
||
54 | 773 |
^ (((y * 100) + m) * 100) + d |
774 |
! ! |
|
775 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
776 |
!Date class methodsFor:'private instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
777 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
778 |
fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
779 |
"return a date, representing the date given by the operatingSystem time. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
780 |
This somewhat clumsy implementation hides the OS's date representation |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
781 |
(i.e. makes this class independent of what the OS starts its time values with). |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
782 |
Dont use this method, the osTime representation is totally unportable." |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
783 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
784 |
^ self basicNew fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
785 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
786 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
787 |
Date fromOSTime:#(0 0) -> on UNIX: this should return 1st Jan 1970 |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
788 |
thats where Unix time starts |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
789 |
On other systems, it may be something different. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
790 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
791 |
Date fromOSTime:#(86400 0) -> on UNIX: the day after |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
792 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
793 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
794 |
|
5515 | 795 |
!Date methodsFor:'Compatibility - ST80'! |
796 |
||
797 |
shortPrintString |
|
798 |
"dummy - for now" |
|
799 |
||
800 |
^ self printString |
|
801 |
||
802 |
"Created: 20.6.1997 / 17:16:48 / cg" |
|
803 |
! ! |
|
804 |
||
5186 | 805 |
!Date methodsFor:'Compatibility - Squeak'! |
4407 | 806 |
|
807 |
mmddyyyy |
|
4462 | 808 |
"return a printed representation of the receiver in the |
809 |
form mmddyyyy. |
|
810 |
The receiver can be reconstructed with: |
|
811 |
Date readMMDDYYYYFrom:aStringOrStream onError:[...]" |
|
812 |
||
4407 | 813 |
^ (self month printStringLeftPaddedTo:2 with:$0) |
814 |
, (self day printStringLeftPaddedTo:2 with:$0) |
|
815 |
, (self year printStringLeftPaddedTo:4 with:$0) |
|
816 |
||
817 |
" |
|
818 |
Date today mmddyyyy |
|
819 |
" |
|
820 |
! ! |
|
821 |
||
699 | 822 |
!Date methodsFor:'accessing'! |
241 | 823 |
|
699 | 824 |
abbreviatedDayName |
825 |
"return the short week-day of the receiver as a string. |
|
826 |
The returned string depends on the language setting. |
|
827 |
Expect things like 'mon', 'tue' ..." |
|
54 | 828 |
|
699 | 829 |
^ self class abbreviatedNameOfDay:(self dayInWeek) |
92 | 830 |
|
831 |
" |
|
699 | 832 |
Date today abbreviatedDayName |
833 |
(Date day:15 month:4 year:1959) abbreviatedDayName |
|
92 | 834 |
" |
835 |
! |
|
836 |
||
699 | 837 |
abbreviatedMonthName |
838 |
"return the month of the receiver as a string. |
|
839 |
The returned string depends on the language setting. |
|
840 |
Expect things like 'jan', 'feb' ..." |
|
54 | 841 |
|
699 | 842 |
^ self class abbreviatedNameOfMonth:(self month) |
68 | 843 |
|
92 | 844 |
" |
699 | 845 |
Date today abbreviatedMonthName |
846 |
(Date day:15 month:4 year:1959) abbreviatedMonthName |
|
92 | 847 |
" |
848 |
! |
|
849 |
||
850 |
asDays |
|
851 |
"return the number of days elapsed since 01-Jan-1901 |
|
852 |
and the receiver's day; starts with 0 for 1-1-1901. |
|
853 |
Date>>fromDays: is the reverse operation. |
|
854 |
For ST-80 compatibility." |
|
68 | 855 |
|
856 |
|yr| |
|
857 |
||
858 |
yr := self year. |
|
859 |
^ (self class yearAsDays:yr) |
|
860 |
+ (self class daysUntilMonth:self month forYear:yr) |
|
861 |
+ self day |
|
85 | 862 |
- 1 |
68 | 863 |
|
92 | 864 |
" |
865 |
(Date day: 5 month: 8 year: 1962) asDays -> should be 22496 |
|
866 |
(Date day: 1 month: 1 year: 1901) asDays -> 0 |
|
867 |
Date today asDays |
|
868 |
Date fromDays:(Date today asDays + 7) |
|
869 |
" |
|
77 | 870 |
! |
871 |
||
872 |
asSeconds |
|
92 | 873 |
"return the seconds between 1.jan.1901 and the same time in the receivers |
874 |
day. (i.e. midnight to midnight). |
|
1501 | 875 |
This does not include any leapSeconds ... strictly speaking, this is incorrect. |
77 | 876 |
ST-80 compatibility." |
877 |
||
92 | 878 |
^ 60*60*24 * self asDays |
77 | 879 |
|
92 | 880 |
" |
881 |
(Date day: 5 month: 8 year: 1962) asSeconds |
|
882 |
(Date day: 1 month: 1 year: 1901) asSeconds |
|
883 |
(Date today addDays:7) asSeconds - Date today asSeconds |
|
1501 | 884 |
" |
885 |
||
886 |
"Modified: 1.7.1996 / 14:19:17 / cg" |
|
699 | 887 |
! |
888 |
||
889 |
day |
|
890 |
"return the day (1..31) of the receiver" |
|
891 |
||
892 |
^ dateEncoding \\ 100 |
|
893 |
||
894 |
" |
|
895 |
Date today day |
|
896 |
" |
|
897 |
! |
|
898 |
||
899 |
dayCount |
|
900 |
"return the number of days since 1st. Jan. 1901; |
|
901 |
starting with 0 for this date. |
|
902 |
Date>>fromDays: is the reverse operation. |
|
903 |
Obsolete: |
|
904 |
please use asDays for ST-80 compatibility" |
|
905 |
||
906 |
^ self asDays. |
|
907 |
||
908 |
" |
|
909 |
(Date day:1 month:1 year:1901) dayCount |
|
910 |
Date fromDays:(Date day:1 month:1 year:1994) dayCount |
|
911 |
Date today dayCount |
|
912 |
" |
|
913 |
! |
|
914 |
||
915 |
dayInWeek |
|
916 |
"return the week-day of the receiver - 1 for monday, 7 for sunday" |
|
917 |
||
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
918 |
|wday| |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
919 |
|
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
920 |
wday := (1 "know, that 1st Jan 1901 was a tuesday" |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
921 |
+ self asDays) \\ 7 + 1. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
922 |
wday < 1 ifTrue:[ |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
923 |
wday := wday + 7 |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
924 |
]. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
925 |
^ wday |
699 | 926 |
|
927 |
" |
|
1501 | 928 |
Date today dayInWeek |
3257 | 929 |
Date today weekday |
1501 | 930 |
(Date day:15 month:4 year:1959) dayInWeek |
931 |
(Date day:1 month:1 year:1901) dayInWeek |
|
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
932 |
(Date day:31 month:12 year:1899) dayInWeek |
699 | 933 |
" |
1501 | 934 |
|
3257 | 935 |
"Modified: / 1.2.1998 / 14:10:27 / cg" |
699 | 936 |
! |
937 |
||
938 |
dayName |
|
939 |
"return the week-day of the receiver as a string. |
|
940 |
The returned string depends on the language setting. |
|
941 |
Expect things like 'monday', 'tuesday' ... |
|
942 |
Obsolete: |
|
943 |
use #weekday for ST-80 compatibility" |
|
944 |
||
945 |
^ self weekday |
|
946 |
! |
|
77 | 947 |
|
948 |
dayOfMonth |
|
949 |
"Answer the day of the month represented by me. |
|
950 |
Same as day; for ST-80 compatibility." |
|
951 |
||
952 |
^ self day |
|
92 | 953 |
|
954 |
" |
|
955 |
Date today dayOfMonth |
|
956 |
" |
|
957 |
! |
|
958 |
||
959 |
daysInMonth |
|
960 |
"return the number of days in the month of the receiver" |
|
961 |
||
962 |
^ Date daysInMonth:(self month) forYear:(self year) |
|
963 |
||
964 |
" |
|
965 |
Date today daysInMonth |
|
966 |
" |
|
967 |
! |
|
968 |
||
969 |
daysInYear |
|
970 |
"return the number of days in the year of the receiver" |
|
971 |
||
972 |
^ Date daysInYear:(self year) |
|
973 |
||
974 |
" |
|
975 |
Date today daysInYear |
|
976 |
" |
|
977 |
! |
|
978 |
||
979 |
daysLeftInYear |
|
980 |
"return the number of days left in the year of the receiver" |
|
981 |
||
5282 | 982 |
^ (Date daysInYear:(self year)) - self day |
92 | 983 |
|
984 |
" |
|
5282 | 985 |
Date today daysLeftInYear |
92 | 986 |
" |
77 | 987 |
! |
988 |
||
699 | 989 |
leap |
990 |
"return true, if the receivers year is a leap year" |
|
68 | 991 |
|
699 | 992 |
^ Date leapYear:(self year) |
68 | 993 |
|
92 | 994 |
" |
699 | 995 |
Date today leap |
996 |
(Date day:1 month:1 year:1992) leap |
|
92 | 997 |
" |
68 | 998 |
! |
999 |
||
699 | 1000 |
month |
1001 |
"return the month (1..12) of the receiver" |
|
92 | 1002 |
|
699 | 1003 |
^ (dateEncoding // 100) \\ 100 |
68 | 1004 |
|
92 | 1005 |
" |
699 | 1006 |
Date today month |
92 | 1007 |
" |
68 | 1008 |
! |
1009 |
||
77 | 1010 |
monthIndex |
1011 |
"return the index of the month (e.g. Feb.=2). |
|
1012 |
Same as month; for ST-80 compatibility." |
|
1013 |
||
1014 |
^ self month |
|
1015 |
! |
|
1016 |
||
68 | 1017 |
monthName |
1018 |
"return the month of the receiver as a string. |
|
1019 |
The returned string depends on the language setting. |
|
1020 |
Expect things like 'january', 'february' ..." |
|
1021 |
||
1022 |
^ self class nameOfMonth:(self month) |
|
1023 |
||
92 | 1024 |
" |
1025 |
Date today monthName |
|
1026 |
(Date day:15 month:4 year:1959) monthName |
|
1027 |
" |
|
68 | 1028 |
! |
1029 |
||
699 | 1030 |
weekday |
1031 |
"return the week-day of the receiver as a string. |
|
68 | 1032 |
The returned string depends on the language setting. |
699 | 1033 |
Expect things like 'monday', 'tuesday' ... |
1034 |
For ST-80 compatibility" |
|
1035 |
||
1036 |
^ self class nameOfDay:(self dayInWeek) |
|
1037 |
||
1038 |
" |
|
1039 |
Date today weekday |
|
1040 |
(Date day:15 month:4 year:1959) weekday |
|
1041 |
" |
|
1042 |
! |
|
1043 |
||
1044 |
year |
|
1045 |
"return the year (1..12) of the receiver" |
|
1046 |
||
1047 |
^ dateEncoding // (100*100) |
|
68 | 1048 |
|
699 | 1049 |
" |
1050 |
Date today year |
|
1051 |
" |
|
1052 |
! ! |
|
1053 |
||
1054 |
!Date methodsFor:'arithmetic'! |
|
1055 |
||
1056 |
addDays:days |
|
1057 |
"return a new date representing 'days' after the receiver. |
|
1058 |
The argument should be some kind of integer. |
|
1059 |
For ST-80 compatibility." |
|
1060 |
||
1061 |
^ self class fromDays:(self asDays + days) |
|
1062 |
||
1063 |
" |
|
1064 |
Date today addDays:7 |
|
1065 |
" |
|
1066 |
! |
|
1067 |
||
1068 |
daysUntil:aDate |
|
1069 |
"return the number of days between the receiver and the argument, |
|
1070 |
aDate, whuch should be some kind of date" |
|
1071 |
||
1072 |
^ aDate asDays - self asDays |
|
68 | 1073 |
|
92 | 1074 |
" |
699 | 1075 |
(Date day:24 month:12 year:1994) daysUntil:(Date day:1 month:1 year:1995) |
1076 |
(Date day:1 month:2 year:1992) daysUntil:(Date day:1 month:3 year:1992) |
|
1077 |
(Date day:1 month:2 year:1994) daysUntil:(Date day:1 month:3 year:1994) |
|
1078 |
||
1079 |
|delta| |
|
1080 |
delta := Date today |
|
1081 |
daysUntil:(Date day:25 month:12 year:Date today year). |
|
1082 |
Transcript show:'still '; |
|
1083 |
show:delta ; |
|
1422 | 1084 |
showCR:' days till xmas' |
699 | 1085 |
" |
1086 |
! |
|
1087 |
||
1088 |
minusDays:days |
|
1089 |
"return a new date representing 'days' before the receiver. |
|
1090 |
The argument should be some kind of integer. |
|
1091 |
Obsolete: |
|
1092 |
Please dont use this method since it will vanish. |
|
1093 |
Use #subtractDays: instead for ST-80 compatibility." |
|
1094 |
||
1095 |
^ self subtractDays:days |
|
1096 |
! |
|
1097 |
||
1098 |
plusDays:days |
|
1099 |
"return a new date representing 'days' after the receiver. |
|
1100 |
The argument should be some kind of integer. |
|
1101 |
Obsolete: |
|
1102 |
Please dont use this method since it will vanish. |
|
1103 |
Use #addDays: instead for ST-80 compatibility." |
|
1104 |
||
1105 |
^ self addDays:days |
|
1106 |
! |
|
1107 |
||
1108 |
subtractDate:aDate |
|
1109 |
"return the number of days between the receiver and aDate" |
|
1110 |
||
1111 |
^ self asDays - aDate asDays |
|
1112 |
||
1113 |
" |
|
1114 |
(Date day:1 month:1 year:1995) subtractDate:(Date day:24 month:12 year:1994) |
|
1115 |
(Date day:1 month:3 year:1992) subtractDate:(Date day:1 month:2 year:1992) |
|
1116 |
(Date day:1 month:3 year:1994) subtractDate:(Date day:1 month:2 year:1994) |
|
1117 |
" |
|
1118 |
! |
|
1119 |
||
1120 |
subtractDays:days |
|
1121 |
"return a new date representing 'days' before the receiver. |
|
1122 |
The argument should be some kind of integer. |
|
1123 |
For ST-80 compatibility" |
|
1124 |
||
1125 |
^ self class fromDays:(self asDays - days) |
|
1126 |
||
1127 |
" |
|
1128 |
Date today subtractDays:7 |
|
92 | 1129 |
" |
1 | 1130 |
! ! |
1131 |
||
1132 |
!Date methodsFor:'comparing'! |
|
1133 |
||
29 | 1134 |
< aDate |
1135 |
"return true, if the date represented by the receiver |
|
1136 |
is before the argument, aDate" |
|
1137 |
||
54 | 1138 |
(aDate isMemberOf:Date) ifTrue:[ |
155 | 1139 |
^ dateEncoding < aDate dateEncoding |
54 | 1140 |
]. |
29 | 1141 |
|
54 | 1142 |
"the argument must understand year, month and day to be |
1143 |
comparable, whatever it is" |
|
1144 |
||
1145 |
^ dateEncoding < (Date encodeYear:aDate year |
|
155 | 1146 |
month:aDate month |
1147 |
day:aDate day) |
|
68 | 1148 |
|
1149 |
"Date today < (Date day:24 month:12 year:2000)" |
|
1150 |
"Date today < (Date day:24 month:12 year:1900)" |
|
29 | 1151 |
! |
1 | 1152 |
|
1153 |
= aDate |
|
1154 |
"return true, if the date represented by the receiver |
|
1155 |
is the same as the one represented by argument, aDate" |
|
1156 |
||
155 | 1157 |
(aDate isMemberOf:Date) ifFalse:[^ false]. |
1158 |
||
1159 |
^ dateEncoding = aDate dateEncoding |
|
54 | 1160 |
|
1161 |
"the argument must understand year, month and day to be |
|
1162 |
comparable, whatever it is" |
|
1163 |
||
155 | 1164 |
" |
54 | 1165 |
^ dateEncoding = (Date encodeYear:aDate year |
155 | 1166 |
month:aDate month |
1167 |
day:aDate day) |
|
1168 |
" |
|
68 | 1169 |
"Date today = ((Date today plusDays:7) minusDays:7)" |
92 | 1170 |
! |
1171 |
||
699 | 1172 |
> aDate |
1173 |
"return true, if the date represented by the receiver |
|
1174 |
is after the argument, aDate" |
|
1175 |
||
1176 |
(aDate isMemberOf:Date) ifTrue:[ |
|
1177 |
^ dateEncoding > aDate dateEncoding |
|
1178 |
]. |
|
1179 |
||
1180 |
"the argument must understand year, month and day to be |
|
1181 |
comparable, whatever it is" |
|
1182 |
||
1183 |
^ dateEncoding > (Date encodeYear:aDate year |
|
1184 |
month:aDate month |
|
1185 |
day:aDate day) |
|
1186 |
||
1187 |
"Date today > (Date day:24 month:12 year:2000)" |
|
1188 |
"Date today > (Date day:24 month:12 year:1900)" |
|
1189 |
! |
|
1190 |
||
92 | 1191 |
hash |
1192 |
"return an integer useful for hashing on dates" |
|
1193 |
||
1194 |
^ dateEncoding |
|
1 | 1195 |
! ! |
1196 |
||
1501 | 1197 |
!Date methodsFor:'converting'! |
1198 |
||
1199 |
asAbsoluteTime |
|
1200 |
"return an absoluteTime instance, representing midnight of |
|
1201 |
last night" |
|
1202 |
||
4379 | 1203 |
^ AbsoluteTime year:(self year) month:(self month) day:(self day) |
1204 |
hour:0 minute:0 second:0 millisecond:0 |
|
1501 | 1205 |
|
1206 |
" |
|
1207 |
Date today asAbsoluteTime |
|
1208 |
" |
|
1209 |
||
4379 | 1210 |
"Modified: / 1.7.1996 / 14:53:32 / cg" |
1211 |
"Modified: / 13.7.1999 / 12:31:47 / stefan" |
|
4408 | 1212 |
! |
1213 |
||
1214 |
asDate |
|
1215 |
"return the receiver" |
|
1216 |
||
1217 |
^ self |
|
1501 | 1218 |
! ! |
1219 |
||
54 | 1220 |
!Date methodsFor:'printing & storing'! |
1 | 1221 |
|
408 | 1222 |
printFormat:aFormatArray |
1223 |
"return a string containing a printed representation of the receiver. |
|
1224 |
The formatArray argument consists of 6 or 7 integers which control |
|
1225 |
the resulting string. The entries are: |
|
1226 |
1 day position (1, 2 or 3) |
|
1227 |
2 month position (1..3) |
|
1228 |
3 year position (1..3) |
|
1229 |
4 asciiValue of separator character or separator character |
|
4413 | 1230 |
or collection of 2 separator characters |
408 | 1231 |
5 month format (1: numeric; 2: abbreviated name; 3: fullname |
4413 | 1232 |
4: abbreviated uppercase 5: full uppercase) |
408 | 1233 |
6 year format (1 include century; 2 exclude century) |
4413 | 1234 |
7 (optional) true/false. |
1235 |
if true, print numbers in 2-digit format |
|
1236 |
(i.e. with leading zeros); |
|
1237 |
Taken as false, if ommited |
|
408 | 1238 |
|
1239 |
Day and monthnames are in the currently active language. |
|
1240 |
||
1241 |
This method supports more options than the ST-80 version; month formats |
|
1242 |
4 and 5, non-numeric separators and the optional 7th parameter are not |
|
1243 |
supported by ST-80. Be aware of this for compatibility reasons. |
|
1244 |
||
4413 | 1245 |
Notice that not all formats can be scanned (correctly) by #readFrom: |
1246 |
This is an ST-80 compatibility method - I would have choosen another |
|
1247 |
(less cryptic) formatString ... |
|
1248 |
" |
|
408 | 1249 |
|
1250 |
|mf upperCase day month year components sep1 sep2 leadingZeros| |
|
1251 |
||
1252 |
aFormatArray size > 6 ifTrue:[ |
|
4413 | 1253 |
leadingZeros := aFormatArray at:7 |
408 | 1254 |
] ifFalse:[ |
4413 | 1255 |
leadingZeros := false. |
408 | 1256 |
]. |
1257 |
||
1258 |
components := Array new:3. |
|
1259 |
day := self day. |
|
1260 |
leadingZeros ifTrue:[ |
|
4413 | 1261 |
day := day printStringLeftPaddedTo:2 with:$0 |
408 | 1262 |
] ifFalse:[ |
4413 | 1263 |
day := day printString |
408 | 1264 |
]. |
1265 |
components at:(aFormatArray at:1) put:day. |
|
1266 |
||
1267 |
upperCase := false. |
|
1268 |
mf := aFormatArray at:5. |
|
1269 |
(mf between:4 and:5) ifTrue:[mf := mf - 2. upperCase := true]. |
|
1270 |
||
1271 |
mf == 1 ifTrue:[ |
|
4413 | 1272 |
month := self month. |
1273 |
leadingZeros ifTrue:[ |
|
1274 |
month := month printStringLeftPaddedTo:2 with:$0 |
|
1275 |
] ifFalse:[ |
|
1276 |
month := month printString |
|
1277 |
] |
|
408 | 1278 |
] ifFalse:[ |
4413 | 1279 |
mf == 2 ifTrue:[ |
1280 |
month := self abbreviatedMonthName |
|
1281 |
] ifFalse:[ |
|
1282 |
month := self monthName |
|
1283 |
] |
|
408 | 1284 |
]. |
1285 |
upperCase ifTrue:[month := month asUppercase]. |
|
1286 |
components at:(aFormatArray at:2) put:month. |
|
1287 |
||
1288 |
year := self year. |
|
1289 |
(aFormatArray at:6) == 2 ifTrue:[ |
|
4413 | 1290 |
year := year \\ 100 |
408 | 1291 |
]. |
1292 |
components at:(aFormatArray at:3) put:year printString. |
|
1293 |
||
1294 |
sep1 := (aFormatArray at:4). |
|
1295 |
sep1 isCollection ifTrue:[ |
|
4413 | 1296 |
sep2 := (sep1 at:2). |
1297 |
sep1 := (sep1 at:1). |
|
408 | 1298 |
] ifFalse:[ |
4413 | 1299 |
sep1 := sep2 := sep1 asCharacter |
408 | 1300 |
]. |
1301 |
||
1302 |
^ (components at:1) |
|
1303 |
, sep1 asString |
|
1304 |
, (components at:2) |
|
1305 |
, sep2 asString |
|
1306 |
, (components at:3) |
|
1307 |
||
1308 |
" |
|
4413 | 1309 |
Date today printFormat:#(1 2 3 $- 1 2) |
1310 |
Date today printFormat:#(1 2 3 $. 1 2 true) |
|
1311 |
Date today printFormat:#(2 1 3 32 3 1) |
|
408 | 1312 |
Date today printFormat:#(2 1 3 #(' ' ', ') 3 1) |
1313 |
Date today printFormat:#(1 2 3 $- 2 1) |
|
1314 |
Date today printFormat:#(1 2 3 $- 4 1) |
|
1315 |
" |
|
1316 |
! |
|
1317 |
||
1318 |
printFormat:aFormatArray on:aStream |
|
1319 |
"append a printed representation of the receiver to aStream. |
|
1320 |
The argument, aFormatString controls the format, as described |
|
1321 |
in the #printFormat: method. |
|
1322 |
Notice that not all formats can be scanned (correctly) by #readFrom:" |
|
1323 |
||
1324 |
aStream nextPutAll:(self printFormat:aFormatArray) |
|
1325 |
||
1326 |
||
1327 |
"european formats: |
|
1328 |
||
1329 |
Date today printFormat:#(1 2 3 $- 1 2) on:Transcript. Transcript cr. |
|
1330 |
Date today printFormat:#(1 2 3 $- 2 2) on:Transcript. Transcript cr. |
|
1331 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1332 |
Date today printFormat:#(1 2 3 $- 3 2) on:Transcript. Transcript cr. |
|
1333 |
Date today printFormat:#(1 2 3 $. 1 2) on:Transcript. Transcript cr. |
|
1334 |
Date today printFormat:#(1 2 3 $. 1 2 true) on:Transcript. Transcript cr. |
|
1335 |
||
1336 |
US formats: |
|
1337 |
||
1338 |
Date today printFormat:#(2 1 3 32 3 1) on:Transcript. Transcript cr. |
|
1339 |
Date today printFormat:#(2 1 3 32 2 2) on:Transcript. Transcript cr. |
|
1340 |
Date today printFormat:#(2 1 3 $/ 1 2) on:Transcript. Transcript cr. |
|
1341 |
Date today printFormat:#(2 1 3 $- 2 1) on:Transcript. Transcript cr. |
|
1342 |
Date today printFormat:#(2 1 3 #(' ' ', ') 2 1) on:Transcript. Transcript cr. |
|
1343 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1344 |
Date today printFormat:#(1 2 3 $- 4 1) on:Transcript. Transcript cr. |
|
1345 |
" |
|
699 | 1346 |
! |
1347 |
||
1348 |
printOn:aStream |
|
1349 |
"append a printed representation of the receiver to aStream" |
|
1350 |
||
1351 |
self printFormat:#(1 2 3 $- 2 1) on:aStream |
|
1352 |
||
1353 |
" |
|
1354 |
Date today printOn:Transcript |
|
1355 |
Date today printNL |
|
1356 |
" |
|
1357 |
||
1358 |
"Modified: 27.8.1995 / 01:01:49 / claus" |
|
1359 |
! |
|
1360 |
||
1361 |
storeOn:aStream |
|
1362 |
"append a representation to aStream, from which the receiver |
|
1363 |
can be reconstructed" |
|
1364 |
||
1365 |
aStream nextPutAll:'('; nextPutAll:'Date day:'. |
|
1366 |
self day printOn:aStream. |
|
1367 |
aStream nextPutAll:' month:'. |
|
1368 |
self month printOn:aStream. |
|
1369 |
aStream nextPutAll:' year:'. |
|
1370 |
self year printOn:aStream. |
|
1371 |
aStream nextPutAll:')' |
|
1372 |
||
1373 |
" |
|
1374 |
Date today storeOn:Transcript |
|
1375 |
Date today storeString |
|
1376 |
" |
|
1 | 1377 |
! ! |
699 | 1378 |
|
1379 |
!Date methodsFor:'private accessing'! |
|
1380 |
||
1381 |
dateEncoding |
|
1382 |
"the internal encoding is stricktly private, |
|
1383 |
and should not be used outside." |
|
1384 |
||
1385 |
^ dateEncoding |
|
1386 |
! |
|
1387 |
||
1388 |
dateEncoding:anInteger |
|
1389 |
"the internal encoding is stricktly private, |
|
1390 |
and should not be used outside." |
|
1391 |
||
1392 |
dateEncoding := anInteger |
|
1393 |
! |
|
1394 |
||
1395 |
fromOSTime:osTime |
|
1396 |
"set my dateEncoding from an OS time. |
|
1397 |
This somewhat clumsy implementation hides the OS's date representation |
|
1398 |
(i.e. makes this class independent of what the OS starts its time values with). |
|
1399 |
Dont use this method, the osTime representation is totally unportable." |
|
1400 |
||
1401 |
OperatingSystem computeDatePartsOf:osTime |
|
1501 | 1402 |
for:[:year :month :day | |
1403 |
dateEncoding := (((year * 100) + month) * 100) + day |
|
699 | 1404 |
] |
1501 | 1405 |
|
1406 |
"Modified: 1.7.1996 / 15:23:12 / cg" |
|
699 | 1407 |
! ! |
1408 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
1409 |
!Date class methodsFor:'documentation'! |
699 | 1410 |
|
1411 |
version |
|
5515 | 1412 |
^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.52 2000-08-11 17:39:34 cg Exp $' |
699 | 1413 |
! ! |
1414 |
Date initialize! |