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.parser math.statistics sequences
6 slots.syntax splitting system vocabs vocabs.loader ;
7 FROM: math.ranges => [a..b) ;
10 ERROR: not-in-interval value interval ;
12 : check-interval ( value interval -- value )
13 2dup interval-contains? [ drop ] [ not-in-interval ] if ;
15 HOOK: gmt-offset os ( -- hours minutes seconds )
17 HOOK: now-gmt os ( -- timestamp )
27 C: <duration> duration
29 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
38 { gmt-offset duration } ;
42 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
44 CONSTANT: days-until $[ day-counts cum-sum0 ]
48 GENERIC: leap-year? ( obj -- ? )
51 dup 100 divisor? 400 4 ? divisor? ;
53 M: timestamp leap-year?
56 : (days-in-month) ( year month -- n )
57 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
59 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
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 ;
68 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
70 : gmt-offset-duration ( -- duration )
71 0 0 0 gmt-offset <duration> ; inline
73 : <date> ( year month day -- timestamp )
74 0 0 0 gmt-offset-duration <timestamp> ; inline
76 : <date-gmt> ( year month day -- timestamp )
77 0 0 0 instant <timestamp> ; inline
79 : <year> ( year -- timestamp )
82 : <year-gmt> ( year -- timestamp )
83 1 1 <date-gmt> ; inline
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
92 :: julian-day-number ( year month day -- n )
93 ! Returns a composite date number
94 ! Not valid before year -4800
97 month 12 a * + 3 - :> m
99 day 153 m * 2 + 5 /i + 365 y * +
100 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
102 :: julian-day-number>date ( n -- year month day )
103 ! Inverse of julian-day-number
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
114 e 153 m * 2 + 5 /i - 1 + ;
116 GENERIC: easter ( obj -- obj' )
118 :: easter-month-day ( year -- month day )
120 year 100 /mod :> ( b c )
124 19 a * b + d - g - 15 + 30 mod :> h
126 32 2 e * + 2 i * + h - k - 7 mod :> l
127 a 11 h * + 22 l * + 451 /i :> m
129 h l + 7 m * - 114 + 31 /mod 1 + ;
132 dup easter-month-day <date> ;
135 dup year>> easter-month-day
136 swapd >>day swap >>month ;
138 : >date< ( timestamp -- year month day )
139 [ year>> ] [ month>> ] [ day>> ] tri ;
141 : set-date ( timestamp year month day -- timestamp )
142 [ >>year ] [ >>month ] [ >>day ] tri* ;
144 : >time< ( timestamp -- hour minute second )
145 [ hour>> ] [ minute>> ] [ second>> ] tri ;
147 : set-time ( timestamp hour minute second -- timestamp )
148 [ >>hour ] [ >>minute ] [ >>second ] tri* ;
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 ;
187 GENERIC: +year ( timestamp x -- timestamp )
188 GENERIC: +month ( timestamp x -- timestamp )
189 GENERIC: +day ( timestamp x -- timestamp )
190 GENERIC: +hour ( timestamp x -- timestamp )
191 GENERIC: +minute ( timestamp x -- timestamp )
192 GENERIC: +second ( timestamp x -- timestamp )
194 : /rem ( f n -- q r )
195 ! q is positive or negative, r is positive from 0 <= r < n
196 [ /mod ] keep over 0 < [ + [ -1 + ] dip ] [ drop ] if ; inline
198 : float>whole-part ( float -- int float )
199 [ floor >integer ] keep over - ; inline
201 : adjust-leap-year ( timestamp -- timestamp )
203 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
204 [ 3 >>month 1 >>day ] when ;
207 [ + ] curry change-year adjust-leap-year ;
210 float>whole-part swapd days-per-year * +day swap +year ;
212 : months/years ( n -- months years )
213 12 /rem [ 1 - 12 ] when-zero swap ; inline
216 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
219 float>whole-part swapd average-month * +day swap +month ;
222 [ over >date< julian-day-number + julian-day-number>date set-date ] unless-zero ;
225 float>whole-part swapd 24 * +hour swap +day ;
227 : hours/days ( n -- hours days )
228 24 /rem swap ; inline
231 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
234 float>whole-part swapd 60 * +minute swap +hour ;
236 : minutes/hours ( n -- minutes hours )
237 60 /rem swap ; inline
240 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
243 float>whole-part swapd 60 * +second swap +minute ;
245 : seconds/minutes ( n -- seconds minutes )
246 60 /rem swap ; inline
249 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
251 : (time+) ( timestamp duration -- timestamp )
263 GENERIC#: time+ 1 ( time1 time2 -- time3 )
265 M: timestamp time+ [ clone ] dip (time+) ;
267 : duration+ ( duration1 duration2 -- duration3 )
270 [ [ month>> ] bi@ + ]
273 [ [ minute>> ] bi@ + ]
274 [ [ second>> ] bi@ + ]
275 } 2cleave <duration> ; inline
278 dup timestamp? [ swap time+ ] [ duration+ ] if ;
280 : duration>years ( duration -- x )
281 ! Uses average month/year length since duration loses calendar data
285 [ month>> months-per-year / + ]
286 [ day>> days-per-year / + ]
287 [ hour>> hours-per-year / + ]
288 [ minute>> minutes-per-year / + ]
289 [ second>> seconds-per-year / + ]
292 M: duration <=> [ duration>years ] compare ;
294 : duration>months ( duration -- x ) duration>years months-per-year * ;
295 : duration>days ( duration -- x ) duration>years days-per-year * ;
296 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
297 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
298 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
299 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
300 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
301 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
305 : gmt ( timestamp -- timestamp )
306 instant >>gmt-offset ; inline
308 : local-time ( timestamp -- timestamp )
309 gmt-offset-duration >>gmt-offset ; inline
311 : convert-timezone ( timestamp duration -- timestamp )
312 [ over gmt-offset>> time- (time+) ] [ >>gmt-offset ] bi ;
314 : convert-local-time ( timestamp -- timestamp )
315 gmt-offset-duration convert-timezone ;
317 : convert-gmt ( timestamp -- timestamp )
318 instant convert-timezone ;
320 : >local-time ( timestamp -- timestamp' )
321 clone convert-local-time ;
323 : >gmt ( timestamp -- timestamp' )
326 : >timezone ( timestamp duration -- timestamp' )
327 [ clone ] [ convert-timezone ] bi* ;
330 ALIAS: convert-utc convert-gmt
333 M: timestamp <=> [ >gmt tuple-slots ] compare ;
335 : same-year? ( ts1 ts2 -- ? )
336 [ >gmt slots{ year } ] same? ;
338 : quarter ( timestamp -- [1,4] )
339 month>> 3 /mod [ drop 1 + ] unless-zero ; inline
341 : same-quarter? ( ts1 ts2 -- ? )
342 [ >gmt [ year>> ] [ quarter ] bi 2array ] same? ;
344 : same-month? ( ts1 ts2 -- ? )
345 [ >gmt slots{ year month } ] same? ;
347 :: (day-of-year) ( year month day -- n )
348 month days-until nth day + {
353 : day-of-year ( timestamp -- n )
354 >date< (day-of-year) ;
356 : same-day? ( ts1 ts2 -- ? )
357 [ >gmt slots{ year month day } ] same? ;
359 : (day-of-week) ( year month day -- n )
361 ! http://web.textfiles.com/computers/formulas.txt
362 ! good for any date since October 15, 1582
364 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
365 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
366 [ 1 + 3 * 5 /i + ] keep 2 * +
369 : day-of-week ( timestamp -- n )
370 >date< (day-of-week) ;
372 : (week-number) ( timestamp -- [0,53] )
373 [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
377 : week-number ( timestamp -- [1,53] )
379 { 0 [ year>> 1 - end-of-year (week-number) ] }
380 { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
384 : same-week? ( ts1 ts2 -- ? )
385 [ >gmt [ year>> ] [ week-number ] bi 2array ] same? ;
387 : same-hour? ( ts1 ts2 -- ? )
388 [ >gmt slots{ year month day hour } ] same? ;
390 : same-minute? ( ts1 ts2 -- ? )
391 [ >gmt slots{ year month day hour minute } ] same? ;
393 : same-second? ( ts1 ts2 -- ? )
394 [ >gmt slots{ year month day hour minute second } ] same? ;
398 : (time-) ( timestamp timestamp -- n )
399 [ [ >date< julian-day-number ] bi@ - 86400 * ]
400 [ [ >time< [ 3600 * ] [ 60 * + ] [ + ] tri* ] bi@ - + ]
401 [ [ gmt-offset>> duration>seconds ] bi@ swap - + ] 2tri ;
405 GENERIC: time- ( time1 time2 -- time3 )
408 ! Exact calendar-time difference
411 : duration* ( obj1 obj2 -- obj3 )
412 dup real? [ swap ] when
424 : before ( duration -- -duration )
427 : duration- ( duration1 duration2 -- duration3 )
430 [ [ month>> ] bi@ - ]
433 [ [ minute>> ] bi@ - ]
434 [ [ second>> ] bi@ - ]
435 } 2cleave <duration> ; inline
438 over timestamp? [ before time+ ] [ duration- ] if ;
440 : unix-1970 ( -- timestamp )
441 1970 <year-gmt> ; inline
443 : millis>timestamp ( x -- timestamp )
444 unix-1970 swap 1000 / +second ;
446 : timestamp>millis ( timestamp -- n )
447 unix-1970 (time-) 1000 * >integer ;
449 : micros>timestamp ( x -- timestamp )
450 unix-1970 swap 1000000 / +second ;
452 : timestamp>micros ( timestamp -- n )
453 unix-1970 (time-) 1000000 * >integer ;
455 : now ( -- timestamp )
456 now-gmt gmt-offset-duration [ (time+) ] [ >>gmt-offset ] bi ;
458 : hence ( duration -- timestamp ) now swap time+ ;
459 : ago ( duration -- timestamp ) now swap time- ;
461 GENERIC: days-in-year ( obj -- n )
463 M: integer days-in-year leap-year? 366 365 ? ;
465 M: timestamp days-in-year year>> days-in-year ;
467 : days-in-month ( timestamp -- n )
468 >date< drop (days-in-month) ;
470 : midnight ( timestamp -- timestamp ) 0 0 0 set-time ; inline
471 : noon ( timestamp -- timestamp ) 12 0 0 set-time ; inline
473 : today ( -- timestamp ) now midnight ; inline
474 : tomorrow ( -- timestamp ) 1 days hence midnight ; inline
475 : yesterday ( -- timestamp ) 1 days ago midnight ; inline
476 : overtomorrow ( -- timestamp ) 2 days hence midnight ; inline
477 : ereyesterday ( -- timestamp ) 2 days ago midnight ; inline
479 : today? ( timestamp -- ? ) now same-day? ; inline
480 : tomorrow? ( timestamp -- ? ) 1 days hence same-day? ; inline
481 : yesterday? ( timestamp -- ? ) 1 days ago same-day? ; inline
483 ALIAS: start-of-day midnight
485 : end-of-day ( timestamp -- timestamp )
486 23 >>hour 59 >>minute 59+999/1000 >>second ; inline
488 : start-of-month ( timestamp -- timestamp )
489 midnight 1 >>day ; inline
491 : end-of-month ( timestamp -- timestamp )
492 [ end-of-day ] [ days-in-month ] bi >>day ;
494 : start-of-quarter ( timestamp -- timestamp )
495 [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
497 : end-of-quarter ( timestamp -- timestamp )
498 dup quarter 1 - 3 * 3 + >>month end-of-month ; inline
500 : first-day-of-month ( timestamp -- timestamp )
503 : last-day-of-month ( timestamp -- timestamp )
504 dup days-in-month >>day ; inline
506 GENERIC: first-day-of-year ( object -- timestamp )
507 M: timestamp first-day-of-year first-day-of-month 1 >>month ;
508 M: integer first-day-of-year <year> ;
510 GENERIC: last-day-of-year ( object -- timestamp )
511 M: timestamp last-day-of-year 12 >>month 31 >>day ;
512 M: integer last-day-of-year 12 31 <date> ;
514 : first-day-of-decade ( object -- timestamp )
515 first-day-of-year [ dup 10 mod - ] change-year ;
517 : last-day-of-decade ( object -- timestamp )
518 last-day-of-year [ dup 10 mod - 9 + ] change-year ;
520 : first-day-of-century ( object -- timestamp )
521 first-day-of-year [ dup 100 mod - ] change-year ;
523 : last-day-of-century ( object -- timestamp )
524 last-day-of-year [ dup 100 mod - 99 + ] change-year ;
526 : first-day-of-millennium ( object -- timestamp )
527 first-day-of-year [ dup 1000 mod - ] change-year ;
529 : last-day-of-millennium ( object -- timestamp )
530 last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
532 : start-of-year ( object -- timestamp )
533 first-day-of-year start-of-day ;
535 : end-of-year ( object -- timestamp )
536 last-day-of-year end-of-day ;
538 : start-of-decade ( object -- timestamp )
539 first-day-of-decade start-of-day ;
541 : end-of-decade ( object -- timestamp )
542 last-day-of-decade end-of-day ;
544 : end-of-century ( object -- timestamp )
545 last-day-of-century end-of-day ;
547 : start-of-millennium ( object -- timestamp )
548 first-day-of-millennium start-of-day ;
550 : end-of-millennium ( object -- timestamp )
551 last-day-of-millennium end-of-day ;
553 : start-of-hour ( timestamp -- timestamp ) 0 >>minute 0 >>second ;
554 : end-of-hour ( timestamp -- timestamp ) 59 >>minute 59+999/1000 >>second ;
556 : start-of-minute ( timestamp -- timestamp ) 0 >>second ;
557 : end-of-minute ( timestamp -- timestamp ) 59+999/1000 >>second ;
559 : start-of-second ( timestamp -- timestamp ) [ floor ] change-second ;
560 : end-of-second ( timestamp -- timestamp ) [ floor 999/1000 + ] change-second ;
564 : day-offset ( timestamp m -- timestamp n )
565 over day-of-week - ; inline
567 : day-this-week ( timestamp n -- timestamp )
568 day-offset days (time+) ;
570 : closest-day ( timestamp n -- timestamp )
571 [ dup day-of-week 7 swap - ] [ + 7 mod ] bi*
572 { 0 1 2 3 -3 -2 -1 } nth days (time+) ;
574 :: nth-day-this-month ( timestamp n day -- timestamp )
576 timestamp start-of-month day day-this-week
577 [ [ month>> ] same? ] keep swap
578 [ 1 weeks (time+) ] unless
579 n [ weeks (time+) ] unless-zero ;
583 GENERIC: january ( obj -- timestamp )
584 GENERIC: february ( obj -- timestamp )
585 GENERIC: march ( obj -- timestamp )
586 GENERIC: april ( obj -- timestamp )
587 GENERIC: may ( obj -- timestamp )
588 GENERIC: june ( obj -- timestamp )
589 GENERIC: july ( obj -- timestamp )
590 GENERIC: august ( obj -- timestamp )
591 GENERIC: september ( obj -- timestamp )
592 GENERIC: october ( obj -- timestamp )
593 GENERIC: november ( obj -- timestamp )
594 GENERIC: december ( obj -- timestamp )
596 M: integer january 1 1 <date> ;
597 M: integer february 2 1 <date> ;
598 M: integer march 3 1 <date> ;
599 M: integer april 4 1 <date> ;
600 M: integer may 5 1 <date> ;
601 M: integer june 6 1 <date> ;
602 M: integer july 7 1 <date> ;
603 M: integer august 8 1 <date> ;
604 M: integer september 9 1 <date> ;
605 M: integer october 10 1 <date> ;
606 M: integer november 11 1 <date> ;
607 M: integer december 12 1 <date> ;
609 M: timestamp january 1 >>month ;
610 M: timestamp february 2 >>month ;
611 M: timestamp march 3 >>month ;
612 M: timestamp april 4 >>month ;
613 M: timestamp may 5 >>month ;
614 M: timestamp june 6 >>month ;
615 M: timestamp july 7 >>month ;
616 M: timestamp august 8 >>month ;
617 M: timestamp september 9 >>month ;
618 M: timestamp october 10 >>month ;
619 M: timestamp november 11 >>month ;
620 M: timestamp december 12 >>month ;
622 : closest-sunday ( timestamp -- timestamp ) 0 closest-day ;
623 : closest-monday ( timestamp -- timestamp ) 1 closest-day ;
624 : closest-tuesday ( timestamp -- timestamp ) 2 closest-day ;
625 : closest-wednesday ( timestamp -- timestamp ) 3 closest-day ;
626 : closest-thursday ( timestamp -- timestamp ) 4 closest-day ;
627 : closest-friday ( timestamp -- timestamp ) 5 closest-day ;
628 : closest-saturday ( timestamp -- timestamp ) 6 closest-day ;
630 : sunday ( timestamp -- timestamp ) 0 day-this-week ;
631 : monday ( timestamp -- timestamp ) 1 day-this-week ;
632 : tuesday ( timestamp -- timestamp ) 2 day-this-week ;
633 : wednesday ( timestamp -- timestamp ) 3 day-this-week ;
634 : thursday ( timestamp -- timestamp ) 4 day-this-week ;
635 : friday ( timestamp -- timestamp ) 5 day-this-week ;
636 : saturday ( timestamp -- timestamp ) 6 day-this-week ;
638 ALIAS: first-day-of-week sunday
639 ALIAS: last-day-of-week saturday
641 : day< ( timestamp quot -- timestamp )
642 over clone [ call dup ] dip after=? [ -7 days (time+) ] when ; inline
643 : day<= ( timestamp quot -- timestamp )
644 over clone [ call dup ] dip after? [ -7 days (time+) ] when ; inline
645 : day> ( timestamp quot -- timestamp )
646 over clone [ call dup ] dip before=? [ 7 days (time+) ] when ; inline
647 : day>= ( timestamp quot -- timestamp )
648 over clone [ call dup ] dip before? [ 7 days (time+) ] when ; inline
650 : sunday< ( timestamp -- timestamp ) [ sunday ] day< ;
651 : monday< ( timestamp -- timestamp ) [ monday ] day< ;
652 : tuesday< ( timestamp -- timestamp ) [ tuesday ] day< ;
653 : wednesday< ( timestamp -- timestamp ) [ wednesday ] day< ;
654 : thursday< ( timestamp -- timestamp ) [ thursday ] day< ;
655 : friday< ( timestamp -- timestamp ) [ friday ] day< ;
656 : saturday< ( timestamp -- timestamp ) [ saturday ] day< ;
658 : sunday<= ( timestamp -- timestamp ) [ sunday ] day<= ;
659 : monday<= ( timestamp -- timestamp ) [ monday ] day<= ;
660 : tuesday<= ( timestamp -- timestamp ) [ tuesday ] day<= ;
661 : wednesday<= ( timestamp -- timestamp ) [ wednesday ] day<= ;
662 : thursday<= ( timestamp -- timestamp ) [ thursday ] day<= ;
663 : friday<= ( timestamp -- timestamp ) [ friday ] day<= ;
664 : saturday<= ( timestamp -- timestamp ) [ saturday ] day<= ;
666 : sunday> ( timestamp -- timestamp ) [ sunday ] day> ;
667 : monday> ( timestamp -- timestamp ) [ monday ] day> ;
668 : tuesday> ( timestamp -- timestamp ) [ tuesday ] day> ;
669 : wednesday> ( timestamp -- timestamp ) [ wednesday ] day> ;
670 : thursday> ( timestamp -- timestamp ) [ thursday ] day> ;
671 : friday> ( timestamp -- timestamp ) [ friday ] day> ;
672 : saturday> ( timestamp -- timestamp ) [ saturday ] day> ;
674 : sunday>= ( timestamp -- timestamp ) [ sunday ] day>= ;
675 : monday>= ( timestamp -- timestamp ) [ monday ] day>= ;
676 : tuesday>= ( timestamp -- timestamp ) [ tuesday ] day>= ;
677 : wednesday>= ( timestamp -- timestamp ) [ wednesday ] day>= ;
678 : thursday>= ( timestamp -- timestamp ) [ thursday ] day>= ;
679 : friday>= ( timestamp -- timestamp ) [ friday ] day>= ;
680 : saturday>= ( timestamp -- timestamp ) [ saturday ] day>= ;
682 : next-sunday ( timestamp -- timestamp ) closest-sunday sunday> ;
683 : next-monday ( timestamp -- timestamp ) closest-monday monday> ;
684 : next-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday> ;
685 : next-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday> ;
686 : next-thursday ( timestamp -- timestamp ) closest-thursday thursday> ;
687 : next-friday ( timestamp -- timestamp ) closest-friday friday> ;
688 : next-saturday ( timestamp -- timestamp ) closest-saturday saturday> ;
690 : last-sunday ( timestamp -- timestamp ) closest-sunday sunday< ;
691 : last-monday ( timestamp -- timestamp ) closest-monday monday< ;
692 : last-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday< ;
693 : last-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday< ;
694 : last-thursday ( timestamp -- timestamp ) closest-thursday thursday< ;
695 : last-friday ( timestamp -- timestamp ) closest-friday friday< ;
696 : last-saturday ( timestamp -- timestamp ) closest-saturday saturday< ;
698 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
699 : monday? ( timestamp -- ? ) day-of-week 1 = ;
700 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
701 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
702 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
703 : friday? ( timestamp -- ? ) day-of-week 5 = ;
704 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
706 : january? ( timestamp -- ? ) month>> 1 = ;
707 : february? ( timestamp -- ? ) month>> 2 = ;
708 : march? ( timestamp -- ? ) month>> 3 = ;
709 : april? ( timestamp -- ? ) month>> 4 = ;
710 : may? ( timestamp -- ? ) month>> 5 = ;
711 : june? ( timestamp -- ? ) month>> 6 = ;
712 : july? ( timestamp -- ? ) month>> 7 = ;
713 : august? ( timestamp -- ? ) month>> 8 = ;
714 : september? ( timestamp -- ? ) month>> 9 = ;
715 : october? ( timestamp -- ? ) month>> 10 = ;
716 : november? ( timestamp -- ? ) month>> 11 = ;
717 : december? ( timestamp -- ? ) month>> 12 = ;
719 : weekend? ( timestamp -- ? ) day-of-week { 0 6 } member? ;
720 : weekday? ( timestamp -- ? ) day-of-week weekend? not ;
722 : same-or-next-business-day ( timestamp -- timestamp )
725 { 6 [ 2 days (time+) ] }
729 : same-or-previous-business-day ( timestamp -- timestamp )
731 { 0 [ -2 days (time+) ] }
736 : weekdays-between ( date1 date2 -- n )
738 [ swap time- duration>days 5 * ]
739 [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
741 day-of-week 6 = [ [ 1 - ] dip ] when
742 day-of-week 0 = [ 1 - ] when ;
744 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
746 : weekdays-between2 ( date1 date2 -- n )
747 [ swap time- duration>days 1 + ]
748 [ [ day-of-week ] bi@ 6 swap - ] 2bi
751 [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
753 : sunday-of-month ( timestamp n -- timestamp ) 0 nth-day-this-month ;
754 : monday-of-month ( timestamp n -- timestamp ) 1 nth-day-this-month ;
755 : tuesday-of-month ( timestamp n -- timestamp ) 2 nth-day-this-month ;
756 : wednesday-of-month ( timestamp n -- timestamp ) 3 nth-day-this-month ;
757 : thursday-of-month ( timestamp n -- timestamp ) 4 nth-day-this-month ;
758 : friday-of-month ( timestamp n -- timestamp ) 5 nth-day-this-month ;
759 : saturday-of-month ( timestamp n -- timestamp ) 6 nth-day-this-month ;
761 : last-sunday-of-month ( timestamp -- timestamp ) last-day-of-month sunday<= ;
762 : last-monday-of-month ( timestamp -- timestamp ) last-day-of-month monday<= ;
763 : last-tuesday-of-month ( timestamp -- timestamp ) last-day-of-month tuesday<= ;
764 : last-wednesday-of-month ( timestamp -- timestamp ) last-day-of-month wednesday<= ;
765 : last-thursday-of-month ( timestamp -- timestamp ) last-day-of-month thursday<= ;
766 : last-friday-of-month ( timestamp -- timestamp ) last-day-of-month friday<= ;
767 : last-saturday-of-month ( timestamp -- timestamp ) last-day-of-month saturday<= ;
769 : start-of-week ( timestamp -- timestamp )
772 : end-of-week ( timestamp -- timestamp )
773 saturday end-of-day ;
775 : o'clock ( timestamp n -- timestamp )
776 [ midnight ] dip >>hour ;
778 : am ( timestamp n -- timestamp )
779 1 12 [a,b] check-interval 12 mod o'clock ;
781 : pm ( timestamp n -- timestamp )
782 1 12 [a,b] check-interval 12 mod 12 + o'clock ;
784 : time-since-midnight ( timestamp -- duration )
785 instant swap >time< set-time ;
787 : since-1970 ( duration -- timestamp )
788 unix-1970 swap (time+) ; inline
790 : timestamp>unix-time ( timestamp -- seconds )
791 unix-1970 (time-) ; inline
793 : unix-time>timestamp ( seconds -- timestamp )
794 unix-1970 swap +second ; inline
796 ! January and February need a fixup with this algorithm.
797 ! Find a better algorithm.
798 : ymd>ordinal ( year month day -- ordinal )
799 [ leap-year? dup -2 -3 ? ]
800 [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
801 [ ] tri* + + >integer
804 : timestamp>year-dates-gmt ( timestamp -- seq )
805 [ year>> 1 1 julian-day-number ] [ days-in-year ] bi
807 [a..b) [ julian-day-number>date <date-gmt> ] map ;
809 : year-ordinal>timestamp ( year ordinal -- timestamp )
810 [ 1 1 julian-day-number ] dip
811 + 1 - julian-day-number>date <date> ;
813 GENERIC: weeks-in-week-year ( obj -- n )
815 M: integer weeks-in-week-year
816 { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
818 M: timestamp weeks-in-week-year
819 { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
822 { [ os unix? ] [ "calendar.unix" ] }
823 { [ os windows? ] [ "calendar.windows" ] }
826 { "threads" "calendar" } "calendar.threads" require-when