author | Claus Gittinger <cg@exept.de> |
Thu, 23 Aug 2001 23:07:35 +0200 | |
changeset 5935 | f7e334a3fdbb |
parent 5858 | df08a7ccbdb2 |
child 6419 | 5cf00f5e656d |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5515 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
54 | 15 |
Magnitude subclass:#Date |
972 | 16 |
instanceVariableNames:'dateEncoding' |
17 |
classVariableNames:'DayNames MonthNames DayAbbrevs MonthAbbrevs EnvironmentChange' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-General' |
|
1 | 20 |
! |
21 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
22 |
!Date class methodsFor:'documentation'! |
54 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
54 | 38 |
documentation |
39 |
" |
|
92 | 40 |
Instances of Date represent dates as year, month and day encoded in the |
41 |
(private & hidden) instance dateEncoding. The value found there is |
|
42 |
year*100*100 + month*100 + day (which makes magnitude-like comparison of |
|
43 |
dates easy, but is not guaranteed for future versions). |
|
1228 | 44 |
Do not depend on the internal representation. |
54 | 45 |
|
92 | 46 |
The old representation used days since 1st Jan. 1901 internally - |
47 |
with the new implementation, it is possible to reasonably represent almost |
|
48 |
any Date. (which insurance companies will like, since they can now |
|
49 |
represent even very old peoples birthday :-) |
|
68 | 50 |
Notice: no correction for pre-Julian dates is done. |
54 | 51 |
|
92 | 52 |
The printed representation of dates is controlled by resource definitions - |
53 |
thus national variants are already supported (see file 'resources/Date.rs'). |
|
54 |
||
55 |
Compatibility notice: |
|
1228 | 56 |
due to some historic reasons, there are some methods found twice |
57 |
with different names in this class. The old ST/X methods will vanish in |
|
58 |
one of the next releases, and kept for a while to support existing |
|
59 |
applications (the info on how these methods should be named came |
|
60 |
somewhat late from the testers ..). |
|
92 | 61 |
|
1228 | 62 |
Please do not use methods marked as obsolete in their comment. |
92 | 63 |
|
64 |
Most useful methods: |
|
65 |
||
1228 | 66 |
Date today |
67 |
(Date today) addDays: |
|
68 |
(Date today) subtractDays: |
|
1295 | 69 |
|
70 |
[author:] |
|
71 |
Claus Gittinger |
|
72 |
||
73 |
[see also:] |
|
74 |
Time AbstractTime |
|
75 |
Filename |
|
76 |
OperatingSystem |
|
54 | 77 |
" |
78 |
! ! |
|
79 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
80 |
!Date class methodsFor:'instance creation'! |
699 | 81 |
|
82 |
fromDays:dayCount |
|
83 |
"return a new Date, given the day-number starting with 0 at 1.Jan 1901; |
|
84 |
(i.e. 'Date fromDays:0' returns 1st Jan. 1901). |
|
85 |
Date asDays is the reverse operation. |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
86 |
Added for GNU/ST-80 compatibility" |
699 | 87 |
|
88 |
|yr rest d| |
|
89 |
||
90 |
"approx. year" |
|
91 |
yr := (dayCount // 366) + 1901. |
|
92 |
rest := dayCount - (self yearAsDays:yr) + 1. "+1 for ST-80 compatibility" |
|
93 |
d := self daysInYear:yr. |
|
94 |
(rest > d) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
95 |
"adjust" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
96 |
yr := yr + 1. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
97 |
rest := rest - d. |
699 | 98 |
]. |
99 |
||
1501 | 100 |
^ self newDay:rest year:yr |
699 | 101 |
|
102 |
" |
|
103 |
Date fromDays:0 -> 1 jan 1901 |
|
104 |
Date fromDays:365 -> 1 jan 1902 |
|
105 |
Date fromDays:730 -> 1 jan 1903 |
|
106 |
Date fromDays:1095 -> 1 jan 1903 |
|
107 |
Date fromDays:1460 ->31 dec 1904 since 1904 was a leap year |
|
108 |
" |
|
109 |
||
1501 | 110 |
"Modified: 1.7.1996 / 14:24:25 / cg" |
699 | 111 |
! |
112 |
||
113 |
newDay:day month:month year:year |
|
114 |
"return a new Date, given the day, month and year. |
|
115 |
For your convenience, month may be either an integer |
|
116 |
or the months name as a string. |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
117 |
|
699 | 118 |
Year may be the actual year (such as 1890, 2001) or the number |
119 |
of years since 1900 (which is rubbish ST-80 compatibility: |
|
120 |
it will be totally useless in a few years ...). |
|
121 |
You better not use this short-year feature in your programs." |
|
122 |
||
123 |
|monthIndex ok yr| |
|
124 |
||
125 |
yr := year. |
|
126 |
yr < 100 ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
127 |
yr := yr + 1900. |
699 | 128 |
]. |
129 |
||
130 |
month isInteger ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
131 |
monthIndex := month |
699 | 132 |
] ifFalse:[ |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
133 |
monthIndex := self indexOfMonth:month |
699 | 134 |
]. |
135 |
(monthIndex == 2 and:[day == 29]) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
136 |
ok := self leapYear:yr |
699 | 137 |
] ifFalse:[ |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
138 |
ok := day <= (self daysInMonth:month forYear:yr) |
699 | 139 |
]. |
140 |
((day > 0) and:[ok]) ifTrue:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
141 |
^ self basicNew dateEncoding:(((yr * 100) + monthIndex) * 100) + day |
699 | 142 |
]. |
143 |
||
144 |
"this error is triggered if you try to create a date from an |
|
145 |
invalid year/month/day combination; |
|
146 |
Such as 29-feb-year, where year is no leap year |
|
147 |
" |
|
148 |
self error:'invalid date' |
|
149 |
||
150 |
" |
|
151 |
Date newDay:8 month:'may' year:1993 |
|
152 |
Date newDay:8 month:5 year:1994 |
|
153 |
Date newDay:29 month:'feb' year:1994 |
|
154 |
Date newDay:29 month:'feb' year:1993 |
|
155 |
Date newDay:28 month:'feb' year:5 |
|
156 |
Date newDay:28 month:'feb' year:95 |
|
157 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
158 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
159 |
"Modified: 19.4.1996 / 15:28:15 / cg" |
699 | 160 |
! |
161 |
||
162 |
newDay:dayInYear year:year |
|
163 |
"return a new Date, given the year and the day-in-year (starting at 1). |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
164 |
See also: Date today / Time now / AbsoluteTime now. |
699 | 165 |
ST-80 compatibility" |
166 |
||
167 |
|monthAndDay| |
|
168 |
||
169 |
(dayInYear between:1 and:365) ifFalse:[ |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
170 |
((dayInYear == 366) and:[self leapYear:year]) ifFalse:[ |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
171 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
172 |
this error is triggered, when you try to create a |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
173 |
day from an invalid day-in-year; |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
174 |
for example, 366 in a non-leap year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
175 |
I dont know, if ST-80 wraps to the next year(s) in this case. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
176 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
177 |
^ self error:'invalid date' |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
178 |
] |
699 | 179 |
]. |
180 |
monthAndDay := self monthAndDayFromDayInYear:dayInYear forYear:year. |
|
1501 | 181 |
^ self newDay:(monthAndDay at:2) month:(monthAndDay at:1) year:year |
699 | 182 |
|
183 |
" |
|
184 |
Date newDay:150 year:1994 |
|
185 |
Date newDay:1 year:1994 |
|
186 |
Date newDay:1 year:1901 |
|
187 |
Date newDay:1 year:1902 |
|
188 |
Date newDay:365 year:1992 |
|
189 |
Date newDay:366 year:1992 |
|
190 |
Date newDay:365 year:1994 |
|
191 |
Date newDay:366 year:1994 |
|
192 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
193 |
|
1501 | 194 |
"Modified: 1.7.1996 / 14:22:24 / cg" |
699 | 195 |
! |
196 |
||
197 |
readFrom:aStringOrStream onError:exceptionBlock |
|
198 |
"return a new Date, reading a printed representation from aStream. |
|
199 |
Notice, that this is not the storeString format and |
|
200 |
is different from the format expected by readFrom:. |
|
201 |
BUG: |
|
202 |
This method assumes american format (i.e. month-day-year) instead |
|
203 |
of the german/french and other day-month-year. |
|
4998 | 204 |
There ought to be a internationalized variant of this." |
699 | 205 |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
206 |
|newDate| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
207 |
|
972 | 208 |
ErrorSignal handle:[:ex | |
209 |
^ exceptionBlock value |
|
699 | 210 |
] do:[ |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
211 |
|str month day year| |
972 | 212 |
str := aStringOrStream readStream. |
699 | 213 |
|
972 | 214 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
215 |
(str peek isDigit) ifTrue:[ |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
216 |
day := Integer readFrom:str |
972 | 217 |
]. |
218 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
219 |
(str peek isLetter) ifTrue:[ |
|
220 |
month := str nextAlphaNumericWord. |
|
221 |
day isNil ifTrue:[ |
|
222 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
223 |
day := Integer readFrom:str. |
972 | 224 |
] |
225 |
] ifFalse:[ |
|
226 |
month := self nameOfMonth:day. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
227 |
day := Integer readFrom:str |
972 | 228 |
]. |
229 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
230 |
year := Integer readFrom:str. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
231 |
|
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
232 |
newDate := self newDay:day month:month year:year |
699 | 233 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
234 |
^ newDate |
699 | 235 |
|
236 |
" |
|
237 |
Date readFromString:'31 December 1992' |
|
4997 | 238 |
Date readFrom:'19:11:1999' |
239 |
Date readFromString:'December, 5 1992' |
|
240 |
Date readFromString:'3-jan-95' |
|
241 |
Date readFromString:'12/31/1992' |
|
242 |
Date readFromString:'15.4.1992' -> german; leads to an error |
|
243 |
Date readFromString:'10.4.1992' -> german; leads to a wrong date |
|
244 |
Date readFromString:'10.4.1992' onError:['wrong date'] |
|
245 |
Date readFromString:'32.4.1992' onError:['wrong date'] |
|
246 |
Date readFromString:'fooBar' onError:['wrong date'] |
|
247 |
Date readFromString:'10.4' onError:['wrong date'] |
|
248 |
Date readFromString:'10041999' onError:['wrong date'] |
|
249 |
" |
|
250 |
||
251 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
252 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
253 |
! |
|
254 |
||
255 |
readFrom:aStringOrStream printFormat:aFormatArray |
|
256 |
"return a new Date, reading a printed representation from aStream. |
|
257 |
1 day position (1, 2 or 3) |
|
258 |
2 month position (1..3) |
|
259 |
3 year position (1..3) |
|
260 |
" |
|
261 |
^ self readFrom:aStringOrStream |
|
262 |
printFormat:aFormatArray |
|
263 |
onError:[self error:'expected: ' , self name] |
|
264 |
||
265 |
" |
|
266 |
Date readFrom:'19:11:1999' printFormat:#( 1 2 3 ) |
|
267 |
Date readFrom:'19-nov-1999' printFormat:#( 1 2 3 ) |
|
268 |
Date readFrom:'19:11:1999' printFormat:#( 2 1 3 ) -> exception: wrong month |
|
269 |
Date readFrom:'5:12:1999' printFormat:#( 2 1 3 ) |
|
270 |
Date readFrom:'may-12-1999' printFormat:#( 2 1 3 ) |
|
271 |
Date readFrom:'1999 may 12' printFormat:#( 3 2 1 ) |
|
272 |
" |
|
273 |
||
274 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
275 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
276 |
! |
|
277 |
||
278 |
readFrom:aStringOrStream printFormat:aFormatArray onError:exceptionBlock |
|
279 |
"return a new Date, reading a printed representation from aStream. |
|
280 |
1 day position (1, 2 or 3) |
|
281 |
2 month position (1..3) |
|
282 |
3 year position (1..3) |
|
283 |
" |
|
284 |
|newDate| |
|
285 |
||
286 |
ErrorSignal handle:[:ex | |
|
287 |
^ exceptionBlock value |
|
288 |
] do:[ |
|
289 |
|str arg| |
|
290 |
||
291 |
str := aStringOrStream readStream. |
|
292 |
arg := Array new:3. |
|
293 |
||
294 |
1 to:3 do:[:i||v| |
|
295 |
[str peek isLetterOrDigit] whileFalse:[str next]. |
|
296 |
||
297 |
v := (str peek isDigit) ifTrue:[Integer readFrom:str] |
|
298 |
ifFalse:[str nextAlphaNumericWord]. |
|
299 |
arg at:i put:v |
|
300 |
]. |
|
301 |
newDate := self newDay:(arg at:(aFormatArray at:1)) |
|
302 |
month:(arg at:(aFormatArray at:2)) |
|
303 |
year:(arg at:(aFormatArray at:3)) |
|
304 |
]. |
|
305 |
^ newDate |
|
306 |
||
307 |
" |
|
308 |
Date readFromString:'31 December 1992' |
|
309 |
Date readFrom:'19:11:1999' |
|
699 | 310 |
Date readFromString:'December, 5 1992' |
311 |
Date readFromString:'3-jan-95' |
|
312 |
Date readFromString:'12/31/1992' |
|
313 |
Date readFromString:'15.4.1992' -> german; leads to an error |
|
314 |
Date readFromString:'10.4.1992' -> german; leads to a wrong date |
|
315 |
Date readFromString:'10.4.1992' onError:['wrong date'] |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
316 |
Date readFromString:'32.4.1992' onError:['wrong date'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
317 |
Date readFromString:'fooBar' onError:['wrong date'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3257
diff
changeset
|
318 |
Date readFromString:'10.4' onError:['wrong date'] |
4421 | 319 |
Date readFromString:'10041999' onError:['wrong date'] |
320 |
" |
|
321 |
||
322 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
323 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
|
324 |
! |
|
325 |
||
326 |
readMMDDYYYYFrom:aStringOrStream onError:exceptionBlock |
|
327 |
"return a new Date, reading a printed representation from aStream. |
|
328 |
Notice, that this is not the storeString format and |
|
4998 | 329 |
is different from the format expected by readFrom:" |
4421 | 330 |
|
331 |
|newDate| |
|
332 |
||
333 |
ErrorSignal handle:[:ex | |
|
334 |
^ exceptionBlock value |
|
335 |
] do:[ |
|
336 |
|str month day year| |
|
337 |
||
338 |
str := aStringOrStream readStream. |
|
339 |
month := str next:2. |
|
340 |
month := Integer readFrom:month. |
|
341 |
day := str next:2. |
|
342 |
day := Integer readFrom:day. |
|
343 |
year := str next:4. |
|
344 |
year := Integer readFrom:year. |
|
345 |
||
346 |
newDate := self newDay:day month:month year:year |
|
347 |
]. |
|
348 |
^ newDate |
|
349 |
||
350 |
" |
|
351 |
Date readMMDDYYYYFrom:'10041999' onError:['wrong date'] |
|
699 | 352 |
" |
353 |
||
354 |
"Created: 16.11.1995 / 22:50:17 / cg" |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1501
diff
changeset
|
355 |
"Modified: 8.10.1996 / 19:25:39 / cg" |
699 | 356 |
! |
357 |
||
358 |
today |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
359 |
"return a date, representing today. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
360 |
See also: Time now / AbsoluteTime now." |
699 | 361 |
|
1501 | 362 |
^ self fromOSTime:(OperatingSystem getOSTime) |
699 | 363 |
|
364 |
" |
|
365 |
Date today |
|
366 |
" |
|
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
367 |
|
1501 | 368 |
"Modified: 1.7.1996 / 15:20:16 / cg" |
5548 | 369 |
! |
370 |
||
371 |
tomorrow |
|
372 |
"return a date, representing tomorrow. |
|
373 |
See also: Time now / AbsoluteTime now." |
|
374 |
||
375 |
^ Date today addDays:1 |
|
376 |
||
377 |
" |
|
378 |
Date tomorrow |
|
379 |
Date tomorrow dayInWeek |
|
380 |
" |
|
381 |
! |
|
382 |
||
383 |
yesterday |
|
384 |
"return a date, representing yesterday. |
|
385 |
See also: Time now / AbsoluteTime now." |
|
386 |
||
387 |
^ Date today subtractDays:1 |
|
388 |
||
389 |
" |
|
390 |
Date yesterday |
|
391 |
Date yesterday dayInWeek |
|
392 |
" |
|
699 | 393 |
! ! |
394 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
395 |
!Date class methodsFor:'general queries'! |
699 | 396 |
|
397 |
abbreviatedNameOfDay:dayIndex |
|
398 |
"given a day index (1..7), return the abbreviated name |
|
399 |
of the day" |
|
400 |
||
5830 | 401 |
(DayAbbrevs isNil or:[EnvironmentChange]) ifTrue:[ |
402 |
self initNames |
|
699 | 403 |
]. |
404 |
^ DayAbbrevs at:dayIndex |
|
405 |
||
406 |
" |
|
407 |
Date abbreviatedNameOfDay:4 |
|
408 |
" |
|
409 |
! |
|
410 |
||
411 |
abbreviatedNameOfMonth:monthIndex |
|
412 |
"given a month index (1..12), return the abbreviated name |
|
413 |
of the month" |
|
414 |
||
5830 | 415 |
(MonthAbbrevs isNil or:[EnvironmentChange]) ifTrue:[ |
416 |
self initNames |
|
699 | 417 |
]. |
418 |
^ MonthAbbrevs at:monthIndex |
|
419 |
||
420 |
" |
|
421 |
Date abbreviatedNameOfMonth:11 |
|
422 |
Date abbreviatedNameOfMonth:12 |
|
423 |
" |
|
424 |
! |
|
425 |
||
426 |
dateAndTimeNow |
|
427 |
"return an array containing the date and time of now" |
|
428 |
||
429 |
^ Time dateAndTimeNow |
|
430 |
||
431 |
" |
|
432 |
Date dateAndTimeNow |
|
433 |
" |
|
434 |
! |
|
435 |
||
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
436 |
dayOfFirstWeekInYear:aYear |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
437 |
"for a given year, return the day corresponding to that years monday of week-01. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
438 |
The 1st week is the one, in which the first thursday is found; |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
439 |
and the 1st day of that week is the preceeding monday |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
440 |
(that means: that the returned day might be a day of the previous year)" |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
441 |
|
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
442 |
|day dayInWeekOf1stJan firstThursday firstDayInWeek| |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
443 |
|
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
444 |
"/ find the first thursday ... |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
445 |
day := AbsoluteTime newDay:1 year:aYear. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
446 |
dayInWeekOf1stJan := day dayInWeek. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
447 |
dayInWeekOf1stJan > 4 ifTrue:[ |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
448 |
firstThursday := day addDays:(4 - dayInWeekOf1stJan + 7). |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
449 |
] ifFalse:[ |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
450 |
firstThursday := day addDays:(4 - dayInWeekOf1stJan). |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
451 |
]. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
452 |
"/ back to the preceeding monday |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
453 |
firstDayInWeek := firstThursday subtractDays:3. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
454 |
^ firstDayInWeek |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
455 |
|
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
456 |
" |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
457 |
Date dayOfFirstWeekInYear:1998 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
458 |
Date dayOfFirstWeekInYear:1999 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
459 |
Date dayOfFirstWeekInYear:2000 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
460 |
Date dayOfFirstWeekInYear:2001 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
461 |
" |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
462 |
! |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
463 |
|
699 | 464 |
dayOfWeek:dayName |
465 |
"given the name of a day (either string or symbol), |
|
466 |
return the day-index (1 for monday; 7 for sunday). |
|
467 |
Return 0 for invalid day name" |
|
468 |
||
5830 | 469 |
(DayNames isNil or:[EnvironmentChange]) ifTrue:[ |
470 |
self initNames |
|
699 | 471 |
]. |
472 |
^ DayNames indexOf:dayName |
|
473 |
||
474 |
" |
|
475 |
Date dayOfWeek:'wednesday' |
|
476 |
" |
|
477 |
! |
|
478 |
||
479 |
daysInMonth:month forYear:yearInteger |
|
480 |
"given the name of a month and a year, return the number |
|
481 |
of days this month has (modified GNU). |
|
482 |
return 0 if the month name was invalid. |
|
483 |
For your convenience, month maybe an integer or name-string." |
|
484 |
||
485 |
|monthIndex "{ Class: SmallInteger }"| |
|
486 |
||
487 |
month isInteger ifTrue:[ |
|
488 |
monthIndex := month |
|
489 |
] ifFalse:[ |
|
490 |
monthIndex := self indexOfMonth:month |
|
491 |
]. |
|
492 |
||
493 |
^ self daysInMonthIndex:monthIndex forYear:yearInteger |
|
494 |
||
495 |
" |
|
496 |
Date daysInMonth:2 forYear:1980 |
|
497 |
Date daysInMonth:2 forYear:1981 |
|
498 |
Date daysInMonth:'feb' forYear:1981 |
|
499 |
" |
|
500 |
! |
|
501 |
||
502 |
daysInYear:yearInteger |
|
503 |
"return the number of days in a year" |
|
504 |
||
505 |
(self leapYear:yearInteger) ifTrue:[^ 366]. |
|
506 |
^ 365 |
|
507 |
||
508 |
" |
|
509 |
Date daysInYear:1900 |
|
510 |
Date daysInYear:1901 |
|
511 |
Date daysInYear:1904 |
|
512 |
Date daysInYear:1980 |
|
513 |
Date daysInYear:1981 |
|
514 |
" |
|
515 |
! |
|
516 |
||
517 |
daysUntilMonth:month forYear:yearInteger |
|
518 |
"given the name of a month and a year, return the number |
|
519 |
of days from 1st january to last of prev month of that year. |
|
520 |
Return 0 if the month name/index is invalid or is january. |
|
521 |
For your convenience, month maybe an integer or name-string." |
|
522 |
||
523 |
|monthIndex "{ Class: SmallInteger }" |
|
524 |
sumDays "{ Class: SmallInteger }" | |
|
525 |
||
526 |
month isInteger ifTrue:[ |
|
527 |
monthIndex := month |
|
528 |
] ifFalse:[ |
|
529 |
monthIndex := self indexOfMonth:month |
|
530 |
]. |
|
531 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
532 |
||
533 |
sumDays := 0. |
|
534 |
1 to:monthIndex-1 do:[:m | |
|
535 |
sumDays := sumDays + (self daysInMonthIndex:m forYear:yearInteger) |
|
536 |
]. |
|
537 |
^ sumDays |
|
538 |
||
539 |
" |
|
540 |
Date daysUntilMonth:'feb' forYear:1993 |
|
541 |
Date daysUntilMonth:'jan' forYear:1993 |
|
542 |
" |
|
543 |
! |
|
544 |
||
5778 | 545 |
firstDayOfYear:inYear |
546 |
"return the weekDay-index of the 1st-january of the given year. |
|
547 |
1->monday, 2->tuesday, ... 7->sunday" |
|
548 |
||
549 |
| t d | |
|
550 |
||
551 |
((inYear < 1) or:[ inYear > 9999]) ifTrue:[ |
|
552 |
^ 0. |
|
553 |
]. |
|
554 |
||
555 |
"/ normal Gregorian calendar: one extra day per four years |
|
556 |
t := 4 + inYear + ((inYear + 3) // 4). |
|
557 |
||
558 |
inYear > 1800 ifTrue:[ |
|
559 |
"/ Julian calendar: regular Gregorian less 3 days per 400 |
|
560 |
t := t - ((inYear - 1701) // 100) + ((inYear - 1601) // 400). |
|
561 |
]. |
|
562 |
inYear > 1752 ifTrue:[ |
|
563 |
"/ calendar changeover |
|
564 |
t := t + 3. |
|
565 |
]. |
|
566 |
d := t \\ 7. |
|
567 |
"/ convert from 0 (sunday) .. 6 (saturday) |
|
568 |
"/ to 1 (munday) .. 7 (sunday) |
|
569 |
d == 0 ifTrue:[^ 7]. |
|
570 |
^ d |
|
571 |
||
572 |
" |
|
573 |
self firstDayOfYear:2000 |
|
574 |
(Date newDay:1 year:2000) dayInWeek |
|
575 |
self firstDayOfYear:1999 |
|
576 |
(Date newDay:1 year:1999) dayInWeek |
|
577 |
self firstDayOfYear:1998 |
|
578 |
(Date newDay:1 year:1998) dayInWeek |
|
579 |
self firstDayOfYear:2001 |
|
580 |
(Date newDay:1 year:2001) dayInWeek |
|
581 |
self firstDayOfYear:2002 |
|
582 |
(Date newDay:1 year:2002) dayInWeek |
|
583 |
self firstDayOfYear:2006 |
|
584 |
(Date newDay:1 year:2006) dayInWeek |
|
585 |
||
586 |
self firstDayOfYear:1800 |
|
587 |
(Date newDay:1 year:1800) dayInWeek |
|
588 |
" |
|
589 |
! |
|
590 |
||
699 | 591 |
indexOfMonth:aMonthString |
592 |
"given the name of a month (either string or symbol), |
|
593 |
return the month-index (1 for jan; 12 for december). |
|
594 |
The given string may be a full or abbreviated name, |
|
595 |
case is ignored. |
|
596 |
Return 0 for invalid month name." |
|
597 |
||
598 |
|idx name| |
|
599 |
||
600 |
EnvironmentChange ifTrue:[ |
|
601 |
self initNames |
|
602 |
]. |
|
603 |
name := aMonthString asLowercase. |
|
604 |
idx := MonthAbbrevs indexOf:name. |
|
605 |
idx ~~ 0 ifTrue:[^ idx]. |
|
606 |
idx := MonthNames indexOf:name. |
|
607 |
idx ~~ 0 ifTrue:[^ idx]. |
|
608 |
||
609 |
name at:1 put:(name at:1) asUppercase. |
|
610 |
idx := MonthAbbrevs indexOf:name. |
|
611 |
idx ~~ 0 ifTrue:[^ idx]. |
|
612 |
idx := MonthNames indexOf:name. |
|
613 |
idx ~~ 0 ifTrue:[^ idx]. |
|
614 |
||
615 |
^ idx |
|
616 |
||
617 |
" |
|
618 |
Date indexOfMonth:'jan' |
|
619 |
Date indexOfMonth:'Jan' |
|
620 |
Date indexOfMonth:'December' |
|
621 |
" |
|
622 |
! |
|
623 |
||
624 |
isLeapYear:yearInteger |
|
625 |
"Return true, if a year is a leap year. |
|
626 |
Obsolete: |
|
627 |
Please use the ST-80 compatible #leapYear for new programs, |
|
628 |
since this method will vanish." |
|
629 |
||
630 |
^ self leapYear:yearInteger |
|
631 |
! |
|
632 |
||
633 |
leapYear:yearInteger |
|
634 |
"return true, if yearInteger is a leap year." |
|
635 |
||
636 |
|y "{ Class: SmallInteger }"| |
|
637 |
||
638 |
y := yearInteger. |
|
639 |
(y \\ 4 == 0) ifTrue:[ |
|
640 |
(y \\ 100 ~~ 0) ifTrue:[^ true]. |
|
641 |
(y \\ 400 == 0) ifTrue:[^ true] |
|
642 |
]. |
|
643 |
^ false |
|
644 |
||
645 |
" |
|
646 |
Date leapYear:1992 |
|
647 |
Date leapYear:1994 |
|
648 |
Date leapYear:1900 |
|
649 |
Date leapYear:2000 |
|
650 |
" |
|
651 |
! |
|
652 |
||
653 |
monthAndDayFromDayInYear:aDayInYear forYear:yearInteger |
|
654 |
"given a day-in-year (1..365) return an Array containing the |
|
655 |
month index and the day-in-month. Return nil if the argument is invalid." |
|
656 |
||
657 |
|restDays daysInMonth| |
|
658 |
||
659 |
restDays := aDayInYear. |
|
660 |
restDays < 1 ifTrue:[^ nil]. |
|
661 |
||
662 |
1 to:12 do:[:m | |
|
663 |
daysInMonth := self daysInMonthIndex:m forYear:yearInteger. |
|
664 |
restDays <= daysInMonth ifTrue:[ |
|
665 |
^ Array with:m with:restDays |
|
666 |
]. |
|
667 |
restDays := restDays - daysInMonth |
|
668 |
]. |
|
669 |
restDays > daysInMonth ifTrue:[^ nil]. |
|
670 |
^ Array with:12 with:restDays |
|
671 |
||
672 |
" |
|
673 |
Date monthAndDayFromDayInYear:66 forYear:1980 |
|
674 |
Date monthAndDayFromDayInYear:66 forYear:1981 |
|
675 |
" |
|
676 |
! |
|
677 |
||
678 |
nameOfDay:dayIndex |
|
679 |
"given a day index (1..7), return the name of the day" |
|
680 |
||
5830 | 681 |
(DayNames isNil or:[EnvironmentChange]) ifTrue:[ |
682 |
self initNames |
|
699 | 683 |
]. |
684 |
^ DayNames at:dayIndex |
|
685 |
||
686 |
" |
|
687 |
Date nameOfDay:4 |
|
688 |
" |
|
689 |
! |
|
690 |
||
691 |
nameOfMonth:monthIndex |
|
692 |
"given a month index (1..12), return the name of the month" |
|
693 |
||
5830 | 694 |
(MonthNames isNil or:[EnvironmentChange]) ifTrue:[ |
695 |
self initNames |
|
699 | 696 |
]. |
697 |
^ MonthNames at:monthIndex |
|
698 |
||
699 |
" |
|
700 |
Date nameOfMonth:11 |
|
701 |
Date nameOfMonth:12 |
|
702 |
Date nameOfMonth:4 |
|
703 |
" |
|
704 |
! |
|
705 |
||
5548 | 706 |
weekInYearOf:aDateOrTimestamp |
707 |
"for a given date or timeStamp, return the week-number. |
|
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
708 |
the returned week number starts with 1 for the first week which has a thursday in it. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
709 |
The above definition can lead to the 1st week starting in the old year!!" |
5548 | 710 |
|
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
711 |
|dayInYear numDays dayOfFirstWeekInYear dayOfFirstWeekInNextYear week| |
5548 | 712 |
|
713 |
dayInYear := aDateOrTimestamp dayInYear. |
|
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
714 |
dayOfFirstWeekInYear := self dayOfFirstWeekInYear:aDateOrTimestamp year. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
715 |
|
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
716 |
numDays := aDateOrTimestamp asDate subtractDate:dayOfFirstWeekInYear asDate. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
717 |
"/ careful with the first days - could be W52/W53 of the previous year ... |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
718 |
numDays < 0 ifTrue:[ |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
719 |
"/ not in a week here ... |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
720 |
"/ can be either 52 or 53, depending on the previous year. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
721 |
^ self weekInYearOf:(Date day:31 month:12 year:aDateOrTimestamp year - 1) |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
722 |
]. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
723 |
|
5548 | 724 |
"/ compute the week |
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
725 |
week := numDays // 7 + 1. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
726 |
|
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
727 |
"/ careful with the last days - could be W01 of the next year ... |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
728 |
week == 53 ifTrue:[ |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
729 |
dayOfFirstWeekInNextYear := self dayOfFirstWeekInYear:aDateOrTimestamp year + 1. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
730 |
aDateOrTimestamp asDate >= dayOfFirstWeekInNextYear asDate ifTrue:[ |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
731 |
^ 1 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
732 |
]. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
733 |
]. |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
734 |
^ week |
5548 | 735 |
|
736 |
" |
|
5663 | 737 |
AbsoluteTime now weekInYear |
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
738 |
Date weekInYearOf:(Date day:31 month:12 year:1999) was a friday; |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
739 |
Date weekInYearOf:(AbsoluteTime newDay:1 year:2000) was a saturday; therefore last week of 1999 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
740 |
Date weekInYearOf:(AbsoluteTime newDay:2 year:2000) was a sunday; therefore last week of 1999 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
741 |
Date weekInYearOf:(AbsoluteTime newDay:3 year:2000) was a monday -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
742 |
Date weekInYearOf:(AbsoluteTime newDay:4 year:2000) was a tuesday -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
743 |
Date weekInYearOf:(AbsoluteTime newDay:5 year:2000) was a wed -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
744 |
Date weekInYearOf:(AbsoluteTime newDay:6 year:2000) was a thursday -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
745 |
Date weekInYearOf:(AbsoluteTime newDay:7 year:2000) was a fri -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
746 |
Date weekInYearOf:(AbsoluteTime newDay:8 year:2000) was a sat -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
747 |
Date weekInYearOf:(AbsoluteTime newDay:9 year:2000) was a sun -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
748 |
Date weekInYearOf:(AbsoluteTime newDay:10 year:2000) was a monday -> W02 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
749 |
Date weekInYearOf:(AbsoluteTime newDay:16 year:2000) was a sunday -> W02 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
750 |
Date weekInYearOf:(AbsoluteTime newDay:17 year:2000) was a monday -> W03 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
751 |
Date weekInYearOf:(Date day:1 month:1 year:2001) |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
752 |
Date weekInYearOf:(Date day:2 month:1 year:2001) |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
753 |
Date weekInYearOf:(Date day:3 month:1 year:2001) |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
754 |
Date weekInYearOf:(Date day:24 month:12 year:2001) -> W52 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
755 |
Date weekInYearOf:(Date day:30 month:12 year:2001) -> W52 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
756 |
Date weekInYearOf:(Date day:31 month:12 year:2001) -> W01 |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
757 |
Date weekInYearOf:(Date day:1 month:1 year:2002) |
5778 | 758 |
Date weekInYearOf:(Date day:2 month:1 year:2002) |
759 |
Date weekInYearOf:(Date day:7 month:1 year:2002) |
|
5548 | 760 |
" |
761 |
! |
|
762 |
||
699 | 763 |
yearAsDays: yearInteger |
764 |
"Returns the number of days since Jan 1, 1901. (GNU) |
|
765 |
to the first Jan of the year, yearInteger. |
|
766 |
For 1901 this is zero, for 1902 its 365. |
|
767 |
Defined for years >= 1901" |
|
768 |
||
769 |
|y "{ Class: SmallInteger }"| |
|
770 |
||
771 |
y := yearInteger - 1900. |
|
772 |
y := y - 1. |
|
773 |
^ (y * 365) |
|
774 |
+ (y // 4) |
|
775 |
- (y // 100) |
|
776 |
+ ((y + 300) // 400) |
|
777 |
||
778 |
" |
|
779 |
Date yearAsDays:1901 |
|
780 |
Date yearAsDays:1902 |
|
781 |
Date yearAsDays:1903 |
|
782 |
Date yearAsDays:1904 |
|
783 |
Date yearAsDays:1905 |
|
784 |
Date yearAsDays:1994 |
|
785 |
(Date yearAsDays:2001) - (Date yearAsDays:2000) |
|
786 |
" |
|
787 |
! ! |
|
788 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
789 |
!Date class methodsFor:'handling language changes'! |
699 | 790 |
|
791 |
initialize |
|
792 |
"check for case where Resource-classes are absent" |
|
793 |
ResourcePack isNil ifTrue:[ |
|
794 |
self initNames |
|
795 |
] ifFalse:[ |
|
796 |
Smalltalk addDependent:self. |
|
797 |
EnvironmentChange := true |
|
798 |
] |
|
799 |
! |
|
800 |
||
1468 | 801 |
update:something with:aParameter from:changedObject |
699 | 802 |
((something == #Language) or:[something == #LanguageTerritory]) ifTrue:[ |
1468 | 803 |
"just remember change for next access" |
804 |
EnvironmentChange := true |
|
699 | 805 |
] |
1468 | 806 |
|
807 |
"Created: 15.6.1996 / 15:19:25 / cg" |
|
699 | 808 |
! ! |
809 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
810 |
!Date class methodsFor:'obsolete instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
811 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
812 |
day:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
813 |
"return a new Date, given the day, month and year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
814 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
815 |
use newDay:month:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
816 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
817 |
^ self newDay:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
818 |
! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
819 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
820 |
day:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
821 |
"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
|
822 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
823 |
use newDay:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
824 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
825 |
^ self newDay:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
826 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
827 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
828 |
!Date class methodsFor:'private'! |
1 | 829 |
|
699 | 830 |
daysInMonthIndex: monthIndex forYear: yearInteger |
831 |
"return the number of days in month monthIndex of |
|
832 |
year yearInteger (modified GNU). |
|
833 |
Return 0 for invalid month index. |
|
834 |
This is the internal version of daysInMonth:forYear:" |
|
835 |
||
836 |
|days| |
|
837 |
||
838 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
839 |
||
840 |
days := #(31 28 31 "Jan Feb Mar" |
|
841 |
30 31 30 "Apr May Jun" |
|
842 |
31 31 30 "Jul Aug Sep" |
|
843 |
31 30 31 "Oct Nov Dec" |
|
844 |
) at: monthIndex. |
|
845 |
||
846 |
(monthIndex == 2) ifTrue:[ |
|
847 |
(self leapYear:yearInteger) ifTrue:[ |
|
848 |
^ days + 1 |
|
849 |
] |
|
850 |
]. |
|
851 |
^ days |
|
852 |
||
853 |
" |
|
854 |
Date daysInMonthIndex:2 forYear:1994 |
|
855 |
Date daysInMonthIndex:2 forYear:1980 |
|
856 |
Date daysInMonthIndex:2 forYear:1981 |
|
857 |
" |
|
858 |
! |
|
859 |
||
1 | 860 |
initNames |
861 |
"read the language specific names" |
|
862 |
||
10 | 863 |
|resources| |
864 |
||
54 | 865 |
DayNames := #('monday' |
155 | 866 |
'tuesday' |
867 |
'wednesday' |
|
868 |
'thursday' |
|
869 |
'friday' |
|
870 |
'saturday' |
|
871 |
'sunday'). |
|
1 | 872 |
|
54 | 873 |
DayAbbrevs := #('mon' |
155 | 874 |
'tue' |
875 |
'wed' |
|
876 |
'thu' |
|
877 |
'fri' |
|
878 |
'sat' |
|
879 |
'sun'). |
|
1 | 880 |
|
54 | 881 |
MonthNames := #('january' |
155 | 882 |
'february' |
883 |
'march' |
|
884 |
'april' |
|
885 |
'may' |
|
886 |
'june' |
|
887 |
'july' |
|
888 |
'august' |
|
889 |
'september' |
|
890 |
'october' |
|
891 |
'november' |
|
892 |
'december'). |
|
1 | 893 |
|
54 | 894 |
MonthAbbrevs := #('jan' |
155 | 895 |
'feb' |
896 |
'mar' |
|
897 |
'apr' |
|
898 |
'may' |
|
899 |
'jun' |
|
900 |
'jul' |
|
901 |
'aug' |
|
902 |
'sep' |
|
903 |
'oct' |
|
904 |
'nov' |
|
905 |
'dec'). |
|
54 | 906 |
|
907 |
"check for case where Resource-classes are absent" |
|
908 |
ResourcePack notNil ifTrue:[ |
|
155 | 909 |
resources := ResourcePack for:self. |
54 | 910 |
|
155 | 911 |
DayNames := resources array:DayNames. |
912 |
DayAbbrevs := resources array:DayAbbrevs. |
|
913 |
MonthNames := resources array:MonthNames. |
|
914 |
MonthAbbrevs := resources array:MonthAbbrevs. |
|
54 | 915 |
]. |
1 | 916 |
|
29 | 917 |
EnvironmentChange := false |
10 | 918 |
|
919 |
"Date initNames" |
|
54 | 920 |
! ! |
921 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
922 |
!Date class methodsFor:'private encoding'! |
54 | 923 |
|
924 |
encodeYear:y month:m day:d |
|
92 | 925 |
"the internal encoding is stricktly private, |
926 |
and should not be used outside." |
|
927 |
||
54 | 928 |
^ (((y * 100) + m) * 100) + d |
929 |
! ! |
|
930 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
931 |
!Date class methodsFor:'private instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
932 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
933 |
fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
934 |
"return a date, representing the date given by the operatingSystem time. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
935 |
This somewhat clumsy implementation hides the OS's date representation |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
936 |
(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
|
937 |
Dont use this method, the osTime representation is totally unportable." |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
938 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
939 |
^ self basicNew fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
940 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
941 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
942 |
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
|
943 |
thats where Unix time starts |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
944 |
On other systems, it may be something different. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
945 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
946 |
Date fromOSTime:#(86400 0) -> on UNIX: the day after |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
947 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
948 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
949 |
|
5515 | 950 |
!Date methodsFor:'Compatibility - ST80'! |
951 |
||
952 |
shortPrintString |
|
953 |
"dummy - for now" |
|
954 |
||
955 |
^ self printString |
|
956 |
||
957 |
"Created: 20.6.1997 / 17:16:48 / cg" |
|
958 |
! ! |
|
959 |
||
5186 | 960 |
!Date methodsFor:'Compatibility - Squeak'! |
4407 | 961 |
|
962 |
mmddyyyy |
|
4462 | 963 |
"return a printed representation of the receiver in the |
964 |
form mmddyyyy. |
|
965 |
The receiver can be reconstructed with: |
|
966 |
Date readMMDDYYYYFrom:aStringOrStream onError:[...]" |
|
967 |
||
4407 | 968 |
^ (self month printStringLeftPaddedTo:2 with:$0) |
969 |
, (self day printStringLeftPaddedTo:2 with:$0) |
|
970 |
, (self year printStringLeftPaddedTo:4 with:$0) |
|
971 |
||
972 |
" |
|
973 |
Date today mmddyyyy |
|
974 |
" |
|
975 |
! ! |
|
976 |
||
699 | 977 |
!Date methodsFor:'accessing'! |
241 | 978 |
|
699 | 979 |
abbreviatedDayName |
980 |
"return the short week-day of the receiver as a string. |
|
981 |
The returned string depends on the language setting. |
|
982 |
Expect things like 'mon', 'tue' ..." |
|
54 | 983 |
|
699 | 984 |
^ self class abbreviatedNameOfDay:(self dayInWeek) |
92 | 985 |
|
986 |
" |
|
699 | 987 |
Date today abbreviatedDayName |
988 |
(Date day:15 month:4 year:1959) abbreviatedDayName |
|
92 | 989 |
" |
990 |
! |
|
991 |
||
699 | 992 |
abbreviatedMonthName |
993 |
"return the month of the receiver as a string. |
|
994 |
The returned string depends on the language setting. |
|
995 |
Expect things like 'jan', 'feb' ..." |
|
54 | 996 |
|
699 | 997 |
^ self class abbreviatedNameOfMonth:(self month) |
68 | 998 |
|
92 | 999 |
" |
699 | 1000 |
Date today abbreviatedMonthName |
1001 |
(Date day:15 month:4 year:1959) abbreviatedMonthName |
|
92 | 1002 |
" |
1003 |
! |
|
1004 |
||
1005 |
asDays |
|
1006 |
"return the number of days elapsed since 01-Jan-1901 |
|
1007 |
and the receiver's day; starts with 0 for 1-1-1901. |
|
1008 |
Date>>fromDays: is the reverse operation. |
|
1009 |
For ST-80 compatibility." |
|
68 | 1010 |
|
1011 |
|yr| |
|
1012 |
||
1013 |
yr := self year. |
|
1014 |
^ (self class yearAsDays:yr) |
|
1015 |
+ (self class daysUntilMonth:self month forYear:yr) |
|
1016 |
+ self day |
|
85 | 1017 |
- 1 |
68 | 1018 |
|
92 | 1019 |
" |
1020 |
(Date day: 5 month: 8 year: 1962) asDays -> should be 22496 |
|
1021 |
(Date day: 1 month: 1 year: 1901) asDays -> 0 |
|
1022 |
Date today asDays |
|
1023 |
Date fromDays:(Date today asDays + 7) |
|
1024 |
" |
|
77 | 1025 |
! |
1026 |
||
1027 |
asSeconds |
|
92 | 1028 |
"return the seconds between 1.jan.1901 and the same time in the receivers |
1029 |
day. (i.e. midnight to midnight). |
|
1501 | 1030 |
This does not include any leapSeconds ... strictly speaking, this is incorrect. |
77 | 1031 |
ST-80 compatibility." |
1032 |
||
92 | 1033 |
^ 60*60*24 * self asDays |
77 | 1034 |
|
92 | 1035 |
" |
1036 |
(Date day: 5 month: 8 year: 1962) asSeconds |
|
1037 |
(Date day: 1 month: 1 year: 1901) asSeconds |
|
1038 |
(Date today addDays:7) asSeconds - Date today asSeconds |
|
1501 | 1039 |
" |
1040 |
||
1041 |
"Modified: 1.7.1996 / 14:19:17 / cg" |
|
699 | 1042 |
! |
1043 |
||
1044 |
day |
|
1045 |
"return the day (1..31) of the receiver" |
|
1046 |
||
1047 |
^ dateEncoding \\ 100 |
|
1048 |
||
1049 |
" |
|
1050 |
Date today day |
|
1051 |
" |
|
1052 |
! |
|
1053 |
||
1054 |
dayCount |
|
1055 |
"return the number of days since 1st. Jan. 1901; |
|
1056 |
starting with 0 for this date. |
|
1057 |
Date>>fromDays: is the reverse operation. |
|
1058 |
Obsolete: |
|
1059 |
please use asDays for ST-80 compatibility" |
|
1060 |
||
1061 |
^ self asDays. |
|
1062 |
||
1063 |
" |
|
1064 |
(Date day:1 month:1 year:1901) dayCount |
|
1065 |
Date fromDays:(Date day:1 month:1 year:1994) dayCount |
|
1066 |
Date today dayCount |
|
1067 |
" |
|
1068 |
! |
|
1069 |
||
1070 |
dayInWeek |
|
5778 | 1071 |
"return the week-day of the receiver - 1 for monday, 7 for sunday. |
1072 |
WARNING: does not care for pre-julian dates |
|
1073 |
(i.e. do not use this for dates before 1752) |
|
1074 |
" |
|
699 | 1075 |
|
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1076 |
|wday| |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1077 |
|
5778 | 1078 |
wday := (1 "knowing, that 1st Jan 1901 was a tuesday" |
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1079 |
+ self asDays) \\ 7 + 1. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1080 |
wday < 1 ifTrue:[ |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1081 |
wday := wday + 7 |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1082 |
]. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
1083 |
^ wday |
699 | 1084 |
|
1085 |
" |
|
5778 | 1086 |
Date today dayInWeek |
5548 | 1087 |
Date tomorrow dayInWeek |
1088 |
Date yesterday dayInWeek |
|
5778 | 1089 |
(Date today subtractDays:2) dayInWeek |
3257 | 1090 |
Date today weekday |
5548 | 1091 |
Date tomorrow weekday |
1092 |
Date yesterday weekday |
|
1501 | 1093 |
(Date day:15 month:4 year:1959) dayInWeek |
1094 |
(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
|
1095 |
(Date day:31 month:12 year:1899) dayInWeek |
699 | 1096 |
" |
1501 | 1097 |
|
3257 | 1098 |
"Modified: / 1.2.1998 / 14:10:27 / cg" |
699 | 1099 |
! |
1100 |
||
5548 | 1101 |
dayInYear |
1102 |
"return the day-nr within the year of the receiver - 1 .. 365/366" |
|
1103 |
||
1104 |
^ self asAbsoluteTime dayInYear |
|
1105 |
||
1106 |
" |
|
1107 |
Date today dayInYear |
|
1108 |
(Date newDay:1 year:1999) dayInYear |
|
1109 |
(Date newDay:1 year:2000) dayInYear |
|
1110 |
(Date newDay:2 year:2000) dayInYear |
|
1111 |
" |
|
1112 |
! |
|
1113 |
||
699 | 1114 |
dayName |
1115 |
"return the week-day of the receiver as a string. |
|
1116 |
The returned string depends on the language setting. |
|
1117 |
Expect things like 'monday', 'tuesday' ... |
|
1118 |
Obsolete: |
|
1119 |
use #weekday for ST-80 compatibility" |
|
1120 |
||
1121 |
^ self weekday |
|
1122 |
! |
|
77 | 1123 |
|
1124 |
dayOfMonth |
|
1125 |
"Answer the day of the month represented by me. |
|
1126 |
Same as day; for ST-80 compatibility." |
|
1127 |
||
1128 |
^ self day |
|
92 | 1129 |
|
1130 |
" |
|
1131 |
Date today dayOfMonth |
|
1132 |
" |
|
1133 |
! |
|
1134 |
||
1135 |
daysInMonth |
|
1136 |
"return the number of days in the month of the receiver" |
|
1137 |
||
1138 |
^ Date daysInMonth:(self month) forYear:(self year) |
|
1139 |
||
1140 |
" |
|
1141 |
Date today daysInMonth |
|
1142 |
" |
|
1143 |
! |
|
1144 |
||
1145 |
daysInYear |
|
1146 |
"return the number of days in the year of the receiver" |
|
1147 |
||
1148 |
^ Date daysInYear:(self year) |
|
1149 |
||
1150 |
" |
|
1151 |
Date today daysInYear |
|
1152 |
" |
|
1153 |
! |
|
1154 |
||
5778 | 1155 |
daysLeftInMonth |
1156 |
"return the number of days left in the month of the receiver" |
|
1157 |
||
1158 |
^ self daysInMonth - self day |
|
1159 |
||
1160 |
" |
|
1161 |
Date today daysLeftInMonth |
|
1162 |
" |
|
1163 |
! |
|
1164 |
||
92 | 1165 |
daysLeftInYear |
1166 |
"return the number of days left in the year of the receiver" |
|
1167 |
||
5282 | 1168 |
^ (Date daysInYear:(self year)) - self day |
92 | 1169 |
|
1170 |
" |
|
5282 | 1171 |
Date today daysLeftInYear |
92 | 1172 |
" |
77 | 1173 |
! |
1174 |
||
699 | 1175 |
leap |
1176 |
"return true, if the receivers year is a leap year" |
|
68 | 1177 |
|
699 | 1178 |
^ Date leapYear:(self year) |
68 | 1179 |
|
92 | 1180 |
" |
699 | 1181 |
Date today leap |
1182 |
(Date day:1 month:1 year:1992) leap |
|
92 | 1183 |
" |
68 | 1184 |
! |
1185 |
||
699 | 1186 |
month |
1187 |
"return the month (1..12) of the receiver" |
|
92 | 1188 |
|
699 | 1189 |
^ (dateEncoding // 100) \\ 100 |
68 | 1190 |
|
92 | 1191 |
" |
699 | 1192 |
Date today month |
92 | 1193 |
" |
68 | 1194 |
! |
1195 |
||
77 | 1196 |
monthIndex |
1197 |
"return the index of the month (e.g. Feb.=2). |
|
1198 |
Same as month; for ST-80 compatibility." |
|
1199 |
||
1200 |
^ self month |
|
1201 |
! |
|
1202 |
||
68 | 1203 |
monthName |
1204 |
"return the month of the receiver as a string. |
|
1205 |
The returned string depends on the language setting. |
|
1206 |
Expect things like 'january', 'february' ..." |
|
1207 |
||
1208 |
^ self class nameOfMonth:(self month) |
|
1209 |
||
92 | 1210 |
" |
1211 |
Date today monthName |
|
1212 |
(Date day:15 month:4 year:1959) monthName |
|
1213 |
" |
|
68 | 1214 |
! |
1215 |
||
5548 | 1216 |
weekInYear |
1217 |
"return the week number of the receiver - 1 for Jan, 1st. |
|
1218 |
each week is Mon .. Sun (i.e. Sunday belongs to the previous week)" |
|
1219 |
||
1220 |
^ self class weekInYearOf:self |
|
1221 |
||
1222 |
" |
|
1223 |
Date today weekInYear |
|
1224 |
(Date newDay:1 year:2000) weekInYear was a saturday |
|
1225 |
(Date newDay:2 year:2000) weekInYear was a sunday |
|
1226 |
(Date newDay:3 year:2000) weekInYear was a monday |
|
1227 |
(Date newDay:4 year:2000) weekInYear was a tuesday |
|
1228 |
(Date newDay:5 year:2000) weekInYear was a wed |
|
1229 |
(Date newDay:6 year:2000) weekInYear was a thursday |
|
1230 |
(Date newDay:7 year:2000) weekInYear was a fri |
|
1231 |
(Date newDay:8 year:2000) weekInYear was a sat |
|
1232 |
(Date newDay:9 year:2000) weekInYear was a sun |
|
1233 |
(Date newDay:10 year:2000) weekInYear was a monday |
|
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
1234 |
(Date day:3 month:1 year:2001) weekInYear |
5548 | 1235 |
" |
1236 |
! |
|
1237 |
||
699 | 1238 |
weekday |
1239 |
"return the week-day of the receiver as a string. |
|
68 | 1240 |
The returned string depends on the language setting. |
699 | 1241 |
Expect things like 'monday', 'tuesday' ... |
1242 |
For ST-80 compatibility" |
|
1243 |
||
1244 |
^ self class nameOfDay:(self dayInWeek) |
|
1245 |
||
1246 |
" |
|
1247 |
Date today weekday |
|
1248 |
(Date day:15 month:4 year:1959) weekday |
|
1249 |
" |
|
1250 |
! |
|
1251 |
||
1252 |
year |
|
1253 |
"return the year (1..12) of the receiver" |
|
1254 |
||
1255 |
^ dateEncoding // (100*100) |
|
68 | 1256 |
|
699 | 1257 |
" |
1258 |
Date today year |
|
1259 |
" |
|
1260 |
! ! |
|
1261 |
||
1262 |
!Date methodsFor:'arithmetic'! |
|
1263 |
||
1264 |
addDays:days |
|
1265 |
"return a new date representing 'days' after the receiver. |
|
1266 |
The argument should be some kind of integer. |
|
1267 |
For ST-80 compatibility." |
|
1268 |
||
1269 |
^ self class fromDays:(self asDays + days) |
|
1270 |
||
1271 |
" |
|
1272 |
Date today addDays:7 |
|
1273 |
" |
|
1274 |
! |
|
1275 |
||
1276 |
daysUntil:aDate |
|
1277 |
"return the number of days between the receiver and the argument, |
|
1278 |
aDate, whuch should be some kind of date" |
|
1279 |
||
1280 |
^ aDate asDays - self asDays |
|
68 | 1281 |
|
92 | 1282 |
" |
699 | 1283 |
(Date day:24 month:12 year:1994) daysUntil:(Date day:1 month:1 year:1995) |
1284 |
(Date day:1 month:2 year:1992) daysUntil:(Date day:1 month:3 year:1992) |
|
1285 |
(Date day:1 month:2 year:1994) daysUntil:(Date day:1 month:3 year:1994) |
|
1286 |
||
1287 |
|delta| |
|
1288 |
delta := Date today |
|
1289 |
daysUntil:(Date day:25 month:12 year:Date today year). |
|
1290 |
Transcript show:'still '; |
|
1291 |
show:delta ; |
|
1422 | 1292 |
showCR:' days till xmas' |
699 | 1293 |
" |
1294 |
! |
|
1295 |
||
1296 |
minusDays:days |
|
1297 |
"return a new date representing 'days' before the receiver. |
|
1298 |
The argument should be some kind of integer. |
|
1299 |
Obsolete: |
|
1300 |
Please dont use this method since it will vanish. |
|
1301 |
Use #subtractDays: instead for ST-80 compatibility." |
|
1302 |
||
1303 |
^ self subtractDays:days |
|
1304 |
! |
|
1305 |
||
1306 |
plusDays:days |
|
1307 |
"return a new date representing 'days' after the receiver. |
|
1308 |
The argument should be some kind of integer. |
|
1309 |
Obsolete: |
|
1310 |
Please dont use this method since it will vanish. |
|
1311 |
Use #addDays: instead for ST-80 compatibility." |
|
1312 |
||
1313 |
^ self addDays:days |
|
1314 |
! |
|
1315 |
||
1316 |
subtractDate:aDate |
|
1317 |
"return the number of days between the receiver and aDate" |
|
1318 |
||
1319 |
^ self asDays - aDate asDays |
|
1320 |
||
1321 |
" |
|
1322 |
(Date day:1 month:1 year:1995) subtractDate:(Date day:24 month:12 year:1994) |
|
1323 |
(Date day:1 month:3 year:1992) subtractDate:(Date day:1 month:2 year:1992) |
|
1324 |
(Date day:1 month:3 year:1994) subtractDate:(Date day:1 month:2 year:1994) |
|
1325 |
" |
|
1326 |
! |
|
1327 |
||
1328 |
subtractDays:days |
|
1329 |
"return a new date representing 'days' before the receiver. |
|
1330 |
The argument should be some kind of integer. |
|
1331 |
For ST-80 compatibility" |
|
1332 |
||
1333 |
^ self class fromDays:(self asDays - days) |
|
1334 |
||
1335 |
" |
|
1336 |
Date today subtractDays:7 |
|
92 | 1337 |
" |
1 | 1338 |
! ! |
1339 |
||
1340 |
!Date methodsFor:'comparing'! |
|
1341 |
||
29 | 1342 |
< aDate |
1343 |
"return true, if the date represented by the receiver |
|
1344 |
is before the argument, aDate" |
|
1345 |
||
54 | 1346 |
(aDate isMemberOf:Date) ifTrue:[ |
155 | 1347 |
^ dateEncoding < aDate dateEncoding |
54 | 1348 |
]. |
29 | 1349 |
|
54 | 1350 |
"the argument must understand year, month and day to be |
1351 |
comparable, whatever it is" |
|
1352 |
||
1353 |
^ dateEncoding < (Date encodeYear:aDate year |
|
155 | 1354 |
month:aDate month |
1355 |
day:aDate day) |
|
68 | 1356 |
|
1357 |
"Date today < (Date day:24 month:12 year:2000)" |
|
1358 |
"Date today < (Date day:24 month:12 year:1900)" |
|
29 | 1359 |
! |
1 | 1360 |
|
1361 |
= aDate |
|
1362 |
"return true, if the date represented by the receiver |
|
1363 |
is the same as the one represented by argument, aDate" |
|
1364 |
||
155 | 1365 |
(aDate isMemberOf:Date) ifFalse:[^ false]. |
1366 |
||
1367 |
^ dateEncoding = aDate dateEncoding |
|
54 | 1368 |
|
1369 |
"the argument must understand year, month and day to be |
|
1370 |
comparable, whatever it is" |
|
1371 |
||
155 | 1372 |
" |
54 | 1373 |
^ dateEncoding = (Date encodeYear:aDate year |
155 | 1374 |
month:aDate month |
1375 |
day:aDate day) |
|
1376 |
" |
|
68 | 1377 |
"Date today = ((Date today plusDays:7) minusDays:7)" |
92 | 1378 |
! |
1379 |
||
699 | 1380 |
> aDate |
1381 |
"return true, if the date represented by the receiver |
|
1382 |
is after the argument, aDate" |
|
1383 |
||
1384 |
(aDate isMemberOf:Date) ifTrue:[ |
|
1385 |
^ dateEncoding > aDate dateEncoding |
|
1386 |
]. |
|
1387 |
||
1388 |
"the argument must understand year, month and day to be |
|
1389 |
comparable, whatever it is" |
|
1390 |
||
1391 |
^ dateEncoding > (Date encodeYear:aDate year |
|
1392 |
month:aDate month |
|
1393 |
day:aDate day) |
|
1394 |
||
1395 |
"Date today > (Date day:24 month:12 year:2000)" |
|
1396 |
"Date today > (Date day:24 month:12 year:1900)" |
|
1397 |
! |
|
1398 |
||
92 | 1399 |
hash |
1400 |
"return an integer useful for hashing on dates" |
|
1401 |
||
1402 |
^ dateEncoding |
|
1 | 1403 |
! ! |
1404 |
||
1501 | 1405 |
!Date methodsFor:'converting'! |
1406 |
||
1407 |
asAbsoluteTime |
|
5679 | 1408 |
"return an absoluteTime instance, representing midnight of last night" |
1501 | 1409 |
|
4379 | 1410 |
^ AbsoluteTime year:(self year) month:(self month) day:(self day) |
1411 |
hour:0 minute:0 second:0 millisecond:0 |
|
1501 | 1412 |
|
1413 |
" |
|
1414 |
Date today asAbsoluteTime |
|
1415 |
" |
|
1416 |
||
4379 | 1417 |
"Modified: / 1.7.1996 / 14:53:32 / cg" |
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
1418 |
"Modified: / 13.7.1999 / 12:31:47 / stefan" |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
1419 |
! |
4408 | 1420 |
|
1421 |
asDate |
|
1422 |
"return the receiver" |
|
1423 |
||
1424 |
^ self |
|
5679 | 1425 |
! |
1426 |
||
1427 |
asTimestamp |
|
1428 |
"return an absoluteTime instance, representing midnight of last night" |
|
1429 |
||
1430 |
^ self asAbsoluteTime |
|
1431 |
||
1432 |
" |
|
1433 |
Date today asTimestamp |
|
5775
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
1434 |
" |
92b02a4a7e83
week in year - again (based upon thursday-rule)
Claus Gittinger <cg@exept.de>
parents:
5679
diff
changeset
|
1435 |
! ! |
1501 | 1436 |
|
54 | 1437 |
!Date methodsFor:'printing & storing'! |
1 | 1438 |
|
5778 | 1439 |
addPrintBindingsTo:aDictionary |
1440 |
"add bindings for printing to aDictionary." |
|
1441 |
||
5827
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1442 |
|d ds ds0 m ms ms0 y w mn dn ws ws0 dw| |
5778 | 1443 |
|
1444 |
d := self day. |
|
1445 |
ds := d printString. |
|
1446 |
ds0 := d printStringLeftPaddedTo:2 with:$0. |
|
1447 |
||
1448 |
m := self month. |
|
1449 |
ms := m printString. |
|
1450 |
ms0 := m printStringLeftPaddedTo:2 with:$0. |
|
1451 |
||
1452 |
y := self year. |
|
1453 |
w := self weekInYear. |
|
1454 |
ws := w printString. |
|
1455 |
ws0 := w printStringLeftPaddedTo:2 with:$0. |
|
1456 |
mn := self monthName. |
|
1457 |
dn := self dayName. |
|
1458 |
||
5827
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1459 |
aDictionary at:#day put:ds0. "/ 0-padded |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1460 |
aDictionary at:#Day put:ds. "/ unpadded |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1461 |
aDictionary at:#month put:ms0. "/ 0-padded |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1462 |
aDictionary at:#Month put:ms. "/ unpadded |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1463 |
aDictionary at:#mon put:ms0. "/ 0-padded - for backward compatibility only |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1464 |
aDictionary at:#Mon put:ms. "/ unpadded - for backward compatibility only |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1465 |
aDictionary at:#year put:y. "/ padded |
5778 | 1466 |
aDictionary at:#week put:ws0. |
1467 |
aDictionary at:#Week put:ws. |
|
1468 |
||
1469 |
aDictionary at:$d put:ds0. |
|
1470 |
aDictionary at:$D put:ds. |
|
1471 |
aDictionary at:$m put:ms0. |
|
1472 |
aDictionary at:$M put:ms. |
|
1473 |
aDictionary at:$y put:y. |
|
1474 |
aDictionary at:$Y put:((y \\ 100)printStringLeftPaddedTo:2 with:$0). |
|
1475 |
aDictionary at:$w put:ws0. |
|
1476 |
aDictionary at:$W put:ws. |
|
1477 |
||
1478 |
aDictionary at:#monthName put:mn. |
|
1479 |
aDictionary at:#MonthName put:mn asUppercaseFirst. |
|
1480 |
aDictionary at:#MONTHNAME put:mn asUppercase. |
|
1481 |
||
1482 |
aDictionary at:#dayName put:dn. |
|
1483 |
aDictionary at:#DayName put:dn asUppercaseFirst. |
|
1484 |
aDictionary at:#DAYNAME put:dn asUppercase. |
|
1485 |
||
5827
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1486 |
aDictionary at:#weekDay put:(dw := self dayInWeek). |
71a6e54e02e5
same format-codes in printFormat as AbsoluteTIme
Claus Gittinger <cg@exept.de>
parents:
5778
diff
changeset
|
1487 |
aDictionary at:#dayInWeek put:dw. "/ for backward compatibility only |
5778 | 1488 |
aDictionary at:#shortDayName put:(dn := self abbreviatedDayName). |
1489 |
aDictionary at:#ShortDayName put:dn asUppercaseFirst. |
|
1490 |
aDictionary at:#SHORTDAYNAME put:dn asUppercase. |
|
1491 |
||
1492 |
aDictionary at:#shortMonthName put:(mn := self abbreviatedMonthName). |
|
1493 |
aDictionary at:#ShortMonthName put:mn asUppercaseFirst. |
|
1494 |
aDictionary at:#SHORTMONTHNAME put:mn asUppercase. |
|
1495 |
||
1496 |
aDictionary at:#nth put:(#('th' 'st' 'nd' 'rd' 'th' 'th' 'th' 'th' 'th' 'th') at:d \\ 10 + 1). |
|
1497 |
"/ 0 1 2 3 4 5 6 7 8 9 |
|
1498 |
! |
|
1499 |
||
408 | 1500 |
printFormat:aFormatArray |
5778 | 1501 |
"Obsolete, backward ST-80 compatible formatted printing - see printStringFormat: |
1502 |
Return a string containing a printed representation of the receiver. |
|
408 | 1503 |
The formatArray argument consists of 6 or 7 integers which control |
1504 |
the resulting string. The entries are: |
|
1505 |
1 day position (1, 2 or 3) |
|
1506 |
2 month position (1..3) |
|
1507 |
3 year position (1..3) |
|
1508 |
4 asciiValue of separator character or separator character |
|
4413 | 1509 |
or collection of 2 separator characters |
408 | 1510 |
5 month format (1: numeric; 2: abbreviated name; 3: fullname |
4413 | 1511 |
4: abbreviated uppercase 5: full uppercase) |
408 | 1512 |
6 year format (1 include century; 2 exclude century) |
4413 | 1513 |
7 (optional) true/false. |
1514 |
if true, print numbers in 2-digit format |
|
1515 |
(i.e. with leading zeros); |
|
1516 |
Taken as false, if ommited |
|
408 | 1517 |
|
1518 |
Day and monthnames are in the currently active language. |
|
1519 |
||
1520 |
This method supports more options than the ST-80 version; month formats |
|
1521 |
4 and 5, non-numeric separators and the optional 7th parameter are not |
|
1522 |
supported by ST-80. Be aware of this for compatibility reasons. |
|
1523 |
||
4413 | 1524 |
Notice that not all formats can be scanned (correctly) by #readFrom: |
1525 |
This is an ST-80 compatibility method - I would have choosen another |
|
1526 |
(less cryptic) formatString ... |
|
1527 |
" |
|
408 | 1528 |
|
1529 |
|mf upperCase day month year components sep1 sep2 leadingZeros| |
|
1530 |
||
1531 |
aFormatArray size > 6 ifTrue:[ |
|
4413 | 1532 |
leadingZeros := aFormatArray at:7 |
408 | 1533 |
] ifFalse:[ |
4413 | 1534 |
leadingZeros := false. |
408 | 1535 |
]. |
1536 |
||
1537 |
components := Array new:3. |
|
1538 |
day := self day. |
|
1539 |
leadingZeros ifTrue:[ |
|
4413 | 1540 |
day := day printStringLeftPaddedTo:2 with:$0 |
408 | 1541 |
] ifFalse:[ |
4413 | 1542 |
day := day printString |
408 | 1543 |
]. |
1544 |
components at:(aFormatArray at:1) put:day. |
|
1545 |
||
1546 |
upperCase := false. |
|
1547 |
mf := aFormatArray at:5. |
|
1548 |
(mf between:4 and:5) ifTrue:[mf := mf - 2. upperCase := true]. |
|
1549 |
||
1550 |
mf == 1 ifTrue:[ |
|
4413 | 1551 |
month := self month. |
1552 |
leadingZeros ifTrue:[ |
|
1553 |
month := month printStringLeftPaddedTo:2 with:$0 |
|
1554 |
] ifFalse:[ |
|
1555 |
month := month printString |
|
1556 |
] |
|
408 | 1557 |
] ifFalse:[ |
4413 | 1558 |
mf == 2 ifTrue:[ |
1559 |
month := self abbreviatedMonthName |
|
1560 |
] ifFalse:[ |
|
1561 |
month := self monthName |
|
1562 |
] |
|
408 | 1563 |
]. |
1564 |
upperCase ifTrue:[month := month asUppercase]. |
|
1565 |
components at:(aFormatArray at:2) put:month. |
|
1566 |
||
1567 |
year := self year. |
|
1568 |
(aFormatArray at:6) == 2 ifTrue:[ |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1569 |
year := year \\ 100. |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1570 |
year := year printStringLeftPaddedTo:2 with:$0 |
408 | 1571 |
]. |
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1572 |
year := year printString. |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1573 |
components at:(aFormatArray at:3) put:year. |
408 | 1574 |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1575 |
sep1 := sep2 := (aFormatArray at:4). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1576 |
sep1 isString ifFalse:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1577 |
sep1 isCollection ifTrue:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1578 |
sep2 := (sep1 at:2). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1579 |
sep1 := (sep1 at:1). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1580 |
] ifFalse:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1581 |
sep1 := sep2 := sep1 asCharacter |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1582 |
] |
408 | 1583 |
]. |
1584 |
||
1585 |
^ (components at:1) |
|
1586 |
, sep1 asString |
|
1587 |
, (components at:2) |
|
1588 |
, sep2 asString |
|
1589 |
, (components at:3) |
|
1590 |
||
1591 |
" |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1592 |
Date today printFormat:#(1 2 3 '' 1 2) |
4413 | 1593 |
Date today printFormat:#(1 2 3 $- 1 2) |
1594 |
Date today printFormat:#(1 2 3 $. 1 2 true) |
|
1595 |
Date today printFormat:#(2 1 3 32 3 1) |
|
408 | 1596 |
Date today printFormat:#(2 1 3 #(' ' ', ') 3 1) |
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1597 |
Date today printFormat:#(1 2 3 $- 2 1) |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1598 |
Date today printFormat:#(1 2 3 $- 4 1) |
408 | 1599 |
" |
1600 |
! |
|
1601 |
||
1602 |
printFormat:aFormatArray on:aStream |
|
1603 |
"append a printed representation of the receiver to aStream. |
|
1604 |
The argument, aFormatString controls the format, as described |
|
1605 |
in the #printFormat: method. |
|
1606 |
Notice that not all formats can be scanned (correctly) by #readFrom:" |
|
1607 |
||
1608 |
aStream nextPutAll:(self printFormat:aFormatArray) |
|
1609 |
||
1610 |
||
1611 |
"european formats: |
|
1612 |
||
1613 |
Date today printFormat:#(1 2 3 $- 1 2) on:Transcript. Transcript cr. |
|
1614 |
Date today printFormat:#(1 2 3 $- 2 2) on:Transcript. Transcript cr. |
|
1615 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1616 |
Date today printFormat:#(1 2 3 $- 3 2) on:Transcript. Transcript cr. |
|
1617 |
Date today printFormat:#(1 2 3 $. 1 2) on:Transcript. Transcript cr. |
|
1618 |
Date today printFormat:#(1 2 3 $. 1 2 true) on:Transcript. Transcript cr. |
|
1619 |
||
1620 |
US formats: |
|
1621 |
||
1622 |
Date today printFormat:#(2 1 3 32 3 1) on:Transcript. Transcript cr. |
|
1623 |
Date today printFormat:#(2 1 3 32 2 2) on:Transcript. Transcript cr. |
|
1624 |
Date today printFormat:#(2 1 3 $/ 1 2) on:Transcript. Transcript cr. |
|
1625 |
Date today printFormat:#(2 1 3 $- 2 1) on:Transcript. Transcript cr. |
|
1626 |
Date today printFormat:#(2 1 3 #(' ' ', ') 2 1) on:Transcript. Transcript cr. |
|
1627 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1628 |
Date today printFormat:#(1 2 3 $- 4 1) on:Transcript. Transcript cr. |
|
1629 |
" |
|
699 | 1630 |
! |
1631 |
||
1632 |
printOn:aStream |
|
1633 |
"append a printed representation of the receiver to aStream" |
|
1634 |
||
1635 |
self printFormat:#(1 2 3 $- 2 1) on:aStream |
|
1636 |
||
1637 |
" |
|
1638 |
Date today printOn:Transcript |
|
1639 |
Date today printNL |
|
1640 |
" |
|
1641 |
||
1642 |
"Modified: 27.8.1995 / 01:01:49 / claus" |
|
1643 |
! |
|
1644 |
||
5778 | 1645 |
printOn:aStream format:aFormatString |
1646 |
"print using a format string; |
|
1647 |
valid format items are: |
|
1648 |
%d day, 01..31 0-padded to length 2 |
|
1649 |
%m month, 01..12 0-padded to length 2 |
|
1650 |
%w week in year, 00..53 0-padded to length 2 |
|
1651 |
%y year, full |
|
1652 |
||
1653 |
special: |
|
1654 |
%D day - unpadded |
|
1655 |
%M month - unpadded |
|
1656 |
%W week in year - unpadded |
|
1657 |
%Y year, last 2 digits only (danger) |
|
1658 |
||
1659 |
%weekDay - day in week (1->monday, 2->tuesday, ... ,7->sunday) |
|
1660 |
||
1661 |
%dayName - full day name |
|
1662 |
%DayName - full day name, first character uppercase |
|
1663 |
%DAYNAME - full day name, all uppercase |
|
1664 |
||
1665 |
%monthName - full month name |
|
1666 |
%MonthName - full month name, first character uppercase |
|
1667 |
%MONTHNAME - full month name, all uppercase |
|
1668 |
||
1669 |
%shortDayName - short (abbreviated) day name |
|
1670 |
%ShortDayName - short (abbreviated) day name, first character uppercase |
|
1671 |
%SHORTDAYNAME - short (abbreviated) day name, all uppercase |
|
1672 |
||
1673 |
%shortMonthName - short (abbreviated) month name |
|
1674 |
%ShortMonthName - short (abbreviated) month name, first character uppercase |
|
1675 |
%SHORTMONTHNAME - short (abbreviated) month name, all uppercase |
|
1676 |
||
1677 |
%nth - counting day-in-month (1->'st'; 2->'nd'; 3->'rd'; 4...->'th') |
|
1678 |
" |
|
1679 |
||
1680 |
aStream nextPutAll:(self printStringFormat:aFormatString) |
|
1681 |
||
1682 |
" |
|
1683 |
Date today printOn:Transcript format:'%y%m%d' (iso format) |
|
1684 |
Date today printOn:Transcript format:'%y-%m-%d' (iso format) |
|
1685 |
Date today printOn:Transcript format:'%y-W%w' (iso format - working week) |
|
1686 |
Date today printOn:Transcript format:'%d-%m-%y' (european trivia format) |
|
1687 |
Date today printOn:Transcript format:'%m/%d/%y' (us trivia format) |
|
1688 |
Date today printOn:Transcript format:'%D-%(monthName)-%y' (us trivia format) |
|
1689 |
Date today printOn:Transcript format:'%D-%(MonthName)-%y' (us trivia format) |
|
1690 |
Date today printOn:Transcript format:'%D-%(MONTHNAME)-%y' (us trivia format) |
|
1691 |
Date today printOn:Transcript format:'%(DayName), %D%(nth) of %(MonthName), %y' |
|
1692 |
Date today printOn:Transcript format:'%(ShortDayName), %D-%(ShortMonthName)-%y' |
|
1693 |
Date today printOn:Transcript format:'%d%m%Y' (millenium bug format - danger) |
|
1694 |
Date today printOn:Transcript format:'Today is the %(weekDay) day of the week' |
|
1695 |
" |
|
1696 |
! |
|
1697 |
||
1698 |
printStringFormat:aFormatString |
|
1699 |
"print using a format string; |
|
1700 |
valid format items are: |
|
1701 |
%d day, 01..31 0-padded to length 2 |
|
1702 |
%m month, 01..12 0-padded to length 2 |
|
1703 |
%w week in year, 00..53 0-padded to length 2 |
|
1704 |
%y year, full |
|
1705 |
||
1706 |
special: |
|
1707 |
%D day - unpadded |
|
1708 |
%M month - unpadded |
|
1709 |
%W week in year - unpadded |
|
1710 |
%Y year, last 2 digits only (danger) |
|
1711 |
||
1712 |
%weekDay - day in week (1->monday, 2->tuesday, ... ,7->sunday) |
|
1713 |
||
1714 |
%dayName - full day name |
|
1715 |
%DayName - full day name, first character uppercase |
|
1716 |
%DAYNAME - full day name, all uppercase |
|
1717 |
||
1718 |
%monthName - full month name |
|
1719 |
%MonthName - full month name, first character uppercase |
|
1720 |
%MONTHNAME - full month name, all uppercase |
|
1721 |
||
1722 |
%shortDayName - short (abbreviated) day name |
|
1723 |
%ShortDayName - short (abbreviated) day name, first character uppercase |
|
1724 |
%SHORTDAYNAME - short (abbreviated) day name, all uppercase |
|
1725 |
||
1726 |
%shortMonthName - short (abbreviated) month name |
|
1727 |
%ShortMonthName - short (abbreviated) month name, first character uppercase |
|
1728 |
%SHORTMONTHNAME - short (abbreviated) month name, all uppercase |
|
1729 |
||
1730 |
%nth - counting day-in-month (1->'st'; 2->'nd'; 3->'rd'; 4...->'th') |
|
1731 |
" |
|
1732 |
||
1733 |
|dict| |
|
1734 |
||
1735 |
dict := IdentityDictionary new. |
|
1736 |
self addPrintBindingsTo:dict. |
|
1737 |
||
1738 |
^ (aFormatString expandPlaceholdersWith:dict) |
|
1739 |
||
1740 |
" |
|
1741 |
Date today printStringFormat:'%y%m%d' (iso format) |
|
1742 |
Date today printStringFormat:'%y-%m-%d' (iso format) |
|
1743 |
Date today printStringFormat:'%y-W%w' (iso format - working week) |
|
1744 |
Date today printStringFormat:'%d-%m-%y' (european trivia format) |
|
1745 |
Date today printStringFormat:'%m/%d/%y' (us trivia format) |
|
1746 |
Date today printStringFormat:'%D-%(monthName)-%y' (us trivia format) |
|
1747 |
Date today printStringFormat:'%D-%(MonthName)-%y' (us trivia format) |
|
1748 |
Date today printStringFormat:'%D-%(MONTHNAME)-%y' (us trivia format) |
|
1749 |
Date today printStringFormat:'%(DayName), %D%(nth) of %(MonthName), %y' |
|
1750 |
Date today printStringFormat:'%(ShortDayName), %D-%(ShortMonthName)-%y' |
|
1751 |
Date today printStringFormat:'%d%m%Y' (millenium bug format - danger) |
|
1752 |
Date today printStringFormat:'Today is the %(weekDay) day of the week' |
|
1753 |
" |
|
1754 |
! |
|
1755 |
||
699 | 1756 |
storeOn:aStream |
1757 |
"append a representation to aStream, from which the receiver |
|
1758 |
can be reconstructed" |
|
1759 |
||
1760 |
aStream nextPutAll:'('; nextPutAll:'Date day:'. |
|
1761 |
self day printOn:aStream. |
|
1762 |
aStream nextPutAll:' month:'. |
|
1763 |
self month printOn:aStream. |
|
1764 |
aStream nextPutAll:' year:'. |
|
1765 |
self year printOn:aStream. |
|
1766 |
aStream nextPutAll:')' |
|
1767 |
||
1768 |
" |
|
1769 |
Date today storeOn:Transcript |
|
1770 |
Date today storeString |
|
1771 |
" |
|
1 | 1772 |
! ! |
699 | 1773 |
|
1774 |
!Date methodsFor:'private accessing'! |
|
1775 |
||
1776 |
dateEncoding |
|
1777 |
"the internal encoding is stricktly private, |
|
1778 |
and should not be used outside." |
|
1779 |
||
1780 |
^ dateEncoding |
|
1781 |
! |
|
1782 |
||
1783 |
dateEncoding:anInteger |
|
1784 |
"the internal encoding is stricktly private, |
|
1785 |
and should not be used outside." |
|
1786 |
||
1787 |
dateEncoding := anInteger |
|
1788 |
! |
|
1789 |
||
1790 |
fromOSTime:osTime |
|
1791 |
"set my dateEncoding from an OS time. |
|
1792 |
This somewhat clumsy implementation hides the OS's date representation |
|
1793 |
(i.e. makes this class independent of what the OS starts its time values with). |
|
1794 |
Dont use this method, the osTime representation is totally unportable." |
|
1795 |
||
5858 | 1796 |
|v y m d| |
1797 |
||
1798 |
v := OperatingSystem computeTimeAndDateFrom:osTime. |
|
1799 |
y := v at:1. |
|
1800 |
m := v at:2. |
|
1801 |
d := v at:3. |
|
1802 |
"/ OperatingSystem computeDatePartsOf:osTime |
|
1803 |
"/ for:[:year :month :day | |
|
1804 |
"/ y := year. |
|
1805 |
"/ m := month. |
|
1806 |
"/ d := day. |
|
1807 |
"/ ]. |
|
1808 |
dateEncoding := (((y * 100) + m) * 100) + d |
|
1501 | 1809 |
|
1810 |
"Modified: 1.7.1996 / 15:23:12 / cg" |
|
699 | 1811 |
! ! |
1812 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
1813 |
!Date class methodsFor:'documentation'! |
699 | 1814 |
|
1815 |
version |
|
5858 | 1816 |
^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.61 2001-05-08 14:56:32 cg Exp $' |
699 | 1817 |
! ! |
1818 |
Date initialize! |