1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.tuple combinators
4 combinators.short-circuit kernel locals math math.functions
5 math.intervals math.order math.parser sequences splitting system
7 QUALIFIED-WITH: math.ranges R
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: gmt os ( -- timestamp )
27 C: <duration> duration
29 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
38 { gmt-offset duration } ;
40 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
42 GENERIC: leap-year? ( obj -- ? )
45 dup 100 divisor? 400 4 ? divisor? ;
47 M: timestamp leap-year?
50 : (days-in-month) ( year month -- n )
51 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
53 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
55 month 1 12 [a,b] check-interval
56 day 1 year month (days-in-month) [a,b] check-interval
57 hour 0 23 [a,b] check-interval
58 minute 0 59 [a,b] check-interval
59 second 0 60 [a,b) check-interval
60 gmt-offset timestamp boa ;
62 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
64 : gmt-offset-duration ( -- duration )
65 0 0 0 gmt-offset <duration> ; inline
67 : <date> ( year month day -- timestamp )
68 0 0 0 gmt-offset-duration <timestamp> ; inline
70 : <date-gmt> ( year month day -- timestamp )
71 0 0 0 instant <timestamp> ; inline
73 : <year> ( year -- timestamp )
76 : <year-gmt> ( year -- timestamp )
77 1 1 <date-gmt> ; inline
79 CONSTANT: average-month 30+5/12
80 CONSTANT: months-per-year 12
81 CONSTANT: days-per-year 3652425/10000
82 CONSTANT: hours-per-year 876582/100
83 CONSTANT: minutes-per-year 5259492/10
84 CONSTANT: seconds-per-year 31556952
86 :: julian-day-number ( year month day -- n )
87 ! Returns a composite date number
88 ! Not valid before year -4800
91 month 12 a * + 3 - :> m
93 day 153 m * 2 + 5 /i + 365 y * +
94 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
96 :: julian-day-number>date ( n -- year month day )
97 ! Inverse of julian-day-number
99 4 a * 3 + 146097 /i :> b
100 a 146097 b * 4 /i - :> c
101 4 c * 3 + 1461 /i :> d
102 c 1461 d * 4 /i - :> e
103 5 e * 2 + 153 /i :> m
108 e 153 m * 2 + 5 /i - 1 + ;
110 GENERIC: easter ( obj -- obj' )
112 :: easter-month-day ( year -- month day )
114 year 100 /mod :> ( b c )
118 19 a * b + d - g - 15 + 30 mod :> h
120 32 2 e * + 2 i * + h - k - 7 mod :> l
121 a 11 h * + 22 l * + 451 /i :> m
123 h l + 7 m * - 114 + 31 /mod 1 + ;
126 dup easter-month-day <date> ;
130 dup year>> easter-month-day
131 swapd >>day swap >>month ;
133 : >date< ( timestamp -- year month day )
134 [ year>> ] [ month>> ] [ day>> ] tri ;
136 : >time< ( timestamp -- hour minute second )
137 [ hour>> ] [ minute>> ] [ second>> ] tri ;
139 : set-time! ( timestamp hours minutes seconds -- timestamp )
140 [ >>hour ] [ >>minute ] [ >>second ] tri* ;
142 : set-time ( timestamp hours minutes seconds -- timestamp )
145 : time>hms ( str -- hms-seq )
146 ":" split [ string>number ] map
149 : time>offset ( str -- hms-seq )
150 "-" ?head [ time>hms ] dip
151 [ [ neg ] map ] when ;
153 : years ( x -- duration ) instant swap >>year ;
154 : months ( x -- duration ) instant swap >>month ;
155 : days ( x -- duration ) instant swap >>day ;
156 : weeks ( x -- duration ) 7 * days ;
157 : hours ( x -- duration ) instant swap >>hour ;
158 : minutes ( x -- duration ) instant swap >>minute ;
159 : seconds ( x -- duration ) instant swap >>second ;
160 : milliseconds ( x -- duration ) 1000 / seconds ;
161 : microseconds ( x -- duration ) 1000000 / seconds ;
162 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
166 GENERIC: +year ( timestamp x -- timestamp )
167 GENERIC: +month ( timestamp x -- timestamp )
168 GENERIC: +day ( timestamp x -- timestamp )
169 GENERIC: +hour ( timestamp x -- timestamp )
170 GENERIC: +minute ( timestamp x -- timestamp )
171 GENERIC: +second ( timestamp x -- timestamp )
173 : /rem ( f n -- q r )
174 ! q is positive or negative, r is positive from 0 <= r < n
175 [ / floor >integer ] 2keep rem ;
177 : float>whole-part ( float -- int float )
178 [ floor >integer ] keep over - ;
180 : adjust-leap-year ( timestamp -- timestamp )
182 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
183 [ 3 >>month 1 >>day ] when ;
186 [ + ] curry change-year adjust-leap-year ;
189 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
191 : months/years ( n -- months years )
192 12 /rem [ 1 - 12 ] when-zero swap ; inline
195 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
198 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
202 over >date< julian-day-number + julian-day-number>date
203 [ >>year ] [ >>month ] [ >>day ] tri*
207 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
209 : hours/days ( n -- hours days )
213 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
216 float>whole-part swapd 60 * +minute swap +hour ;
218 : minutes/hours ( n -- minutes hours )
222 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
225 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
227 : seconds/minutes ( n -- seconds minutes )
228 60 /rem swap >integer ;
231 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
233 : (time+) ( timestamp duration -- timestamp' duration )
234 [ second>> +second ] keep
235 [ minute>> +minute ] keep
236 [ hour>> +hour ] keep
238 [ month>> +month ] keep
239 [ year>> +year ] keep ; inline
241 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
242 [ bi@ + ] curry 2keep ; inline
246 GENERIC#: time+ 1 ( time1 time2 -- time3 )
249 [ clone ] dip (time+) drop ;
264 : duration>years ( duration -- x )
265 ! Uses average month/year length since duration loses calendar
270 [ month>> months-per-year / + ]
271 [ day>> days-per-year / + ]
272 [ hour>> hours-per-year / + ]
273 [ minute>> minutes-per-year / + ]
274 [ second>> seconds-per-year / + ]
277 M: duration <=> [ duration>years ] compare ;
279 : duration>months ( duration -- x ) duration>years months-per-year * ;
280 : duration>days ( duration -- x ) duration>years days-per-year * ;
281 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
282 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
283 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
284 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
285 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
286 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
288 GENERIC: time- ( time1 time2 -- time3 )
290 : convert-timezone ( timestamp duration -- timestamp' )
291 over gmt-offset>> over = [ drop ] [
292 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
295 : >local-time ( timestamp -- timestamp' )
296 clone gmt-offset-duration convert-timezone ;
298 : >gmt ( timestamp -- timestamp' )
299 clone dup gmt-offset>> dup instant =
301 [ neg +second 0 ] change-second
302 [ neg +minute 0 ] change-minute
303 [ neg +hour 0 ] change-hour
304 [ neg +day 0 ] change-day
305 [ neg +month 0 ] change-month
306 [ neg +year 0 ] change-year drop
309 M: timestamp <=> [ >gmt tuple-slots ] compare ;
311 : same-day? ( ts1 ts2 -- ? )
312 [ >gmt >date< <date> ] same? ;
314 : (time-) ( timestamp timestamp -- n )
316 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
317 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
320 ! Exact calendar-time difference
323 : time* ( obj1 obj2 -- obj3 )
324 dup real? [ swap ] when
336 : before ( duration -- -duration )
341 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
342 [ bi@ - ] curry 2keep ; inline
359 : unix-1970 ( -- timestamp )
360 1970 <year-gmt> ; inline
362 : millis>timestamp ( x -- timestamp )
363 [ unix-1970 ] dip 1000 / +second ;
365 : timestamp>millis ( timestamp -- n )
366 unix-1970 (time-) 1000 * >integer ;
368 : micros>timestamp ( x -- timestamp )
369 [ unix-1970 ] dip 1000000 / +second ;
371 : timestamp>micros ( timestamp -- n )
372 unix-1970 (time-) 1000000 * >integer ;
374 : now ( -- timestamp )
375 gmt gmt-offset-duration (time+) >>gmt-offset ;
377 : hence ( duration -- timestamp ) now swap time+ ;
379 : ago ( duration -- timestamp ) now swap time- ;
381 : zeller-congruence ( year month day -- n )
383 ! http://web.textfiles.com/computers/formulas.txt
384 ! good for any date since October 15, 1582
386 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
387 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
388 [ 1 + 3 * 5 /i + ] keep 2 * +
391 GENERIC: days-in-year ( obj -- n )
393 M: integer days-in-year leap-year? 366 365 ? ;
395 M: timestamp days-in-year year>> days-in-year ;
397 : days-in-month ( timestamp -- n )
398 >date< drop (days-in-month) ;
400 : day-of-week ( timestamp -- n )
401 >date< zeller-congruence ;
403 :: (day-of-year) ( year month day -- n )
404 day-counts month head-slice sum day +
406 year month day <date>
411 : day-of-year ( timestamp -- n )
412 >date< (day-of-year) ;
414 : midnight! ( timestamp -- new-timestamp )
415 0 >>hour 0 >>minute 0 >>second ; inline
417 : midnight ( timestamp -- new-timestamp )
418 clone midnight! ; inline
420 : noon ( timestamp -- new-timestamp )
421 midnight 12 >>hour ; inline
423 : today ( -- timestamp )
424 now midnight! ; inline
426 : tomorrow ( -- timestamp )
427 1 days hence midnight! ; inline
429 : yesterday ( -- timestamp )
430 1 days ago midnight! ; inline
432 GENERIC: beginning-of-day ( object -- new-timestamp )
433 M: timestamp beginning-of-day midnight ;
435 : end-of-day! ( timestamp -- timestamp )
436 23 >>hour 59 >>minute 59+999/1000 >>second ;
438 GENERIC: end-of-day ( object -- new-timestamp )
439 M: timestamp end-of-day clone end-of-day! ;
441 : beginning-of-month ( timestamp -- new-timestamp )
442 midnight 1 >>day ; inline
444 : end-of-month ( timestamp -- new-timestamp )
445 [ end-of-day ] [ days-in-month ] bi >>day ;
447 GENERIC: beginning-of-year ( object -- new-timestamp )
448 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
449 M: integer beginning-of-year <year> ;
451 GENERIC: end-of-year ( object -- new-timestamp )
452 M: timestamp end-of-year end-of-day 12 >>month 31 >>day ;
453 M: integer end-of-year 12 31 <date> end-of-day! ;
457 : day-offset ( timestamp m -- new-timestamp n )
458 over day-of-week - ; inline
460 : day-this-week ( timestamp n -- new-timestamp )
461 day-offset days time+ ;
463 :: nth-day-this-month ( timestamp n day -- new-timestamp )
464 timestamp beginning-of-month day day-this-week
465 dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless ;
467 : last-day-this-month ( timestamp day -- new-timestamp )
468 [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
472 GENERIC: january ( obj -- timestamp )
473 GENERIC: february ( obj -- timestamp )
474 GENERIC: march ( obj -- timestamp )
475 GENERIC: april ( obj -- timestamp )
476 GENERIC: may ( obj -- timestamp )
477 GENERIC: june ( obj -- timestamp )
478 GENERIC: july ( obj -- timestamp )
479 GENERIC: august ( obj -- timestamp )
480 GENERIC: september ( obj -- timestamp )
481 GENERIC: october ( obj -- timestamp )
482 GENERIC: november ( obj -- timestamp )
483 GENERIC: december ( obj -- timestamp )
485 M: integer january 1 1 <date> ;
486 M: integer february 2 1 <date> ;
487 M: integer march 3 1 <date> ;
488 M: integer april 4 1 <date> ;
489 M: integer may 5 1 <date> ;
490 M: integer june 6 1 <date> ;
491 M: integer july 7 1 <date> ;
492 M: integer august 8 1 <date> ;
493 M: integer september 9 1 <date> ;
494 M: integer october 10 1 <date> ;
495 M: integer november 11 1 <date> ;
496 M: integer december 12 1 <date> ;
498 M: timestamp january clone 1 >>month ;
499 M: timestamp february clone 2 >>month ;
500 M: timestamp march clone 3 >>month ;
501 M: timestamp april clone 4 >>month ;
502 M: timestamp may clone 5 >>month ;
503 M: timestamp june clone 6 >>month ;
504 M: timestamp july clone 7 >>month ;
505 M: timestamp august clone 8 >>month ;
506 M: timestamp september clone 9 >>month ;
507 M: timestamp october clone 10 >>month ;
508 M: timestamp november clone 11 >>month ;
509 M: timestamp december clone 12 >>month ;
511 : <january> ( year day -- timestamp ) 1 swap <date> ; inline
512 : <february> ( year day -- timestamp ) 2 swap <date> ; inline
513 : <march> ( year day -- timestamp ) 3 swap <date> ; inline
514 : <april> ( year day -- timestamp ) 4 swap <date> ; inline
515 : <may> ( year day -- timestamp ) 5 swap <date> ; inline
516 : <june> ( year day -- timestamp ) 6 swap <date> ; inline
517 : <july> ( year day -- timestamp ) 7 swap <date> ; inline
518 : <august> ( year day -- timestamp ) 8 swap <date> ; inline
519 : <september> ( year day -- timestamp ) 9 swap <date> ; inline
520 : <october> ( year day -- timestamp ) 10 swap <date> ; inline
521 : <november> ( year day -- timestamp ) 11 swap <date> ; inline
522 : <december> ( year day -- timestamp ) 12 swap <date> ; inline
524 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
525 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
526 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
527 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
528 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
529 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
530 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
532 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
533 : monday? ( timestamp -- ? ) day-of-week 1 = ;
534 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
535 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
536 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
537 : friday? ( timestamp -- ? ) day-of-week 5 = ;
538 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
540 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
541 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
542 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
543 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
544 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
545 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
546 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
548 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
549 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
550 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
551 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
552 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
553 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
554 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
556 : beginning-of-week ( timestamp -- new-timestamp )
559 : o'clock ( timestamp n -- new-timestamp )
560 [ midnight ] dip >>hour ;
562 : am ( timestamp n -- new-timestamp )
563 0 12 [a,b] check-interval o'clock ;
565 : pm ( timestamp n -- new-timestamp )
566 0 12 [a,b] check-interval 12 + o'clock ;
568 : time-since-midnight ( timestamp -- duration )
569 dup midnight time- ; inline
571 : since-1970 ( duration -- timestamp )
572 unix-1970 time+ ; inline
574 : timestamp>unix-time ( timestamp -- seconds )
575 unix-1970 (time-) ; inline
577 : unix-time>timestamp ( seconds -- timestamp )
578 [ unix-1970 ] dip +second ; inline
580 : (week-number) ( timestamp -- [0,53] )
581 [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
583 : week-number ( timestamp -- [1,53] )
585 { 0 [ year>> 1 - end-of-year (week-number) ] }
586 { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
590 : quarter ( timestamp -- [1,4] )
591 month>> 3 /mod [ drop 1 + ] unless-zero ; inline
593 ! January and February need a fixup with this algorithm.
594 ! Find a better algorithm.
595 : ymd>ordinal ( year month day -- ordinal )
596 [ leap-year? dup -2 -3 ? ]
597 [ tuck dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
598 [ ] tri* + + >integer
600 rot 3 < [ - ] [ drop ] if ;
602 : timestamp>year-dates ( timestamp -- seq )
603 [ beginning-of-year >date< julian-day-number ]
606 R:[a,b) [ julian-day-number>date <date> ] map ;
608 : year-ordinal>timestamp ( year ordinal -- timestamp )
609 [ 1 1 julian-day-number ] dip
610 + 1 - julian-day-number>date <date> ;
612 GENERIC: weeks-in-week-year ( obj -- n )
613 M: integer weeks-in-week-year
614 { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
616 M: timestamp weeks-in-week-year
617 { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
620 { [ os unix? ] [ "calendar.unix" ] }
621 { [ os windows? ] [ "calendar.windows" ] }
624 { "threads" "calendar" } "calendar.threads" require-when