]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
dc9442259b53c20b1d1cf5c0bed082f3f9b3a0d6
[factor.git] / basis / calendar / calendar.factor
1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math math.functions namespaces sequences
4 strings system vocabs.loader threads accessors combinators
5 locals classes.tuple math.order summary combinators.short-circuit ;
6 IN: calendar
7
8 HOOK: gmt-offset os ( -- hours minutes seconds )
9
10 TUPLE: duration
11     { year real }
12     { month real }
13     { day real }
14     { hour real }
15     { minute real }
16     { second real } ;
17
18 C: <duration> duration
19
20 TUPLE: timestamp
21     { year integer }
22     { month integer }
23     { day integer }
24     { hour integer }
25     { minute integer }
26     { second real }
27     { gmt-offset duration } ;
28
29 C: <timestamp> timestamp
30
31 : gmt-offset-duration ( -- duration )
32     0 0 0 gmt-offset <duration> ;
33
34 : <date> ( year month day -- timestamp )
35     0 0 0 gmt-offset-duration <timestamp> ;
36
37 ERROR: not-a-month n ;
38 M: not-a-month summary
39     drop "Months are indexed starting at 1" ;
40
41 <PRIVATE
42 : check-month ( n -- n )
43     dup zero? [ not-a-month ] when ;
44 PRIVATE>
45
46 : month-names ( -- array )
47     {
48         "January" "February" "March" "April" "May" "June"
49         "July" "August" "September" "October" "November" "December"
50     } ;
51
52 : month-name ( n -- string )
53     check-month 1- month-names nth ;
54
55 : month-abbreviations ( -- array )
56     {
57         "Jan" "Feb" "Mar" "Apr" "May" "Jun"
58         "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
59     } ;
60
61 : month-abbreviation ( n -- string )
62     check-month 1- month-abbreviations nth ;
63
64 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
65
66 : day-names ( -- array )
67     {
68         "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
69     } ;
70
71 : day-name ( n -- string ) day-names nth ;
72
73 : day-abbreviations2 ( -- array )
74     { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" } ;
75
76 : day-abbreviation2 ( n -- string )
77     day-abbreviations2 nth ;
78
79 : day-abbreviations3 ( -- array )
80     { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" } ;
81
82 : day-abbreviation3 ( n -- string )
83     day-abbreviations3 nth ;
84
85 : average-month ( -- ratio ) 30+5/12 ; inline
86 : months-per-year ( -- integer ) 12 ; inline
87 : days-per-year ( -- ratio ) 3652425/10000 ; inline
88 : hours-per-year ( -- ratio ) 876582/100 ; inline
89 : minutes-per-year ( -- ratio ) 5259492/10 ; inline
90 : seconds-per-year ( -- integer ) 31556952 ; inline
91
92 :: julian-day-number ( year month day -- n )
93     #! Returns a composite date number
94     #! Not valid before year -4800
95     [let* | a [ 14 month - 12 /i ]
96             y [ year 4800 + a - ]
97             m [ month 12 a * + 3 - ] |
98         day 153 m * 2 + 5 /i + 365 y * +
99         y 4 /i + y 100 /i - y 400 /i + 32045 -
100     ] ;
101
102 :: julian-day-number>date ( n -- year month day )
103     #! Inverse of julian-day-number
104     [let* | a [ n 32044 + ]
105             b [ 4 a * 3 + 146097 /i ]
106             c [ a 146097 b * 4 /i - ]
107             d [ 4 c * 3 + 1461 /i ]
108             e [ c 1461 d * 4 /i - ]
109             m [ 5 e * 2 + 153 /i ] |
110         100 b * d + 4800 -
111         m 10 /i + m 3 +
112         12 m 10 /i * -
113         e 153 m * 2 + 5 /i - 1+
114     ] ;
115
116 : >date< ( timestamp -- year month day )
117     [ year>> ] [ month>> ] [ day>> ] tri ;
118
119 : >time< ( timestamp -- hour minute second )
120     [ hour>> ] [ minute>> ] [ second>> ] tri ;
121
122 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
123 : years ( x -- duration ) instant clone swap >>year ;
124 : months ( x -- duration ) instant clone swap >>month ;
125 : days ( x -- duration ) instant clone swap >>day ;
126 : weeks ( x -- duration ) 7 * days ;
127 : hours ( x -- duration ) instant clone swap >>hour ;
128 : minutes ( x -- duration ) instant clone swap >>minute ;
129 : seconds ( x -- duration ) instant clone swap >>second ;
130 : milliseconds ( x -- duration ) 1000 / seconds ;
131 : microseconds ( x -- duration ) 1000000 / seconds ;
132 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
133
134 GENERIC: leap-year? ( obj -- ? )
135
136 M: integer leap-year? ( year -- ? )
137     dup 100 mod zero? 400 4 ? mod zero? ;
138
139 M: timestamp leap-year? ( timestamp -- ? )
140     year>> leap-year? ;
141
142 <PRIVATE
143
144 GENERIC: +year ( timestamp x -- timestamp )
145 GENERIC: +month ( timestamp x -- timestamp )
146 GENERIC: +day ( timestamp x -- timestamp )
147 GENERIC: +hour ( timestamp x -- timestamp )
148 GENERIC: +minute ( timestamp x -- timestamp )
149 GENERIC: +second ( timestamp x -- timestamp )
150
151 : /rem ( f n -- q r )
152     #! q is positive or negative, r is positive from 0 <= r < n
153     [ / floor >integer ] 2keep rem ;
154
155 : float>whole-part ( float -- int float )
156     [ floor >integer ] keep over - ;
157
158 : adjust-leap-year ( timestamp -- timestamp )
159     dup
160     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
161     [ 3 >>month 1 >>day ] when ;
162
163 : unless-zero ( n quot -- )
164     [ dup zero? [ drop ] ] dip if ; inline
165
166 M: integer +year ( timestamp n -- timestamp )
167     [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
168
169 M: real +year ( timestamp n -- timestamp )
170     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
171
172 : months/years ( n -- months years )
173     12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
174
175 M: integer +month ( timestamp n -- timestamp )
176     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
177
178 M: real +month ( timestamp n -- timestamp )
179     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
180
181 M: integer +day ( timestamp n -- timestamp )
182     [
183         over >date< julian-day-number + julian-day-number>date
184         [ >>year ] [ >>month ] [ >>day ] tri*
185     ] unless-zero ;
186
187 M: real +day ( timestamp n -- timestamp )
188     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
189
190 : hours/days ( n -- hours days )
191     24 /rem swap ;
192
193 M: integer +hour ( timestamp n -- timestamp )
194     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
195
196 M: real +hour ( timestamp n -- timestamp )
197     float>whole-part swapd 60 * +minute swap +hour ;
198
199 : minutes/hours ( n -- minutes hours )
200     60 /rem swap ;
201
202 M: integer +minute ( timestamp n -- timestamp )
203     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
204
205 M: real +minute ( timestamp n -- timestamp )
206     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
207
208 : seconds/minutes ( n -- seconds minutes )
209     60 /rem swap >integer ;
210
211 M: number +second ( timestamp n -- timestamp )
212     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
213
214 : (time+) ( timestamp duration -- timestamp' duration )
215     [ second>> +second ] keep
216     [ minute>> +minute ] keep
217     [ hour>>   +hour   ] keep
218     [ day>>    +day    ] keep
219     [ month>>  +month  ] keep
220     [ year>>   +year   ] keep ; inline
221
222 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
223     [ bi@ + ] curry 2keep ; inline
224
225 PRIVATE>
226
227 GENERIC# time+ 1 ( time1 time2 -- time3 )
228
229 M: timestamp time+
230     [ clone ] dip (time+) drop ;
231
232 M: duration time+
233     dup timestamp? [
234         swap time+
235     ] [
236         [ year>> ] +slots
237         [ month>> ] +slots
238         [ day>> ] +slots
239         [ hour>> ] +slots
240         [ minute>> ] +slots
241         [ second>> ] +slots
242         2drop <duration>
243     ] if ;
244
245 : duration>years ( duration -- x )
246     #! Uses average month/year length since duration loses calendar
247     #! data
248     0 swap
249     {
250         [ year>> + ]
251         [ month>> months-per-year / + ]
252         [ day>> days-per-year / + ]
253         [ hour>> hours-per-year / + ]
254         [ minute>> minutes-per-year / + ]
255         [ second>> seconds-per-year / + ]
256     } cleave ;
257
258 M: duration <=> [ duration>years ] compare ;
259
260 : duration>months ( duration -- x ) duration>years months-per-year * ;
261 : duration>days ( duration -- x ) duration>years days-per-year * ;
262 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
263 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
264 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
265 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
266 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
267 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
268
269 GENERIC: time- ( time1 time2 -- time3 )
270
271 : convert-timezone ( timestamp duration -- timestamp )
272     over gmt-offset>> over = [ drop ] [
273         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
274     ] if ;
275
276 : >local-time ( timestamp -- timestamp )
277     gmt-offset-duration convert-timezone ;
278
279 : >gmt ( timestamp -- timestamp )
280     instant convert-timezone ;
281
282 M: timestamp <=> ( ts1 ts2 -- n )
283     [ >gmt tuple-slots ] compare ;
284
285 : (time-) ( timestamp timestamp -- n )
286     [ >gmt ] bi@
287     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
288     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
289
290 M: timestamp time-
291     #! Exact calendar-time difference
292     (time-) seconds ;
293
294 : time* ( obj1 obj2 -- obj3 )
295     dup real? [ swap ] when
296     dup real? [ * ] [
297         {
298             [   year>> * ]
299             [  month>> * ]
300             [    day>> * ]
301             [   hour>> * ]
302             [ minute>> * ]
303             [ second>> * ]
304         } 2cleave <duration>
305     ] if ;
306
307 : before ( duration -- -duration )
308     -1 time* ;
309
310 M: duration time-
311     before time+ ;
312
313 : <zero> ( -- timestamp )
314     0 0 0 0 0 0 instant <timestamp> ;
315
316 : valid-timestamp? ( timestamp -- ? )
317     clone instant >>gmt-offset
318     dup <zero> time- <zero> time+ = ;
319
320 : unix-1970 ( -- timestamp )
321     1970 1 1 0 0 0 instant <timestamp> ;
322
323 : millis>timestamp ( x -- timestamp )
324     [ unix-1970 ] dip milliseconds time+ ;
325
326 : timestamp>millis ( timestamp -- n )
327     unix-1970 (time-) 1000 * >integer ;
328
329 : micros>timestamp ( x -- timestamp )
330     [ unix-1970 ] dip microseconds time+ ;
331
332 : timestamp>micros ( timestamp -- n )
333     unix-1970 (time-) 1000000 * >integer ;
334
335 : gmt ( -- timestamp )
336     #! GMT time, right now
337     unix-1970 micros microseconds time+ ;
338
339 : now ( -- timestamp ) gmt >local-time ;
340 : hence ( duration -- timestamp ) now swap time+ ;
341 : ago ( duration -- timestamp ) now swap time- ;
342
343 : zeller-congruence ( year month day -- n )
344     #! Zeller Congruence
345     #! http://web.textfiles.com/computers/formulas.txt
346     #! good for any date since October 15, 1582
347     [
348         dup 2 <= [ [ 1- ] [ 12 + ] bi* ] when
349         [ dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + ] dip
350         [ 1+ 3 * 5 /i + ] keep 2 * +
351     ] dip 1+ + 7 mod ;
352
353 GENERIC: days-in-year ( obj -- n )
354
355 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
356 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
357
358 : (days-in-month) ( year month -- n )
359     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
360
361 : days-in-month ( timestamp -- n )
362     >date< drop (days-in-month) ;
363
364 : day-of-week ( timestamp -- n )
365     >date< zeller-congruence ;
366
367 :: (day-of-year) ( year month day -- n )
368     day-counts month head-slice sum day +
369     year leap-year? [
370         year month day <date>
371         year 3 1 <date>
372         after=? [ 1+ ] when
373     ] when ;
374
375 : day-of-year ( timestamp -- n )
376     >date< (day-of-year) ;
377
378 <PRIVATE
379 : day-offset ( timestamp m -- timestamp n )
380     over day-of-week - ; inline
381
382 : day-this-week ( timestamp n -- timestamp )
383     day-offset days time+ ;
384 PRIVATE>
385
386 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
387 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
388 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
389 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
390 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
391 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
392 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
393
394 : midnight ( timestamp -- new-timestamp )
395     clone 0 >>hour 0 >>minute 0 >>second ; inline
396
397 : noon ( timestamp -- new-timestamp )
398     midnight 12 >>hour ; inline
399
400 : beginning-of-month ( timestamp -- new-timestamp )
401     midnight 1 >>day ;
402
403 : beginning-of-week ( timestamp -- new-timestamp )
404     midnight sunday ;
405
406 : beginning-of-year ( timestamp -- new-timestamp )
407     beginning-of-month 1 >>month ;
408
409 : time-since-midnight ( timestamp -- duration )
410     dup midnight time- ;
411
412 : since-1970 ( duration -- timestamp )
413     unix-1970 time+ >local-time ;
414
415 M: timestamp sleep-until timestamp>micros sleep-until ;
416
417 M: duration sleep hence sleep-until ;
418
419 {
420     { [ os unix? ] [ "calendar.unix" ] }
421     { [ os windows? ] [ "calendar.windows" ] }
422 } cond require