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