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.order sequences summary system vocabs vocabs.loader
9 HOOK: gmt-offset os ( -- hours minutes seconds )
11 HOOK: gmt os ( -- timestamp )
21 C: <duration> duration
23 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
32 { gmt-offset duration } ;
34 C: <timestamp> timestamp
36 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
38 : gmt-offset-duration ( -- duration )
39 0 0 0 gmt-offset <duration> ; inline
41 : <date> ( year month day -- timestamp )
42 0 0 0 gmt-offset-duration <timestamp> ; inline
44 : <date-gmt> ( year month day -- timestamp )
45 0 0 0 instant <timestamp> ; inline
47 : <year> ( year -- timestamp )
50 : <year-gmt> ( year -- timestamp )
51 1 1 <date-gmt> ; inline
54 M: not-a-month summary
55 drop "Months are indexed starting at 1" ;
59 : check-month ( n -- n )
60 [ throw-not-a-month ] when-zero ;
66 "January" "February" "March" "April" "May" "June"
67 "July" "August" "September" "October" "November" "December"
70 GENERIC: month-name ( obj -- string )
72 M: integer month-name check-month 1 - month-names nth ;
73 M: timestamp month-name month>> 1 - month-names nth ;
75 ERROR: not-a-month-abbreviation string ;
77 CONSTANT: month-abbreviations
79 "Jan" "Feb" "Mar" "Apr" "May" "Jun"
80 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
83 CONSTANT: month-abbreviations-hash
85 { "Jan" 1 } { "Feb" 2 } { "Mar" 3 }
86 { "Apr" 4 } { "May" 5 } { "Jun" 6 }
87 { "Jul" 7 } { "Aug" 8 } { "Sep" 9 }
88 { "Oct" 10 } { "Nov" 11 } { "Dec" 12 }
91 : month-abbreviation ( n -- string )
92 check-month 1 - month-abbreviations nth ;
94 : month-abbreviation-index ( string -- n )
95 month-abbreviations-hash ?at
96 [ throw-not-a-month-abbreviation ] unless ;
98 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
101 { "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" }
103 CONSTANT: day-abbreviations2
104 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
106 : day-abbreviation2 ( n -- string )
107 day-abbreviations2 nth ; inline
109 CONSTANT: day-abbreviations3
110 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
112 CONSTANT: day-abbreviations3-hash
114 { "Sun" 0 } { "Mon" 1 } { "Tue" 2 } { "Wed" 3 }
115 { "Thu" 4 } { "Fri" 5 } { "Sat" 6 }
118 CONSTANT: average-month 30+5/12
119 CONSTANT: months-per-year 12
120 CONSTANT: days-per-year 3652425/10000
121 CONSTANT: hours-per-year 876582/100
122 CONSTANT: minutes-per-year 5259492/10
123 CONSTANT: seconds-per-year 31556952
125 :: julian-day-number ( year month day -- n )
126 #! Returns a composite date number
127 #! Not valid before year -4800
128 14 month - 12 /i :> a
130 month 12 a * + 3 - :> m
132 day 153 m * 2 + 5 /i + 365 y * +
133 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
135 :: julian-day-number>date ( n -- year month day )
136 #! Inverse of julian-day-number
138 4 a * 3 + 146097 /i :> b
139 a 146097 b * 4 /i - :> c
140 4 c * 3 + 1461 /i :> d
141 c 1461 d * 4 /i - :> e
142 5 e * 2 + 153 /i :> m
147 e 153 m * 2 + 5 /i - 1 + ;
149 GENERIC: easter ( obj -- obj' )
151 :: easter-month-day ( year -- month day )
153 year 100 /mod :> ( b c )
157 19 a * b + d - g - 15 + 30 mod :> h
159 32 2 e * + 2 i * + h - k - 7 mod :> l
160 a 11 h * + 22 l * + 451 /i :> m
162 h l + 7 m * - 114 + 31 /mod 1 + ;
164 M: integer easter ( year -- timestamp )
165 dup easter-month-day <date> ;
167 M: timestamp easter ( timestamp -- timestamp )
169 dup year>> easter-month-day
170 swapd >>day swap >>month ;
172 : >date< ( timestamp -- year month day )
173 [ year>> ] [ month>> ] [ day>> ] tri ;
175 : >time< ( timestamp -- hour minute second )
176 [ hour>> ] [ minute>> ] [ second>> ] tri ;
178 : years ( x -- duration ) instant swap >>year ;
179 : months ( x -- duration ) instant swap >>month ;
180 : days ( x -- duration ) instant swap >>day ;
181 : weeks ( x -- duration ) 7 * 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 ;
189 GENERIC: leap-year? ( obj -- ? )
191 M: integer leap-year? ( year -- ? )
192 dup 100 divisor? 400 4 ? divisor? ;
194 M: timestamp leap-year? ( timestamp -- ? )
199 GENERIC: +year ( timestamp x -- timestamp )
200 GENERIC: +month ( timestamp x -- timestamp )
201 GENERIC: +day ( timestamp x -- timestamp )
202 GENERIC: +hour ( timestamp x -- timestamp )
203 GENERIC: +minute ( timestamp x -- timestamp )
204 GENERIC: +second ( timestamp x -- timestamp )
206 : /rem ( f n -- q r )
207 #! q is positive or negative, r is positive from 0 <= r < n
208 [ / floor >integer ] 2keep rem ;
210 : float>whole-part ( float -- int float )
211 [ floor >integer ] keep over - ;
213 : adjust-leap-year ( timestamp -- timestamp )
215 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
216 [ 3 >>month 1 >>day ] when ;
218 M: integer +year ( timestamp n -- timestamp )
219 [ + ] curry change-year adjust-leap-year ;
221 M: real +year ( timestamp n -- timestamp )
222 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
224 : months/years ( n -- months years )
225 12 /rem [ 1 - 12 ] when-zero swap ; inline
227 M: integer +month ( timestamp n -- timestamp )
228 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
230 M: real +month ( timestamp n -- timestamp )
231 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
233 M: integer +day ( timestamp n -- timestamp )
235 over >date< julian-day-number + julian-day-number>date
236 [ >>year ] [ >>month ] [ >>day ] tri*
239 M: real +day ( timestamp n -- timestamp )
240 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
242 : hours/days ( n -- hours days )
245 M: integer +hour ( timestamp n -- timestamp )
246 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
248 M: real +hour ( timestamp n -- timestamp )
249 float>whole-part swapd 60 * +minute swap +hour ;
251 : minutes/hours ( n -- minutes hours )
254 M: integer +minute ( timestamp n -- timestamp )
255 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
257 M: real +minute ( timestamp n -- timestamp )
258 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
260 : seconds/minutes ( n -- seconds minutes )
261 60 /rem swap >integer ;
263 M: number +second ( timestamp n -- timestamp )
264 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
266 : (time+) ( timestamp duration -- timestamp' duration )
267 [ second>> +second ] keep
268 [ minute>> +minute ] keep
269 [ hour>> +hour ] keep
271 [ month>> +month ] keep
272 [ year>> +year ] keep ; inline
274 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
275 [ bi@ + ] curry 2keep ; inline
279 GENERIC# time+ 1 ( time1 time2 -- time3 )
282 [ clone ] dip (time+) drop ;
297 : duration>years ( duration -- x )
298 #! Uses average month/year length since duration loses calendar
303 [ month>> months-per-year / + ]
304 [ day>> days-per-year / + ]
305 [ hour>> hours-per-year / + ]
306 [ minute>> minutes-per-year / + ]
307 [ second>> seconds-per-year / + ]
310 M: duration <=> [ duration>years ] compare ;
312 : duration>months ( duration -- x ) duration>years months-per-year * ;
313 : duration>days ( duration -- x ) duration>years days-per-year * ;
314 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
315 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
316 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
317 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
318 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
319 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
321 GENERIC: time- ( time1 time2 -- time3 )
323 : convert-timezone ( timestamp duration -- timestamp' )
324 over gmt-offset>> over = [ drop ] [
325 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
328 : >local-time ( timestamp -- timestamp' )
329 clone gmt-offset-duration convert-timezone ;
331 : >gmt ( timestamp -- timestamp' )
332 clone 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 M: timestamp <=> ( ts1 ts2 -- n )
343 [ >gmt tuple-slots ] compare ;
345 : same-day? ( ts1 ts2 -- ? )
346 [ >gmt >date< <date> ] same? ;
348 : (time-) ( timestamp timestamp -- n )
350 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
351 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
354 #! Exact calendar-time difference
357 : time* ( obj1 obj2 -- obj3 )
358 dup real? [ swap ] when
370 : before ( duration -- -duration )
375 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
376 [ bi@ - ] curry 2keep ; inline
393 : <zero> ( -- timestamp )
394 0 0 0 <date-gmt> ; inline
396 : valid-timestamp? ( timestamp -- ? )
397 clone instant >>gmt-offset
398 dup <zero> time- <zero> time+ = ;
400 : unix-1970 ( -- timestamp )
401 1970 <year-gmt> ; inline
403 : millis>timestamp ( x -- timestamp )
404 [ unix-1970 ] dip 1000 / +second ;
406 : timestamp>millis ( timestamp -- n )
407 unix-1970 (time-) 1000 * >integer ;
409 : micros>timestamp ( x -- timestamp )
410 [ unix-1970 ] dip 1000000 / +second ;
412 : timestamp>micros ( timestamp -- n )
413 unix-1970 (time-) 1000000 * >integer ;
415 : now ( -- timestamp )
416 gmt gmt-offset-duration (time+) >>gmt-offset ;
418 : hence ( duration -- timestamp ) now swap time+ ;
420 : ago ( duration -- timestamp ) now swap time- ;
422 : zeller-congruence ( year month day -- n )
424 #! http://web.textfiles.com/computers/formulas.txt
425 #! good for any date since October 15, 1582
427 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
428 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
429 [ 1 + 3 * 5 /i + ] keep 2 * +
432 GENERIC: days-in-year ( obj -- n )
434 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
435 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
437 : (days-in-month) ( year month -- n )
438 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
440 : days-in-month ( timestamp -- n )
441 >date< drop (days-in-month) ;
443 : day-of-week ( timestamp -- n )
444 >date< zeller-congruence ;
446 GENERIC: day-name ( obj -- string )
447 M: integer day-name day-names nth ;
448 M: timestamp day-name day-of-week day-names nth ;
450 :: (day-of-year) ( year month day -- n )
451 day-counts month head-slice sum day +
453 year month day <date>
458 : day-of-year ( timestamp -- n )
459 >date< (day-of-year) ;
461 : midnight ( timestamp -- new-timestamp )
462 clone 0 >>hour 0 >>minute 0 >>second ; inline
464 : noon ( timestamp -- new-timestamp )
465 midnight 12 >>hour ; inline
467 : today ( -- timestamp )
468 now midnight ; inline
470 : tomorrow ( -- timestamp )
471 1 days hence midnight ; inline
473 : yesterday ( -- timestamp )
474 1 days ago midnight ; inline
476 : beginning-of-month ( timestamp -- new-timestamp )
477 midnight 1 >>day ; inline
479 : end-of-month ( timestamp -- new-timestamp )
480 [ midnight ] [ days-in-month ] bi >>day ;
484 : day-offset ( timestamp m -- new-timestamp n )
485 over day-of-week - ; inline
487 : day-this-week ( timestamp n -- new-timestamp )
488 day-offset days time+ ;
490 :: nth-day-this-month ( timestamp n day -- new-timestamp )
491 timestamp beginning-of-month day day-this-week
492 dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless
493 n 1 - [ weeks time+ ] unless-zero ;
495 : last-day-this-month ( timestamp day -- new-timestamp )
496 [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
500 GENERIC: january ( obj -- timestamp )
501 GENERIC: february ( obj -- timestamp )
502 GENERIC: march ( obj -- timestamp )
503 GENERIC: april ( obj -- timestamp )
504 GENERIC: may ( obj -- timestamp )
505 GENERIC: june ( obj -- timestamp )
506 GENERIC: july ( obj -- timestamp )
507 GENERIC: august ( obj -- timestamp )
508 GENERIC: september ( obj -- timestamp )
509 GENERIC: october ( obj -- timestamp )
510 GENERIC: november ( obj -- timestamp )
511 GENERIC: december ( obj -- timestamp )
513 M: integer january 1 1 <date> ;
514 M: integer february 2 1 <date> ;
515 M: integer march 3 1 <date> ;
516 M: integer april 4 1 <date> ;
517 M: integer may 5 1 <date> ;
518 M: integer june 6 1 <date> ;
519 M: integer july 7 1 <date> ;
520 M: integer august 8 1 <date> ;
521 M: integer september 9 1 <date> ;
522 M: integer october 10 1 <date> ;
523 M: integer november 11 1 <date> ;
524 M: integer december 12 1 <date> ;
526 M: timestamp january clone 1 >>month ;
527 M: timestamp february clone 2 >>month ;
528 M: timestamp march clone 3 >>month ;
529 M: timestamp april clone 4 >>month ;
530 M: timestamp may clone 5 >>month ;
531 M: timestamp june clone 6 >>month ;
532 M: timestamp july clone 7 >>month ;
533 M: timestamp august clone 8 >>month ;
534 M: timestamp september clone 9 >>month ;
535 M: timestamp october clone 10 >>month ;
536 M: timestamp november clone 11 >>month ;
537 M: timestamp december clone 12 >>month ;
539 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
540 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
541 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
542 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
543 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
544 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
545 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
547 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
548 : monday? ( timestamp -- ? ) day-of-week 1 = ;
549 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
550 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
551 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
552 : friday? ( timestamp -- ? ) day-of-week 5 = ;
553 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
555 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
556 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
557 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
558 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
559 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
560 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
561 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
563 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
564 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
565 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
566 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
567 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
568 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
569 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
571 CONSTANT: day-predicates
572 { sunday? monday? tuesday? wednesday? thursday? friday? saturday? }
574 : day-predicate ( string -- predicate )
577 : day-abbreviation3 ( n -- string )
578 day-abbreviations3 nth ; inline
580 ERROR: not-a-day-abbreviation string ;
582 : day-abbreviation3-index ( string -- n )
583 day-abbreviations3-hash ?at [ not-a-day-abbreviation ] unless ; inline
585 : day-abbreviation3-predicate ( string -- predicate )
586 day-abbreviation3-index day-predicates nth ;
588 : beginning-of-week ( timestamp -- new-timestamp )
591 : o'clock ( timestamp n -- new-timestamp )
592 [ midnight ] dip >>hour ;
594 ERROR: twelve-hour-expected n ;
596 : check-twelve-hour ( n -- n )
597 dup 0 12 between? [ twelve-hour-expected ] unless ;
599 : am ( timestamp n -- new-timestamp )
600 check-twelve-hour o'clock ;
602 : pm ( timestamp n -- new-timestamp )
603 check-twelve-hour 12 + o'clock ;
605 GENERIC: beginning-of-year ( object -- new-timestamp )
606 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
607 M: integer beginning-of-year <year> ;
609 GENERIC: end-of-year ( object -- new-timestamp )
610 M: timestamp end-of-year 12 >>month 31 >>day ;
611 M: integer end-of-year 12 31 <date> ;
613 : time-since-midnight ( timestamp -- duration )
614 dup midnight time- ; inline
616 : since-1970 ( duration -- timestamp )
617 unix-1970 time+ ; inline
619 : timestamp>unix-time ( timestamp -- seconds )
620 unix-1970 (time-) ; inline
622 : unix-time>timestamp ( seconds -- timestamp )
623 [ unix-1970 ] dip +second ; inline
626 { [ os unix? ] [ "calendar.unix" ] }
627 { [ os windows? ] [ "calendar.windows" ] }
630 { "threads" "calendar" } "calendar.threads" require-when