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 : duration>years ( duration -- x )
288 ! Uses average month/year length since duration loses calendar data
292 [ month>> months-per-year / + ]
293 [ day>> days-per-year / + ]
294 [ hour>> hours-per-year / + ]
295 [ minute>> minutes-per-year / + ]
296 [ second>> seconds-per-year / + ]
299 M: duration <=> [ duration>years ] compare ;
301 : duration>months ( duration -- x ) duration>years months-per-year * ;
302 : duration>days ( duration -- x ) duration>years days-per-year * ;
303 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
304 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
305 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
306 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
307 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
308 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
312 : convert-timezone! ( timestamp duration -- timestamp )
313 [ over gmt-offset>> time- (time+) drop ] [ >>gmt-offset ] bi ;
315 : >local-time! ( timestamp -- timestamp )
316 gmt-offset-duration convert-timezone! ;
318 : >local-time ( timestamp -- timestamp' )
321 : >gmt! ( timestamp -- timestamp )
322 instant convert-timezone! ;
324 : >gmt ( timestamp -- timestamp' )
327 M: timestamp <=> [ >gmt tuple-slots ] compare ;
329 : same-year? ( ts1 ts2 -- ? )
330 [ >gmt slots{ year } ] same? ;
332 : quarter ( timestamp -- [1,4] )
333 month>> 3 /mod [ drop 1 + ] unless-zero ; inline
335 : same-quarter? ( ts1 ts2 -- ? )
336 [ >gmt [ year>> ] [ quarter ] bi 2array ] same? ;
338 : same-month? ( ts1 ts2 -- ? )
339 [ >gmt slots{ year month } ] same? ;
341 :: (day-of-year) ( year month day -- n )
342 day-counts month head-slice sum day +
344 year month day <date>
349 : day-of-year ( timestamp -- n )
350 >date< (day-of-year) ;
352 : same-day? ( ts1 ts2 -- ? )
353 [ >gmt slots{ year month day } ] same? ;
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 [ >gmt [ year>> ] [ week-number ] bi 2array ] same? ;
383 : same-hour? ( ts1 ts2 -- ? )
384 [ >gmt slots{ year month day hour } ] same? ;
386 : same-minute? ( ts1 ts2 -- ? )
387 [ >gmt slots{ year month day hour minute } ] same? ;
389 : same-second? ( ts1 ts2 -- ? )
390 [ >gmt slots{ year month day hour minute second } ] same? ;
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 ] dip 1000 / +second ;
442 : timestamp>millis ( timestamp -- n )
443 unix-1970 (time-) 1000 * >integer ;
445 : micros>timestamp ( x -- timestamp )
446 [ unix-1970 ] dip 1000000 / +second ;
448 : timestamp>micros ( timestamp -- n )
449 unix-1970 (time-) 1000000 * >integer ;
451 : now ( -- timestamp )
452 gmt gmt-offset-duration (time+) >>gmt-offset ;
454 : now-gmt ( -- timestamp ) gmt ;
456 : hence ( duration -- timestamp ) now swap time+ ;
457 : hence-gmt ( duration -- timestamp ) now-gmt swap time+ ;
459 : ago ( duration -- timestamp ) now swap time- ;
460 : ago-gmt ( duration -- timestamp ) now-gmt swap time- ;
462 GENERIC: days-in-year ( obj -- n )
464 M: integer days-in-year leap-year? 366 365 ? ;
466 M: timestamp days-in-year year>> days-in-year ;
468 : days-in-month ( timestamp -- n )
469 >date< drop (days-in-month) ;
471 : midnight! ( timestamp -- timestamp ) 0 0 0 set-time! ; inline
472 : midnight ( timestamp -- new-timestamp ) clone midnight! ; inline
473 : midnight-gmt! ( timestamp -- timestamp ) 0 0 0 set-time! instant >>gmt-offset ; inline
474 : midnight-gmt ( timestamp -- new-timestamp ) clone midnight-gmt! ; inline
476 : noon! ( timestamp -- timestamp ) 12 0 0 set-time! ; inline
477 : noon ( timestamp -- new-timestamp ) clone noon! ; inline
478 : noon-gmt! ( timestamp -- timestamp ) 12 0 0 set-time! instant >>gmt-offset ; inline
479 : noon-gmt ( timestamp -- new-timestamp ) clone noon-gmt! ; inline
481 : today ( -- timestamp ) now midnight! ; inline
482 : today-gmt ( -- timestamp ) now midnight-gmt! ; inline
483 : tomorrow ( -- timestamp ) 1 days hence midnight! ; inline
484 : tomorrow-gmt ( -- timestamp ) 1 days hence midnight-gmt! ; inline
485 : overtomorrow ( -- timestamp ) 2 days hence midnight! ; inline
486 : overtomorrow-gmt ( -- timestamp ) 2 days hence midnight-gmt! ; inline
487 : yesterday ( -- timestamp ) 1 days ago midnight! ; inline
488 : yesterday-gmt ( -- timestamp ) 1 days ago midnight-gmt! ; inline
489 : ereyesterday ( -- timestamp ) 2 days ago midnight! ; inline
490 : ereyesterday-gmt ( -- timestamp ) 2 days ago midnight-gmt! ; inline
492 : start-of-day! ( timestamp -- timestamp ) midnight! ; inline
493 : start-of-day ( object -- new-timestamp ) midnight ; inline
495 : end-of-day! ( timestamp -- timestamp )
496 23 >>hour 59 >>minute 59+999/1000 >>second ; inline
498 : end-of-day ( object -- new-timestamp )
499 clone end-of-day! ; inline
501 : start-of-month ( timestamp -- new-timestamp )
502 midnight 1 >>day ; inline
504 : end-of-month ( timestamp -- new-timestamp )
505 [ end-of-day ] [ days-in-month ] bi >>day ;
507 : start-of-quarter ( timestamp -- new-timestamp )
508 [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
510 : end-of-quarter ( timestamp -- new-timestamp )
511 [ clone ] [ quarter 1 - 3 * 3 + ] bi >>month end-of-month ; inline
513 : first-day-of-month ( object -- new-timestamp )
516 : last-day-of-month ( object -- new-timestamp )
517 clone dup days-in-month >>day ; inline
519 GENERIC: first-day-of-year ( object -- new-timestamp )
520 M: timestamp first-day-of-year first-day-of-month 1 >>month ;
521 M: integer first-day-of-year <year> ;
523 GENERIC: last-day-of-year ( object -- new-timestamp )
524 M: timestamp last-day-of-year clone 12 >>month 31 >>day ;
525 M: integer last-day-of-year 12 31 <date> ;
527 GENERIC: first-day-of-decade ( object -- new-timestamp )
528 M: timestamp first-day-of-decade first-day-of-year [ dup 10 mod - ] change-year ;
529 M: integer first-day-of-decade first-day-of-year [ dup 10 mod - ] change-year ;
531 GENERIC: last-day-of-decade ( object -- new-timestamp )
532 M: timestamp last-day-of-decade last-day-of-year [ dup 10 mod - 9 + ] change-year ;
533 M: integer last-day-of-decade last-day-of-year [ dup 10 mod - 9 + ] change-year ;
535 GENERIC: first-day-of-century ( object -- new-timestamp )
536 M: timestamp first-day-of-century first-day-of-year [ dup 100 mod - ] change-year ;
537 M: integer first-day-of-century first-day-of-year [ dup 100 mod - ] change-year ;
539 GENERIC: last-day-of-century ( object -- new-timestamp )
540 M: timestamp last-day-of-century last-day-of-year [ dup 100 mod - 99 + ] change-year ;
541 M: integer last-day-of-century last-day-of-year [ dup 100 mod - 99 + ] change-year ;
543 GENERIC: first-day-of-millennium ( object -- new-timestamp )
544 M: timestamp first-day-of-millennium first-day-of-year [ dup 1000 mod - ] change-year ;
545 M: integer first-day-of-millennium first-day-of-year [ dup 1000 mod - ] change-year ;
547 GENERIC: last-day-of-millennium ( object -- new-timestamp )
548 M: timestamp last-day-of-millennium last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
549 M: integer last-day-of-millennium last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
551 : start-of-year ( object -- new-timestamp )
552 first-day-of-year start-of-day! ;
554 : end-of-year ( object -- new-timestamp )
555 last-day-of-year end-of-day! ;
557 : start-of-decade ( object -- new-timestamp )
558 first-day-of-decade start-of-day! ;
560 : end-of-decade ( object -- new-timestamp )
561 last-day-of-decade end-of-day! ;
563 : end-of-century ( object -- new-timestamp )
564 last-day-of-century end-of-day! ;
566 : start-of-millennium ( object -- new-timestamp )
567 first-day-of-millennium start-of-day! ;
569 : end-of-millennium ( object -- new-timestamp )
570 last-day-of-millennium end-of-day! ;
572 : start-of-hour ( timestamp -- new-timestamp ) clone 0 >>minute 0 >>second ;
573 : end-of-hour ( timestamp -- new-timestamp ) clone 59 >>minute 59+999/1000 >>second ;
575 : start-of-minute ( timestamp -- new-timestamp ) clone 0 >>second ;
576 : end-of-minute ( timestamp -- new-timestamp ) clone 59+999/1000 >>second ;
578 GENERIC: start-of-second ( object -- new-timestamp )
579 M: timestamp start-of-second clone [ floor ] change-second ;
580 M: real start-of-second floor ;
582 GENERIC: end-of-second ( object -- new-timestamp )
583 M: timestamp end-of-second clone [ floor 999/1000 + ] change-second ;
584 M: real end-of-second floor 999/1000 + ;
588 : day-offset ( timestamp m -- new-timestamp n )
589 over day-of-week - ; inline
591 : day-this-week ( timestamp n -- new-timestamp )
592 day-offset days time+ ;
594 : closest-day ( timestamp n -- new-timestamp )
595 [ dup day-of-week ] dip
597 rot 7 swap - <rotated> nth days time+ ;
599 :: nth-day-this-month ( timestamp n day -- new-timestamp )
600 timestamp start-of-month day day-this-week
601 dup timestamp [ month>> ] same?
602 [ 1 weeks time+ ] unless
603 n [ weeks time+ ] unless-zero ;
607 GENERIC: january ( obj -- timestamp )
608 GENERIC: february ( obj -- timestamp )
609 GENERIC: march ( obj -- timestamp )
610 GENERIC: april ( obj -- timestamp )
611 GENERIC: may ( obj -- timestamp )
612 GENERIC: june ( obj -- timestamp )
613 GENERIC: july ( obj -- timestamp )
614 GENERIC: august ( obj -- timestamp )
615 GENERIC: september ( obj -- timestamp )
616 GENERIC: october ( obj -- timestamp )
617 GENERIC: november ( obj -- timestamp )
618 GENERIC: december ( obj -- timestamp )
620 M: integer january 1 1 <date> ;
621 M: integer february 2 1 <date> ;
622 M: integer march 3 1 <date> ;
623 M: integer april 4 1 <date> ;
624 M: integer may 5 1 <date> ;
625 M: integer june 6 1 <date> ;
626 M: integer july 7 1 <date> ;
627 M: integer august 8 1 <date> ;
628 M: integer september 9 1 <date> ;
629 M: integer october 10 1 <date> ;
630 M: integer november 11 1 <date> ;
631 M: integer december 12 1 <date> ;
633 M: timestamp january clone 1 >>month ;
634 M: timestamp february clone 2 >>month ;
635 M: timestamp march clone 3 >>month ;
636 M: timestamp april clone 4 >>month ;
637 M: timestamp may clone 5 >>month ;
638 M: timestamp june clone 6 >>month ;
639 M: timestamp july clone 7 >>month ;
640 M: timestamp august clone 8 >>month ;
641 M: timestamp september clone 9 >>month ;
642 M: timestamp october clone 10 >>month ;
643 M: timestamp november clone 11 >>month ;
644 M: timestamp december clone 12 >>month ;
646 GENERIC: january-gmt ( obj -- timestamp )
647 GENERIC: february-gmt ( obj -- timestamp )
648 GENERIC: march-gmt ( obj -- timestamp )
649 GENERIC: april-gmt ( obj -- timestamp )
650 GENERIC: may-gmt ( obj -- timestamp )
651 GENERIC: june-gmt ( obj -- timestamp )
652 GENERIC: july-gmt ( obj -- timestamp )
653 GENERIC: august-gmt ( obj -- timestamp )
654 GENERIC: september-gmt ( obj -- timestamp )
655 GENERIC: october-gmt ( obj -- timestamp )
656 GENERIC: november-gmt ( obj -- timestamp )
657 GENERIC: december-gmt ( obj -- timestamp )
659 M: integer january-gmt 1 1 <date-gmt> ;
660 M: integer february-gmt 2 1 <date-gmt> ;
661 M: integer march-gmt 3 1 <date-gmt> ;
662 M: integer april-gmt 4 1 <date-gmt> ;
663 M: integer may-gmt 5 1 <date-gmt> ;
664 M: integer june-gmt 6 1 <date-gmt> ;
665 M: integer july-gmt 7 1 <date-gmt> ;
666 M: integer august-gmt 8 1 <date-gmt> ;
667 M: integer september-gmt 9 1 <date-gmt> ;
668 M: integer october-gmt 10 1 <date-gmt> ;
669 M: integer november-gmt 11 1 <date-gmt> ;
670 M: integer december-gmt 12 1 <date-gmt> ;
672 M: timestamp january-gmt >gmt 1 >>month ;
673 M: timestamp february-gmt >gmt 2 >>month ;
674 M: timestamp march-gmt >gmt 3 >>month ;
675 M: timestamp april-gmt >gmt 4 >>month ;
676 M: timestamp may-gmt >gmt 5 >>month ;
677 M: timestamp june-gmt >gmt 6 >>month ;
678 M: timestamp july-gmt >gmt 7 >>month ;
679 M: timestamp august-gmt >gmt 8 >>month ;
680 M: timestamp september-gmt >gmt 9 >>month ;
681 M: timestamp october-gmt >gmt 10 >>month ;
682 M: timestamp november-gmt >gmt 11 >>month ;
683 M: timestamp december-gmt >gmt 12 >>month ;
685 : closest-sunday ( timestamp -- new-timestamp ) 0 closest-day ;
686 : closest-monday ( timestamp -- new-timestamp ) 1 closest-day ;
687 : closest-tuesday ( timestamp -- new-timestamp ) 2 closest-day ;
688 : closest-wednesday ( timestamp -- new-timestamp ) 3 closest-day ;
689 : closest-thursday ( timestamp -- new-timestamp ) 4 closest-day ;
690 : closest-friday ( timestamp -- new-timestamp ) 5 closest-day ;
691 : closest-saturday ( timestamp -- new-timestamp ) 6 closest-day ;
693 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
694 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
695 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
696 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
697 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
698 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
699 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
701 : sunday-gmt ( timestamp -- new-timestamp ) sunday >gmt! ;
702 : monday-gmt ( timestamp -- new-timestamp ) monday >gmt! ;
703 : tuesday-gmt ( timestamp -- new-timestamp ) tuesday >gmt! ;
704 : wednesday-gmt ( timestamp -- new-timestamp ) wednesday >gmt! ;
705 : thursday-gmt ( timestamp -- new-timestamp ) thursday >gmt! ;
706 : friday-gmt ( timestamp -- new-timestamp ) friday >gmt! ;
707 : saturday-gmt ( timestamp -- new-timestamp ) saturday >gmt! ;
709 : first-day-of-week ( object -- new-timestamp ) sunday ; inline
710 : last-day-of-week ( object -- new-timestamp ) saturday ; inline
712 : day< ( quot -- new-timestamp ) keep over before=? [ 7 days time- ] when ; inline
713 : day<= ( quot -- new-timestamp ) keep over before? [ 7 days time- ] when ; inline
714 : day> ( quot -- new-timestamp ) keep over after=? [ 7 days time+ ] when ; inline
715 : day>= ( quot -- new-timestamp ) keep over after? [ 7 days time+ ] when ; inline
717 : sunday< ( timestamp -- new-timestamp ) [ sunday ] day< ;
718 : monday< ( timestamp -- new-timestamp ) [ monday ] day< ;
719 : tuesday< ( timestamp -- new-timestamp ) [ tuesday ] day< ;
720 : wednesday< ( timestamp -- new-timestamp ) [ wednesday ] day< ;
721 : thursday< ( timestamp -- new-timestamp ) [ thursday ] day< ;
722 : friday< ( timestamp -- new-timestamp ) [ friday ] day< ;
723 : saturday< ( timestamp -- new-timestamp ) [ saturday ] day< ;
725 : sunday<= ( timestamp -- new-timestamp ) [ sunday ] day<= ;
726 : monday<= ( timestamp -- new-timestamp ) [ monday ] day<= ;
727 : tuesday<= ( timestamp -- new-timestamp ) [ tuesday ] day<= ;
728 : wednesday<= ( timestamp -- new-timestamp ) [ wednesday ] day<= ;
729 : thursday<= ( timestamp -- new-timestamp ) [ thursday ] day<= ;
730 : friday<= ( timestamp -- new-timestamp ) [ friday ] day<= ;
731 : saturday<= ( timestamp -- new-timestamp ) [ saturday ] day<= ;
733 : sunday> ( timestamp -- new-timestamp ) [ sunday ] day> ;
734 : monday> ( timestamp -- new-timestamp ) [ monday ] day> ;
735 : tuesday> ( timestamp -- new-timestamp ) [ tuesday ] day> ;
736 : wednesday> ( timestamp -- new-timestamp ) [ wednesday ] day> ;
737 : thursday> ( timestamp -- new-timestamp ) [ thursday ] day> ;
738 : friday> ( timestamp -- new-timestamp ) [ friday ] day> ;
739 : saturday> ( timestamp -- new-timestamp ) [ saturday ] day> ;
741 : sunday>= ( timestamp -- new-timestamp ) [ sunday ] day>= ;
742 : monday>= ( timestamp -- new-timestamp ) [ monday ] day>= ;
743 : tuesday>= ( timestamp -- new-timestamp ) [ tuesday ] day>= ;
744 : wednesday>= ( timestamp -- new-timestamp ) [ wednesday ] day>= ;
745 : thursday>= ( timestamp -- new-timestamp ) [ thursday ] day>= ;
746 : friday>= ( timestamp -- new-timestamp ) [ friday ] day>= ;
747 : saturday>= ( timestamp -- new-timestamp ) [ saturday ] day>= ;
749 : next-sunday ( timestamp -- new-timestamp ) closest-sunday sunday> ;
750 : next-monday ( timestamp -- new-timestamp ) closest-monday monday> ;
751 : next-tuesday ( timestamp -- new-timestamp ) closest-tuesday tuesday> ;
752 : next-wednesday ( timestamp -- new-timestamp ) closest-wednesday wednesday> ;
753 : next-thursday ( timestamp -- new-timestamp ) closest-thursday thursday> ;
754 : next-friday ( timestamp -- new-timestamp ) closest-friday friday> ;
755 : next-saturday ( timestamp -- new-timestamp ) closest-saturday saturday> ;
757 : last-sunday ( timestamp -- new-timestamp ) closest-sunday sunday< ;
758 : last-monday ( timestamp -- new-timestamp ) closest-monday monday< ;
759 : last-tuesday ( timestamp -- new-timestamp ) closest-tuesday tuesday< ;
760 : last-wednesday ( timestamp -- new-timestamp ) closest-wednesday wednesday< ;
761 : last-thursday ( timestamp -- new-timestamp ) closest-thursday thursday< ;
762 : last-friday ( timestamp -- new-timestamp ) closest-friday friday< ;
763 : last-saturday ( timestamp -- new-timestamp ) closest-saturday saturday< ;
765 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
766 : monday? ( timestamp -- ? ) day-of-week 1 = ;
767 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
768 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
769 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
770 : friday? ( timestamp -- ? ) day-of-week 5 = ;
771 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
773 : january? ( obj -- timestamp ) month>> 1 = ;
774 : february? ( obj -- timestamp ) month>> 2 = ;
775 : march? ( obj -- timestamp ) month>> 3 = ;
776 : april? ( obj -- timestamp ) month>> 4 = ;
777 : may? ( obj -- timestamp ) month>> 5 = ;
778 : june? ( obj -- timestamp ) month>> 6 = ;
779 : july? ( obj -- timestamp ) month>> 7 = ;
780 : august? ( obj -- timestamp ) month>> 8 = ;
781 : september? ( obj -- timestamp ) month>> 9 = ;
782 : october? ( obj -- timestamp ) month>> 10 = ;
783 : november? ( obj -- timestamp ) month>> 11 = ;
784 : december? ( obj -- timestamp ) month>> 12 = ;
786 GENERIC: weekend? ( obj -- ? )
787 M: timestamp weekend? day-of-week weekend? ;
788 M: integer weekend? { 0 6 } member? ;
790 GENERIC: weekday? ( obj -- ? )
791 M: timestamp weekday? day-of-week weekday? ;
792 M: integer weekday? weekend? not ;
794 : same-or-next-business-day ( timestamp -- timestamp' )
797 { 6 [ 2 days time+ ] }
801 : same-or-previous-business-day ( timestamp -- timestamp' )
803 { 0 [ 2 days time- ] }
808 : weekdays-between ( date1 date2 -- n )
810 [ swap time- duration>days 5 * ]
811 [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
813 day-of-week 6 = [ [ 1 - ] dip ] when
814 day-of-week 0 = [ 1 - ] when ;
816 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
818 : weekdays-between2 ( date1 date2 -- n )
819 [ swap time- duration>days 1 + ]
820 [ [ day-of-week ] bi@ 6 swap - ] 2bi
823 [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
825 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
826 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
827 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
828 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
829 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
830 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
831 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
833 : last-sunday-of-month ( timestamp -- new-timestamp ) last-day-of-month sunday<= ;
834 : last-monday-of-month ( timestamp -- new-timestamp ) last-day-of-month monday<= ;
835 : last-tuesday-of-month ( timestamp -- new-timestamp ) last-day-of-month tuesday<= ;
836 : last-wednesday-of-month ( timestamp -- new-timestamp ) last-day-of-month wednesday<= ;
837 : last-thursday-of-month ( timestamp -- new-timestamp ) last-day-of-month thursday<= ;
838 : last-friday-of-month ( timestamp -- new-timestamp ) last-day-of-month friday<= ;
839 : last-saturday-of-month ( timestamp -- new-timestamp ) last-day-of-month saturday<= ;
841 : start-of-week ( timestamp -- new-timestamp )
844 : end-of-week ( timestamp -- new-timestamp )
845 saturday end-of-day! ;
847 : o'clock ( timestamp n -- new-timestamp )
848 [ midnight ] dip >>hour ;
850 : am ( timestamp n -- new-timestamp )
851 0 12 [a,b] check-interval o'clock ;
853 : pm ( timestamp n -- new-timestamp )
854 0 12 [a,b] check-interval 12 + o'clock ;
856 : time-since-midnight ( timestamp -- duration )
857 dup midnight time- ; inline
859 : since-1970 ( duration -- timestamp )
860 unix-1970 time+ ; inline
862 : timestamp>unix-time ( timestamp -- seconds )
863 unix-1970 (time-) ; inline
865 : unix-time>timestamp ( seconds -- timestamp )
866 [ unix-1970 ] dip +second ; inline
868 ! January and February need a fixup with this algorithm.
869 ! Find a better algorithm.
870 : ymd>ordinal ( year month day -- ordinal )
871 [ leap-year? dup -2 -3 ? ]
872 [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
873 [ ] tri* + + >integer
876 : timestamp>year-dates-gmt ( timestamp -- seq )
877 [ start-of-year >date< julian-day-number ]
880 [a..b) [ julian-day-number>date <date-gmt> ] map ;
882 : year-ordinal>timestamp ( year ordinal -- timestamp )
883 [ 1 1 julian-day-number ] dip
884 + 1 - julian-day-number>date <date> ;
886 GENERIC: weeks-in-week-year ( obj -- n )
888 M: integer weeks-in-week-year
889 { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
891 M: timestamp weeks-in-week-year
892 { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
895 { [ os unix? ] [ "calendar.unix" ] }
896 { [ os windows? ] [ "calendar.windows" ] }
899 { "threads" "calendar" } "calendar.threads" require-when