]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
scryfall: better moxfield words
[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 literals math math.functions
5 math.intervals math.order math.statistics sequences slots.syntax
6 system vocabs vocabs.loader ;
7 FROM: ranges => [a..b) ;
8 IN: calendar
9
10 ERROR: not-in-interval value interval ;
11
12 : check-interval ( value interval -- value )
13     2dup interval-contains? [ drop ] [ not-in-interval ] if ;
14
15 HOOK: gmt-offset os ( -- hours minutes seconds )
16
17 HOOK: now-gmt os ( -- timestamp )
18
19 TUPLE: duration
20     { year real }
21     { month real }
22     { day real }
23     { hour real }
24     { minute real }
25     { second real } ;
26
27 C: <duration> duration
28
29 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
30
31 TUPLE: timestamp
32     { year integer }
33     { month integer }
34     { day integer }
35     { hour integer }
36     { minute integer }
37     { second real }
38     { gmt-offset duration } ;
39
40 <PRIVATE
41 <<
42 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
43 >>
44 CONSTANT: cumulative-day-counts $[ day-counts cum-sum0 ]
45
46 PRIVATE>
47
48 GENERIC: leap-year? ( obj -- ? )
49
50 M: integer leap-year?
51     dup 100 divisor? 400 4 ? divisor? ;
52
53 M: timestamp leap-year?
54     year>> leap-year? ;
55
56 : (days-in-month) ( year month -- n )
57     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
58
59 :: <timestamp> ( $year $month $day $hour $minute $second $gmt-offset -- timestamp )
60     $year
61     $month 1 12 [a,b] check-interval
62     $day 1 $year $month (days-in-month) [a,b] check-interval
63     $hour 0 23 [a,b] check-interval
64     $minute 0 59 [a,b] check-interval
65     $second 0 60 [a,b) check-interval
66     $gmt-offset timestamp boa ;
67
68 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
69
70 : gmt-offset-duration ( -- duration )
71     0 0 0 gmt-offset <duration> ; inline
72
73 : <date> ( year month day -- timestamp )
74     0 0 0 gmt-offset-duration <timestamp> ; inline
75
76 : <date-gmt> ( year month day -- timestamp )
77     0 0 0 instant <timestamp> ; inline
78
79 : <year> ( year -- timestamp )
80     1 1 <date> ; inline
81
82 : <year-gmt> ( year -- timestamp )
83     1 1 <date-gmt> ; inline
84
85 CONSTANT: average-month 30+5/12
86 CONSTANT: months-per-year 12
87 CONSTANT: days-per-year 3652425/10000
88 CONSTANT: hours-per-year 876582/100
89 CONSTANT: minutes-per-year 5259492/10
90 CONSTANT: seconds-per-year 31556952
91
92 :: julian-day-number ( $year $month $day -- n )
93     ! Returns a composite date number
94     ! Not valid before year -4800
95     14 $month - 12 /i :> $a
96     $year 4800 + $a - :> $y
97     $month 12 $a * + 3 - :> $m
98
99     $day 153 $m * 2 + 5 /i + 365 $y * +
100     $y 4 /i + $y 100 /i - $y 400 /i + 32045 - ;
101
102 :: julian-day-number>date ( $n -- year month day )
103     ! Inverse of julian-day-number
104     $n 32044 + :> $a
105     4 $a * 3 + 146097 /i :> $b
106     $a 146097 $b * 4 /i - :> $c
107     4 $c * 3 + 1461 /i :> $d
108     $c 1461 $d * 4 /i - :> $e
109     5 $e * 2 + 153 /i :> $m
110
111     100 $b * $d + 4800 -
112     $m 10 /i + $m 3 +
113     12 $m 10 /i * -
114     $e 153 $m * 2 + 5 /i - 1 + ;
115
116 GENERIC: easter ( obj -- obj' )
117
118 :: easter-month-day ( $year -- month day )
119     $year 19 mod :> $a
120     $year 100 /mod :> ( $b $c )
121     $b 4 /mod :> ( $d $e )
122     $b 8 + 25 /i :> $f
123     $b $f - 1 + 3 /i :> $g
124     19 $a * $b + $d - $g - 15 + 30 mod :> $h
125     $c 4 /mod :> ( $i $k )
126     32 2 $e * + 2 $i * + $h - $k - 7 mod :> $l
127     $a 11 $h * + 22 $l * + 451 /i :> $m
128
129     $h $l + 7 $m * - 114 + 31 /mod 1 + ;
130
131 M: integer easter
132     dup easter-month-day <date> ;
133
134 M: timestamp easter
135     clone dup year>> easter-month-day
136     swapd >>day swap >>month ;
137
138 : >date< ( timestamp -- year month day )
139     [ year>> ] [ month>> ] [ day>> ] tri ;
140
141 : set-date ( timestamp year month day -- timestamp )
142     [ >>year ] [ >>month ] [ >>day ] tri* ;
143
144 : >time< ( timestamp -- hour minute second )
145     [ hour>> ] [ minute>> ] [ second>> ] tri ;
146
147 : set-time ( timestamp hour minute second -- timestamp )
148     [ >>hour ] [ >>minute ] [ >>second ] tri* ;
149
150 : years ( x -- duration ) instant swap >>year ;
151 : bienniums ( x -- duration ) instant swap 2 * >>year ;
152 : trienniums ( x -- duration ) instant swap 3 * >>year ;
153 : quadrenniums ( x -- duration ) instant swap 4 * >>year ;
154 : quinquenniums ( x -- duration ) instant swap 5 * >>year ;
155 : sexenniums ( x -- duration ) instant swap 6 * >>year ;
156 : septenniums ( x -- duration ) instant swap 7 * >>year ;
157 : octenniums ( x -- duration ) instant swap 8 * >>year ;
158 : novenniums ( x -- duration ) instant swap 9 * >>year ;
159 : lustrums ( x -- duration ) instant swap 5 * >>year ;
160 : decades ( x -- duration ) instant swap 10 * >>year ;
161 : indictions ( x -- duration ) instant swap 15 * >>year ;
162 : score ( x -- duration ) instant swap 20 * >>year ;
163 : jubilees ( x -- duration ) instant swap 50 * >>year ;
164 : centuries ( x -- duration ) instant swap 100 * >>year ;
165 : millennia ( x -- duration ) instant swap 1000 * >>year ;
166 : millenniums ( x -- duration ) instant swap 1000 * >>year ;
167 : kila-annum ( x -- duration ) instant swap 1000 * >>year ;
168 : mega-annum ( x -- duration ) instant swap 1,000,000 * >>year ;
169 : giga-annum ( x -- duration ) instant swap 1,000,000,000 * >>year ;
170 : ages ( x -- duration ) instant swap 1,000,000 * >>year ;
171 : epochs ( x -- duration ) instant swap 10,000,000 * >>year ;
172 : eras ( x -- duration ) instant swap 100,000,000 * >>year ;
173 : eons ( x -- duration ) instant swap 500,000,000 * >>year ;
174 : months ( x -- duration ) instant swap >>month ;
175 : days ( x -- duration ) instant swap >>day ;
176 : weeks ( x -- duration ) 7 * days ;
177 : fortnight ( x -- duration ) 14 * days ;
178 : hours ( x -- duration ) instant swap >>hour ;
179 : minutes ( x -- duration ) instant swap >>minute ;
180 : seconds ( x -- duration ) instant swap >>second ;
181 : milliseconds ( x -- duration ) 1000 / seconds ;
182 : microseconds ( x -- duration ) 1000000 / seconds ;
183 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
184
185 DEFER: days-in-month
186
187 <PRIVATE
188
189 GENERIC: +year ( timestamp x -- timestamp )
190 GENERIC: +month ( timestamp x -- timestamp )
191 GENERIC: +day ( timestamp x -- timestamp )
192 GENERIC: +hour ( timestamp x -- timestamp )
193 GENERIC: +minute ( timestamp x -- timestamp )
194 GENERIC: +second ( timestamp x -- timestamp )
195
196 : /rem ( f n -- q r )
197     ! q is positive or negative, r is positive from 0 <= r < n
198     [ /mod ] keep over 0 < [ + [ -1 + ] dip ] [ drop ] if ; inline
199
200 : float>whole-part ( float -- int float )
201     [ floor >integer ] keep over - ; inline
202
203 : adjust-leap-year ( timestamp -- timestamp )
204     dup
205     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
206     [ 3 >>month 1 >>day ] when ;
207
208 M: integer +year
209     [ + ] curry change-year adjust-leap-year ;
210
211 M: real +year
212     float>whole-part swapd days-per-year * +day swap +year ;
213
214 : months/years ( n -- months years )
215     12 /rem [ 1 - 12 ] when-zero swap ; inline
216
217 M: integer +month
218     [
219         over month>> + months/years
220         [ >>month dup days-in-month '[ _ min ] change-day ] dip +year
221     ] unless-zero ;
222
223 M: real +month
224     float>whole-part swapd average-month * +day swap +month ;
225
226 M: integer +day
227     [ over >date< julian-day-number + julian-day-number>date set-date ] unless-zero ;
228
229 M: real +day
230     float>whole-part swapd 24 * +hour swap +day ;
231
232 : hours/days ( n -- hours days )
233     24 /rem swap ; inline
234
235 M: integer +hour
236     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
237
238 M: real +hour
239     float>whole-part swapd 60 * +minute swap +hour ;
240
241 : minutes/hours ( n -- minutes hours )
242     60 /rem swap ; inline
243
244 M: integer +minute
245     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
246
247 M: real +minute
248     float>whole-part swapd 60 * +second swap +minute ;
249
250 : seconds/minutes ( n -- seconds minutes )
251     60 /rem swap ; inline
252
253 M: number +second
254     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
255
256 : (time+) ( timestamp duration -- timestamp )
257     {
258         [ second>> +second ]
259         [ minute>> +minute ]
260         [ hour>>   +hour   ]
261         [ day>>    +day    ]
262         [ month>>  +month  ]
263         [ year>>   +year   ]
264      } cleave ; inline
265
266 PRIVATE>
267
268 GENERIC#: time+ 1 ( time1 time2 -- time3 )
269
270 M: timestamp time+ [ clone ] dip (time+) ;
271
272 : duration+ ( duration1 duration2 -- duration3 )
273     {
274         [ [ year>> ] bi@ + ]
275         [ [ month>> ] bi@ + ]
276         [ [ day>> ] bi@ + ]
277         [ [ hour>> ] bi@ + ]
278         [ [ minute>> ] bi@ + ]
279         [ [ second>> ] bi@ + ]
280     } 2cleave <duration> ; inline
281
282 M: duration time+
283     dup timestamp? [ swap time+ ] [ duration+ ] if ;
284
285 : duration>years ( duration -- x )
286     ! Uses average month/year length since duration loses calendar data
287     0 swap
288     {
289         [ year>> + ]
290         [ month>> months-per-year / + ]
291         [ day>> days-per-year / + ]
292         [ hour>> hours-per-year / + ]
293         [ minute>> minutes-per-year / + ]
294         [ second>> seconds-per-year / + ]
295     } cleave ;
296
297 M: duration <=> [ duration>years ] compare ;
298
299 : duration>months ( duration -- x ) duration>years months-per-year * ;
300 : duration>days ( duration -- x ) duration>years days-per-year * ;
301 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
302 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
303 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
304 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
305 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
306 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
307
308 DEFER: time-
309
310 : gmt ( timestamp -- timestamp )
311     instant >>gmt-offset ; inline
312
313 : local-time ( timestamp -- timestamp )
314     gmt-offset-duration >>gmt-offset ; inline
315
316 : convert-timezone ( timestamp duration -- timestamp )
317     [ over gmt-offset>> time- (time+) ] [ >>gmt-offset ] bi ;
318
319 : convert-local-time ( timestamp -- timestamp )
320     gmt-offset-duration convert-timezone ;
321
322 : convert-gmt ( timestamp -- timestamp )
323     instant convert-timezone ;
324
325 : >local-time ( timestamp -- timestamp' )
326     clone convert-local-time ;
327
328 : >gmt ( timestamp -- timestamp' )
329     clone convert-gmt ;
330
331 : >timezone ( timestamp duration -- timestamp' )
332     [ clone ] [ convert-timezone ] bi* ;
333
334 ALIAS: utc gmt
335 ALIAS: convert-utc convert-gmt
336 ALIAS: >utc >gmt
337
338 M: timestamp <=> [ >gmt tuple-slots ] compare ;
339
340 : same-year? ( ts1 ts2 -- ? )
341     [ year>> ] bi@ = ; inline
342
343 : quarter ( timestamp -- [1,4] )
344     month>> 3 /mod [ drop 1 + ] unless-zero ; inline
345
346 : same-quarter? ( ts1 ts2 -- ? )
347     [ [ year>> ] [ quarter ] bi 2array ] same? ;
348
349 : same-month? ( ts1 ts2 -- ? )
350     [ slots{ year month } ] same? ;
351
352 :: (day-of-year) ( $year $month $day -- n )
353     $month cumulative-day-counts nth $day + {
354         [ $year leap-year? ]
355         [ $month 3 >= ]
356     } 0&& [ 1 + ] when ;
357
358 : day-of-year ( timestamp -- n )
359     >date< (day-of-year) ;
360
361 : same-day? ( ts1 ts2 -- ? )
362     [ slots{ year month day } ] same? ;
363
364 : same-day-of-year? ( ts1 ts2 -- ? )
365     [ slots{ month day } ] same? ;
366
367 : (day-of-week) ( year month day -- n )
368     ! Zeller Congruence
369     ! http://web.textfiles.com/computers/formulas.txt
370     ! good for any date since October 15, 1582
371     [
372         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
373         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
374         [ 1 + 3 * 5 /i + ] keep 2 * +
375     ] dip 1 + + 7 mod ;
376
377 : day-of-week ( timestamp -- n )
378     >date< (day-of-week) ;
379
380 : (week-number) ( timestamp -- [0,53] )
381     [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
382
383 DEFER: end-of-year
384
385 : week-number ( timestamp -- [1,53] )
386     dup (week-number) {
387         {  0 [ year>> 1 - end-of-year (week-number) ] }
388         { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
389         [ nip ]
390     } case ;
391
392 : same-week? ( ts1 ts2 -- ? )
393     [ [ year>> ] [ week-number ] bi 2array ] same? ;
394
395 : same-hour? ( ts1 ts2 -- ? )
396     [ >gmt slots{ year month day hour } ] same? ;
397
398 : same-minute? ( ts1 ts2 -- ? )
399     [ >gmt slots{ year month day hour minute } ] same? ;
400
401 : same-second? ( ts1 ts2 -- ? )
402     [ >gmt ] bi@
403     {
404         [ [ second>> floor ] bi@ = ]
405         [ [ slots{ year month day hour minute } ] same? ]
406     } 2&& ;
407
408 <PRIVATE
409
410 : (time-) ( timestamp timestamp -- n )
411     [ [ >date< julian-day-number ] bi@ - 86400 * ]
412     [ [ >time< [ 3600 * ] [ 60 * + ] [ + ] tri* ] bi@ - + ]
413     [ [ gmt-offset>> duration>seconds ] bi@ swap - + ] 2tri ;
414
415 PRIVATE>
416
417 GENERIC: time- ( time1 time2 -- time3 )
418
419 M: timestamp time-
420     ! Exact calendar-time difference
421     (time-) seconds ;
422
423 : duration* ( obj1 obj2 -- obj3 )
424     dup real? [ swap ] when
425     dup real? [ * ] [
426         {
427             [   year>> * ]
428             [  month>> * ]
429             [    day>> * ]
430             [   hour>> * ]
431             [ minute>> * ]
432             [ second>> * ]
433         } 2cleave <duration>
434     ] if ;
435
436 : before ( duration -- -duration )
437     -1 duration* ;
438
439 : duration- ( duration1 duration2 -- duration3 )
440     {
441         [ [ year>> ] bi@ - ]
442         [ [ month>> ] bi@ - ]
443         [ [ day>> ] bi@ - ]
444         [ [ hour>> ] bi@ - ]
445         [ [ minute>> ] bi@ - ]
446         [ [ second>> ] bi@ - ]
447     } 2cleave <duration> ; inline
448
449 M: duration time-
450     over timestamp? [ before time+ ] [ duration- ] if ;
451
452 : unix-1970 ( -- timestamp )
453     1970 <year-gmt> ; inline
454
455 : millis>timestamp ( x -- timestamp )
456     unix-1970 swap 1000 / +second ;
457
458 : timestamp>millis ( timestamp -- n )
459     unix-1970 (time-) 1000 * >integer ;
460
461 : micros>timestamp ( x -- timestamp )
462     unix-1970 swap 1000000 / +second ;
463
464 : timestamp>micros ( timestamp -- n )
465     unix-1970 (time-) 1000000 * >integer ;
466
467 : now ( -- timestamp )
468     now-gmt gmt-offset-duration [ (time+) ] [ >>gmt-offset ] bi ;
469
470 : hence ( duration -- timestamp ) now swap time+ ;
471 : ago ( duration -- timestamp ) now swap time- ;
472 : days-since ( time -- n ) ago duration>days ;
473 : days-until ( time -- n ) now time- duration>days ;
474
475 GENERIC: days-in-year ( obj -- n )
476
477 M: integer days-in-year leap-year? 366 365 ? ;
478
479 M: timestamp days-in-year year>> days-in-year ;
480
481 : days-in-month ( timestamp -- n )
482     >date< drop (days-in-month) ;
483
484 : midnight ( timestamp -- timestamp' ) clone 0 0 0 set-time ; inline
485 : noon ( timestamp -- timestamp' ) clone 12 0 0 set-time ; inline
486
487 : today ( -- timestamp ) now midnight ; inline
488 : tomorrow ( -- timestamp ) 1 days hence midnight ; inline
489 : yesterday ( -- timestamp ) 1 days ago midnight ; inline
490 : overtomorrow ( -- timestamp ) 2 days hence midnight ; inline
491 : ereyesterday ( -- timestamp ) 2 days ago midnight ; inline
492
493 : today? ( timestamp -- ? ) now same-day? ; inline
494 : tomorrow? ( timestamp -- ? ) 1 days hence same-day? ; inline
495 : yesterday? ( timestamp -- ? ) 1 days ago same-day? ; inline
496
497 ALIAS: start-of-day midnight
498
499 : end-of-day ( timestamp -- timestamp' )
500     clone 23 >>hour 59 >>minute 59+999/1000 >>second ; inline
501
502 : start-of-month ( timestamp -- timestamp' )
503     midnight 1 >>day ; inline
504
505 : end-of-month ( timestamp -- timestamp' )
506     [ end-of-day ] [ days-in-month ] bi >>day ;
507
508 : start-of-quarter ( timestamp -- timestamp' )
509     [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
510
511 : end-of-quarter ( timestamp -- timestamp' )
512     dup quarter 1 - 3 * 3 + >>month end-of-month ; inline
513
514 : first-day-of-month ( timestamp -- timestamp' )
515     clone 1 >>day ;
516
517 : last-day-of-month ( timestamp -- timestamp' )
518     clone dup days-in-month >>day ; inline
519
520 GENERIC: first-day-of-year ( object -- timestamp )
521 M: timestamp first-day-of-year clone 1 >>month 1 >>day ;
522 M: integer first-day-of-year <year> ;
523
524 GENERIC: last-day-of-year ( object -- timestamp )
525 M: timestamp last-day-of-year clone 12 >>month 31 >>day ;
526 M: integer last-day-of-year 12 31 <date> ;
527
528 : first-day-of-decade ( object -- timestamp' )
529     first-day-of-year [ dup 10 mod - ] change-year ;
530
531 : last-day-of-decade ( object -- timestamp' )
532     last-day-of-year [ dup 10 mod - 9 + ] change-year ;
533
534 : first-day-of-century ( object -- timestamp' )
535     first-day-of-year [ dup 100 mod - ] change-year ;
536
537 : last-day-of-century ( object -- timestamp' )
538     last-day-of-year [ dup 100 mod - 99 + ] change-year ;
539
540 : first-day-of-millennium ( object -- timestamp' )
541     first-day-of-year [ dup 1000 mod - ] change-year ;
542
543 : last-day-of-millennium ( object -- timestamp' )
544     last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
545
546 : start-of-year ( object -- timestamp )
547     first-day-of-year start-of-day ;
548
549 : end-of-year ( object -- timestamp )
550     last-day-of-year end-of-day ;
551
552 : start-of-decade ( object -- timestamp )
553     first-day-of-decade start-of-day ;
554
555 : end-of-decade ( object -- timestamp )
556     last-day-of-decade end-of-day ;
557
558 : end-of-century ( object -- timestamp )
559     last-day-of-century end-of-day ;
560
561 : start-of-millennium ( object -- timestamp )
562     first-day-of-millennium start-of-day ;
563
564 : end-of-millennium ( object -- timestamp )
565     last-day-of-millennium end-of-day ;
566
567 : start-of-hour ( timestamp -- timestamp' ) clone 0 >>minute 0 >>second ;
568 : end-of-hour ( timestamp -- timestamp' ) clone 59 >>minute 59+999/1000 >>second ;
569
570 : start-of-minute ( timestamp -- timestamp' ) clone 0 >>second ;
571 : end-of-minute ( timestamp -- timestamp' ) clone 59+999/1000 >>second ;
572
573 : start-of-second ( timestamp -- timestamp' ) clone [ floor ] change-second ;
574 : end-of-second ( timestamp -- timestamp' ) clone [ floor 999/1000 + ] change-second ;
575
576 <PRIVATE
577
578 : day-offset ( timestamp m -- timestamp n )
579     over day-of-week - ; inline
580
581 : day-this-week ( timestamp n -- timestamp' )
582     day-offset days (time+) ;
583
584 : closest-day ( timestamp n -- timestamp' )
585     [ dup day-of-week 7 swap - ] [ + 7 mod ] bi*
586     { 0 1 2 3 -3 -2 -1 } nth days time+ ;
587
588 :: nth-day-this-month ( $timestamp $n $day -- timestamp' )
589     $timestamp clone
590     $timestamp start-of-month $day day-this-week
591     [ [ month>> ] same? ] keep swap
592     [ $n ] [ $n 1 + ] if weeks time+ ;
593
594 PRIVATE>
595
596 GENERIC: january ( obj -- timestamp' )
597 GENERIC: february ( obj -- timestamp' )
598 GENERIC: march ( obj -- timestamp' )
599 GENERIC: april ( obj -- timestamp' )
600 GENERIC: may ( obj -- timestamp' )
601 GENERIC: june ( obj -- timestamp' )
602 GENERIC: july ( obj -- timestamp' )
603 GENERIC: august ( obj -- timestamp' )
604 GENERIC: september ( obj -- timestamp' )
605 GENERIC: october ( obj -- timestamp' )
606 GENERIC: november ( obj -- timestamp' )
607 GENERIC: december ( obj -- timestamp' )
608
609 M: integer january 1 1 <date> ;
610 M: integer february 2 1 <date> ;
611 M: integer march 3 1 <date> ;
612 M: integer april 4 1 <date> ;
613 M: integer may 5 1 <date> ;
614 M: integer june 6 1 <date> ;
615 M: integer july 7 1 <date> ;
616 M: integer august 8 1 <date> ;
617 M: integer september 9 1 <date> ;
618 M: integer october 10 1 <date> ;
619 M: integer november 11 1 <date> ;
620 M: integer december 12 1 <date> ;
621
622 M: timestamp january clone 1 >>month ;
623 M: timestamp february clone 2 >>month ;
624 M: timestamp march clone 3 >>month ;
625 M: timestamp april clone 4 >>month ;
626 M: timestamp may clone 5 >>month ;
627 M: timestamp june clone 6 >>month ;
628 M: timestamp july clone 7 >>month ;
629 M: timestamp august clone 8 >>month ;
630 M: timestamp september clone 9 >>month ;
631 M: timestamp october clone 10 >>month ;
632 M: timestamp november clone 11 >>month ;
633 M: timestamp december clone 12 >>month ;
634
635 : closest-sunday ( timestamp -- timestamp' ) 0 closest-day ;
636 : closest-monday ( timestamp -- timestamp' ) 1 closest-day ;
637 : closest-tuesday ( timestamp -- timestamp' ) 2 closest-day ;
638 : closest-wednesday ( timestamp -- timestamp' ) 3 closest-day ;
639 : closest-thursday ( timestamp -- timestamp' ) 4 closest-day ;
640 : closest-friday ( timestamp -- timestamp' ) 5 closest-day ;
641 : closest-saturday ( timestamp -- timestamp' ) 6 closest-day ;
642
643 : sunday ( timestamp -- timestamp' ) 0 day-this-week ;
644 : monday ( timestamp -- timestamp' ) 1 day-this-week ;
645 : tuesday ( timestamp -- timestamp' ) 2 day-this-week ;
646 : wednesday ( timestamp -- timestamp' ) 3 day-this-week ;
647 : thursday ( timestamp -- timestamp' ) 4 day-this-week ;
648 : friday ( timestamp -- timestamp' ) 5 day-this-week ;
649 : saturday ( timestamp -- timestamp' ) 6 day-this-week ;
650
651 ALIAS: first-day-of-week sunday
652 ALIAS: last-day-of-week saturday
653
654 : day< ( timestamp quot -- timestamp' )
655     over clone [ call dup ] dip after=? [ -7 days time+ ] when ; inline
656 : day<= ( timestamp quot -- timestamp' )
657     over clone [ call dup ] dip after? [ -7 days time+ ] when ; inline
658 : day> ( timestamp quot -- timestamp' )
659     over clone [ call dup ] dip before=? [ 7 days time+ ] when ; inline
660 : day>= ( timestamp quot -- timestamp' )
661     over clone [ call dup ] dip before? [ 7 days time+ ] when ; inline
662
663 : sunday< ( timestamp -- timestamp' ) [ sunday ] day< ;
664 : monday< ( timestamp -- timestamp' ) [ monday ] day< ;
665 : tuesday< ( timestamp -- timestamp' ) [ tuesday ] day< ;
666 : wednesday< ( timestamp -- timestamp' ) [ wednesday ] day< ;
667 : thursday< ( timestamp -- timestamp' ) [ thursday ] day< ;
668 : friday< ( timestamp -- timestamp' ) [ friday ] day< ;
669 : saturday< ( timestamp -- timestamp' ) [ saturday ] day< ;
670
671 : sunday<= ( timestamp -- timestamp' ) [ sunday ] day<= ;
672 : monday<= ( timestamp -- timestamp' ) [ monday ] day<= ;
673 : tuesday<= ( timestamp -- timestamp' ) [ tuesday ] day<= ;
674 : wednesday<= ( timestamp -- timestamp' ) [ wednesday ] day<= ;
675 : thursday<= ( timestamp -- timestamp' ) [ thursday ] day<= ;
676 : friday<= ( timestamp -- timestamp' ) [ friday ] day<= ;
677 : saturday<= ( timestamp -- timestamp' ) [ saturday ] day<= ;
678
679 : sunday> ( timestamp -- timestamp' ) [ sunday ] day> ;
680 : monday> ( timestamp -- timestamp' ) [ monday ] day> ;
681 : tuesday> ( timestamp -- timestamp' ) [ tuesday ] day> ;
682 : wednesday> ( timestamp -- timestamp' ) [ wednesday ] day> ;
683 : thursday> ( timestamp -- timestamp' ) [ thursday ] day> ;
684 : friday> ( timestamp -- timestamp' ) [ friday ] day> ;
685 : saturday> ( timestamp -- timestamp' ) [ saturday ] day> ;
686
687 : sunday>= ( timestamp -- timestamp' ) [ sunday ] day>= ;
688 : monday>= ( timestamp -- timestamp' ) [ monday ] day>= ;
689 : tuesday>= ( timestamp -- timestamp' ) [ tuesday ] day>= ;
690 : wednesday>= ( timestamp -- timestamp' ) [ wednesday ] day>= ;
691 : thursday>= ( timestamp -- timestamp' ) [ thursday ] day>= ;
692 : friday>= ( timestamp -- timestamp' ) [ friday ] day>= ;
693 : saturday>= ( timestamp -- timestamp' ) [ saturday ] day>= ;
694
695 : next-sunday ( timestamp -- timestamp' ) closest-sunday sunday> ;
696 : next-monday ( timestamp -- timestamp' ) closest-monday monday> ;
697 : next-tuesday ( timestamp -- timestamp' ) closest-tuesday tuesday> ;
698 : next-wednesday ( timestamp -- timestamp' ) closest-wednesday wednesday> ;
699 : next-thursday ( timestamp -- timestamp' ) closest-thursday thursday> ;
700 : next-friday ( timestamp -- timestamp' ) closest-friday friday> ;
701 : next-saturday ( timestamp -- timestamp' ) closest-saturday saturday> ;
702
703 : last-sunday ( timestamp -- timestamp' ) closest-sunday sunday< ;
704 : last-monday ( timestamp -- timestamp' ) closest-monday monday< ;
705 : last-tuesday ( timestamp -- timestamp' ) closest-tuesday tuesday< ;
706 : last-wednesday ( timestamp -- timestamp' ) closest-wednesday wednesday< ;
707 : last-thursday ( timestamp -- timestamp' ) closest-thursday thursday< ;
708 : last-friday ( timestamp -- timestamp' ) closest-friday friday< ;
709 : last-saturday ( timestamp -- timestamp' ) closest-saturday saturday< ;
710
711 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
712 : monday? ( timestamp -- ? ) day-of-week 1 = ;
713 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
714 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
715 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
716 : friday? ( timestamp -- ? ) day-of-week 5 = ;
717 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
718
719 : january? ( timestamp -- ? ) month>> 1 = ;
720 : february? ( timestamp -- ? ) month>> 2 = ;
721 : march? ( timestamp -- ? ) month>> 3  = ;
722 : april? ( timestamp -- ? ) month>> 4 = ;
723 : may? ( timestamp -- ? ) month>> 5 = ;
724 : june? ( timestamp -- ? ) month>> 6 = ;
725 : july? ( timestamp -- ? ) month>> 7 = ;
726 : august? ( timestamp -- ? ) month>> 8 = ;
727 : september? ( timestamp -- ? ) month>> 9 = ;
728 : october? ( timestamp -- ? ) month>> 10 = ;
729 : november? ( timestamp -- ? ) month>> 11 = ;
730 : december? ( timestamp -- ? ) month>> 12 = ;
731
732 : weekend? ( timestamp -- ? ) day-of-week { 0 6 } member? ;
733 : weekday? ( timestamp -- ? ) day-of-week weekend? not ;
734
735 : same-or-next-business-day ( timestamp -- timestamp' )
736     dup day-of-week {
737         { 0 [ monday ] }
738         { 6 [ 2 days time+ ] }
739         [ drop ]
740     } case ;
741
742 : same-or-previous-business-day ( timestamp -- timestamp' )
743     dup day-of-week {
744         { 0 [ -2 days time+ ] }
745         { 6 [ friday ] }
746         [ drop ]
747     } case ;
748
749 : weekdays-between ( date1 date2 -- n )
750     [
751         [ swap time- duration>days 5 * ]
752         [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
753     ] 2keep
754     day-of-week 6 = [ [ 1 - ] dip ] when
755     day-of-week 0 = [ 1 - ] when ;
756
757 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
758
759 : weekdays-between2 ( date1 date2 -- n )
760     [ swap time- duration>days 1 + ]
761     [ [ day-of-week ] bi@ 6 swap - ] 2bi
762
763     [ + + 1.4 /i ]
764     [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
765
766 : sunday-of-month ( timestamp n -- timestamp' ) 0 nth-day-this-month ;
767 : monday-of-month ( timestamp n -- timestamp' ) 1 nth-day-this-month ;
768 : tuesday-of-month ( timestamp n -- timestamp' ) 2 nth-day-this-month ;
769 : wednesday-of-month ( timestamp n -- timestamp' ) 3 nth-day-this-month ;
770 : thursday-of-month ( timestamp n -- timestamp' ) 4 nth-day-this-month ;
771 : friday-of-month ( timestamp n -- timestamp' ) 5 nth-day-this-month ;
772 : saturday-of-month ( timestamp n -- timestamp' ) 6 nth-day-this-month ;
773
774 : last-sunday-of-month ( timestamp -- timestamp' ) last-day-of-month sunday<= ;
775 : last-monday-of-month ( timestamp -- timestamp' ) last-day-of-month monday<= ;
776 : last-tuesday-of-month ( timestamp -- timestamp' ) last-day-of-month tuesday<= ;
777 : last-wednesday-of-month ( timestamp -- timestamp' ) last-day-of-month wednesday<= ;
778 : last-thursday-of-month ( timestamp -- timestamp' ) last-day-of-month thursday<= ;
779 : last-friday-of-month ( timestamp -- timestamp' ) last-day-of-month friday<= ;
780 : last-saturday-of-month ( timestamp -- timestamp' ) last-day-of-month saturday<= ;
781
782 : start-of-week ( timestamp -- timestamp' )
783     sunday midnight ;
784
785 : end-of-week ( timestamp -- timestamp' )
786     saturday end-of-day ;
787
788 : o'clock ( timestamp n -- timestamp' )
789     [ midnight ] dip >>hour ;
790
791 : am ( timestamp n -- timestamp' )
792     1 12 [a,b] check-interval 12 mod o'clock ;
793
794 : pm ( timestamp n -- timestamp' )
795     1 12 [a,b] check-interval 12 mod 12 + o'clock ;
796
797 : time-since-midnight ( timestamp -- duration )
798     instant swap >time< set-time ;
799
800 : since-1970 ( duration -- timestamp )
801     unix-1970 swap (time+) ; inline
802
803 : timestamp>unix-time ( timestamp -- seconds )
804     unix-1970 (time-) ; inline
805
806 : unix-time>timestamp ( seconds -- timestamp )
807     unix-1970 swap +second ; inline
808
809 ! January and February need a fixup with this algorithm.
810 ! Find a better algorithm.
811 : ymd>ordinal ( year month day -- ordinal )
812     [ leap-year? dup -2 -3 ? ]
813     [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
814     [ ] tri* + + >integer
815     swap 367 366 ? mod ;
816
817 : timestamp>year-dates-gmt ( timestamp -- seq )
818     [ year>> 1 1 julian-day-number ] [ days-in-year ] bi
819     [ drop ] [ + ] 2bi
820     [a..b) [ julian-day-number>date <date-gmt> ] map ;
821
822 : year-ordinal>timestamp ( year ordinal -- timestamp )
823     [ 1 1 julian-day-number ] dip
824     + 1 - julian-day-number>date <date-gmt> ;
825
826 GENERIC: weeks-in-week-year ( obj -- n )
827
828 M: integer weeks-in-week-year
829     { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
830
831 M: timestamp weeks-in-week-year
832     { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
833
834 {
835     { [ os unix? ] [ "calendar.unix" ] }
836     { [ os windows? ] [ "calendar.windows" ] }
837 } cond require
838
839 { "threads" "calendar" } "calendar.threads" require-when