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