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