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 sequences.rotated slots.syntax splitting system vocabs
8 FROM: math.ranges => [a..b) ;
11 ERROR: not-in-interval value interval ;
13 : check-interval ( value interval -- value )
14 2dup interval-contains? [ drop ] [ not-in-interval ] if ;
16 HOOK: gmt-offset os ( -- hours minutes seconds )
18 HOOK: now-gmt os ( -- timestamp )
28 C: <duration> duration
30 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
39 { gmt-offset duration } ;
43 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
45 CONSTANT: days-until $[ day-counts cum-sum0 ]
49 GENERIC: leap-year? ( obj -- ? )
52 dup 100 divisor? 400 4 ? divisor? ;
54 M: timestamp leap-year?
57 : (days-in-month) ( year month -- n )
58 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
60 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
62 month 1 12 [a,b] check-interval
63 day 1 year month (days-in-month) [a,b] check-interval
64 hour 0 23 [a,b] check-interval
65 minute 0 59 [a,b] check-interval
66 second 0 60 [a,b) check-interval
67 gmt-offset timestamp boa ;
69 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
71 : gmt-offset-duration ( -- duration )
72 0 0 0 gmt-offset <duration> ; inline
74 : <date> ( year month day -- timestamp )
75 0 0 0 gmt-offset-duration <timestamp> ; inline
77 : <date-gmt> ( year month day -- timestamp )
78 0 0 0 instant <timestamp> ; inline
80 : <year> ( year -- timestamp )
83 : <year-gmt> ( year -- timestamp )
84 1 1 <date-gmt> ; inline
86 CONSTANT: average-month 30+5/12
87 CONSTANT: months-per-year 12
88 CONSTANT: days-per-year 3652425/10000
89 CONSTANT: hours-per-year 876582/100
90 CONSTANT: minutes-per-year 5259492/10
91 CONSTANT: seconds-per-year 31556952
93 :: julian-day-number ( year month day -- n )
94 ! Returns a composite date number
95 ! Not valid before year -4800
98 month 12 a * + 3 - :> m
100 day 153 m * 2 + 5 /i + 365 y * +
101 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
103 :: julian-day-number>date ( n -- year month day )
104 ! Inverse of julian-day-number
106 4 a * 3 + 146097 /i :> b
107 a 146097 b * 4 /i - :> c
108 4 c * 3 + 1461 /i :> d
109 c 1461 d * 4 /i - :> e
110 5 e * 2 + 153 /i :> m
115 e 153 m * 2 + 5 /i - 1 + ;
117 GENERIC: easter ( obj -- obj' )
119 :: easter-month-day ( year -- month day )
121 year 100 /mod :> ( b c )
125 19 a * b + d - g - 15 + 30 mod :> h
127 32 2 e * + 2 i * + h - k - 7 mod :> l
128 a 11 h * + 22 l * + 451 /i :> m
130 h l + 7 m * - 114 + 31 /mod 1 + ;
133 dup easter-month-day <date> ;
136 dup year>> easter-month-day
137 swapd >>day swap >>month ;
139 : >date< ( timestamp -- year month day )
140 [ year>> ] [ month>> ] [ day>> ] tri ;
142 : set-date ( timestamp year month day -- timestamp )
143 [ >>year ] [ >>month ] [ >>day ] tri* ;
145 : >time< ( timestamp -- hour minute second )
146 [ hour>> ] [ minute>> ] [ second>> ] tri ;
148 : set-time ( timestamp hour minute second -- timestamp )
149 [ >>hour ] [ >>minute ] [ >>second ] tri* ;
151 : years ( x -- duration ) instant swap >>year ;
152 : bienniums ( x -- duration ) instant swap 2 * >>year ;
153 : trienniums ( x -- duration ) instant swap 3 * >>year ;
154 : quadrenniums ( x -- duration ) instant swap 4 * >>year ;
155 : quinquenniums ( x -- duration ) instant swap 5 * >>year ;
156 : sexenniums ( x -- duration ) instant swap 6 * >>year ;
157 : septenniums ( x -- duration ) instant swap 7 * >>year ;
158 : octenniums ( x -- duration ) instant swap 8 * >>year ;
159 : novenniums ( x -- duration ) instant swap 9 * >>year ;
160 : lustrums ( x -- duration ) instant swap 5 * >>year ;
161 : decades ( x -- duration ) instant swap 10 * >>year ;
162 : indictions ( x -- duration ) instant swap 15 * >>year ;
163 : score ( x -- duration ) instant swap 20 * >>year ;
164 : jubilees ( x -- duration ) instant swap 50 * >>year ;
165 : centuries ( x -- duration ) instant swap 100 * >>year ;
166 : millennia ( x -- duration ) instant swap 1000 * >>year ;
167 : millenniums ( x -- duration ) instant swap 1000 * >>year ;
168 : kila-annum ( x -- duration ) instant swap 1000 * >>year ;
169 : mega-annum ( x -- duration ) instant swap 1,000,000 * >>year ;
170 : giga-annum ( x -- duration ) instant swap 1,000,000,000 * >>year ;
171 : ages ( x -- duration ) instant swap 1,000,000 * >>year ;
172 : epochs ( x -- duration ) instant swap 10,000,000 * >>year ;
173 : eras ( x -- duration ) instant swap 100,000,000 * >>year ;
174 : eons ( x -- duration ) instant swap 500,000,000 * >>year ;
175 : months ( x -- duration ) instant swap >>month ;
176 : days ( x -- duration ) instant swap >>day ;
177 : weeks ( x -- duration ) 7 * days ;
178 : fortnight ( x -- duration ) 14 * days ;
179 : hours ( x -- duration ) instant swap >>hour ;
180 : minutes ( x -- duration ) instant swap >>minute ;
181 : seconds ( x -- duration ) instant swap >>second ;
182 : milliseconds ( x -- duration ) 1000 / seconds ;
183 : microseconds ( x -- duration ) 1000000 / seconds ;
184 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
188 GENERIC: +year ( timestamp x -- timestamp )
189 GENERIC: +month ( timestamp x -- timestamp )
190 GENERIC: +day ( timestamp x -- timestamp )
191 GENERIC: +hour ( timestamp x -- timestamp )
192 GENERIC: +minute ( timestamp x -- timestamp )
193 GENERIC: +second ( timestamp x -- timestamp )
195 : /rem ( f n -- q r )
196 ! q is positive or negative, r is positive from 0 <= r < n
197 [ / floor >integer ] 2keep rem ;
199 : float>whole-part ( float -- int float )
200 [ floor >integer ] keep over - ;
202 : adjust-leap-year ( timestamp -- timestamp )
204 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
205 [ 3 >>month 1 >>day ] when ;
208 [ + ] curry change-year adjust-leap-year ;
211 float>whole-part swapd days-per-year * +day swap +year ;
213 : months/years ( n -- months years )
214 12 /rem [ 1 - 12 ] when-zero swap ; inline
217 over month>> + months/years [ >>month ] dip +year ;
220 float>whole-part swapd average-month * +day swap +month ;
223 over >date< julian-day-number + julian-day-number>date
224 [ >>year ] [ >>month ] [ >>day ] tri* ;
227 float>whole-part swapd 24 * +hour swap +day ;
229 : hours/days ( n -- hours days )
233 over hour>> + hours/days [ >>hour ] dip +day ;
236 float>whole-part swapd 60 * +minute swap +hour ;
238 : minutes/hours ( n -- minutes hours )
242 over minute>> + minutes/hours [ >>minute ] dip +hour ;
245 float>whole-part swapd 60 * +second swap +minute ;
247 : seconds/minutes ( n -- seconds minutes )
248 60 /rem swap >integer ;
251 over second>> + seconds/minutes [ >>second ] dip +minute ;
253 : (time+) ( timestamp duration -- timestamp )
265 GENERIC#: time+ 1 ( time1 time2 -- time3 )
267 M: timestamp time+ [ clone ] dip (time+) ;
269 : duration+ ( duration1 duration2 -- duration3 )
272 [ [ month>> ] bi@ + ]
275 [ [ minute>> ] bi@ + ]
276 [ [ second>> ] bi@ + ]
277 } 2cleave <duration> ; inline
280 dup timestamp? [ swap time+ ] [ duration+ ] if ;
282 : duration>years ( duration -- x )
283 ! Uses average month/year length since duration loses calendar data
287 [ month>> months-per-year / + ]
288 [ day>> days-per-year / + ]
289 [ hour>> hours-per-year / + ]
290 [ minute>> minutes-per-year / + ]
291 [ second>> seconds-per-year / + ]
294 M: duration <=> [ duration>years ] compare ;
296 : duration>months ( duration -- x ) duration>years months-per-year * ;
297 : duration>days ( duration -- x ) duration>years days-per-year * ;
298 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
299 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
300 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
301 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
302 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
303 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
307 : gmt ( timestamp -- timestamp )
308 instant >>gmt-offset ; inline
310 : local-time ( timestamp -- timestamp )
311 gmt-offset-duration >>gmt-offset ; inline
313 : convert-timezone ( timestamp duration -- timestamp )
314 [ over gmt-offset>> time- (time+) ] [ >>gmt-offset ] bi ;
316 : >local-time ( timestamp -- timestamp )
317 gmt-offset-duration convert-timezone ;
319 : >gmt ( timestamp -- timestamp )
320 instant convert-timezone ;
322 M: timestamp <=> [ clone >gmt tuple-slots ] compare ;
326 : same-times? ( timestamp1 timestamp2 quot -- ? )
327 [ clone >gmt ] prepose same? ; inline
331 : same-year? ( ts1 ts2 -- ? )
332 [ slots{ year } ] same-times? ;
334 : quarter ( timestamp -- [1,4] )
335 month>> 3 /i 1 + ; inline
337 : same-quarter? ( ts1 ts2 -- ? )
338 [ [ year>> ] [ quarter ] bi 2array ] same-times? ;
340 : same-month? ( ts1 ts2 -- ? )
341 [ slots{ year month } ] same-times? ;
343 :: (day-of-year) ( year month day -- n )
344 month days-until nth day + {
349 : day-of-year ( timestamp -- n )
350 >date< (day-of-year) ;
352 : same-day? ( ts1 ts2 -- ? )
353 [ slots{ year month day } ] same-times? ;
355 : (day-of-week) ( year month day -- n )
357 ! http://web.textfiles.com/computers/formulas.txt
358 ! good for any date since October 15, 1582
360 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
361 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
362 [ 1 + 3 * 5 /i + ] keep 2 * +
365 : day-of-week ( timestamp -- n )
366 >date< (day-of-week) ;
368 : (week-number) ( timestamp -- [0,53] )
369 [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
373 : week-number ( timestamp -- [1,53] )
375 { 0 [ year>> 1 - end-of-year (week-number) ] }
376 { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
380 : same-week? ( ts1 ts2 -- ? )
381 [ [ year>> ] [ week-number ] bi 2array ] same-times? ;
383 : same-hour? ( ts1 ts2 -- ? )
384 [ slots{ year month day hour } ] same-times? ;
386 : same-minute? ( ts1 ts2 -- ? )
387 [ slots{ year month day hour minute } ] same-times? ;
389 : same-second? ( ts1 ts2 -- ? )
390 [ slots{ year month day hour minute second } ] same-times? ;
394 : (time-) ( timestamp timestamp -- n )
396 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
397 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
401 GENERIC: time- ( time1 time2 -- time3 )
404 ! Exact calendar-time difference
407 : time* ( obj1 obj2 -- obj3 )
408 dup real? [ swap ] when
420 : before ( duration -- -duration )
423 : duration- ( duration1 duration2 -- duration3 )
426 [ [ month>> ] bi@ - ]
429 [ [ minute>> ] bi@ - ]
430 [ [ second>> ] bi@ - ]
431 } 2cleave <duration> ; inline
434 over timestamp? [ before time+ ] [ duration- ] if ;
436 : unix-1970 ( -- timestamp )
437 1970 <year-gmt> ; inline
439 : millis>timestamp ( x -- timestamp )
440 unix-1970 swap 1000 / +second ;
442 : timestamp>millis ( timestamp -- n )
443 unix-1970 (time-) 1000 * >integer ;
445 : micros>timestamp ( x -- timestamp )
446 unix-1970 swap 1000000 / +second ;
448 : timestamp>micros ( timestamp -- n )
449 unix-1970 (time-) 1000000 * >integer ;
451 : now ( -- timestamp )
452 now-gmt gmt-offset-duration [ (time+) ] [ >>gmt-offset ] bi ;
454 : hence ( duration -- timestamp ) now swap time+ ;
455 : ago ( duration -- timestamp ) now swap time- ;
457 GENERIC: days-in-year ( obj -- n )
459 M: integer days-in-year leap-year? 366 365 ? ;
461 M: timestamp days-in-year year>> days-in-year ;
463 : days-in-month ( timestamp -- n )
464 >date< drop (days-in-month) ;
466 : midnight ( timestamp -- timestamp ) 0 0 0 set-time ; inline
467 : noon ( timestamp -- timestamp ) 12 0 0 set-time ; inline
469 : today ( -- timestamp ) now midnight ; inline
470 : tomorrow ( -- timestamp ) 1 days hence midnight ; inline
471 : overtomorrow ( -- timestamp ) 2 days hence midnight ; inline
472 : yesterday ( -- timestamp ) 1 days ago midnight ; inline
473 : ereyesterday ( -- timestamp ) 2 days ago midnight ; inline
475 ALIAS: start-of-day midnight
477 : end-of-day ( timestamp -- timestamp )
478 23 >>hour 59 >>minute 59+999/1000 >>second ; inline
480 : start-of-month ( timestamp -- timestamp )
481 midnight 1 >>day ; inline
483 : end-of-month ( timestamp -- timestamp )
484 [ end-of-day ] [ days-in-month ] bi >>day ;
486 : start-of-quarter ( timestamp -- timestamp )
487 [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
489 : end-of-quarter ( timestamp -- timestamp )
490 dup quarter 1 - 3 * 3 + >>month end-of-month ; inline
492 : first-day-of-month ( timestamp -- timestamp )
495 : last-day-of-month ( timestamp -- timestamp )
496 dup days-in-month >>day ; inline
498 GENERIC: first-day-of-year ( object -- timestamp )
499 M: timestamp first-day-of-year first-day-of-month 1 >>month ;
500 M: integer first-day-of-year <year> ;
502 GENERIC: last-day-of-year ( object -- timestamp )
503 M: timestamp last-day-of-year 12 >>month 31 >>day ;
504 M: integer last-day-of-year 12 31 <date> ;
506 : first-day-of-decade ( object -- timestamp )
507 first-day-of-year [ dup 10 mod - ] change-year ;
509 : last-day-of-decade ( object -- timestamp )
510 last-day-of-year [ dup 10 mod - 9 + ] change-year ;
512 : first-day-of-century ( object -- timestamp )
513 first-day-of-year [ dup 100 mod - ] change-year ;
515 : last-day-of-century ( object -- timestamp )
516 last-day-of-year [ dup 100 mod - 99 + ] change-year ;
518 : first-day-of-millennium ( object -- timestamp )
519 first-day-of-year [ dup 1000 mod - ] change-year ;
521 : last-day-of-millennium ( object -- timestamp )
522 last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
524 : start-of-year ( object -- timestamp )
525 first-day-of-year start-of-day ;
527 : end-of-year ( object -- timestamp )
528 last-day-of-year end-of-day ;
530 : start-of-decade ( object -- timestamp )
531 first-day-of-decade start-of-day ;
533 : end-of-decade ( object -- timestamp )
534 last-day-of-decade end-of-day ;
536 : end-of-century ( object -- timestamp )
537 last-day-of-century end-of-day ;
539 : start-of-millennium ( object -- timestamp )
540 first-day-of-millennium start-of-day ;
542 : end-of-millennium ( object -- timestamp )
543 last-day-of-millennium end-of-day ;
545 : start-of-hour ( timestamp -- timestamp ) 0 >>minute 0 >>second ;
546 : end-of-hour ( timestamp -- timestamp ) 59 >>minute 59+999/1000 >>second ;
548 : start-of-minute ( timestamp -- timestamp ) 0 >>second ;
549 : end-of-minute ( timestamp -- timestamp ) 59+999/1000 >>second ;
551 : start-of-second ( timestamp -- timestamp ) [ floor ] change-second ;
552 : end-of-second ( timestamp -- timestamp ) [ floor 999/1000 + ] change-second ;
556 : day-offset ( timestamp m -- timestamp n )
557 over day-of-week - ; inline
559 : day-this-week ( timestamp n -- timestamp )
560 day-offset days (time+) ;
562 : closest-day ( timestamp n -- timestamp )
563 { 0 1 2 3 -3 -2 -1 } pick day-of-week 7 swap -
564 <rotated> nth days (time+) ;
566 :: nth-day-this-month ( timestamp n day -- timestamp )
568 timestamp start-of-month day day-this-week
569 [ [ month>> ] same? ] keep swap
570 [ 1 weeks (time+) ] unless
571 n [ weeks (time+) ] unless-zero ;
575 GENERIC: january ( obj -- timestamp )
576 GENERIC: february ( obj -- timestamp )
577 GENERIC: march ( obj -- timestamp )
578 GENERIC: april ( obj -- timestamp )
579 GENERIC: may ( obj -- timestamp )
580 GENERIC: june ( obj -- timestamp )
581 GENERIC: july ( obj -- timestamp )
582 GENERIC: august ( obj -- timestamp )
583 GENERIC: september ( obj -- timestamp )
584 GENERIC: october ( obj -- timestamp )
585 GENERIC: november ( obj -- timestamp )
586 GENERIC: december ( obj -- timestamp )
588 M: integer january 1 1 <date> ;
589 M: integer february 2 1 <date> ;
590 M: integer march 3 1 <date> ;
591 M: integer april 4 1 <date> ;
592 M: integer may 5 1 <date> ;
593 M: integer june 6 1 <date> ;
594 M: integer july 7 1 <date> ;
595 M: integer august 8 1 <date> ;
596 M: integer september 9 1 <date> ;
597 M: integer october 10 1 <date> ;
598 M: integer november 11 1 <date> ;
599 M: integer december 12 1 <date> ;
601 M: timestamp january 1 >>month ;
602 M: timestamp february 2 >>month ;
603 M: timestamp march 3 >>month ;
604 M: timestamp april 4 >>month ;
605 M: timestamp may 5 >>month ;
606 M: timestamp june 6 >>month ;
607 M: timestamp july 7 >>month ;
608 M: timestamp august 8 >>month ;
609 M: timestamp september 9 >>month ;
610 M: timestamp october 10 >>month ;
611 M: timestamp november 11 >>month ;
612 M: timestamp december 12 >>month ;
614 : closest-sunday ( timestamp -- timestamp ) 0 closest-day ;
615 : closest-monday ( timestamp -- timestamp ) 1 closest-day ;
616 : closest-tuesday ( timestamp -- timestamp ) 2 closest-day ;
617 : closest-wednesday ( timestamp -- timestamp ) 3 closest-day ;
618 : closest-thursday ( timestamp -- timestamp ) 4 closest-day ;
619 : closest-friday ( timestamp -- timestamp ) 5 closest-day ;
620 : closest-saturday ( timestamp -- timestamp ) 6 closest-day ;
622 : sunday ( timestamp -- timestamp ) 0 day-this-week ;
623 : monday ( timestamp -- timestamp ) 1 day-this-week ;
624 : tuesday ( timestamp -- timestamp ) 2 day-this-week ;
625 : wednesday ( timestamp -- timestamp ) 3 day-this-week ;
626 : thursday ( timestamp -- timestamp ) 4 day-this-week ;
627 : friday ( timestamp -- timestamp ) 5 day-this-week ;
628 : saturday ( timestamp -- timestamp ) 6 day-this-week ;
630 ALIAS: first-day-of-week sunday
631 ALIAS: last-day-of-week saturday
633 : day< ( timestamp quot -- timestamp )
634 over clone [ call dup ] dip after=? [ -7 days (time+) ] when ; inline
635 : day<= ( timestamp quot -- timestamp )
636 over clone [ call dup ] dip after? [ -7 days (time+) ] when ; inline
637 : day> ( timestamp quot -- timestamp )
638 over clone [ call dup ] dip before=? [ 7 days (time+) ] when ; inline
639 : day>= ( timestamp quot -- timestamp )
640 over clone [ call dup ] dip before? [ 7 days (time+) ] when ; inline
642 : sunday< ( timestamp -- timestamp ) [ sunday ] day< ;
643 : monday< ( timestamp -- timestamp ) [ monday ] day< ;
644 : tuesday< ( timestamp -- timestamp ) [ tuesday ] day< ;
645 : wednesday< ( timestamp -- timestamp ) [ wednesday ] day< ;
646 : thursday< ( timestamp -- timestamp ) [ thursday ] day< ;
647 : friday< ( timestamp -- timestamp ) [ friday ] day< ;
648 : saturday< ( timestamp -- timestamp ) [ saturday ] day< ;
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 : next-sunday ( timestamp -- timestamp ) closest-sunday sunday> ;
675 : next-monday ( timestamp -- timestamp ) closest-monday monday> ;
676 : next-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday> ;
677 : next-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday> ;
678 : next-thursday ( timestamp -- timestamp ) closest-thursday thursday> ;
679 : next-friday ( timestamp -- timestamp ) closest-friday friday> ;
680 : next-saturday ( timestamp -- timestamp ) closest-saturday saturday> ;
682 : last-sunday ( timestamp -- timestamp ) closest-sunday sunday< ;
683 : last-monday ( timestamp -- timestamp ) closest-monday monday< ;
684 : last-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday< ;
685 : last-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday< ;
686 : last-thursday ( timestamp -- timestamp ) closest-thursday thursday< ;
687 : last-friday ( timestamp -- timestamp ) closest-friday friday< ;
688 : last-saturday ( timestamp -- timestamp ) closest-saturday saturday< ;
690 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
691 : monday? ( timestamp -- ? ) day-of-week 1 = ;
692 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
693 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
694 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
695 : friday? ( timestamp -- ? ) day-of-week 5 = ;
696 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
698 : january? ( timestamp -- ? ) month>> 1 = ;
699 : february? ( timestamp -- ? ) month>> 2 = ;
700 : march? ( timestamp -- ? ) month>> 3 = ;
701 : april? ( timestamp -- ? ) month>> 4 = ;
702 : may? ( timestamp -- ? ) month>> 5 = ;
703 : june? ( timestamp -- ? ) month>> 6 = ;
704 : july? ( timestamp -- ? ) month>> 7 = ;
705 : august? ( timestamp -- ? ) month>> 8 = ;
706 : september? ( timestamp -- ? ) month>> 9 = ;
707 : october? ( timestamp -- ? ) month>> 10 = ;
708 : november? ( timestamp -- ? ) month>> 11 = ;
709 : december? ( timestamp -- ? ) month>> 12 = ;
711 : weekend? ( timestamp -- ? ) day-of-week { 0 6 } member? ;
712 : weekday? ( timestamp -- ? ) day-of-week weekend? not ;
714 : same-or-next-business-day ( timestamp -- timestamp )
717 { 6 [ 2 days (time+) ] }
721 : same-or-previous-business-day ( timestamp -- timestamp )
723 { 0 [ -2 days (time+) ] }
728 : weekdays-between ( date1 date2 -- n )
730 [ swap time- duration>days 5 * ]
731 [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
733 day-of-week 6 = [ [ 1 - ] dip ] when
734 day-of-week 0 = [ 1 - ] when ;
736 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
738 : weekdays-between2 ( date1 date2 -- n )
739 [ swap time- duration>days 1 + ]
740 [ [ day-of-week ] bi@ 6 swap - ] 2bi
743 [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
745 : sunday-of-month ( timestamp n -- timestamp ) 0 nth-day-this-month ;
746 : monday-of-month ( timestamp n -- timestamp ) 1 nth-day-this-month ;
747 : tuesday-of-month ( timestamp n -- timestamp ) 2 nth-day-this-month ;
748 : wednesday-of-month ( timestamp n -- timestamp ) 3 nth-day-this-month ;
749 : thursday-of-month ( timestamp n -- timestamp ) 4 nth-day-this-month ;
750 : friday-of-month ( timestamp n -- timestamp ) 5 nth-day-this-month ;
751 : saturday-of-month ( timestamp n -- timestamp ) 6 nth-day-this-month ;
753 : last-sunday-of-month ( timestamp -- timestamp ) last-day-of-month sunday<= ;
754 : last-monday-of-month ( timestamp -- timestamp ) last-day-of-month monday<= ;
755 : last-tuesday-of-month ( timestamp -- timestamp ) last-day-of-month tuesday<= ;
756 : last-wednesday-of-month ( timestamp -- timestamp ) last-day-of-month wednesday<= ;
757 : last-thursday-of-month ( timestamp -- timestamp ) last-day-of-month thursday<= ;
758 : last-friday-of-month ( timestamp -- timestamp ) last-day-of-month friday<= ;
759 : last-saturday-of-month ( timestamp -- timestamp ) last-day-of-month saturday<= ;
761 : start-of-week ( timestamp -- timestamp )
764 : end-of-week ( timestamp -- timestamp )
765 saturday end-of-day ;
767 : o'clock ( timestamp n -- timestamp )
768 [ midnight ] dip >>hour ;
770 : am ( timestamp n -- timestamp )
771 1 12 [a,b] check-interval dup 12 = [ drop 0 ] when o'clock ;
773 : pm ( timestamp n -- timestamp )
774 1 12 [a,b] check-interval dup 12 = [ drop 0 ] when 12 + o'clock ;
776 : time-since-midnight ( timestamp -- duration )
777 instant swap >time< set-time ;
779 : since-1970 ( duration -- timestamp )
780 unix-1970 swap (time+) ; inline
782 : timestamp>unix-time ( timestamp -- seconds )
783 unix-1970 (time-) ; inline
785 : unix-time>timestamp ( seconds -- timestamp )
786 unix-1970 swap +second ; inline
788 ! January and February need a fixup with this algorithm.
789 ! Find a better algorithm.
790 : ymd>ordinal ( year month day -- ordinal )
791 [ leap-year? dup -2 -3 ? ]
792 [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
793 [ ] tri* + + >integer
796 : timestamp>year-dates-gmt ( timestamp -- seq )
797 [ start-of-year >date< julian-day-number ]
800 [a..b) [ julian-day-number>date <date-gmt> ] map ;
802 : year-ordinal>timestamp ( year ordinal -- timestamp )
803 [ 1 1 julian-day-number ] dip
804 + 1 - julian-day-number>date <date> ;
806 GENERIC: weeks-in-week-year ( obj -- n )
808 M: integer weeks-in-week-year
809 { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
811 M: timestamp weeks-in-week-year
812 { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
815 { [ os unix? ] [ "calendar.unix" ] }
816 { [ os windows? ] [ "calendar.windows" ] }
819 { "threads" "calendar" } "calendar.threads" require-when