1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes.tuple combinators
4 combinators.short-circuit kernel locals math math.functions
5 math.intervals math.order math.parser 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: gmt os ( -- timestamp )
28 C: <duration> duration
30 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
39 { gmt-offset duration } ;
41 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
43 GENERIC: leap-year? ( obj -- ? )
46 dup 100 divisor? 400 4 ? divisor? ;
48 M: timestamp leap-year?
51 : (days-in-month) ( year month -- n )
52 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
54 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
56 month 1 12 [a,b] check-interval
57 day 1 year month (days-in-month) [a,b] check-interval
58 hour 0 23 [a,b] check-interval
59 minute 0 59 [a,b] check-interval
60 second 0 60 [a,b) check-interval
61 gmt-offset timestamp boa ;
63 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
65 : gmt-offset-duration ( -- duration )
66 0 0 0 gmt-offset <duration> ; inline
68 : <date> ( year month day -- timestamp )
69 0 0 0 gmt-offset-duration <timestamp> ; inline
71 : <date-gmt> ( year month day -- timestamp )
72 0 0 0 instant <timestamp> ; inline
74 : <year> ( year -- timestamp )
77 : <year-gmt> ( year -- timestamp )
78 1 1 <date-gmt> ; inline
80 CONSTANT: average-month 30+5/12
81 CONSTANT: months-per-year 12
82 CONSTANT: days-per-year 3652425/10000
83 CONSTANT: hours-per-year 876582/100
84 CONSTANT: minutes-per-year 5259492/10
85 CONSTANT: seconds-per-year 31556952
87 :: julian-day-number ( year month day -- n )
88 ! Returns a composite date number
89 ! Not valid before year -4800
92 month 12 a * + 3 - :> m
94 day 153 m * 2 + 5 /i + 365 y * +
95 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
97 :: julian-day-number>date ( n -- year month day )
98 ! Inverse of julian-day-number
100 4 a * 3 + 146097 /i :> b
101 a 146097 b * 4 /i - :> c
102 4 c * 3 + 1461 /i :> d
103 c 1461 d * 4 /i - :> e
104 5 e * 2 + 153 /i :> m
109 e 153 m * 2 + 5 /i - 1 + ;
111 GENERIC: easter ( obj -- obj' )
113 :: easter-month-day ( year -- month day )
115 year 100 /mod :> ( b c )
119 19 a * b + d - g - 15 + 30 mod :> h
121 32 2 e * + 2 i * + h - k - 7 mod :> l
122 a 11 h * + 22 l * + 451 /i :> m
124 h l + 7 m * - 114 + 31 /mod 1 + ;
127 dup easter-month-day <date> ;
131 dup year>> easter-month-day
132 swapd >>day swap >>month ;
134 : >date< ( timestamp -- year month day )
135 [ year>> ] [ month>> ] [ day>> ] tri ;
137 : >time< ( timestamp -- hour minute second )
138 [ hour>> ] [ minute>> ] [ second>> ] tri ;
140 : set-time! ( timestamp hours minutes seconds -- timestamp )
141 [ >>hour ] [ >>minute ] [ >>second ] tri* ;
143 : set-time ( timestamp hours minutes seconds -- timestamp )
144 [ clone ] 3dip set-time! ;
146 : time>hms ( str -- hms-seq )
147 ":" split [ string>number ] map
150 : time>offset ( str -- hms-seq )
151 "-" ?head [ time>hms ] dip
152 [ [ neg ] map ] when ;
154 : years ( x -- duration ) instant swap >>year ;
155 : bienniums ( x -- duration ) instant swap 2 * >>year ;
156 : trienniums ( x -- duration ) instant swap 3 * >>year ;
157 : quadrenniums ( x -- duration ) instant swap 4 * >>year ;
158 : quinquenniums ( x -- duration ) instant swap 5 * >>year ;
159 : sexenniums ( x -- duration ) instant swap 6 * >>year ;
160 : septenniums ( x -- duration ) instant swap 7 * >>year ;
161 : octenniums ( x -- duration ) instant swap 8 * >>year ;
162 : novenniums ( x -- duration ) instant swap 9 * >>year ;
163 : lustrums ( x -- duration ) instant swap 5 * >>year ;
164 : decades ( x -- duration ) instant swap 10 * >>year ;
165 : indictions ( x -- duration ) instant swap 15 * >>year ;
166 : score ( x -- duration ) instant swap 20 * >>year ;
167 : jubilees ( x -- duration ) instant swap 50 * >>year ;
168 : centuries ( x -- duration ) instant swap 100 * >>year ;
169 : millennia ( x -- duration ) instant swap 1000 * >>year ;
170 : millenniums ( x -- duration ) instant swap 1000 * >>year ;
171 : kila-annum ( x -- duration ) instant swap 1000 * >>year ;
172 : mega-annum ( x -- duration ) instant swap 1,000,000 * >>year ;
173 : giga-annum ( x -- duration ) instant swap 1,000,000,000 * >>year ;
174 : ages ( x -- duration ) instant swap 1,000,000 * >>year ;
175 : epochs ( x -- duration ) instant swap 10,000,000 * >>year ;
176 : eras ( x -- duration ) instant swap 100,000,000 * >>year ;
177 : eons ( x -- duration ) instant swap 500,000,000 * >>year ;
178 : months ( x -- duration ) instant swap >>month ;
179 : days ( x -- duration ) instant swap >>day ;
180 : weeks ( x -- duration ) 7 * days ;
181 : fortnight ( x -- duration ) 14 * days ;
182 : hours ( x -- duration ) instant swap >>hour ;
183 : minutes ( x -- duration ) instant swap >>minute ;
184 : seconds ( x -- duration ) instant swap >>second ;
185 : milliseconds ( x -- duration ) 1000 / seconds ;
186 : microseconds ( x -- duration ) 1000000 / seconds ;
187 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
191 GENERIC: +year ( timestamp x -- timestamp )
192 GENERIC: +month ( timestamp x -- timestamp )
193 GENERIC: +day ( timestamp x -- timestamp )
194 GENERIC: +hour ( timestamp x -- timestamp )
195 GENERIC: +minute ( timestamp x -- timestamp )
196 GENERIC: +second ( timestamp x -- timestamp )
198 : /rem ( f n -- q r )
199 ! q is positive or negative, r is positive from 0 <= r < n
200 [ / floor >integer ] 2keep rem ;
202 : float>whole-part ( float -- int float )
203 [ floor >integer ] keep over - ;
205 : adjust-leap-year ( timestamp -- timestamp )
207 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
208 [ 3 >>month 1 >>day ] when ;
211 [ + ] curry change-year adjust-leap-year ;
214 float>whole-part swapd days-per-year * +day swap +year ;
216 : months/years ( n -- months years )
217 12 /rem [ 1 - 12 ] when-zero swap ; inline
220 over month>> + months/years [ >>month ] dip +year ;
223 float>whole-part swapd average-month * +day swap +month ;
226 over >date< julian-day-number + julian-day-number>date
227 [ >>year ] [ >>month ] [ >>day ] tri* ;
230 float>whole-part swapd 24 * +hour swap +day ;
232 : hours/days ( n -- hours days )
236 over hour>> + hours/days [ >>hour ] dip +day ;
239 float>whole-part swapd 60 * +minute swap +hour ;
241 : minutes/hours ( n -- minutes hours )
245 over minute>> + minutes/hours [ >>minute ] dip +hour ;
248 float>whole-part swapd 60 * +second swap +minute ;
250 : seconds/minutes ( n -- seconds minutes )
251 60 /rem swap >integer ;
254 over second>> + seconds/minutes [ >>second ] dip +minute ;
256 : (time+) ( timestamp duration -- timestamp' duration )
269 GENERIC#: time+ 1 ( time1 time2 -- time3 )
272 [ clone ] dip (time+) drop ;
274 : duration+ ( duration1 duration2 -- duration3 )
277 [ [ month>> ] bi@ + ]
280 [ [ minute>> ] bi@ + ]
281 [ [ second>> ] bi@ + ]
282 } 2cleave <duration> ; inline
285 dup timestamp? [ swap time+ ] [ duration+ ] if ;
287 GENERIC#: time+! 1 ( time1 time2 -- time3 )
293 dup timestamp? [ swap time+! ] [ duration+ ] if ;
295 : duration>years ( duration -- x )
296 ! Uses average month/year length since duration loses calendar data
300 [ month>> months-per-year / + ]
301 [ day>> days-per-year / + ]
302 [ hour>> hours-per-year / + ]
303 [ minute>> minutes-per-year / + ]
304 [ second>> seconds-per-year / + ]
307 M: duration <=> [ duration>years ] compare ;
309 : duration>months ( duration -- x ) duration>years months-per-year * ;
310 : duration>days ( duration -- x ) duration>years days-per-year * ;
311 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
312 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
313 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
314 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
315 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
316 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
318 GENERIC: time- ( time1 time2 -- time3 )
320 : convert-timezone ( timestamp duration -- timestamp' )
321 over gmt-offset>> over = [ drop ] [
322 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
325 : >local-time ( timestamp -- timestamp' )
326 clone gmt-offset-duration convert-timezone ;
328 : normalize-timestamp! ( timestamp -- timestamp ) 0 seconds time+! ;
329 : normalize-timestamp ( timestamp -- timestamp' ) 0 seconds time+ ;
331 : (>gmt) ( timestamp -- timestamp' )
332 dup gmt-offset>> dup instant =
334 [ neg +second 0 ] change-second
335 [ neg +minute 0 ] change-minute
336 [ neg +hour 0 ] change-hour
337 [ neg +day 0 ] change-day
338 [ neg +month 0 ] change-month
339 [ neg +year 0 ] change-year drop
342 : >gmt! ( timestamp -- timestamp ) normalize-timestamp! (>gmt) ;
343 : >gmt ( timestamp -- timestamp' ) normalize-timestamp (>gmt) ;
345 M: timestamp <=> [ >gmt tuple-slots ] compare ;
347 : same-year? ( ts1 ts2 -- ? )
348 [ >gmt slots{ year } ] same? ;
350 : quarter ( timestamp -- [1,4] )
351 month>> 3 /mod [ drop 1 + ] unless-zero ; inline
353 : same-quarter? ( ts1 ts2 -- ? )
354 [ >gmt [ year>> ] [ quarter ] bi 2array ] same? ;
356 : same-month? ( ts1 ts2 -- ? )
357 [ >gmt slots{ year month } ] same? ;
359 :: (day-of-year) ( year month day -- n )
360 day-counts month head-slice sum day +
362 year month day <date>
367 : day-of-year ( timestamp -- n )
368 >date< (day-of-year) ;
370 : same-day? ( ts1 ts2 -- ? )
371 [ >gmt slots{ year month day } ] same? ;
373 : zeller-congruence ( year month day -- n )
375 ! http://web.textfiles.com/computers/formulas.txt
376 ! good for any date since October 15, 1582
378 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
379 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
380 [ 1 + 3 * 5 /i + ] keep 2 * +
383 : day-of-week ( timestamp -- n )
384 >date< zeller-congruence ;
386 : (week-number) ( timestamp -- [0,53] )
387 [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
390 : week-number ( timestamp -- [1,53] )
392 { 0 [ year>> 1 - end-of-year (week-number) ] }
393 { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
397 : same-week? ( ts1 ts2 -- ? )
398 [ >gmt [ year>> ] [ week-number ] bi 2array ] same? ;
400 : same-hour? ( ts1 ts2 -- ? )
401 [ >gmt slots{ year month day hour } ] same? ;
403 : same-minute? ( ts1 ts2 -- ? )
404 [ >gmt slots{ year month day hour minute } ] same? ;
406 : same-second? ( ts1 ts2 -- ? )
407 [ >gmt slots{ year month day hour minute second } ] same? ;
409 : (time-) ( timestamp timestamp -- n )
411 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
412 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
415 ! Exact calendar-time difference
418 : time* ( obj1 obj2 -- obj3 )
419 dup real? [ swap ] when
431 : before ( duration -- -duration )
434 : duration- ( duration1 duration2 -- duration3 )
437 [ [ month>> ] bi@ - ]
440 [ [ minute>> ] bi@ - ]
441 [ [ second>> ] bi@ - ]
442 } 2cleave <duration> ; inline
445 over timestamp? [ before time+ ] [ duration- ] if ;
447 : unix-1970 ( -- timestamp )
448 1970 <year-gmt> ; inline
450 : millis>timestamp ( x -- timestamp )
451 [ unix-1970 ] dip 1000 / +second ;
453 : timestamp>millis ( timestamp -- n )
454 unix-1970 (time-) 1000 * >integer ;
456 : micros>timestamp ( x -- timestamp )
457 [ unix-1970 ] dip 1000000 / +second ;
459 : timestamp>micros ( timestamp -- n )
460 unix-1970 (time-) 1000000 * >integer ;
462 : now ( -- timestamp )
463 gmt gmt-offset-duration (time+) >>gmt-offset ;
465 : now-gmt ( -- timestamp ) gmt ;
467 : hence ( duration -- timestamp ) now swap time+ ;
468 : hence-gmt ( duration -- timestamp ) now-gmt swap time+ ;
470 : ago ( duration -- timestamp ) now swap time- ;
471 : ago-gmt ( duration -- timestamp ) now-gmt swap time- ;
473 GENERIC: days-in-year ( obj -- n )
475 M: integer days-in-year leap-year? 366 365 ? ;
477 M: timestamp days-in-year year>> days-in-year ;
479 : days-in-month ( timestamp -- n )
480 >date< drop (days-in-month) ;
482 : midnight! ( timestamp -- timestamp ) 0 0 0 set-time! ; inline
483 : midnight ( timestamp -- new-timestamp ) clone midnight! ; inline
484 : midnight-gmt! ( timestamp -- timestamp ) 0 0 0 set-time! instant >>gmt-offset ; inline
485 : midnight-gmt ( timestamp -- new-timestamp ) clone midnight-gmt! ; inline
487 : noon! ( timestamp -- timestamp ) 12 0 0 set-time! ; inline
488 : noon ( timestamp -- new-timestamp ) clone noon! ; inline
489 : noon-gmt! ( timestamp -- timestamp ) 12 0 0 set-time! instant >>gmt-offset ; inline
490 : noon-gmt ( timestamp -- new-timestamp ) clone noon-gmt! ; inline
492 : today ( -- timestamp ) now midnight! ; inline
493 : today-gmt ( -- timestamp ) now midnight-gmt! ; inline
494 : tomorrow ( -- timestamp ) 1 days hence midnight! ; inline
495 : tomorrow-gmt ( -- timestamp ) 1 days hence midnight-gmt! ; inline
496 : overtomorrow ( -- timestamp ) 2 days hence midnight! ; inline
497 : overtomorrow-gmt ( -- timestamp ) 2 days hence midnight-gmt! ; inline
498 : yesterday ( -- timestamp ) 1 days ago midnight! ; inline
499 : yesterday-gmt ( -- timestamp ) 1 days ago midnight-gmt! ; inline
500 : ereyesterday ( -- timestamp ) 2 days ago midnight! ; inline
501 : ereyesterday-gmt ( -- timestamp ) 2 days ago midnight-gmt! ; inline
503 : start-of-day! ( timestamp -- timestamp ) midnight! ; inline
504 : start-of-day ( object -- new-timestamp ) midnight ; inline
506 : end-of-day! ( timestamp -- timestamp )
507 23 >>hour 59 >>minute 59+999/1000 >>second ; inline
509 : end-of-day ( object -- new-timestamp )
510 clone end-of-day! ; inline
512 : start-of-month ( timestamp -- new-timestamp )
513 midnight 1 >>day ; inline
515 : end-of-month ( timestamp -- new-timestamp )
516 [ end-of-day ] [ days-in-month ] bi >>day ;
518 : start-of-quarter ( timestamp -- new-timestamp )
519 [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
521 : end-of-quarter ( timestamp -- new-timestamp )
522 [ clone ] [ quarter 1 - 3 * 3 + ] bi >>month end-of-month ; inline
524 : first-day-of-month ( object -- new-timestamp )
527 : last-day-of-month ( object -- new-timestamp )
528 clone dup days-in-month >>day ; inline
530 GENERIC: first-day-of-year ( object -- new-timestamp )
531 M: timestamp first-day-of-year first-day-of-month 1 >>month ;
532 M: integer first-day-of-year <year> ;
534 GENERIC: last-day-of-year ( object -- new-timestamp )
535 M: timestamp last-day-of-year clone 12 >>month 31 >>day ;
536 M: integer last-day-of-year 12 31 <date> ;
538 GENERIC: first-day-of-decade ( object -- new-timestamp )
539 M: timestamp first-day-of-decade first-day-of-year [ dup 10 mod - ] change-year ;
540 M: integer first-day-of-decade first-day-of-year [ dup 10 mod - ] change-year ;
542 GENERIC: last-day-of-decade ( object -- new-timestamp )
543 M: timestamp last-day-of-decade last-day-of-year [ dup 10 mod - 9 + ] change-year ;
544 M: integer last-day-of-decade last-day-of-year [ dup 10 mod - 9 + ] change-year ;
546 GENERIC: first-day-of-century ( object -- new-timestamp )
547 M: timestamp first-day-of-century first-day-of-year [ dup 100 mod - ] change-year ;
548 M: integer first-day-of-century first-day-of-year [ dup 100 mod - ] change-year ;
550 GENERIC: last-day-of-century ( object -- new-timestamp )
551 M: timestamp last-day-of-century last-day-of-year [ dup 100 mod - 99 + ] change-year ;
552 M: integer last-day-of-century last-day-of-year [ dup 100 mod - 99 + ] change-year ;
554 GENERIC: first-day-of-millennium ( object -- new-timestamp )
555 M: timestamp first-day-of-millennium first-day-of-year [ dup 1000 mod - ] change-year ;
556 M: integer first-day-of-millennium first-day-of-year [ dup 1000 mod - ] change-year ;
558 GENERIC: last-day-of-millennium ( object -- new-timestamp )
559 M: timestamp last-day-of-millennium last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
560 M: integer last-day-of-millennium last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
562 : start-of-year ( object -- new-timestamp )
563 first-day-of-year start-of-day! ;
565 : end-of-year ( object -- new-timestamp )
566 last-day-of-year end-of-day! ;
568 : start-of-decade ( object -- new-timestamp )
569 first-day-of-decade start-of-day! ;
571 : end-of-decade ( object -- new-timestamp )
572 last-day-of-decade end-of-day! ;
574 : end-of-century ( object -- new-timestamp )
575 last-day-of-century end-of-day! ;
577 : start-of-millennium ( object -- new-timestamp )
578 first-day-of-millennium start-of-day! ;
580 : end-of-millennium ( object -- new-timestamp )
581 last-day-of-millennium end-of-day! ;
583 : start-of-hour ( timestamp -- new-timestamp ) clone 0 >>minute 0 >>second ;
584 : end-of-hour ( timestamp -- new-timestamp ) clone 59 >>minute 59+999/1000 >>second ;
586 : start-of-minute ( timestamp -- new-timestamp ) clone 0 >>second ;
587 : end-of-minute ( timestamp -- new-timestamp ) clone 59+999/1000 >>second ;
589 GENERIC: start-of-second ( object -- new-timestamp )
590 M: timestamp start-of-second clone [ floor ] change-second ;
591 M: real start-of-second floor ;
593 GENERIC: end-of-second ( object -- new-timestamp )
594 M: timestamp end-of-second clone [ floor 999/1000 + ] change-second ;
595 M: real end-of-second floor 999/1000 + ;
599 : day-offset ( timestamp m -- new-timestamp n )
600 over day-of-week - ; inline
602 : day-this-week ( timestamp n -- new-timestamp )
603 day-offset days time+ ;
605 : closest-day ( timestamp n -- new-timestamp )
606 [ dup day-of-week ] dip
608 rot 7 swap - <rotated> nth days time+ ;
610 :: nth-day-this-month ( timestamp n day -- new-timestamp )
611 timestamp start-of-month day day-this-week
612 dup timestamp [ month>> ] same?
613 [ 1 weeks time+ ] unless
614 n [ weeks time+ ] unless-zero ;
618 GENERIC: january ( obj -- timestamp )
619 GENERIC: february ( obj -- timestamp )
620 GENERIC: march ( obj -- timestamp )
621 GENERIC: april ( obj -- timestamp )
622 GENERIC: may ( obj -- timestamp )
623 GENERIC: june ( obj -- timestamp )
624 GENERIC: july ( obj -- timestamp )
625 GENERIC: august ( obj -- timestamp )
626 GENERIC: september ( obj -- timestamp )
627 GENERIC: october ( obj -- timestamp )
628 GENERIC: november ( obj -- timestamp )
629 GENERIC: december ( obj -- timestamp )
631 M: integer january 1 1 <date> ;
632 M: integer february 2 1 <date> ;
633 M: integer march 3 1 <date> ;
634 M: integer april 4 1 <date> ;
635 M: integer may 5 1 <date> ;
636 M: integer june 6 1 <date> ;
637 M: integer july 7 1 <date> ;
638 M: integer august 8 1 <date> ;
639 M: integer september 9 1 <date> ;
640 M: integer october 10 1 <date> ;
641 M: integer november 11 1 <date> ;
642 M: integer december 12 1 <date> ;
644 M: timestamp january clone 1 >>month ;
645 M: timestamp february clone 2 >>month ;
646 M: timestamp march clone 3 >>month ;
647 M: timestamp april clone 4 >>month ;
648 M: timestamp may clone 5 >>month ;
649 M: timestamp june clone 6 >>month ;
650 M: timestamp july clone 7 >>month ;
651 M: timestamp august clone 8 >>month ;
652 M: timestamp september clone 9 >>month ;
653 M: timestamp october clone 10 >>month ;
654 M: timestamp november clone 11 >>month ;
655 M: timestamp december clone 12 >>month ;
657 GENERIC: january-gmt ( obj -- timestamp )
658 GENERIC: february-gmt ( obj -- timestamp )
659 GENERIC: march-gmt ( obj -- timestamp )
660 GENERIC: april-gmt ( obj -- timestamp )
661 GENERIC: may-gmt ( obj -- timestamp )
662 GENERIC: june-gmt ( obj -- timestamp )
663 GENERIC: july-gmt ( obj -- timestamp )
664 GENERIC: august-gmt ( obj -- timestamp )
665 GENERIC: september-gmt ( obj -- timestamp )
666 GENERIC: october-gmt ( obj -- timestamp )
667 GENERIC: november-gmt ( obj -- timestamp )
668 GENERIC: december-gmt ( obj -- timestamp )
670 M: integer january-gmt 1 1 <date-gmt> ;
671 M: integer february-gmt 2 1 <date-gmt> ;
672 M: integer march-gmt 3 1 <date-gmt> ;
673 M: integer april-gmt 4 1 <date-gmt> ;
674 M: integer may-gmt 5 1 <date-gmt> ;
675 M: integer june-gmt 6 1 <date-gmt> ;
676 M: integer july-gmt 7 1 <date-gmt> ;
677 M: integer august-gmt 8 1 <date-gmt> ;
678 M: integer september-gmt 9 1 <date-gmt> ;
679 M: integer october-gmt 10 1 <date-gmt> ;
680 M: integer november-gmt 11 1 <date-gmt> ;
681 M: integer december-gmt 12 1 <date-gmt> ;
683 M: timestamp january-gmt >gmt 1 >>month ;
684 M: timestamp february-gmt >gmt 2 >>month ;
685 M: timestamp march-gmt >gmt 3 >>month ;
686 M: timestamp april-gmt >gmt 4 >>month ;
687 M: timestamp may-gmt >gmt 5 >>month ;
688 M: timestamp june-gmt >gmt 6 >>month ;
689 M: timestamp july-gmt >gmt 7 >>month ;
690 M: timestamp august-gmt >gmt 8 >>month ;
691 M: timestamp september-gmt >gmt 9 >>month ;
692 M: timestamp october-gmt >gmt 10 >>month ;
693 M: timestamp november-gmt >gmt 11 >>month ;
694 M: timestamp december-gmt >gmt 12 >>month ;
696 : closest-sunday ( timestamp -- new-timestamp ) 0 closest-day ;
697 : closest-monday ( timestamp -- new-timestamp ) 1 closest-day ;
698 : closest-tuesday ( timestamp -- new-timestamp ) 2 closest-day ;
699 : closest-wednesday ( timestamp -- new-timestamp ) 3 closest-day ;
700 : closest-thursday ( timestamp -- new-timestamp ) 4 closest-day ;
701 : closest-friday ( timestamp -- new-timestamp ) 5 closest-day ;
702 : closest-saturday ( timestamp -- new-timestamp ) 6 closest-day ;
704 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
705 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
706 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
707 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
708 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
709 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
710 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
712 : sunday-gmt ( timestamp -- new-timestamp ) sunday >gmt! ;
713 : monday-gmt ( timestamp -- new-timestamp ) monday >gmt! ;
714 : tuesday-gmt ( timestamp -- new-timestamp ) tuesday >gmt! ;
715 : wednesday-gmt ( timestamp -- new-timestamp ) wednesday >gmt! ;
716 : thursday-gmt ( timestamp -- new-timestamp ) thursday >gmt! ;
717 : friday-gmt ( timestamp -- new-timestamp ) friday >gmt! ;
718 : saturday-gmt ( timestamp -- new-timestamp ) saturday >gmt! ;
720 : first-day-of-week ( object -- new-timestamp ) sunday ; inline
721 : last-day-of-week ( object -- new-timestamp ) saturday ; inline
723 : day< ( quot -- new-timestamp ) keep over before=? [ 7 days time- ] when ; inline
724 : day<= ( quot -- new-timestamp ) keep over before? [ 7 days time- ] when ; inline
725 : day> ( quot -- new-timestamp ) keep over after=? [ 7 days time+ ] when ; inline
726 : day>= ( quot -- new-timestamp ) keep over after? [ 7 days time+ ] when ; inline
728 : sunday< ( timestamp -- new-timestamp ) [ sunday ] day< ;
729 : monday< ( timestamp -- new-timestamp ) [ monday ] day< ;
730 : tuesday< ( timestamp -- new-timestamp ) [ tuesday ] day< ;
731 : wednesday< ( timestamp -- new-timestamp ) [ wednesday ] day< ;
732 : thursday< ( timestamp -- new-timestamp ) [ thursday ] day< ;
733 : friday< ( timestamp -- new-timestamp ) [ friday ] day< ;
734 : saturday< ( timestamp -- new-timestamp ) [ saturday ] day< ;
736 : sunday<= ( timestamp -- new-timestamp ) [ sunday ] day<= ;
737 : monday<= ( timestamp -- new-timestamp ) [ monday ] day<= ;
738 : tuesday<= ( timestamp -- new-timestamp ) [ tuesday ] day<= ;
739 : wednesday<= ( timestamp -- new-timestamp ) [ wednesday ] day<= ;
740 : thursday<= ( timestamp -- new-timestamp ) [ thursday ] day<= ;
741 : friday<= ( timestamp -- new-timestamp ) [ friday ] day<= ;
742 : saturday<= ( timestamp -- new-timestamp ) [ saturday ] day<= ;
744 : sunday> ( timestamp -- new-timestamp ) [ sunday ] day> ;
745 : monday> ( timestamp -- new-timestamp ) [ monday ] day> ;
746 : tuesday> ( timestamp -- new-timestamp ) [ tuesday ] day> ;
747 : wednesday> ( timestamp -- new-timestamp ) [ wednesday ] day> ;
748 : thursday> ( timestamp -- new-timestamp ) [ thursday ] day> ;
749 : friday> ( timestamp -- new-timestamp ) [ friday ] day> ;
750 : saturday> ( timestamp -- new-timestamp ) [ saturday ] day> ;
752 : sunday>= ( timestamp -- new-timestamp ) [ sunday ] day>= ;
753 : monday>= ( timestamp -- new-timestamp ) [ monday ] day>= ;
754 : tuesday>= ( timestamp -- new-timestamp ) [ tuesday ] day>= ;
755 : wednesday>= ( timestamp -- new-timestamp ) [ wednesday ] day>= ;
756 : thursday>= ( timestamp -- new-timestamp ) [ thursday ] day>= ;
757 : friday>= ( timestamp -- new-timestamp ) [ friday ] day>= ;
758 : saturday>= ( timestamp -- new-timestamp ) [ saturday ] day>= ;
760 : next-sunday ( timestamp -- new-timestamp ) closest-sunday sunday> ;
761 : next-monday ( timestamp -- new-timestamp ) closest-monday monday> ;
762 : next-tuesday ( timestamp -- new-timestamp ) closest-tuesday tuesday> ;
763 : next-wednesday ( timestamp -- new-timestamp ) closest-wednesday wednesday> ;
764 : next-thursday ( timestamp -- new-timestamp ) closest-thursday thursday> ;
765 : next-friday ( timestamp -- new-timestamp ) closest-friday friday> ;
766 : next-saturday ( timestamp -- new-timestamp ) closest-saturday saturday> ;
768 : last-sunday ( timestamp -- new-timestamp ) closest-sunday sunday< ;
769 : last-monday ( timestamp -- new-timestamp ) closest-monday monday< ;
770 : last-tuesday ( timestamp -- new-timestamp ) closest-tuesday tuesday< ;
771 : last-wednesday ( timestamp -- new-timestamp ) closest-wednesday wednesday< ;
772 : last-thursday ( timestamp -- new-timestamp ) closest-thursday thursday< ;
773 : last-friday ( timestamp -- new-timestamp ) closest-friday friday< ;
774 : last-saturday ( timestamp -- new-timestamp ) closest-saturday saturday< ;
776 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
777 : monday? ( timestamp -- ? ) day-of-week 1 = ;
778 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
779 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
780 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
781 : friday? ( timestamp -- ? ) day-of-week 5 = ;
782 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
784 : january? ( obj -- timestamp ) month>> 1 = ;
785 : february? ( obj -- timestamp ) month>> 2 = ;
786 : march? ( obj -- timestamp ) month>> 3 = ;
787 : april? ( obj -- timestamp ) month>> 4 = ;
788 : may? ( obj -- timestamp ) month>> 5 = ;
789 : june? ( obj -- timestamp ) month>> 6 = ;
790 : july? ( obj -- timestamp ) month>> 7 = ;
791 : august? ( obj -- timestamp ) month>> 8 = ;
792 : september? ( obj -- timestamp ) month>> 9 = ;
793 : october? ( obj -- timestamp ) month>> 10 = ;
794 : november? ( obj -- timestamp ) month>> 11 = ;
795 : december? ( obj -- timestamp ) month>> 12 = ;
797 GENERIC: weekend? ( obj -- ? )
798 M: timestamp weekend? day-of-week weekend? ;
799 M: integer weekend? { 0 6 } member? ;
801 GENERIC: weekday? ( obj -- ? )
802 M: timestamp weekday? day-of-week weekday? ;
803 M: integer weekday? weekend? not ;
805 : same-or-next-business-day ( timestamp -- timestamp' )
808 { 6 [ 2 days time+ ] }
812 : same-or-previous-business-day ( timestamp -- timestamp' )
814 { 0 [ 2 days time- ] }
819 : weekdays-between ( date1 date2 -- n )
821 [ swap time- duration>days 5 * ]
822 [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
824 day-of-week 6 = [ [ 1 - ] dip ] when
825 day-of-week 0 = [ 1 - ] when ;
827 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
829 : weekdays-between2 ( date1 date2 -- n )
830 [ swap time- duration>days 1 + ]
831 [ [ day-of-week ] bi@ 6 swap - ] 2bi
834 [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
836 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
837 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
838 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
839 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
840 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
841 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
842 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
844 : last-sunday-of-month ( timestamp -- new-timestamp ) last-day-of-month sunday<= ;
845 : last-monday-of-month ( timestamp -- new-timestamp ) last-day-of-month monday<= ;
846 : last-tuesday-of-month ( timestamp -- new-timestamp ) last-day-of-month tuesday<= ;
847 : last-wednesday-of-month ( timestamp -- new-timestamp ) last-day-of-month wednesday<= ;
848 : last-thursday-of-month ( timestamp -- new-timestamp ) last-day-of-month thursday<= ;
849 : last-friday-of-month ( timestamp -- new-timestamp ) last-day-of-month friday<= ;
850 : last-saturday-of-month ( timestamp -- new-timestamp ) last-day-of-month saturday<= ;
852 : start-of-week ( timestamp -- new-timestamp )
855 : end-of-week ( timestamp -- new-timestamp )
856 saturday end-of-day! ;
858 : o'clock ( timestamp n -- new-timestamp )
859 [ midnight ] dip >>hour ;
861 : am ( timestamp n -- new-timestamp )
862 0 12 [a,b] check-interval o'clock ;
864 : pm ( timestamp n -- new-timestamp )
865 0 12 [a,b] check-interval 12 + o'clock ;
867 : time-since-midnight ( timestamp -- duration )
868 dup midnight time- ; inline
870 : since-1970 ( duration -- timestamp )
871 unix-1970 time+ ; inline
873 : timestamp>unix-time ( timestamp -- seconds )
874 unix-1970 (time-) ; inline
876 : unix-time>timestamp ( seconds -- timestamp )
877 [ unix-1970 ] dip +second ; inline
879 ! January and February need a fixup with this algorithm.
880 ! Find a better algorithm.
881 : ymd>ordinal ( year month day -- ordinal )
882 [ leap-year? dup -2 -3 ? ]
883 [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
884 [ ] tri* + + >integer
887 : timestamp>year-dates ( timestamp -- seq )
888 [ start-of-year >date< julian-day-number ]
891 [a..b) [ julian-day-number>date <date> ] map ;
893 : year-ordinal>timestamp ( year ordinal -- timestamp )
894 [ 1 1 julian-day-number ] dip
895 + 1 - julian-day-number>date <date> ;
897 GENERIC: weeks-in-week-year ( obj -- n )
898 M: integer weeks-in-week-year
899 { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
901 M: timestamp weeks-in-week-year
902 { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
905 { [ os unix? ] [ "calendar.unix" ] }
906 { [ os windows? ] [ "calendar.windows" ] }
909 { "threads" "calendar" } "calendar.threads" require-when