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