]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
802e3ae10ef7ffc9cb3c54b17cc4e46c27c26f68
[factor.git] / basis / calendar / calendar.factor
1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes.tuple combinators
4 combinators.short-circuit kernel locals math math.functions
5 math.order sequences summary system vocabs vocabs.loader ;
6 IN: calendar
7
8 HOOK: gmt-offset os ( -- hours minutes seconds )
9
10 HOOK: gmt os ( -- timestamp )
11
12 TUPLE: duration
13     { year real }
14     { month real }
15     { day real }
16     { hour real }
17     { minute real }
18     { second real } ;
19
20 C: <duration> duration
21
22 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
23
24 TUPLE: timestamp
25     { year integer }
26     { month integer }
27     { day integer }
28     { hour integer }
29     { minute integer }
30     { second real }
31     { gmt-offset duration } ;
32
33 C: <timestamp> timestamp
34
35 : gmt-offset-duration ( -- duration )
36     0 0 0 gmt-offset <duration> ; inline
37
38 : <date> ( year month day -- timestamp )
39     0 0 0 gmt-offset-duration <timestamp> ; inline
40
41 : <date-gmt> ( year month day -- timestamp )
42     0 0 0 instant <timestamp> ; inline
43
44 : <year> ( year -- timestamp )
45     1 1 <date> ; inline
46
47 : <year-gmt> ( year -- timestamp )
48     1 1 <date-gmt> ; inline
49
50 ERROR: not-a-month ;
51 M: not-a-month summary
52     drop "Months are indexed starting at 1" ;
53
54 <PRIVATE
55
56 : check-month ( n -- n )
57     [ not-a-month ] when-zero ;
58
59 PRIVATE>
60
61 CONSTANT: month-names 
62     {
63         "January" "February" "March" "April" "May" "June"
64         "July" "August" "September" "October" "November" "December"
65     }
66
67 GENERIC: month-name ( obj -- string )
68
69 M: integer month-name check-month 1 - month-names nth ;
70 M: timestamp month-name month>> 1 - month-names nth ;
71
72 CONSTANT: month-abbreviations
73     {
74         "Jan" "Feb" "Mar" "Apr" "May" "Jun"
75         "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
76     }
77
78 : month-abbreviation ( n -- string )
79     check-month 1 - month-abbreviations nth ;
80
81 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
82
83 CONSTANT: day-names
84     { "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" }
85
86 CONSTANT: day-abbreviations2
87     { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
88
89 : day-abbreviation2 ( n -- string )
90     day-abbreviations2 nth ; inline
91
92 CONSTANT: day-abbreviations3
93     { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
94
95 : day-abbreviation3 ( n -- string )
96     day-abbreviations3 nth ; inline
97
98 CONSTANT: average-month 30+5/12
99 CONSTANT: months-per-year 12
100 CONSTANT: days-per-year 3652425/10000
101 CONSTANT: hours-per-year 876582/100
102 CONSTANT: minutes-per-year 5259492/10
103 CONSTANT: seconds-per-year 31556952
104
105 :: julian-day-number ( year month day -- n )
106     #! Returns a composite date number
107     #! Not valid before year -4800
108     14 month - 12 /i :> a
109     year 4800 + a - :> y
110     month 12 a * + 3 - :> m
111
112     day 153 m * 2 + 5 /i + 365 y * +
113     y 4 /i + y 100 /i - y 400 /i + 32045 - ;
114
115 :: julian-day-number>date ( n -- year month day )
116     #! Inverse of julian-day-number
117     n 32044 + :> a
118     4 a * 3 + 146097 /i :> b
119     a 146097 b * 4 /i - :> c
120     4 c * 3 + 1461 /i :> d
121     c 1461 d * 4 /i - :> e
122     5 e * 2 + 153 /i :> m
123
124     100 b * d + 4800 -
125     m 10 /i + m 3 +
126     12 m 10 /i * -
127     e 153 m * 2 + 5 /i - 1 + ;
128
129 GENERIC: easter ( obj -- obj' )
130
131 :: easter-month-day ( year -- month day )
132     year 19 mod :> a
133     year 100 /mod :> ( b c )
134     b 4 /mod :> ( d e )
135     b 8 + 25 /i :> f
136     b f - 1 + 3 /i :> g
137     19 a * b + d - g - 15 + 30 mod :> h
138     c 4 /mod :> ( i k )
139     32 2 e * + 2 i * + h - k - 7 mod :> l
140     a 11 h * + 22 l * + 451 /i :> m
141
142     h l + 7 m * - 114 + 31 /mod 1 + ;
143
144 M: integer easter ( year -- timestamp )
145     dup easter-month-day <date> ;
146
147 M: timestamp easter ( timestamp -- timestamp )
148     clone
149     dup year>> easter-month-day
150     swapd >>day swap >>month ;
151
152 : >date< ( timestamp -- year month day )
153     [ year>> ] [ month>> ] [ day>> ] tri ;
154
155 : >time< ( timestamp -- hour minute second )
156     [ hour>> ] [ minute>> ] [ second>> ] tri ;
157
158 : years ( x -- duration ) instant swap >>year ;
159 : months ( x -- duration ) instant swap >>month ;
160 : days ( x -- duration ) instant swap >>day ;
161 : weeks ( x -- duration ) 7 * days ;
162 : hours ( x -- duration ) instant swap >>hour ;
163 : minutes ( x -- duration ) instant swap >>minute ;
164 : seconds ( x -- duration ) instant swap >>second ;
165 : milliseconds ( x -- duration ) 1000 / seconds ;
166 : microseconds ( x -- duration ) 1000000 / seconds ;
167 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
168
169 GENERIC: leap-year? ( obj -- ? )
170
171 M: integer leap-year? ( year -- ? )
172     dup 100 divisor? 400 4 ? divisor? ;
173
174 M: timestamp leap-year? ( timestamp -- ? )
175     year>> leap-year? ;
176
177 <PRIVATE
178
179 GENERIC: +year ( timestamp x -- timestamp )
180 GENERIC: +month ( timestamp x -- timestamp )
181 GENERIC: +day ( timestamp x -- timestamp )
182 GENERIC: +hour ( timestamp x -- timestamp )
183 GENERIC: +minute ( timestamp x -- timestamp )
184 GENERIC: +second ( timestamp x -- timestamp )
185
186 : /rem ( f n -- q r )
187     #! q is positive or negative, r is positive from 0 <= r < n
188     [ / floor >integer ] 2keep rem ;
189
190 : float>whole-part ( float -- int float )
191     [ floor >integer ] keep over - ;
192
193 : adjust-leap-year ( timestamp -- timestamp )
194     dup
195     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
196     [ 3 >>month 1 >>day ] when ;
197
198 M: integer +year ( timestamp n -- timestamp )
199     [ + ] curry change-year adjust-leap-year ;
200
201 M: real +year ( timestamp n -- timestamp )
202     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
203
204 : months/years ( n -- months years )
205     12 /rem [ 1 - 12 ] when-zero swap ; inline
206
207 M: integer +month ( timestamp n -- timestamp )
208     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
209
210 M: real +month ( timestamp n -- timestamp )
211     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
212
213 M: integer +day ( timestamp n -- timestamp )
214     [
215         over >date< julian-day-number + julian-day-number>date
216         [ >>year ] [ >>month ] [ >>day ] tri*
217     ] unless-zero ;
218
219 M: real +day ( timestamp n -- timestamp )
220     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
221
222 : hours/days ( n -- hours days )
223     24 /rem swap ;
224
225 M: integer +hour ( timestamp n -- timestamp )
226     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
227
228 M: real +hour ( timestamp n -- timestamp )
229     float>whole-part swapd 60 * +minute swap +hour ;
230
231 : minutes/hours ( n -- minutes hours )
232     60 /rem swap ;
233
234 M: integer +minute ( timestamp n -- timestamp )
235     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
236
237 M: real +minute ( timestamp n -- timestamp )
238     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
239
240 : seconds/minutes ( n -- seconds minutes )
241     60 /rem swap >integer ;
242
243 M: number +second ( timestamp n -- timestamp )
244     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
245
246 : (time+) ( timestamp duration -- timestamp' duration )
247     [ second>> +second ] keep
248     [ minute>> +minute ] keep
249     [ hour>>   +hour   ] keep
250     [ day>>    +day    ] keep
251     [ month>>  +month  ] keep
252     [ year>>   +year   ] keep ; inline
253
254 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
255     [ bi@ + ] curry 2keep ; inline
256
257 PRIVATE>
258
259 GENERIC# time+ 1 ( time1 time2 -- time3 )
260
261 M: timestamp time+
262     [ clone ] dip (time+) drop ;
263
264 M: duration time+
265     dup timestamp? [
266         swap time+
267     ] [
268         [ year>> ] +slots
269         [ month>> ] +slots
270         [ day>> ] +slots
271         [ hour>> ] +slots
272         [ minute>> ] +slots
273         [ second>> ] +slots
274         2drop <duration>
275     ] if ;
276
277 : duration>years ( duration -- x )
278     #! Uses average month/year length since duration loses calendar
279     #! data
280     0 swap
281     {
282         [ year>> + ]
283         [ month>> months-per-year / + ]
284         [ day>> days-per-year / + ]
285         [ hour>> hours-per-year / + ]
286         [ minute>> minutes-per-year / + ]
287         [ second>> seconds-per-year / + ]
288     } cleave ;
289
290 M: duration <=> [ duration>years ] compare ;
291
292 : duration>months ( duration -- x ) duration>years months-per-year * ;
293 : duration>days ( duration -- x ) duration>years days-per-year * ;
294 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
295 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
296 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
297 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
298 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
299 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
300
301 GENERIC: time- ( time1 time2 -- time3 )
302
303 : convert-timezone ( timestamp duration -- timestamp' )
304     over gmt-offset>> over = [ drop ] [
305         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
306     ] if ;
307
308 : >local-time ( timestamp -- timestamp' )
309     gmt-offset-duration convert-timezone ;
310
311 : >gmt ( timestamp -- timestamp' )
312     dup gmt-offset>> dup instant =
313     [ drop ] [
314         [ neg +second 0 ] change-second
315         [ neg +minute 0 ] change-minute
316         [ neg +hour   0 ] change-hour
317         [ neg +day    0 ] change-day
318         [ neg +month  0 ] change-month
319         [ neg +year   0 ] change-year drop
320     ] if ;
321
322 M: timestamp <=> ( ts1 ts2 -- n )
323     [ >gmt tuple-slots ] compare ;
324
325 : same-day? ( ts1 ts2 -- ? )
326     [ >gmt >date< <date> ] bi@ = ;
327
328 : (time-) ( timestamp timestamp -- n )
329     [ >gmt ] bi@
330     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
331     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
332
333 M: timestamp time-
334     #! Exact calendar-time difference
335     (time-) seconds ;
336
337 : time* ( obj1 obj2 -- obj3 )
338     dup real? [ swap ] when
339     dup real? [ * ] [
340         {
341             [   year>> * ]
342             [  month>> * ]
343             [    day>> * ]
344             [   hour>> * ]
345             [ minute>> * ]
346             [ second>> * ]
347         } 2cleave <duration>
348     ] if ;
349
350 : before ( duration -- -duration )
351     -1 time* ;
352
353 <PRIVATE
354
355 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
356     [ bi@ - ] curry 2keep ; inline
357
358 PRIVATE>
359
360 M: duration time-
361     over timestamp? [
362         before time+
363     ] [
364         [ year>> ] -slots
365         [ month>> ] -slots
366         [ day>> ] -slots
367         [ hour>> ] -slots
368         [ minute>> ] -slots
369         [ second>> ] -slots
370         2drop <duration>
371     ] if ;
372
373 : <zero> ( -- timestamp )
374     0 0 0 <date-gmt> ; inline
375
376 : valid-timestamp? ( timestamp -- ? )
377     clone instant >>gmt-offset
378     dup <zero> time- <zero> time+ = ;
379
380 : unix-1970 ( -- timestamp )
381     1970 <year-gmt> ; inline
382
383 : millis>timestamp ( x -- timestamp )
384     [ unix-1970 ] dip 1000 / +second ;
385
386 : timestamp>millis ( timestamp -- n )
387     unix-1970 (time-) 1000 * >integer ;
388
389 : micros>timestamp ( x -- timestamp )
390     [ unix-1970 ] dip 1000000 / +second ;
391
392 : timestamp>micros ( timestamp -- n )
393     unix-1970 (time-) 1000000 * >integer ;
394
395 : now ( -- timestamp )
396     gmt gmt-offset-duration (time+) >>gmt-offset ;
397
398 : hence ( duration -- timestamp ) now swap time+ ;
399
400 : ago ( duration -- timestamp ) now swap time- ;
401
402 : zeller-congruence ( year month day -- n )
403     #! Zeller Congruence
404     #! http://web.textfiles.com/computers/formulas.txt
405     #! good for any date since October 15, 1582
406     [
407         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
408         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
409         [ 1 + 3 * 5 /i + ] keep 2 * +
410     ] dip 1 + + 7 mod ;
411
412 GENERIC: days-in-year ( obj -- n )
413
414 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
415 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
416
417 : (days-in-month) ( year month -- n )
418     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
419
420 : days-in-month ( timestamp -- n )
421     >date< drop (days-in-month) ;
422
423 : day-of-week ( timestamp -- n )
424     >date< zeller-congruence ;
425
426 GENERIC: day-name ( obj -- string )
427 M: integer day-name day-names nth ;
428 M: timestamp day-name day-of-week day-names nth ;
429
430 :: (day-of-year) ( year month day -- n )
431     day-counts month head-slice sum day +
432     year leap-year? [
433         year month day <date>
434         year 3 1 <date>
435         after=? [ 1 + ] when
436     ] when ;
437
438 : day-of-year ( timestamp -- n )
439     >date< (day-of-year) ;
440
441 : midnight ( timestamp -- new-timestamp )
442     clone 0 >>hour 0 >>minute 0 >>second ; inline
443
444 : noon ( timestamp -- new-timestamp )
445     midnight 12 >>hour ; inline
446
447 : today ( -- timestamp )
448     now midnight ; inline
449
450 : beginning-of-month ( timestamp -- new-timestamp )
451     midnight 1 >>day ; inline
452
453 : end-of-month ( timestamp -- new-timestamp )
454     [ midnight ] [ days-in-month ] bi >>day ;
455
456 <PRIVATE
457
458 : day-offset ( timestamp m -- new-timestamp n )
459     over day-of-week - ; inline
460
461 : day-this-week ( timestamp n -- new-timestamp )
462     day-offset days time+ ;
463
464 :: nth-day-this-month ( timestamp n day -- new-timestamp )
465     timestamp beginning-of-month day day-this-week
466     dup timestamp [ month>> ] bi@ = [ 1 weeks time+ ] unless
467     n 1 - [ weeks time+ ] unless-zero ;
468
469 : last-day-this-month ( timestamp day -- new-timestamp )
470     [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
471
472 PRIVATE>
473
474 GENERIC: january ( obj -- timestamp )
475 GENERIC: february ( obj -- timestamp )
476 GENERIC: march ( obj -- timestamp )
477 GENERIC: april ( obj -- timestamp )
478 GENERIC: may ( obj -- timestamp )
479 GENERIC: june ( obj -- timestamp )
480 GENERIC: july ( obj -- timestamp )
481 GENERIC: august ( obj -- timestamp )
482 GENERIC: september ( obj -- timestamp )
483 GENERIC: october ( obj -- timestamp )
484 GENERIC: november ( obj -- timestamp )
485 GENERIC: december ( obj -- timestamp )
486
487 M: integer january 1 1 <date> ;
488 M: integer february 2 1 <date> ;
489 M: integer march 3 1 <date> ;
490 M: integer april 4 1 <date> ;
491 M: integer may 5 1 <date> ;
492 M: integer june 6 1 <date> ;
493 M: integer july 7 1 <date> ;
494 M: integer august 8 1 <date> ;
495 M: integer september 9 1 <date> ;
496 M: integer october 10 1 <date> ;
497 M: integer november 11 1 <date> ;
498 M: integer december 12 1 <date> ;
499
500 M: timestamp january clone 1 >>month ;
501 M: timestamp february clone 2 >>month ;
502 M: timestamp march clone 3 >>month ;
503 M: timestamp april clone 4 >>month ;
504 M: timestamp may clone 5 >>month ;
505 M: timestamp june clone 6 >>month ;
506 M: timestamp july clone 7 >>month ;
507 M: timestamp august clone 8 >>month ;
508 M: timestamp september clone 9 >>month ;
509 M: timestamp october clone 10 >>month ;
510 M: timestamp november clone 11 >>month ;
511 M: timestamp december clone 12 >>month ;
512
513 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
514 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
515 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
516 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
517 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
518 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
519 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
520
521 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
522 : monday? ( timestamp -- ? ) day-of-week 1 = ;
523 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
524 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
525 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
526 : friday? ( timestamp -- ? ) day-of-week 5 = ;
527 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
528
529 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
530 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
531 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
532 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
533 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
534 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
535 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
536
537 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
538 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
539 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
540 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
541 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
542 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
543 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
544
545 : beginning-of-week ( timestamp -- new-timestamp )
546     midnight sunday ;
547
548 GENERIC: beginning-of-year ( object -- new-timestamp )
549 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
550 M: integer beginning-of-year <year> ;
551
552 GENERIC: end-of-year ( object -- new-timestamp )
553 M: timestamp end-of-year 12 >>month 31 >>day ;
554 M: integer end-of-year 12 31 <date> ;
555
556 : time-since-midnight ( timestamp -- duration )
557     dup midnight time- ; inline
558
559 : since-1970 ( duration -- timestamp )
560     unix-1970 time+ ; inline
561
562 : timestamp>unix-time ( timestamp -- seconds )
563     unix-1970 (time-) ; inline
564
565 : unix-time>timestamp ( seconds -- timestamp )
566     [ unix-1970 ] dip +second ; inline
567
568 {
569     { [ os unix? ] [ "calendar.unix" ] }
570     { [ os windows? ] [ "calendar.windows" ] }
571 } cond require
572
573 { "threads" "calendar" } "calendar.threads" require-when