author | Claus Gittinger <cg@exept.de> |
Mon, 22 Feb 1999 21:40:34 +0100 | |
changeset 3995 | 777470826394 |
parent 3257 | db002a9bf071 |
child 4379 | 20ba95273826 |
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 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
20 |
!Date class methodsFor:'documentation'! |
54 | 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 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
78 |
!Date class methodsFor:'instance creation'! |
699 | 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 |
||
1501 | 98 |
^ self newDay:rest year:yr |
699 | 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 |
||
1501 | 108 |
"Modified: 1.7.1996 / 14:24:25 / 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. |
|
1501 | 179 |
^ self newDay:(monthAndDay at:2) month:(monthAndDay at:1) year:year |
699 | 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 |
|
1501 | 192 |
"Modified: 1.7.1996 / 14:22:24 / 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 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
204 |
|newDate| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
205 |
|
972 | 206 |
ErrorSignal handle:[:ex | |
207 |
^ exceptionBlock value |
|
699 | 208 |
] do:[ |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
209 |
|str month day year| |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
210 |
|
972 | 211 |
str := aStringOrStream readStream. |
699 | 212 |
|
972 | 213 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
214 |
(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
|
215 |
day := Integer readFrom:str |
972 | 216 |
]. |
217 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
218 |
(str peek isLetter) ifTrue:[ |
|
219 |
month := str nextAlphaNumericWord. |
|
220 |
day isNil ifTrue:[ |
|
221 |
[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
|
222 |
day := Integer readFrom:str. |
972 | 223 |
] |
224 |
] ifFalse:[ |
|
225 |
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
|
226 |
day := Integer readFrom:str |
972 | 227 |
]. |
228 |
[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
|
229 |
year := Integer readFrom:str. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
230 |
|
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
231 |
newDate := self newDay:day month:month year:year |
699 | 232 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
233 |
^ newDate |
699 | 234 |
|
235 |
" |
|
236 |
Date readFromString:'31 December 1992' |
|
237 |
Date readFromString:'December, 5, 1992' |
|
238 |
Date readFromString:'December, 5 1992' |
|
239 |
Date readFromString:'3-jan-95' |
|
240 |
Date readFromString:'12/31/1992' |
|
241 |
Date readFromString:'15.4.1992' -> german; leads to an error |
|
242 |
Date readFromString:'10.4.1992' -> german; leads to a wrong date |
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
Date readFromString:'10.4' onError:['wrong date'] |
699 | 247 |
" |
248 |
||
249 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
250 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
699 | 251 |
! |
252 |
||
253 |
today |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
254 |
"return a date, representing today. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
255 |
See also: Time now / AbsoluteTime now." |
699 | 256 |
|
1501 | 257 |
^ self fromOSTime:(OperatingSystem getOSTime) |
699 | 258 |
|
259 |
" |
|
260 |
Date today |
|
261 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
262 |
|
1501 | 263 |
"Modified: 1.7.1996 / 15:20:16 / cg" |
699 | 264 |
! ! |
265 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
266 |
!Date class methodsFor:'general queries'! |
699 | 267 |
|
268 |
abbreviatedNameOfDay:dayIndex |
|
269 |
"given a day index (1..7), return the abbreviated name |
|
270 |
of the day" |
|
271 |
||
272 |
EnvironmentChange ifTrue:[ |
|
273 |
self initNames |
|
274 |
]. |
|
275 |
^ DayAbbrevs at:dayIndex |
|
276 |
||
277 |
" |
|
278 |
Date abbreviatedNameOfDay:4 |
|
279 |
" |
|
280 |
! |
|
281 |
||
282 |
abbreviatedNameOfMonth:monthIndex |
|
283 |
"given a month index (1..12), return the abbreviated name |
|
284 |
of the month" |
|
285 |
||
286 |
EnvironmentChange ifTrue:[ |
|
287 |
self initNames |
|
288 |
]. |
|
289 |
^ MonthAbbrevs at:monthIndex |
|
290 |
||
291 |
" |
|
292 |
Date abbreviatedNameOfMonth:11 |
|
293 |
Date abbreviatedNameOfMonth:12 |
|
294 |
" |
|
295 |
! |
|
296 |
||
297 |
dateAndTimeNow |
|
298 |
"return an array containing the date and time of now" |
|
299 |
||
300 |
^ Time dateAndTimeNow |
|
301 |
||
302 |
" |
|
303 |
Date dateAndTimeNow |
|
304 |
" |
|
305 |
! |
|
306 |
||
307 |
dayOfWeek:dayName |
|
308 |
"given the name of a day (either string or symbol), |
|
309 |
return the day-index (1 for monday; 7 for sunday). |
|
310 |
Return 0 for invalid day name" |
|
311 |
||
312 |
EnvironmentChange ifTrue:[ |
|
313 |
self initNames |
|
314 |
]. |
|
315 |
^ DayNames indexOf:dayName |
|
316 |
||
317 |
" |
|
318 |
Date dayOfWeek:'wednesday' |
|
319 |
" |
|
320 |
! |
|
321 |
||
322 |
daysInMonth:month forYear:yearInteger |
|
323 |
"given the name of a month and a year, return the number |
|
324 |
of days this month has (modified GNU). |
|
325 |
return 0 if the month name was invalid. |
|
326 |
For your convenience, month maybe an integer or name-string." |
|
327 |
||
328 |
|monthIndex "{ Class: SmallInteger }"| |
|
329 |
||
330 |
month isInteger ifTrue:[ |
|
331 |
monthIndex := month |
|
332 |
] ifFalse:[ |
|
333 |
monthIndex := self indexOfMonth:month |
|
334 |
]. |
|
335 |
||
336 |
^ self daysInMonthIndex:monthIndex forYear:yearInteger |
|
337 |
||
338 |
" |
|
339 |
Date daysInMonth:2 forYear:1980 |
|
340 |
Date daysInMonth:2 forYear:1981 |
|
341 |
Date daysInMonth:'feb' forYear:1981 |
|
342 |
" |
|
343 |
! |
|
344 |
||
345 |
daysInYear:yearInteger |
|
346 |
"return the number of days in a year" |
|
347 |
||
348 |
(self leapYear:yearInteger) ifTrue:[^ 366]. |
|
349 |
^ 365 |
|
350 |
||
351 |
" |
|
352 |
Date daysInYear:1900 |
|
353 |
Date daysInYear:1901 |
|
354 |
Date daysInYear:1904 |
|
355 |
Date daysInYear:1980 |
|
356 |
Date daysInYear:1981 |
|
357 |
" |
|
358 |
! |
|
359 |
||
360 |
daysUntilMonth:month forYear:yearInteger |
|
361 |
"given the name of a month and a year, return the number |
|
362 |
of days from 1st january to last of prev month of that year. |
|
363 |
Return 0 if the month name/index is invalid or is january. |
|
364 |
For your convenience, month maybe an integer or name-string." |
|
365 |
||
366 |
|monthIndex "{ Class: SmallInteger }" |
|
367 |
sumDays "{ Class: SmallInteger }" | |
|
368 |
||
369 |
month isInteger ifTrue:[ |
|
370 |
monthIndex := month |
|
371 |
] ifFalse:[ |
|
372 |
monthIndex := self indexOfMonth:month |
|
373 |
]. |
|
374 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
375 |
||
376 |
sumDays := 0. |
|
377 |
1 to:monthIndex-1 do:[:m | |
|
378 |
sumDays := sumDays + (self daysInMonthIndex:m forYear:yearInteger) |
|
379 |
]. |
|
380 |
^ sumDays |
|
381 |
||
382 |
" |
|
383 |
Date daysUntilMonth:'feb' forYear:1993 |
|
384 |
Date daysUntilMonth:'jan' forYear:1993 |
|
385 |
" |
|
386 |
! |
|
387 |
||
388 |
indexOfMonth:aMonthString |
|
389 |
"given the name of a month (either string or symbol), |
|
390 |
return the month-index (1 for jan; 12 for december). |
|
391 |
The given string may be a full or abbreviated name, |
|
392 |
case is ignored. |
|
393 |
Return 0 for invalid month name." |
|
394 |
||
395 |
|idx name| |
|
396 |
||
397 |
EnvironmentChange ifTrue:[ |
|
398 |
self initNames |
|
399 |
]. |
|
400 |
name := aMonthString asLowercase. |
|
401 |
idx := MonthAbbrevs indexOf:name. |
|
402 |
idx ~~ 0 ifTrue:[^ idx]. |
|
403 |
idx := MonthNames indexOf:name. |
|
404 |
idx ~~ 0 ifTrue:[^ idx]. |
|
405 |
||
406 |
name at:1 put:(name at:1) asUppercase. |
|
407 |
idx := MonthAbbrevs indexOf:name. |
|
408 |
idx ~~ 0 ifTrue:[^ idx]. |
|
409 |
idx := MonthNames indexOf:name. |
|
410 |
idx ~~ 0 ifTrue:[^ idx]. |
|
411 |
||
412 |
^ idx |
|
413 |
||
414 |
" |
|
415 |
Date indexOfMonth:'jan' |
|
416 |
Date indexOfMonth:'Jan' |
|
417 |
Date indexOfMonth:'December' |
|
418 |
" |
|
419 |
! |
|
420 |
||
421 |
isLeapYear:yearInteger |
|
422 |
"Return true, if a year is a leap year. |
|
423 |
Obsolete: |
|
424 |
Please use the ST-80 compatible #leapYear for new programs, |
|
425 |
since this method will vanish." |
|
426 |
||
427 |
^ self leapYear:yearInteger |
|
428 |
! |
|
429 |
||
430 |
leapYear:yearInteger |
|
431 |
"return true, if yearInteger is a leap year." |
|
432 |
||
433 |
|y "{ Class: SmallInteger }"| |
|
434 |
||
435 |
y := yearInteger. |
|
436 |
(y \\ 4 == 0) ifTrue:[ |
|
437 |
(y \\ 100 ~~ 0) ifTrue:[^ true]. |
|
438 |
(y \\ 400 == 0) ifTrue:[^ true] |
|
439 |
]. |
|
440 |
^ false |
|
441 |
||
442 |
" |
|
443 |
Date leapYear:1992 |
|
444 |
Date leapYear:1994 |
|
445 |
Date leapYear:1900 |
|
446 |
Date leapYear:2000 |
|
447 |
" |
|
448 |
! |
|
449 |
||
450 |
monthAndDayFromDayInYear:aDayInYear forYear:yearInteger |
|
451 |
"given a day-in-year (1..365) return an Array containing the |
|
452 |
month index and the day-in-month. Return nil if the argument is invalid." |
|
453 |
||
454 |
|restDays daysInMonth| |
|
455 |
||
456 |
restDays := aDayInYear. |
|
457 |
restDays < 1 ifTrue:[^ nil]. |
|
458 |
||
459 |
1 to:12 do:[:m | |
|
460 |
daysInMonth := self daysInMonthIndex:m forYear:yearInteger. |
|
461 |
restDays <= daysInMonth ifTrue:[ |
|
462 |
^ Array with:m with:restDays |
|
463 |
]. |
|
464 |
restDays := restDays - daysInMonth |
|
465 |
]. |
|
466 |
restDays > daysInMonth ifTrue:[^ nil]. |
|
467 |
^ Array with:12 with:restDays |
|
468 |
||
469 |
" |
|
470 |
Date monthAndDayFromDayInYear:66 forYear:1980 |
|
471 |
Date monthAndDayFromDayInYear:66 forYear:1981 |
|
472 |
" |
|
473 |
! |
|
474 |
||
475 |
nameOfDay:dayIndex |
|
476 |
"given a day index (1..7), return the name of the day" |
|
477 |
||
478 |
EnvironmentChange ifTrue:[ |
|
479 |
self initNames |
|
480 |
]. |
|
481 |
^ DayNames at:dayIndex |
|
482 |
||
483 |
" |
|
484 |
Date nameOfDay:4 |
|
485 |
" |
|
486 |
! |
|
487 |
||
488 |
nameOfMonth:monthIndex |
|
489 |
"given a month index (1..12), return the name of the month" |
|
490 |
||
491 |
EnvironmentChange ifTrue:[ |
|
492 |
self initNames |
|
493 |
]. |
|
494 |
^ MonthNames at:monthIndex |
|
495 |
||
496 |
" |
|
497 |
Date nameOfMonth:11 |
|
498 |
Date nameOfMonth:12 |
|
499 |
Date nameOfMonth:4 |
|
500 |
" |
|
501 |
! |
|
502 |
||
503 |
yearAsDays: yearInteger |
|
504 |
"Returns the number of days since Jan 1, 1901. (GNU) |
|
505 |
to the first Jan of the year, yearInteger. |
|
506 |
For 1901 this is zero, for 1902 its 365. |
|
507 |
Defined for years >= 1901" |
|
508 |
||
509 |
|y "{ Class: SmallInteger }"| |
|
510 |
||
511 |
y := yearInteger - 1900. |
|
512 |
y := y - 1. |
|
513 |
^ (y * 365) |
|
514 |
+ (y // 4) |
|
515 |
- (y // 100) |
|
516 |
+ ((y + 300) // 400) |
|
517 |
||
518 |
" |
|
519 |
Date yearAsDays:1901 |
|
520 |
Date yearAsDays:1902 |
|
521 |
Date yearAsDays:1903 |
|
522 |
Date yearAsDays:1904 |
|
523 |
Date yearAsDays:1905 |
|
524 |
Date yearAsDays:1994 |
|
525 |
(Date yearAsDays:2001) - (Date yearAsDays:2000) |
|
526 |
" |
|
527 |
! ! |
|
528 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
529 |
!Date class methodsFor:'handling language changes'! |
699 | 530 |
|
531 |
initialize |
|
532 |
"check for case where Resource-classes are absent" |
|
533 |
ResourcePack isNil ifTrue:[ |
|
534 |
self initNames |
|
535 |
] ifFalse:[ |
|
536 |
Smalltalk addDependent:self. |
|
537 |
EnvironmentChange := true |
|
538 |
] |
|
539 |
! |
|
540 |
||
1468 | 541 |
update:something with:aParameter from:changedObject |
699 | 542 |
((something == #Language) or:[something == #LanguageTerritory]) ifTrue:[ |
1468 | 543 |
"just remember change for next access" |
544 |
EnvironmentChange := true |
|
699 | 545 |
] |
1468 | 546 |
|
547 |
"Created: 15.6.1996 / 15:19:25 / cg" |
|
699 | 548 |
! ! |
549 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
550 |
!Date class methodsFor:'obsolete instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
551 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
552 |
day:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
553 |
"return a new Date, given the day, month and year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
554 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
555 |
use newDay:month:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
556 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
557 |
^ self newDay:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
558 |
! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
559 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
560 |
day:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
561 |
"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
|
562 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
563 |
use newDay:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
564 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
565 |
^ self newDay:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
566 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
567 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
568 |
!Date class methodsFor:'private'! |
1 | 569 |
|
699 | 570 |
daysInMonthIndex: monthIndex forYear: yearInteger |
571 |
"return the number of days in month monthIndex of |
|
572 |
year yearInteger (modified GNU). |
|
573 |
Return 0 for invalid month index. |
|
574 |
This is the internal version of daysInMonth:forYear:" |
|
575 |
||
576 |
|days| |
|
577 |
||
578 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
579 |
||
580 |
days := #(31 28 31 "Jan Feb Mar" |
|
581 |
30 31 30 "Apr May Jun" |
|
582 |
31 31 30 "Jul Aug Sep" |
|
583 |
31 30 31 "Oct Nov Dec" |
|
584 |
) at: monthIndex. |
|
585 |
||
586 |
(monthIndex == 2) ifTrue:[ |
|
587 |
(self leapYear:yearInteger) ifTrue:[ |
|
588 |
^ days + 1 |
|
589 |
] |
|
590 |
]. |
|
591 |
^ days |
|
592 |
||
593 |
" |
|
594 |
Date daysInMonthIndex:2 forYear:1994 |
|
595 |
Date daysInMonthIndex:2 forYear:1980 |
|
596 |
Date daysInMonthIndex:2 forYear:1981 |
|
597 |
" |
|
598 |
! |
|
599 |
||
1 | 600 |
initNames |
601 |
"read the language specific names" |
|
602 |
||
10 | 603 |
|resources| |
604 |
||
54 | 605 |
DayNames := #('monday' |
155 | 606 |
'tuesday' |
607 |
'wednesday' |
|
608 |
'thursday' |
|
609 |
'friday' |
|
610 |
'saturday' |
|
611 |
'sunday'). |
|
1 | 612 |
|
54 | 613 |
DayAbbrevs := #('mon' |
155 | 614 |
'tue' |
615 |
'wed' |
|
616 |
'thu' |
|
617 |
'fri' |
|
618 |
'sat' |
|
619 |
'sun'). |
|
1 | 620 |
|
54 | 621 |
MonthNames := #('january' |
155 | 622 |
'february' |
623 |
'march' |
|
624 |
'april' |
|
625 |
'may' |
|
626 |
'june' |
|
627 |
'july' |
|
628 |
'august' |
|
629 |
'september' |
|
630 |
'october' |
|
631 |
'november' |
|
632 |
'december'). |
|
1 | 633 |
|
54 | 634 |
MonthAbbrevs := #('jan' |
155 | 635 |
'feb' |
636 |
'mar' |
|
637 |
'apr' |
|
638 |
'may' |
|
639 |
'jun' |
|
640 |
'jul' |
|
641 |
'aug' |
|
642 |
'sep' |
|
643 |
'oct' |
|
644 |
'nov' |
|
645 |
'dec'). |
|
54 | 646 |
|
647 |
"check for case where Resource-classes are absent" |
|
648 |
ResourcePack notNil ifTrue:[ |
|
155 | 649 |
resources := ResourcePack for:self. |
54 | 650 |
|
155 | 651 |
DayNames := resources array:DayNames. |
652 |
DayAbbrevs := resources array:DayAbbrevs. |
|
653 |
MonthNames := resources array:MonthNames. |
|
654 |
MonthAbbrevs := resources array:MonthAbbrevs. |
|
54 | 655 |
]. |
1 | 656 |
|
29 | 657 |
EnvironmentChange := false |
10 | 658 |
|
659 |
"Date initNames" |
|
54 | 660 |
! ! |
661 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
662 |
!Date class methodsFor:'private encoding'! |
54 | 663 |
|
664 |
encodeYear:y month:m day:d |
|
92 | 665 |
"the internal encoding is stricktly private, |
666 |
and should not be used outside." |
|
667 |
||
54 | 668 |
^ (((y * 100) + m) * 100) + d |
669 |
! ! |
|
670 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
671 |
!Date class methodsFor:'private instance creation'! |
1227
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 |
fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
674 |
"return a date, representing the date given by the operatingSystem time. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
675 |
This somewhat clumsy implementation hides the OS's date representation |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
676 |
(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
|
677 |
Dont use this method, the osTime representation is totally unportable." |
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 |
^ self basicNew fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
680 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
681 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
682 |
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
|
683 |
thats where Unix time starts |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
684 |
On other systems, it may be something different. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
685 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
686 |
Date fromOSTime:#(86400 0) -> on UNIX: the day after |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
687 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
688 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
689 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
690 |
!Date methodsFor:'ST80 compatibility'! |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
691 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
692 |
shortPrintString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
693 |
"dummy - for now" |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
694 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
695 |
^ self printString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
696 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
697 |
"Created: 20.6.1997 / 17:16:48 / cg" |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
698 |
! ! |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
699 |
|
699 | 700 |
!Date methodsFor:'accessing'! |
241 | 701 |
|
699 | 702 |
abbreviatedDayName |
703 |
"return the short week-day of the receiver as a string. |
|
704 |
The returned string depends on the language setting. |
|
705 |
Expect things like 'mon', 'tue' ..." |
|
54 | 706 |
|
699 | 707 |
^ self class abbreviatedNameOfDay:(self dayInWeek) |
92 | 708 |
|
709 |
" |
|
699 | 710 |
Date today abbreviatedDayName |
711 |
(Date day:15 month:4 year:1959) abbreviatedDayName |
|
92 | 712 |
" |
713 |
! |
|
714 |
||
699 | 715 |
abbreviatedMonthName |
716 |
"return the month of the receiver as a string. |
|
717 |
The returned string depends on the language setting. |
|
718 |
Expect things like 'jan', 'feb' ..." |
|
54 | 719 |
|
699 | 720 |
^ self class abbreviatedNameOfMonth:(self month) |
68 | 721 |
|
92 | 722 |
" |
699 | 723 |
Date today abbreviatedMonthName |
724 |
(Date day:15 month:4 year:1959) abbreviatedMonthName |
|
92 | 725 |
" |
726 |
! |
|
727 |
||
728 |
asDays |
|
729 |
"return the number of days elapsed since 01-Jan-1901 |
|
730 |
and the receiver's day; starts with 0 for 1-1-1901. |
|
731 |
Date>>fromDays: is the reverse operation. |
|
732 |
For ST-80 compatibility." |
|
68 | 733 |
|
734 |
|yr| |
|
735 |
||
736 |
yr := self year. |
|
737 |
^ (self class yearAsDays:yr) |
|
738 |
+ (self class daysUntilMonth:self month forYear:yr) |
|
739 |
+ self day |
|
85 | 740 |
- 1 |
68 | 741 |
|
92 | 742 |
" |
743 |
(Date day: 5 month: 8 year: 1962) asDays -> should be 22496 |
|
744 |
(Date day: 1 month: 1 year: 1901) asDays -> 0 |
|
745 |
Date today asDays |
|
746 |
Date fromDays:(Date today asDays + 7) |
|
747 |
" |
|
77 | 748 |
! |
749 |
||
750 |
asSeconds |
|
92 | 751 |
"return the seconds between 1.jan.1901 and the same time in the receivers |
752 |
day. (i.e. midnight to midnight). |
|
1501 | 753 |
This does not include any leapSeconds ... strictly speaking, this is incorrect. |
77 | 754 |
ST-80 compatibility." |
755 |
||
92 | 756 |
^ 60*60*24 * self asDays |
77 | 757 |
|
92 | 758 |
" |
759 |
(Date day: 5 month: 8 year: 1962) asSeconds |
|
760 |
(Date day: 1 month: 1 year: 1901) asSeconds |
|
761 |
(Date today addDays:7) asSeconds - Date today asSeconds |
|
1501 | 762 |
" |
763 |
||
764 |
"Modified: 1.7.1996 / 14:19:17 / cg" |
|
699 | 765 |
! |
766 |
||
767 |
day |
|
768 |
"return the day (1..31) of the receiver" |
|
769 |
||
770 |
^ dateEncoding \\ 100 |
|
771 |
||
772 |
" |
|
773 |
Date today day |
|
774 |
" |
|
775 |
! |
|
776 |
||
777 |
dayCount |
|
778 |
"return the number of days since 1st. Jan. 1901; |
|
779 |
starting with 0 for this date. |
|
780 |
Date>>fromDays: is the reverse operation. |
|
781 |
Obsolete: |
|
782 |
please use asDays for ST-80 compatibility" |
|
783 |
||
784 |
^ self asDays. |
|
785 |
||
786 |
" |
|
787 |
(Date day:1 month:1 year:1901) dayCount |
|
788 |
Date fromDays:(Date day:1 month:1 year:1994) dayCount |
|
789 |
Date today dayCount |
|
790 |
" |
|
791 |
! |
|
792 |
||
793 |
dayInWeek |
|
794 |
"return the week-day of the receiver - 1 for monday, 7 for sunday" |
|
795 |
||
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
796 |
|wday| |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
797 |
|
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
798 |
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
|
799 |
+ self asDays) \\ 7 + 1. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
800 |
wday < 1 ifTrue:[ |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
801 |
wday := wday + 7 |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
802 |
]. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
803 |
^ wday |
699 | 804 |
|
805 |
" |
|
1501 | 806 |
Date today dayInWeek |
3257 | 807 |
Date today weekday |
1501 | 808 |
(Date day:15 month:4 year:1959) dayInWeek |
809 |
(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
|
810 |
(Date day:31 month:12 year:1899) dayInWeek |
699 | 811 |
" |
1501 | 812 |
|
3257 | 813 |
"Modified: / 1.2.1998 / 14:10:27 / cg" |
699 | 814 |
! |
815 |
||
816 |
dayName |
|
817 |
"return the week-day of the receiver as a string. |
|
818 |
The returned string depends on the language setting. |
|
819 |
Expect things like 'monday', 'tuesday' ... |
|
820 |
Obsolete: |
|
821 |
use #weekday for ST-80 compatibility" |
|
822 |
||
823 |
^ self weekday |
|
824 |
! |
|
77 | 825 |
|
826 |
dayOfMonth |
|
827 |
"Answer the day of the month represented by me. |
|
828 |
Same as day; for ST-80 compatibility." |
|
829 |
||
830 |
^ self day |
|
92 | 831 |
|
832 |
" |
|
833 |
Date today dayOfMonth |
|
834 |
" |
|
835 |
! |
|
836 |
||
837 |
daysInMonth |
|
838 |
"return the number of days in the month of the receiver" |
|
839 |
||
840 |
^ Date daysInMonth:(self month) forYear:(self year) |
|
841 |
||
842 |
" |
|
843 |
Date today daysInMonth |
|
844 |
" |
|
845 |
! |
|
846 |
||
847 |
daysInYear |
|
848 |
"return the number of days in the year of the receiver" |
|
849 |
||
850 |
^ Date daysInYear:(self year) |
|
851 |
||
852 |
" |
|
853 |
Date today daysInYear |
|
854 |
" |
|
855 |
! |
|
856 |
||
857 |
daysLeftInYear |
|
858 |
"return the number of days left in the year of the receiver" |
|
859 |
||
860 |
^ Date daysInYear:(self year) - self day |
|
861 |
||
862 |
" |
|
863 |
Date today daysLeftInYear |
|
864 |
" |
|
77 | 865 |
! |
866 |
||
699 | 867 |
leap |
868 |
"return true, if the receivers year is a leap year" |
|
68 | 869 |
|
699 | 870 |
^ Date leapYear:(self year) |
68 | 871 |
|
92 | 872 |
" |
699 | 873 |
Date today leap |
874 |
(Date day:1 month:1 year:1992) leap |
|
92 | 875 |
" |
68 | 876 |
! |
877 |
||
699 | 878 |
month |
879 |
"return the month (1..12) of the receiver" |
|
92 | 880 |
|
699 | 881 |
^ (dateEncoding // 100) \\ 100 |
68 | 882 |
|
92 | 883 |
" |
699 | 884 |
Date today month |
92 | 885 |
" |
68 | 886 |
! |
887 |
||
77 | 888 |
monthIndex |
889 |
"return the index of the month (e.g. Feb.=2). |
|
890 |
Same as month; for ST-80 compatibility." |
|
891 |
||
892 |
^ self month |
|
893 |
! |
|
894 |
||
68 | 895 |
monthName |
896 |
"return the month of the receiver as a string. |
|
897 |
The returned string depends on the language setting. |
|
898 |
Expect things like 'january', 'february' ..." |
|
899 |
||
900 |
^ self class nameOfMonth:(self month) |
|
901 |
||
92 | 902 |
" |
903 |
Date today monthName |
|
904 |
(Date day:15 month:4 year:1959) monthName |
|
905 |
" |
|
68 | 906 |
! |
907 |
||
699 | 908 |
weekday |
909 |
"return the week-day of the receiver as a string. |
|
68 | 910 |
The returned string depends on the language setting. |
699 | 911 |
Expect things like 'monday', 'tuesday' ... |
912 |
For ST-80 compatibility" |
|
913 |
||
914 |
^ self class nameOfDay:(self dayInWeek) |
|
915 |
||
916 |
" |
|
917 |
Date today weekday |
|
918 |
(Date day:15 month:4 year:1959) weekday |
|
919 |
" |
|
920 |
! |
|
921 |
||
922 |
year |
|
923 |
"return the year (1..12) of the receiver" |
|
924 |
||
925 |
^ dateEncoding // (100*100) |
|
68 | 926 |
|
699 | 927 |
" |
928 |
Date today year |
|
929 |
" |
|
930 |
! ! |
|
931 |
||
932 |
!Date methodsFor:'arithmetic'! |
|
933 |
||
934 |
addDays:days |
|
935 |
"return a new date representing 'days' after the receiver. |
|
936 |
The argument should be some kind of integer. |
|
937 |
For ST-80 compatibility." |
|
938 |
||
939 |
^ self class fromDays:(self asDays + days) |
|
940 |
||
941 |
" |
|
942 |
Date today addDays:7 |
|
943 |
" |
|
944 |
! |
|
945 |
||
946 |
daysUntil:aDate |
|
947 |
"return the number of days between the receiver and the argument, |
|
948 |
aDate, whuch should be some kind of date" |
|
949 |
||
950 |
^ aDate asDays - self asDays |
|
68 | 951 |
|
92 | 952 |
" |
699 | 953 |
(Date day:24 month:12 year:1994) daysUntil:(Date day:1 month:1 year:1995) |
954 |
(Date day:1 month:2 year:1992) daysUntil:(Date day:1 month:3 year:1992) |
|
955 |
(Date day:1 month:2 year:1994) daysUntil:(Date day:1 month:3 year:1994) |
|
956 |
||
957 |
|delta| |
|
958 |
delta := Date today |
|
959 |
daysUntil:(Date day:25 month:12 year:Date today year). |
|
960 |
Transcript show:'still '; |
|
961 |
show:delta ; |
|
1422 | 962 |
showCR:' days till xmas' |
699 | 963 |
" |
964 |
! |
|
965 |
||
966 |
minusDays:days |
|
967 |
"return a new date representing 'days' before the receiver. |
|
968 |
The argument should be some kind of integer. |
|
969 |
Obsolete: |
|
970 |
Please dont use this method since it will vanish. |
|
971 |
Use #subtractDays: instead for ST-80 compatibility." |
|
972 |
||
973 |
^ self subtractDays:days |
|
974 |
! |
|
975 |
||
976 |
plusDays:days |
|
977 |
"return a new date representing 'days' after the receiver. |
|
978 |
The argument should be some kind of integer. |
|
979 |
Obsolete: |
|
980 |
Please dont use this method since it will vanish. |
|
981 |
Use #addDays: instead for ST-80 compatibility." |
|
982 |
||
983 |
^ self addDays:days |
|
984 |
! |
|
985 |
||
986 |
subtractDate:aDate |
|
987 |
"return the number of days between the receiver and aDate" |
|
988 |
||
989 |
^ self asDays - aDate asDays |
|
990 |
||
991 |
" |
|
992 |
(Date day:1 month:1 year:1995) subtractDate:(Date day:24 month:12 year:1994) |
|
993 |
(Date day:1 month:3 year:1992) subtractDate:(Date day:1 month:2 year:1992) |
|
994 |
(Date day:1 month:3 year:1994) subtractDate:(Date day:1 month:2 year:1994) |
|
995 |
" |
|
996 |
! |
|
997 |
||
998 |
subtractDays:days |
|
999 |
"return a new date representing 'days' before the receiver. |
|
1000 |
The argument should be some kind of integer. |
|
1001 |
For ST-80 compatibility" |
|
1002 |
||
1003 |
^ self class fromDays:(self asDays - days) |
|
1004 |
||
1005 |
" |
|
1006 |
Date today subtractDays:7 |
|
92 | 1007 |
" |
1 | 1008 |
! ! |
1009 |
||
1010 |
!Date methodsFor:'comparing'! |
|
1011 |
||
29 | 1012 |
< aDate |
1013 |
"return true, if the date represented by the receiver |
|
1014 |
is before the argument, aDate" |
|
1015 |
||
54 | 1016 |
(aDate isMemberOf:Date) ifTrue:[ |
155 | 1017 |
^ dateEncoding < aDate dateEncoding |
54 | 1018 |
]. |
29 | 1019 |
|
54 | 1020 |
"the argument must understand year, month and day to be |
1021 |
comparable, whatever it is" |
|
1022 |
||
1023 |
^ dateEncoding < (Date encodeYear:aDate year |
|
155 | 1024 |
month:aDate month |
1025 |
day:aDate day) |
|
68 | 1026 |
|
1027 |
"Date today < (Date day:24 month:12 year:2000)" |
|
1028 |
"Date today < (Date day:24 month:12 year:1900)" |
|
29 | 1029 |
! |
1 | 1030 |
|
1031 |
= aDate |
|
1032 |
"return true, if the date represented by the receiver |
|
1033 |
is the same as the one represented by argument, aDate" |
|
1034 |
||
155 | 1035 |
(aDate isMemberOf:Date) ifFalse:[^ false]. |
1036 |
||
1037 |
^ dateEncoding = aDate dateEncoding |
|
54 | 1038 |
|
1039 |
"the argument must understand year, month and day to be |
|
1040 |
comparable, whatever it is" |
|
1041 |
||
155 | 1042 |
" |
54 | 1043 |
^ dateEncoding = (Date encodeYear:aDate year |
155 | 1044 |
month:aDate month |
1045 |
day:aDate day) |
|
1046 |
" |
|
68 | 1047 |
"Date today = ((Date today plusDays:7) minusDays:7)" |
92 | 1048 |
! |
1049 |
||
699 | 1050 |
> aDate |
1051 |
"return true, if the date represented by the receiver |
|
1052 |
is after the argument, aDate" |
|
1053 |
||
1054 |
(aDate isMemberOf:Date) ifTrue:[ |
|
1055 |
^ dateEncoding > aDate dateEncoding |
|
1056 |
]. |
|
1057 |
||
1058 |
"the argument must understand year, month and day to be |
|
1059 |
comparable, whatever it is" |
|
1060 |
||
1061 |
^ dateEncoding > (Date encodeYear:aDate year |
|
1062 |
month:aDate month |
|
1063 |
day:aDate day) |
|
1064 |
||
1065 |
"Date today > (Date day:24 month:12 year:2000)" |
|
1066 |
"Date today > (Date day:24 month:12 year:1900)" |
|
1067 |
! |
|
1068 |
||
92 | 1069 |
hash |
1070 |
"return an integer useful for hashing on dates" |
|
1071 |
||
1072 |
^ dateEncoding |
|
1 | 1073 |
! ! |
1074 |
||
1501 | 1075 |
!Date methodsFor:'converting'! |
1076 |
||
1077 |
asAbsoluteTime |
|
1078 |
"return an absoluteTime instance, representing midnight of |
|
1079 |
last night" |
|
1080 |
||
1081 |
^ AbsoluteTime day:(self day) month:(self month) year:(self year) |
|
1082 |
hour:0 minutes:0 seconds:0 milliseconds:0 |
|
1083 |
||
1084 |
" |
|
1085 |
Date today asAbsoluteTime |
|
1086 |
" |
|
1087 |
||
1088 |
"Modified: 1.7.1996 / 14:53:32 / cg" |
|
1089 |
! ! |
|
1090 |
||
54 | 1091 |
!Date methodsFor:'printing & storing'! |
1 | 1092 |
|
408 | 1093 |
printFormat:aFormatArray |
1094 |
"return a string containing a printed representation of the receiver. |
|
1095 |
The formatArray argument consists of 6 or 7 integers which control |
|
1096 |
the resulting string. The entries are: |
|
1097 |
1 day position (1, 2 or 3) |
|
1098 |
2 month position (1..3) |
|
1099 |
3 year position (1..3) |
|
1100 |
4 asciiValue of separator character or separator character |
|
1101 |
or collection of 2 separator characters |
|
1102 |
5 month format (1: numeric; 2: abbreviated name; 3: fullname |
|
1103 |
4: abbreviated uppercase 5: full uppercase) |
|
1104 |
6 year format (1 include century; 2 exclude century) |
|
1105 |
7 (optional) true/false of true, print numbers in 2-digit format |
|
1106 |
False, if ommited |
|
1107 |
||
1108 |
Day and monthnames are in the currently active language. |
|
1109 |
||
1110 |
This method supports more options than the ST-80 version; month formats |
|
1111 |
4 and 5, non-numeric separators and the optional 7th parameter are not |
|
1112 |
supported by ST-80. Be aware of this for compatibility reasons. |
|
1113 |
||
1114 |
Notice that not all formats can be scanned (correctly) by #readFrom:" |
|
1115 |
||
1116 |
|mf upperCase day month year components sep1 sep2 leadingZeros| |
|
1117 |
||
1118 |
aFormatArray size > 6 ifTrue:[ |
|
1119 |
leadingZeros := aFormatArray at:7 |
|
1120 |
] ifFalse:[ |
|
1121 |
leadingZeros := false. |
|
1122 |
]. |
|
1123 |
||
1124 |
components := Array new:3. |
|
1125 |
day := self day. |
|
1126 |
leadingZeros ifTrue:[ |
|
1127 |
day := day printStringLeftPaddedTo:2 with:$0 |
|
1128 |
] ifFalse:[ |
|
1129 |
day := day printString |
|
1130 |
]. |
|
1131 |
components at:(aFormatArray at:1) put:day. |
|
1132 |
||
1133 |
upperCase := false. |
|
1134 |
mf := aFormatArray at:5. |
|
1135 |
(mf between:4 and:5) ifTrue:[mf := mf - 2. upperCase := true]. |
|
1136 |
||
1137 |
mf == 1 ifTrue:[ |
|
1138 |
month := self month. |
|
1139 |
leadingZeros ifTrue:[ |
|
1140 |
month := month printStringLeftPaddedTo:2 with:$0 |
|
1141 |
] ifFalse:[ |
|
1142 |
month := month printString |
|
1143 |
] |
|
1144 |
] ifFalse:[ |
|
1145 |
mf == 2 ifTrue:[ |
|
1146 |
month := self abbreviatedMonthName |
|
1147 |
] ifFalse:[ |
|
1148 |
month := self monthName |
|
1149 |
] |
|
1150 |
]. |
|
1151 |
upperCase ifTrue:[month := month asUppercase]. |
|
1152 |
components at:(aFormatArray at:2) put:month. |
|
1153 |
||
1154 |
year := self year. |
|
1155 |
(aFormatArray at:6) == 2 ifTrue:[ |
|
1156 |
year := year \\ 100 |
|
1157 |
]. |
|
1158 |
components at:(aFormatArray at:3) put:year printString. |
|
1159 |
||
1160 |
sep1 := (aFormatArray at:4). |
|
1161 |
sep1 isCollection ifTrue:[ |
|
1162 |
sep2 := (sep1 at:2). |
|
1163 |
sep1 := (sep1 at:1). |
|
1164 |
] ifFalse:[ |
|
1165 |
sep1 := sep2 := sep1 asCharacter |
|
1166 |
]. |
|
1167 |
||
1168 |
^ (components at:1) |
|
1169 |
, sep1 asString |
|
1170 |
, (components at:2) |
|
1171 |
, sep2 asString |
|
1172 |
, (components at:3) |
|
1173 |
||
1174 |
" |
|
1175 |
Date today printFormat:#(1 2 3 $- 1 2) |
|
1176 |
Date today printFormat:#(1 2 3 $. 1 2 true) |
|
1177 |
Date today printFormat:#(2 1 3 32 3 1) |
|
1178 |
Date today printFormat:#(2 1 3 #(' ' ', ') 3 1) |
|
1179 |
Date today printFormat:#(1 2 3 $- 2 1) |
|
1180 |
Date today printFormat:#(1 2 3 $- 4 1) |
|
1181 |
" |
|
1182 |
! |
|
1183 |
||
1184 |
printFormat:aFormatArray on:aStream |
|
1185 |
"append a printed representation of the receiver to aStream. |
|
1186 |
The argument, aFormatString controls the format, as described |
|
1187 |
in the #printFormat: method. |
|
1188 |
Notice that not all formats can be scanned (correctly) by #readFrom:" |
|
1189 |
||
1190 |
aStream nextPutAll:(self printFormat:aFormatArray) |
|
1191 |
||
1192 |
||
1193 |
"european formats: |
|
1194 |
||
1195 |
Date today printFormat:#(1 2 3 $- 1 2) on:Transcript. Transcript cr. |
|
1196 |
Date today printFormat:#(1 2 3 $- 2 2) on:Transcript. Transcript cr. |
|
1197 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1198 |
Date today printFormat:#(1 2 3 $- 3 2) on:Transcript. Transcript cr. |
|
1199 |
Date today printFormat:#(1 2 3 $. 1 2) on:Transcript. Transcript cr. |
|
1200 |
Date today printFormat:#(1 2 3 $. 1 2 true) on:Transcript. Transcript cr. |
|
1201 |
||
1202 |
US formats: |
|
1203 |
||
1204 |
Date today printFormat:#(2 1 3 32 3 1) on:Transcript. Transcript cr. |
|
1205 |
Date today printFormat:#(2 1 3 32 2 2) on:Transcript. Transcript cr. |
|
1206 |
Date today printFormat:#(2 1 3 $/ 1 2) on:Transcript. Transcript cr. |
|
1207 |
Date today printFormat:#(2 1 3 $- 2 1) on:Transcript. Transcript cr. |
|
1208 |
Date today printFormat:#(2 1 3 #(' ' ', ') 2 1) on:Transcript. Transcript cr. |
|
1209 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1210 |
Date today printFormat:#(1 2 3 $- 4 1) on:Transcript. Transcript cr. |
|
1211 |
" |
|
699 | 1212 |
! |
1213 |
||
1214 |
printOn:aStream |
|
1215 |
"append a printed representation of the receiver to aStream" |
|
1216 |
||
1217 |
self printFormat:#(1 2 3 $- 2 1) on:aStream |
|
1218 |
||
1219 |
" |
|
1220 |
Date today printOn:Transcript |
|
1221 |
Date today printNL |
|
1222 |
" |
|
1223 |
||
1224 |
"Modified: 27.8.1995 / 01:01:49 / claus" |
|
1225 |
! |
|
1226 |
||
1227 |
storeOn:aStream |
|
1228 |
"append a representation to aStream, from which the receiver |
|
1229 |
can be reconstructed" |
|
1230 |
||
1231 |
aStream nextPutAll:'('; nextPutAll:'Date day:'. |
|
1232 |
self day printOn:aStream. |
|
1233 |
aStream nextPutAll:' month:'. |
|
1234 |
self month printOn:aStream. |
|
1235 |
aStream nextPutAll:' year:'. |
|
1236 |
self year printOn:aStream. |
|
1237 |
aStream nextPutAll:')' |
|
1238 |
||
1239 |
" |
|
1240 |
Date today storeOn:Transcript |
|
1241 |
Date today storeString |
|
1242 |
" |
|
1 | 1243 |
! ! |
699 | 1244 |
|
1245 |
!Date methodsFor:'private accessing'! |
|
1246 |
||
1247 |
dateEncoding |
|
1248 |
"the internal encoding is stricktly private, |
|
1249 |
and should not be used outside." |
|
1250 |
||
1251 |
^ dateEncoding |
|
1252 |
! |
|
1253 |
||
1254 |
dateEncoding:anInteger |
|
1255 |
"the internal encoding is stricktly private, |
|
1256 |
and should not be used outside." |
|
1257 |
||
1258 |
dateEncoding := anInteger |
|
1259 |
! |
|
1260 |
||
1261 |
fromOSTime:osTime |
|
1262 |
"set my dateEncoding from an OS time. |
|
1263 |
This somewhat clumsy implementation hides the OS's date representation |
|
1264 |
(i.e. makes this class independent of what the OS starts its time values with). |
|
1265 |
Dont use this method, the osTime representation is totally unportable." |
|
1266 |
||
1267 |
OperatingSystem computeDatePartsOf:osTime |
|
1501 | 1268 |
for:[:year :month :day | |
1269 |
dateEncoding := (((year * 100) + month) * 100) + day |
|
699 | 1270 |
] |
1501 | 1271 |
|
1272 |
"Modified: 1.7.1996 / 15:23:12 / cg" |
|
699 | 1273 |
! ! |
1274 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
1275 |
!Date class methodsFor:'documentation'! |
699 | 1276 |
|
1277 |
version |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
1278 |
^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.41 1999-02-22 20:38:36 cg Exp $' |
699 | 1279 |
! ! |
1280 |
Date initialize! |