author | Claus Gittinger <cg@exept.de> |
Wed, 25 Oct 2000 12:21:57 +0200 | |
changeset 5679 | fd5cf73dabe4 |
parent 5663 | 32a8acdf3e3b |
child 5775 | 92b02a4a7e83 |
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 |
||
401 |
EnvironmentChange ifTrue:[ |
|
402 |
self initNames |
|
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 |
||
415 |
EnvironmentChange ifTrue:[ |
|
416 |
self initNames |
|
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 |
||
436 |
dayOfWeek:dayName |
|
437 |
"given the name of a day (either string or symbol), |
|
438 |
return the day-index (1 for monday; 7 for sunday). |
|
439 |
Return 0 for invalid day name" |
|
440 |
||
441 |
EnvironmentChange ifTrue:[ |
|
442 |
self initNames |
|
443 |
]. |
|
444 |
^ DayNames indexOf:dayName |
|
445 |
||
446 |
" |
|
447 |
Date dayOfWeek:'wednesday' |
|
448 |
" |
|
449 |
! |
|
450 |
||
451 |
daysInMonth:month forYear:yearInteger |
|
452 |
"given the name of a month and a year, return the number |
|
453 |
of days this month has (modified GNU). |
|
454 |
return 0 if the month name was invalid. |
|
455 |
For your convenience, month maybe an integer or name-string." |
|
456 |
||
457 |
|monthIndex "{ Class: SmallInteger }"| |
|
458 |
||
459 |
month isInteger ifTrue:[ |
|
460 |
monthIndex := month |
|
461 |
] ifFalse:[ |
|
462 |
monthIndex := self indexOfMonth:month |
|
463 |
]. |
|
464 |
||
465 |
^ self daysInMonthIndex:monthIndex forYear:yearInteger |
|
466 |
||
467 |
" |
|
468 |
Date daysInMonth:2 forYear:1980 |
|
469 |
Date daysInMonth:2 forYear:1981 |
|
470 |
Date daysInMonth:'feb' forYear:1981 |
|
471 |
" |
|
472 |
! |
|
473 |
||
474 |
daysInYear:yearInteger |
|
475 |
"return the number of days in a year" |
|
476 |
||
477 |
(self leapYear:yearInteger) ifTrue:[^ 366]. |
|
478 |
^ 365 |
|
479 |
||
480 |
" |
|
481 |
Date daysInYear:1900 |
|
482 |
Date daysInYear:1901 |
|
483 |
Date daysInYear:1904 |
|
484 |
Date daysInYear:1980 |
|
485 |
Date daysInYear:1981 |
|
486 |
" |
|
487 |
! |
|
488 |
||
489 |
daysUntilMonth:month forYear:yearInteger |
|
490 |
"given the name of a month and a year, return the number |
|
491 |
of days from 1st january to last of prev month of that year. |
|
492 |
Return 0 if the month name/index is invalid or is january. |
|
493 |
For your convenience, month maybe an integer or name-string." |
|
494 |
||
495 |
|monthIndex "{ Class: SmallInteger }" |
|
496 |
sumDays "{ Class: SmallInteger }" | |
|
497 |
||
498 |
month isInteger ifTrue:[ |
|
499 |
monthIndex := month |
|
500 |
] ifFalse:[ |
|
501 |
monthIndex := self indexOfMonth:month |
|
502 |
]. |
|
503 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
504 |
||
505 |
sumDays := 0. |
|
506 |
1 to:monthIndex-1 do:[:m | |
|
507 |
sumDays := sumDays + (self daysInMonthIndex:m forYear:yearInteger) |
|
508 |
]. |
|
509 |
^ sumDays |
|
510 |
||
511 |
" |
|
512 |
Date daysUntilMonth:'feb' forYear:1993 |
|
513 |
Date daysUntilMonth:'jan' forYear:1993 |
|
514 |
" |
|
515 |
! |
|
516 |
||
517 |
indexOfMonth:aMonthString |
|
518 |
"given the name of a month (either string or symbol), |
|
519 |
return the month-index (1 for jan; 12 for december). |
|
520 |
The given string may be a full or abbreviated name, |
|
521 |
case is ignored. |
|
522 |
Return 0 for invalid month name." |
|
523 |
||
524 |
|idx name| |
|
525 |
||
526 |
EnvironmentChange ifTrue:[ |
|
527 |
self initNames |
|
528 |
]. |
|
529 |
name := aMonthString asLowercase. |
|
530 |
idx := MonthAbbrevs indexOf:name. |
|
531 |
idx ~~ 0 ifTrue:[^ idx]. |
|
532 |
idx := MonthNames indexOf:name. |
|
533 |
idx ~~ 0 ifTrue:[^ idx]. |
|
534 |
||
535 |
name at:1 put:(name at:1) asUppercase. |
|
536 |
idx := MonthAbbrevs indexOf:name. |
|
537 |
idx ~~ 0 ifTrue:[^ idx]. |
|
538 |
idx := MonthNames indexOf:name. |
|
539 |
idx ~~ 0 ifTrue:[^ idx]. |
|
540 |
||
541 |
^ idx |
|
542 |
||
543 |
" |
|
544 |
Date indexOfMonth:'jan' |
|
545 |
Date indexOfMonth:'Jan' |
|
546 |
Date indexOfMonth:'December' |
|
547 |
" |
|
548 |
! |
|
549 |
||
550 |
isLeapYear:yearInteger |
|
551 |
"Return true, if a year is a leap year. |
|
552 |
Obsolete: |
|
553 |
Please use the ST-80 compatible #leapYear for new programs, |
|
554 |
since this method will vanish." |
|
555 |
||
556 |
^ self leapYear:yearInteger |
|
557 |
! |
|
558 |
||
559 |
leapYear:yearInteger |
|
560 |
"return true, if yearInteger is a leap year." |
|
561 |
||
562 |
|y "{ Class: SmallInteger }"| |
|
563 |
||
564 |
y := yearInteger. |
|
565 |
(y \\ 4 == 0) ifTrue:[ |
|
566 |
(y \\ 100 ~~ 0) ifTrue:[^ true]. |
|
567 |
(y \\ 400 == 0) ifTrue:[^ true] |
|
568 |
]. |
|
569 |
^ false |
|
570 |
||
571 |
" |
|
572 |
Date leapYear:1992 |
|
573 |
Date leapYear:1994 |
|
574 |
Date leapYear:1900 |
|
575 |
Date leapYear:2000 |
|
576 |
" |
|
577 |
! |
|
578 |
||
579 |
monthAndDayFromDayInYear:aDayInYear forYear:yearInteger |
|
580 |
"given a day-in-year (1..365) return an Array containing the |
|
581 |
month index and the day-in-month. Return nil if the argument is invalid." |
|
582 |
||
583 |
|restDays daysInMonth| |
|
584 |
||
585 |
restDays := aDayInYear. |
|
586 |
restDays < 1 ifTrue:[^ nil]. |
|
587 |
||
588 |
1 to:12 do:[:m | |
|
589 |
daysInMonth := self daysInMonthIndex:m forYear:yearInteger. |
|
590 |
restDays <= daysInMonth ifTrue:[ |
|
591 |
^ Array with:m with:restDays |
|
592 |
]. |
|
593 |
restDays := restDays - daysInMonth |
|
594 |
]. |
|
595 |
restDays > daysInMonth ifTrue:[^ nil]. |
|
596 |
^ Array with:12 with:restDays |
|
597 |
||
598 |
" |
|
599 |
Date monthAndDayFromDayInYear:66 forYear:1980 |
|
600 |
Date monthAndDayFromDayInYear:66 forYear:1981 |
|
601 |
" |
|
602 |
! |
|
603 |
||
604 |
nameOfDay:dayIndex |
|
605 |
"given a day index (1..7), return the name of the day" |
|
606 |
||
607 |
EnvironmentChange ifTrue:[ |
|
608 |
self initNames |
|
609 |
]. |
|
610 |
^ DayNames at:dayIndex |
|
611 |
||
612 |
" |
|
613 |
Date nameOfDay:4 |
|
614 |
" |
|
615 |
! |
|
616 |
||
617 |
nameOfMonth:monthIndex |
|
618 |
"given a month index (1..12), return the name of the month" |
|
619 |
||
620 |
EnvironmentChange ifTrue:[ |
|
621 |
self initNames |
|
622 |
]. |
|
623 |
^ MonthNames at:monthIndex |
|
624 |
||
625 |
" |
|
626 |
Date nameOfMonth:11 |
|
627 |
Date nameOfMonth:12 |
|
628 |
Date nameOfMonth:4 |
|
629 |
" |
|
630 |
! |
|
631 |
||
5548 | 632 |
weekInYearOf:aDateOrTimestamp |
633 |
"for a given date or timeStamp, return the week-number. |
|
5663 | 634 |
the returned week number starts with 1 for the first monday per year. |
635 |
(Sunday is treated as the last day of the week and belongs to the previous week). |
|
636 |
The days up to the first monday belong to the 52th week of the previous year." |
|
5548 | 637 |
|
638 |
|dayInYear dayInWeekOf1stJan daysIn1stWeek week| |
|
639 |
||
640 |
dayInYear := aDateOrTimestamp dayInYear. |
|
641 |
"/ what weekDay is 1st January of that year ? |
|
642 |
dayInWeekOf1stJan := (AbsoluteTime newDay:1 year:(aDateOrTimestamp year)) dayInWeek. "/ 1 for monday |
|
643 |
"/ how many days left in that week ? |
|
644 |
daysIn1stWeek := 7 - dayInWeekOf1stJan + 1. |
|
5663 | 645 |
dayInYear <= daysIn1stWeek ifTrue:[^ 52]. |
5548 | 646 |
"/ compute the week |
647 |
week := (dayInYear - daysIn1stWeek - 1) // 7. |
|
5663 | 648 |
^ week + 1 |
5548 | 649 |
|
650 |
" |
|
5663 | 651 |
AbsoluteTime now weekInYear |
5548 | 652 |
Date weekInYearOf:(AbsoluteTime newDay:1 year:2000) was a saturday |
653 |
Date weekInYearOf:(AbsoluteTime newDay:2 year:2000) was a sunday |
|
654 |
Date weekInYearOf:(AbsoluteTime newDay:3 year:2000) was a monday |
|
655 |
Date weekInYearOf:(AbsoluteTime newDay:4 year:2000) was a tuesday |
|
656 |
Date weekInYearOf:(AbsoluteTime newDay:5 year:2000) was a wed |
|
657 |
Date weekInYearOf:(AbsoluteTime newDay:6 year:2000) was a thursday |
|
658 |
Date weekInYearOf:(AbsoluteTime newDay:7 year:2000) was a fri |
|
659 |
Date weekInYearOf:(AbsoluteTime newDay:8 year:2000) was a sat |
|
660 |
Date weekInYearOf:(AbsoluteTime newDay:9 year:2000) was a sun |
|
661 |
Date weekInYearOf:(AbsoluteTime newDay:10 year:2000) was a monday |
|
5663 | 662 |
Date weekInYearOf:(AbsoluteTime newDay:16 year:2000) was a sunday |
663 |
Date weekInYearOf:(AbsoluteTime newDay:17 year:2000) was a monday |
|
5548 | 664 |
" |
665 |
! |
|
666 |
||
699 | 667 |
yearAsDays: yearInteger |
668 |
"Returns the number of days since Jan 1, 1901. (GNU) |
|
669 |
to the first Jan of the year, yearInteger. |
|
670 |
For 1901 this is zero, for 1902 its 365. |
|
671 |
Defined for years >= 1901" |
|
672 |
||
673 |
|y "{ Class: SmallInteger }"| |
|
674 |
||
675 |
y := yearInteger - 1900. |
|
676 |
y := y - 1. |
|
677 |
^ (y * 365) |
|
678 |
+ (y // 4) |
|
679 |
- (y // 100) |
|
680 |
+ ((y + 300) // 400) |
|
681 |
||
682 |
" |
|
683 |
Date yearAsDays:1901 |
|
684 |
Date yearAsDays:1902 |
|
685 |
Date yearAsDays:1903 |
|
686 |
Date yearAsDays:1904 |
|
687 |
Date yearAsDays:1905 |
|
688 |
Date yearAsDays:1994 |
|
689 |
(Date yearAsDays:2001) - (Date yearAsDays:2000) |
|
690 |
" |
|
691 |
! ! |
|
692 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
693 |
!Date class methodsFor:'handling language changes'! |
699 | 694 |
|
695 |
initialize |
|
696 |
"check for case where Resource-classes are absent" |
|
697 |
ResourcePack isNil ifTrue:[ |
|
698 |
self initNames |
|
699 |
] ifFalse:[ |
|
700 |
Smalltalk addDependent:self. |
|
701 |
EnvironmentChange := true |
|
702 |
] |
|
703 |
! |
|
704 |
||
1468 | 705 |
update:something with:aParameter from:changedObject |
699 | 706 |
((something == #Language) or:[something == #LanguageTerritory]) ifTrue:[ |
1468 | 707 |
"just remember change for next access" |
708 |
EnvironmentChange := true |
|
699 | 709 |
] |
1468 | 710 |
|
711 |
"Created: 15.6.1996 / 15:19:25 / cg" |
|
699 | 712 |
! ! |
713 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
714 |
!Date class methodsFor:'obsolete instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
715 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
716 |
day:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
717 |
"return a new Date, given the day, month and year. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
718 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
719 |
use newDay:month:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
720 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
721 |
^ self newDay:day month:month year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
722 |
! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
723 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
724 |
day:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
725 |
"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
|
726 |
Obsolete: |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
727 |
use newDay:year: for ST-80 compatibility" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
728 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
729 |
^ self newDay:dayInYear year:year |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
730 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
731 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
732 |
!Date class methodsFor:'private'! |
1 | 733 |
|
699 | 734 |
daysInMonthIndex: monthIndex forYear: yearInteger |
735 |
"return the number of days in month monthIndex of |
|
736 |
year yearInteger (modified GNU). |
|
737 |
Return 0 for invalid month index. |
|
738 |
This is the internal version of daysInMonth:forYear:" |
|
739 |
||
740 |
|days| |
|
741 |
||
742 |
(monthIndex between:1 and:12) ifFalse:[^ 0]. |
|
743 |
||
744 |
days := #(31 28 31 "Jan Feb Mar" |
|
745 |
30 31 30 "Apr May Jun" |
|
746 |
31 31 30 "Jul Aug Sep" |
|
747 |
31 30 31 "Oct Nov Dec" |
|
748 |
) at: monthIndex. |
|
749 |
||
750 |
(monthIndex == 2) ifTrue:[ |
|
751 |
(self leapYear:yearInteger) ifTrue:[ |
|
752 |
^ days + 1 |
|
753 |
] |
|
754 |
]. |
|
755 |
^ days |
|
756 |
||
757 |
" |
|
758 |
Date daysInMonthIndex:2 forYear:1994 |
|
759 |
Date daysInMonthIndex:2 forYear:1980 |
|
760 |
Date daysInMonthIndex:2 forYear:1981 |
|
761 |
" |
|
762 |
! |
|
763 |
||
1 | 764 |
initNames |
765 |
"read the language specific names" |
|
766 |
||
10 | 767 |
|resources| |
768 |
||
54 | 769 |
DayNames := #('monday' |
155 | 770 |
'tuesday' |
771 |
'wednesday' |
|
772 |
'thursday' |
|
773 |
'friday' |
|
774 |
'saturday' |
|
775 |
'sunday'). |
|
1 | 776 |
|
54 | 777 |
DayAbbrevs := #('mon' |
155 | 778 |
'tue' |
779 |
'wed' |
|
780 |
'thu' |
|
781 |
'fri' |
|
782 |
'sat' |
|
783 |
'sun'). |
|
1 | 784 |
|
54 | 785 |
MonthNames := #('january' |
155 | 786 |
'february' |
787 |
'march' |
|
788 |
'april' |
|
789 |
'may' |
|
790 |
'june' |
|
791 |
'july' |
|
792 |
'august' |
|
793 |
'september' |
|
794 |
'october' |
|
795 |
'november' |
|
796 |
'december'). |
|
1 | 797 |
|
54 | 798 |
MonthAbbrevs := #('jan' |
155 | 799 |
'feb' |
800 |
'mar' |
|
801 |
'apr' |
|
802 |
'may' |
|
803 |
'jun' |
|
804 |
'jul' |
|
805 |
'aug' |
|
806 |
'sep' |
|
807 |
'oct' |
|
808 |
'nov' |
|
809 |
'dec'). |
|
54 | 810 |
|
811 |
"check for case where Resource-classes are absent" |
|
812 |
ResourcePack notNil ifTrue:[ |
|
155 | 813 |
resources := ResourcePack for:self. |
54 | 814 |
|
155 | 815 |
DayNames := resources array:DayNames. |
816 |
DayAbbrevs := resources array:DayAbbrevs. |
|
817 |
MonthNames := resources array:MonthNames. |
|
818 |
MonthAbbrevs := resources array:MonthAbbrevs. |
|
54 | 819 |
]. |
1 | 820 |
|
29 | 821 |
EnvironmentChange := false |
10 | 822 |
|
823 |
"Date initNames" |
|
54 | 824 |
! ! |
825 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
826 |
!Date class methodsFor:'private encoding'! |
54 | 827 |
|
828 |
encodeYear:y month:m day:d |
|
92 | 829 |
"the internal encoding is stricktly private, |
830 |
and should not be used outside." |
|
831 |
||
54 | 832 |
^ (((y * 100) + m) * 100) + d |
833 |
! ! |
|
834 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
835 |
!Date class methodsFor:'private instance creation'! |
1227
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
836 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
837 |
fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
838 |
"return a date, representing the date given by the operatingSystem time. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
839 |
This somewhat clumsy implementation hides the OS's date representation |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
840 |
(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
|
841 |
Dont use this method, the osTime representation is totally unportable." |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
842 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
843 |
^ self basicNew fromOSTime:osTime |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
844 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
845 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
846 |
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
|
847 |
thats where Unix time starts |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
848 |
On other systems, it may be something different. |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
849 |
|
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
850 |
Date fromOSTime:#(86400 0) -> on UNIX: the day after |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
851 |
" |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
852 |
! ! |
e89b39909085
commentary & category changes
Claus Gittinger <cg@exept.de>
parents:
972
diff
changeset
|
853 |
|
5515 | 854 |
!Date methodsFor:'Compatibility - ST80'! |
855 |
||
856 |
shortPrintString |
|
857 |
"dummy - for now" |
|
858 |
||
859 |
^ self printString |
|
860 |
||
861 |
"Created: 20.6.1997 / 17:16:48 / cg" |
|
862 |
! ! |
|
863 |
||
5186 | 864 |
!Date methodsFor:'Compatibility - Squeak'! |
4407 | 865 |
|
866 |
mmddyyyy |
|
4462 | 867 |
"return a printed representation of the receiver in the |
868 |
form mmddyyyy. |
|
869 |
The receiver can be reconstructed with: |
|
870 |
Date readMMDDYYYYFrom:aStringOrStream onError:[...]" |
|
871 |
||
4407 | 872 |
^ (self month printStringLeftPaddedTo:2 with:$0) |
873 |
, (self day printStringLeftPaddedTo:2 with:$0) |
|
874 |
, (self year printStringLeftPaddedTo:4 with:$0) |
|
875 |
||
876 |
" |
|
877 |
Date today mmddyyyy |
|
878 |
" |
|
879 |
! ! |
|
880 |
||
699 | 881 |
!Date methodsFor:'accessing'! |
241 | 882 |
|
699 | 883 |
abbreviatedDayName |
884 |
"return the short week-day of the receiver as a string. |
|
885 |
The returned string depends on the language setting. |
|
886 |
Expect things like 'mon', 'tue' ..." |
|
54 | 887 |
|
699 | 888 |
^ self class abbreviatedNameOfDay:(self dayInWeek) |
92 | 889 |
|
890 |
" |
|
699 | 891 |
Date today abbreviatedDayName |
892 |
(Date day:15 month:4 year:1959) abbreviatedDayName |
|
92 | 893 |
" |
894 |
! |
|
895 |
||
699 | 896 |
abbreviatedMonthName |
897 |
"return the month of the receiver as a string. |
|
898 |
The returned string depends on the language setting. |
|
899 |
Expect things like 'jan', 'feb' ..." |
|
54 | 900 |
|
699 | 901 |
^ self class abbreviatedNameOfMonth:(self month) |
68 | 902 |
|
92 | 903 |
" |
699 | 904 |
Date today abbreviatedMonthName |
905 |
(Date day:15 month:4 year:1959) abbreviatedMonthName |
|
92 | 906 |
" |
907 |
! |
|
908 |
||
909 |
asDays |
|
910 |
"return the number of days elapsed since 01-Jan-1901 |
|
911 |
and the receiver's day; starts with 0 for 1-1-1901. |
|
912 |
Date>>fromDays: is the reverse operation. |
|
913 |
For ST-80 compatibility." |
|
68 | 914 |
|
915 |
|yr| |
|
916 |
||
917 |
yr := self year. |
|
918 |
^ (self class yearAsDays:yr) |
|
919 |
+ (self class daysUntilMonth:self month forYear:yr) |
|
920 |
+ self day |
|
85 | 921 |
- 1 |
68 | 922 |
|
92 | 923 |
" |
924 |
(Date day: 5 month: 8 year: 1962) asDays -> should be 22496 |
|
925 |
(Date day: 1 month: 1 year: 1901) asDays -> 0 |
|
926 |
Date today asDays |
|
927 |
Date fromDays:(Date today asDays + 7) |
|
928 |
" |
|
77 | 929 |
! |
930 |
||
931 |
asSeconds |
|
92 | 932 |
"return the seconds between 1.jan.1901 and the same time in the receivers |
933 |
day. (i.e. midnight to midnight). |
|
1501 | 934 |
This does not include any leapSeconds ... strictly speaking, this is incorrect. |
77 | 935 |
ST-80 compatibility." |
936 |
||
92 | 937 |
^ 60*60*24 * self asDays |
77 | 938 |
|
92 | 939 |
" |
940 |
(Date day: 5 month: 8 year: 1962) asSeconds |
|
941 |
(Date day: 1 month: 1 year: 1901) asSeconds |
|
942 |
(Date today addDays:7) asSeconds - Date today asSeconds |
|
1501 | 943 |
" |
944 |
||
945 |
"Modified: 1.7.1996 / 14:19:17 / cg" |
|
699 | 946 |
! |
947 |
||
948 |
day |
|
949 |
"return the day (1..31) of the receiver" |
|
950 |
||
951 |
^ dateEncoding \\ 100 |
|
952 |
||
953 |
" |
|
954 |
Date today day |
|
955 |
" |
|
956 |
! |
|
957 |
||
958 |
dayCount |
|
959 |
"return the number of days since 1st. Jan. 1901; |
|
960 |
starting with 0 for this date. |
|
961 |
Date>>fromDays: is the reverse operation. |
|
962 |
Obsolete: |
|
963 |
please use asDays for ST-80 compatibility" |
|
964 |
||
965 |
^ self asDays. |
|
966 |
||
967 |
" |
|
968 |
(Date day:1 month:1 year:1901) dayCount |
|
969 |
Date fromDays:(Date day:1 month:1 year:1994) dayCount |
|
970 |
Date today dayCount |
|
971 |
" |
|
972 |
! |
|
973 |
||
974 |
dayInWeek |
|
975 |
"return the week-day of the receiver - 1 for monday, 7 for sunday" |
|
976 |
||
3256
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
977 |
|wday| |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
978 |
|
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
979 |
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
|
980 |
+ self asDays) \\ 7 + 1. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
981 |
wday < 1 ifTrue:[ |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
982 |
wday := wday + 7 |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
983 |
]. |
637d16a0c83b
care for date before 1.1.1900 in dayInWeek.
Claus Gittinger <cg@exept.de>
parents:
2706
diff
changeset
|
984 |
^ wday |
699 | 985 |
|
986 |
" |
|
5548 | 987 |
Date today dayInWeek |
988 |
Date tomorrow dayInWeek |
|
989 |
Date yesterday dayInWeek |
|
3257 | 990 |
Date today weekday |
5548 | 991 |
Date tomorrow weekday |
992 |
Date yesterday weekday |
|
1501 | 993 |
(Date day:15 month:4 year:1959) dayInWeek |
994 |
(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
|
995 |
(Date day:31 month:12 year:1899) dayInWeek |
699 | 996 |
" |
1501 | 997 |
|
3257 | 998 |
"Modified: / 1.2.1998 / 14:10:27 / cg" |
699 | 999 |
! |
1000 |
||
5548 | 1001 |
dayInYear |
1002 |
"return the day-nr within the year of the receiver - 1 .. 365/366" |
|
1003 |
||
1004 |
^ self asAbsoluteTime dayInYear |
|
1005 |
||
1006 |
" |
|
1007 |
Date today dayInYear |
|
1008 |
(Date newDay:1 year:1999) dayInYear |
|
1009 |
(Date newDay:1 year:2000) dayInYear |
|
1010 |
(Date newDay:2 year:2000) dayInYear |
|
1011 |
" |
|
1012 |
! |
|
1013 |
||
699 | 1014 |
dayName |
1015 |
"return the week-day of the receiver as a string. |
|
1016 |
The returned string depends on the language setting. |
|
1017 |
Expect things like 'monday', 'tuesday' ... |
|
1018 |
Obsolete: |
|
1019 |
use #weekday for ST-80 compatibility" |
|
1020 |
||
1021 |
^ self weekday |
|
1022 |
! |
|
77 | 1023 |
|
1024 |
dayOfMonth |
|
1025 |
"Answer the day of the month represented by me. |
|
1026 |
Same as day; for ST-80 compatibility." |
|
1027 |
||
1028 |
^ self day |
|
92 | 1029 |
|
1030 |
" |
|
1031 |
Date today dayOfMonth |
|
1032 |
" |
|
1033 |
! |
|
1034 |
||
1035 |
daysInMonth |
|
1036 |
"return the number of days in the month of the receiver" |
|
1037 |
||
1038 |
^ Date daysInMonth:(self month) forYear:(self year) |
|
1039 |
||
1040 |
" |
|
1041 |
Date today daysInMonth |
|
1042 |
" |
|
1043 |
! |
|
1044 |
||
1045 |
daysInYear |
|
1046 |
"return the number of days in the year of the receiver" |
|
1047 |
||
1048 |
^ Date daysInYear:(self year) |
|
1049 |
||
1050 |
" |
|
1051 |
Date today daysInYear |
|
1052 |
" |
|
1053 |
! |
|
1054 |
||
1055 |
daysLeftInYear |
|
1056 |
"return the number of days left in the year of the receiver" |
|
1057 |
||
5282 | 1058 |
^ (Date daysInYear:(self year)) - self day |
92 | 1059 |
|
1060 |
" |
|
5282 | 1061 |
Date today daysLeftInYear |
92 | 1062 |
" |
77 | 1063 |
! |
1064 |
||
699 | 1065 |
leap |
1066 |
"return true, if the receivers year is a leap year" |
|
68 | 1067 |
|
699 | 1068 |
^ Date leapYear:(self year) |
68 | 1069 |
|
92 | 1070 |
" |
699 | 1071 |
Date today leap |
1072 |
(Date day:1 month:1 year:1992) leap |
|
92 | 1073 |
" |
68 | 1074 |
! |
1075 |
||
699 | 1076 |
month |
1077 |
"return the month (1..12) of the receiver" |
|
92 | 1078 |
|
699 | 1079 |
^ (dateEncoding // 100) \\ 100 |
68 | 1080 |
|
92 | 1081 |
" |
699 | 1082 |
Date today month |
92 | 1083 |
" |
68 | 1084 |
! |
1085 |
||
77 | 1086 |
monthIndex |
1087 |
"return the index of the month (e.g. Feb.=2). |
|
1088 |
Same as month; for ST-80 compatibility." |
|
1089 |
||
1090 |
^ self month |
|
1091 |
! |
|
1092 |
||
68 | 1093 |
monthName |
1094 |
"return the month of the receiver as a string. |
|
1095 |
The returned string depends on the language setting. |
|
1096 |
Expect things like 'january', 'february' ..." |
|
1097 |
||
1098 |
^ self class nameOfMonth:(self month) |
|
1099 |
||
92 | 1100 |
" |
1101 |
Date today monthName |
|
1102 |
(Date day:15 month:4 year:1959) monthName |
|
1103 |
" |
|
68 | 1104 |
! |
1105 |
||
5548 | 1106 |
weekInYear |
1107 |
"return the week number of the receiver - 1 for Jan, 1st. |
|
1108 |
each week is Mon .. Sun (i.e. Sunday belongs to the previous week)" |
|
1109 |
||
1110 |
^ self class weekInYearOf:self |
|
1111 |
||
1112 |
" |
|
1113 |
Date today weekInYear |
|
1114 |
(Date newDay:1 year:2000) weekInYear was a saturday |
|
1115 |
(Date newDay:2 year:2000) weekInYear was a sunday |
|
1116 |
(Date newDay:3 year:2000) weekInYear was a monday |
|
1117 |
(Date newDay:4 year:2000) weekInYear was a tuesday |
|
1118 |
(Date newDay:5 year:2000) weekInYear was a wed |
|
1119 |
(Date newDay:6 year:2000) weekInYear was a thursday |
|
1120 |
(Date newDay:7 year:2000) weekInYear was a fri |
|
1121 |
(Date newDay:8 year:2000) weekInYear was a sat |
|
1122 |
(Date newDay:9 year:2000) weekInYear was a sun |
|
1123 |
(Date newDay:10 year:2000) weekInYear was a monday |
|
1124 |
" |
|
1125 |
! |
|
1126 |
||
699 | 1127 |
weekday |
1128 |
"return the week-day of the receiver as a string. |
|
68 | 1129 |
The returned string depends on the language setting. |
699 | 1130 |
Expect things like 'monday', 'tuesday' ... |
1131 |
For ST-80 compatibility" |
|
1132 |
||
1133 |
^ self class nameOfDay:(self dayInWeek) |
|
1134 |
||
1135 |
" |
|
1136 |
Date today weekday |
|
1137 |
(Date day:15 month:4 year:1959) weekday |
|
1138 |
" |
|
1139 |
! |
|
1140 |
||
1141 |
year |
|
1142 |
"return the year (1..12) of the receiver" |
|
1143 |
||
1144 |
^ dateEncoding // (100*100) |
|
68 | 1145 |
|
699 | 1146 |
" |
1147 |
Date today year |
|
1148 |
" |
|
1149 |
! ! |
|
1150 |
||
1151 |
!Date methodsFor:'arithmetic'! |
|
1152 |
||
1153 |
addDays:days |
|
1154 |
"return a new date representing 'days' after the receiver. |
|
1155 |
The argument should be some kind of integer. |
|
1156 |
For ST-80 compatibility." |
|
1157 |
||
1158 |
^ self class fromDays:(self asDays + days) |
|
1159 |
||
1160 |
" |
|
1161 |
Date today addDays:7 |
|
1162 |
" |
|
1163 |
! |
|
1164 |
||
1165 |
daysUntil:aDate |
|
1166 |
"return the number of days between the receiver and the argument, |
|
1167 |
aDate, whuch should be some kind of date" |
|
1168 |
||
1169 |
^ aDate asDays - self asDays |
|
68 | 1170 |
|
92 | 1171 |
" |
699 | 1172 |
(Date day:24 month:12 year:1994) daysUntil:(Date day:1 month:1 year:1995) |
1173 |
(Date day:1 month:2 year:1992) daysUntil:(Date day:1 month:3 year:1992) |
|
1174 |
(Date day:1 month:2 year:1994) daysUntil:(Date day:1 month:3 year:1994) |
|
1175 |
||
1176 |
|delta| |
|
1177 |
delta := Date today |
|
1178 |
daysUntil:(Date day:25 month:12 year:Date today year). |
|
1179 |
Transcript show:'still '; |
|
1180 |
show:delta ; |
|
1422 | 1181 |
showCR:' days till xmas' |
699 | 1182 |
" |
1183 |
! |
|
1184 |
||
1185 |
minusDays:days |
|
1186 |
"return a new date representing 'days' before the receiver. |
|
1187 |
The argument should be some kind of integer. |
|
1188 |
Obsolete: |
|
1189 |
Please dont use this method since it will vanish. |
|
1190 |
Use #subtractDays: instead for ST-80 compatibility." |
|
1191 |
||
1192 |
^ self subtractDays:days |
|
1193 |
! |
|
1194 |
||
1195 |
plusDays:days |
|
1196 |
"return a new date representing 'days' after the receiver. |
|
1197 |
The argument should be some kind of integer. |
|
1198 |
Obsolete: |
|
1199 |
Please dont use this method since it will vanish. |
|
1200 |
Use #addDays: instead for ST-80 compatibility." |
|
1201 |
||
1202 |
^ self addDays:days |
|
1203 |
! |
|
1204 |
||
1205 |
subtractDate:aDate |
|
1206 |
"return the number of days between the receiver and aDate" |
|
1207 |
||
1208 |
^ self asDays - aDate asDays |
|
1209 |
||
1210 |
" |
|
1211 |
(Date day:1 month:1 year:1995) subtractDate:(Date day:24 month:12 year:1994) |
|
1212 |
(Date day:1 month:3 year:1992) subtractDate:(Date day:1 month:2 year:1992) |
|
1213 |
(Date day:1 month:3 year:1994) subtractDate:(Date day:1 month:2 year:1994) |
|
1214 |
" |
|
1215 |
! |
|
1216 |
||
1217 |
subtractDays:days |
|
1218 |
"return a new date representing 'days' before the receiver. |
|
1219 |
The argument should be some kind of integer. |
|
1220 |
For ST-80 compatibility" |
|
1221 |
||
1222 |
^ self class fromDays:(self asDays - days) |
|
1223 |
||
1224 |
" |
|
1225 |
Date today subtractDays:7 |
|
92 | 1226 |
" |
1 | 1227 |
! ! |
1228 |
||
1229 |
!Date methodsFor:'comparing'! |
|
1230 |
||
29 | 1231 |
< aDate |
1232 |
"return true, if the date represented by the receiver |
|
1233 |
is before the argument, aDate" |
|
1234 |
||
54 | 1235 |
(aDate isMemberOf:Date) ifTrue:[ |
155 | 1236 |
^ dateEncoding < aDate dateEncoding |
54 | 1237 |
]. |
29 | 1238 |
|
54 | 1239 |
"the argument must understand year, month and day to be |
1240 |
comparable, whatever it is" |
|
1241 |
||
1242 |
^ dateEncoding < (Date encodeYear:aDate year |
|
155 | 1243 |
month:aDate month |
1244 |
day:aDate day) |
|
68 | 1245 |
|
1246 |
"Date today < (Date day:24 month:12 year:2000)" |
|
1247 |
"Date today < (Date day:24 month:12 year:1900)" |
|
29 | 1248 |
! |
1 | 1249 |
|
1250 |
= aDate |
|
1251 |
"return true, if the date represented by the receiver |
|
1252 |
is the same as the one represented by argument, aDate" |
|
1253 |
||
155 | 1254 |
(aDate isMemberOf:Date) ifFalse:[^ false]. |
1255 |
||
1256 |
^ dateEncoding = aDate dateEncoding |
|
54 | 1257 |
|
1258 |
"the argument must understand year, month and day to be |
|
1259 |
comparable, whatever it is" |
|
1260 |
||
155 | 1261 |
" |
54 | 1262 |
^ dateEncoding = (Date encodeYear:aDate year |
155 | 1263 |
month:aDate month |
1264 |
day:aDate day) |
|
1265 |
" |
|
68 | 1266 |
"Date today = ((Date today plusDays:7) minusDays:7)" |
92 | 1267 |
! |
1268 |
||
699 | 1269 |
> aDate |
1270 |
"return true, if the date represented by the receiver |
|
1271 |
is after the argument, aDate" |
|
1272 |
||
1273 |
(aDate isMemberOf:Date) ifTrue:[ |
|
1274 |
^ dateEncoding > aDate dateEncoding |
|
1275 |
]. |
|
1276 |
||
1277 |
"the argument must understand year, month and day to be |
|
1278 |
comparable, whatever it is" |
|
1279 |
||
1280 |
^ dateEncoding > (Date encodeYear:aDate year |
|
1281 |
month:aDate month |
|
1282 |
day:aDate day) |
|
1283 |
||
1284 |
"Date today > (Date day:24 month:12 year:2000)" |
|
1285 |
"Date today > (Date day:24 month:12 year:1900)" |
|
1286 |
! |
|
1287 |
||
92 | 1288 |
hash |
1289 |
"return an integer useful for hashing on dates" |
|
1290 |
||
1291 |
^ dateEncoding |
|
1 | 1292 |
! ! |
1293 |
||
1501 | 1294 |
!Date methodsFor:'converting'! |
1295 |
||
1296 |
asAbsoluteTime |
|
5679 | 1297 |
"return an absoluteTime instance, representing midnight of last night" |
1501 | 1298 |
|
4379 | 1299 |
^ AbsoluteTime year:(self year) month:(self month) day:(self day) |
1300 |
hour:0 minute:0 second:0 millisecond:0 |
|
1501 | 1301 |
|
1302 |
" |
|
1303 |
Date today asAbsoluteTime |
|
1304 |
" |
|
1305 |
||
4379 | 1306 |
"Modified: / 1.7.1996 / 14:53:32 / cg" |
5679 | 1307 |
"Modified: / 13.7.1999 / 12:31:47 / stefan"! |
4408 | 1308 |
|
1309 |
asDate |
|
1310 |
"return the receiver" |
|
1311 |
||
1312 |
^ self |
|
5679 | 1313 |
! |
1314 |
||
1315 |
asTimestamp |
|
1316 |
"return an absoluteTime instance, representing midnight of last night" |
|
1317 |
||
1318 |
^ self asAbsoluteTime |
|
1319 |
||
1320 |
" |
|
1321 |
Date today asTimestamp |
|
1322 |
"! ! |
|
1323 |
||
1501 | 1324 |
|
54 | 1325 |
!Date methodsFor:'printing & storing'! |
1 | 1326 |
|
408 | 1327 |
printFormat:aFormatArray |
1328 |
"return a string containing a printed representation of the receiver. |
|
1329 |
The formatArray argument consists of 6 or 7 integers which control |
|
1330 |
the resulting string. The entries are: |
|
1331 |
1 day position (1, 2 or 3) |
|
1332 |
2 month position (1..3) |
|
1333 |
3 year position (1..3) |
|
1334 |
4 asciiValue of separator character or separator character |
|
4413 | 1335 |
or collection of 2 separator characters |
408 | 1336 |
5 month format (1: numeric; 2: abbreviated name; 3: fullname |
4413 | 1337 |
4: abbreviated uppercase 5: full uppercase) |
408 | 1338 |
6 year format (1 include century; 2 exclude century) |
4413 | 1339 |
7 (optional) true/false. |
1340 |
if true, print numbers in 2-digit format |
|
1341 |
(i.e. with leading zeros); |
|
1342 |
Taken as false, if ommited |
|
408 | 1343 |
|
1344 |
Day and monthnames are in the currently active language. |
|
1345 |
||
1346 |
This method supports more options than the ST-80 version; month formats |
|
1347 |
4 and 5, non-numeric separators and the optional 7th parameter are not |
|
1348 |
supported by ST-80. Be aware of this for compatibility reasons. |
|
1349 |
||
4413 | 1350 |
Notice that not all formats can be scanned (correctly) by #readFrom: |
1351 |
This is an ST-80 compatibility method - I would have choosen another |
|
1352 |
(less cryptic) formatString ... |
|
1353 |
" |
|
408 | 1354 |
|
1355 |
|mf upperCase day month year components sep1 sep2 leadingZeros| |
|
1356 |
||
1357 |
aFormatArray size > 6 ifTrue:[ |
|
4413 | 1358 |
leadingZeros := aFormatArray at:7 |
408 | 1359 |
] ifFalse:[ |
4413 | 1360 |
leadingZeros := false. |
408 | 1361 |
]. |
1362 |
||
1363 |
components := Array new:3. |
|
1364 |
day := self day. |
|
1365 |
leadingZeros ifTrue:[ |
|
4413 | 1366 |
day := day printStringLeftPaddedTo:2 with:$0 |
408 | 1367 |
] ifFalse:[ |
4413 | 1368 |
day := day printString |
408 | 1369 |
]. |
1370 |
components at:(aFormatArray at:1) put:day. |
|
1371 |
||
1372 |
upperCase := false. |
|
1373 |
mf := aFormatArray at:5. |
|
1374 |
(mf between:4 and:5) ifTrue:[mf := mf - 2. upperCase := true]. |
|
1375 |
||
1376 |
mf == 1 ifTrue:[ |
|
4413 | 1377 |
month := self month. |
1378 |
leadingZeros ifTrue:[ |
|
1379 |
month := month printStringLeftPaddedTo:2 with:$0 |
|
1380 |
] ifFalse:[ |
|
1381 |
month := month printString |
|
1382 |
] |
|
408 | 1383 |
] ifFalse:[ |
4413 | 1384 |
mf == 2 ifTrue:[ |
1385 |
month := self abbreviatedMonthName |
|
1386 |
] ifFalse:[ |
|
1387 |
month := self monthName |
|
1388 |
] |
|
408 | 1389 |
]. |
1390 |
upperCase ifTrue:[month := month asUppercase]. |
|
1391 |
components at:(aFormatArray at:2) put:month. |
|
1392 |
||
1393 |
year := self year. |
|
1394 |
(aFormatArray at:6) == 2 ifTrue:[ |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1395 |
year := year \\ 100. |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1396 |
year := year printStringLeftPaddedTo:2 with:$0 |
408 | 1397 |
]. |
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1398 |
year := year printString. |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1399 |
components at:(aFormatArray at:3) put:year. |
408 | 1400 |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1401 |
sep1 := sep2 := (aFormatArray at:4). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1402 |
sep1 isString ifFalse:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1403 |
sep1 isCollection ifTrue:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1404 |
sep2 := (sep1 at:2). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1405 |
sep1 := (sep1 at:1). |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1406 |
] ifFalse:[ |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1407 |
sep1 := sep2 := sep1 asCharacter |
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1408 |
] |
408 | 1409 |
]. |
1410 |
||
1411 |
^ (components at:1) |
|
1412 |
, sep1 asString |
|
1413 |
, (components at:2) |
|
1414 |
, sep2 asString |
|
1415 |
, (components at:3) |
|
1416 |
||
1417 |
" |
|
5662
c7f7c94bc5bc
fixed printFormat (leading zeros; allow string-as-separator)
Claus Gittinger <cg@exept.de>
parents:
5548
diff
changeset
|
1418 |
Date today printFormat:#(1 2 3 '' 1 2) |
4413 | 1419 |
Date today printFormat:#(1 2 3 $- 1 2) |
1420 |
Date today printFormat:#(1 2 3 $. 1 2 true) |
|
1421 |
Date today printFormat:#(2 1 3 32 3 1) |
|
408 | 1422 |
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
|
1423 |
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
|
1424 |
Date today printFormat:#(1 2 3 $- 4 1) |
408 | 1425 |
" |
1426 |
! |
|
1427 |
||
1428 |
printFormat:aFormatArray on:aStream |
|
1429 |
"append a printed representation of the receiver to aStream. |
|
1430 |
The argument, aFormatString controls the format, as described |
|
1431 |
in the #printFormat: method. |
|
1432 |
Notice that not all formats can be scanned (correctly) by #readFrom:" |
|
1433 |
||
1434 |
aStream nextPutAll:(self printFormat:aFormatArray) |
|
1435 |
||
1436 |
||
1437 |
"european formats: |
|
1438 |
||
1439 |
Date today printFormat:#(1 2 3 $- 1 2) on:Transcript. Transcript cr. |
|
1440 |
Date today printFormat:#(1 2 3 $- 2 2) on:Transcript. Transcript cr. |
|
1441 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1442 |
Date today printFormat:#(1 2 3 $- 3 2) on:Transcript. Transcript cr. |
|
1443 |
Date today printFormat:#(1 2 3 $. 1 2) on:Transcript. Transcript cr. |
|
1444 |
Date today printFormat:#(1 2 3 $. 1 2 true) on:Transcript. Transcript cr. |
|
1445 |
||
1446 |
US formats: |
|
1447 |
||
1448 |
Date today printFormat:#(2 1 3 32 3 1) on:Transcript. Transcript cr. |
|
1449 |
Date today printFormat:#(2 1 3 32 2 2) on:Transcript. Transcript cr. |
|
1450 |
Date today printFormat:#(2 1 3 $/ 1 2) on:Transcript. Transcript cr. |
|
1451 |
Date today printFormat:#(2 1 3 $- 2 1) on:Transcript. Transcript cr. |
|
1452 |
Date today printFormat:#(2 1 3 #(' ' ', ') 2 1) on:Transcript. Transcript cr. |
|
1453 |
Date today printFormat:#(1 2 3 $- 2 1) on:Transcript. Transcript cr. |
|
1454 |
Date today printFormat:#(1 2 3 $- 4 1) on:Transcript. Transcript cr. |
|
1455 |
" |
|
699 | 1456 |
! |
1457 |
||
1458 |
printOn:aStream |
|
1459 |
"append a printed representation of the receiver to aStream" |
|
1460 |
||
1461 |
self printFormat:#(1 2 3 $- 2 1) on:aStream |
|
1462 |
||
1463 |
" |
|
1464 |
Date today printOn:Transcript |
|
1465 |
Date today printNL |
|
1466 |
" |
|
1467 |
||
1468 |
"Modified: 27.8.1995 / 01:01:49 / claus" |
|
1469 |
! |
|
1470 |
||
1471 |
storeOn:aStream |
|
1472 |
"append a representation to aStream, from which the receiver |
|
1473 |
can be reconstructed" |
|
1474 |
||
1475 |
aStream nextPutAll:'('; nextPutAll:'Date day:'. |
|
1476 |
self day printOn:aStream. |
|
1477 |
aStream nextPutAll:' month:'. |
|
1478 |
self month printOn:aStream. |
|
1479 |
aStream nextPutAll:' year:'. |
|
1480 |
self year printOn:aStream. |
|
1481 |
aStream nextPutAll:')' |
|
1482 |
||
1483 |
" |
|
1484 |
Date today storeOn:Transcript |
|
1485 |
Date today storeString |
|
1486 |
" |
|
1 | 1487 |
! ! |
699 | 1488 |
|
1489 |
!Date methodsFor:'private accessing'! |
|
1490 |
||
1491 |
dateEncoding |
|
1492 |
"the internal encoding is stricktly private, |
|
1493 |
and should not be used outside." |
|
1494 |
||
1495 |
^ dateEncoding |
|
1496 |
! |
|
1497 |
||
1498 |
dateEncoding:anInteger |
|
1499 |
"the internal encoding is stricktly private, |
|
1500 |
and should not be used outside." |
|
1501 |
||
1502 |
dateEncoding := anInteger |
|
1503 |
! |
|
1504 |
||
1505 |
fromOSTime:osTime |
|
1506 |
"set my dateEncoding from an OS time. |
|
1507 |
This somewhat clumsy implementation hides the OS's date representation |
|
1508 |
(i.e. makes this class independent of what the OS starts its time values with). |
|
1509 |
Dont use this method, the osTime representation is totally unportable." |
|
1510 |
||
1511 |
OperatingSystem computeDatePartsOf:osTime |
|
1501 | 1512 |
for:[:year :month :day | |
1513 |
dateEncoding := (((year * 100) + month) * 100) + day |
|
699 | 1514 |
] |
1501 | 1515 |
|
1516 |
"Modified: 1.7.1996 / 15:23:12 / cg" |
|
699 | 1517 |
! ! |
1518 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
1519 |
!Date class methodsFor:'documentation'! |
699 | 1520 |
|
1521 |
version |
|
5679 | 1522 |
^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.56 2000-10-25 10:21:57 cg Exp $' |
699 | 1523 |
! ! |
1524 |
Date initialize! |