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 : gmt-offset-duration ( -- duration )
37 0 0 0 gmt-offset <duration> ; inline
39 : <date> ( year month day -- timestamp )
40 0 0 0 gmt-offset-duration <timestamp> ; inline
42 : <date-gmt> ( year month day -- timestamp )
43 0 0 0 instant <timestamp> ; inline
45 : <year> ( year -- timestamp )
48 : <year-gmt> ( year -- timestamp )
49 1 1 <date-gmt> ; inline
52 M: not-a-month summary
53 drop "Months are indexed starting at 1" ;
57 : check-month ( n -- n )
58 [ not-a-month ] when-zero ;
64 "January" "February" "March" "April" "May" "June"
65 "July" "August" "September" "October" "November" "December"
68 GENERIC: month-name ( obj -- string )
70 M: integer month-name check-month 1 - month-names nth ;
71 M: timestamp month-name month>> 1 - month-names nth ;
73 ERROR: not-a-month-abbreviation string ;
75 CONSTANT: month-abbreviations
77 "Jan" "Feb" "Mar" "Apr" "May" "Jun"
78 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
81 CONSTANT: month-abbreviations-hash
83 { "Jan" 1 } { "Feb" 2 } { "Mar" 3 }
84 { "Apr" 4 } { "May" 5 } { "Jun" 6 }
85 { "Jul" 7 } { "Aug" 8 } { "Sep" 9 }
86 { "Oct" 10 } { "Nov" 11 } { "Dec" 12 }
89 : month-abbreviation ( n -- string )
90 check-month 1 - month-abbreviations nth ;
92 : month-abbreviation-index ( string -- n )
93 month-abbreviations-hash ?at
94 [ not-a-month-abbreviation ] unless ;
96 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
99 { "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" }
101 CONSTANT: day-abbreviations2
102 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
104 : day-abbreviation2 ( n -- string )
105 day-abbreviations2 nth ; inline
107 CONSTANT: day-abbreviations3
108 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
110 CONSTANT: day-abbreviations3-hash
112 { "Sun" 0 } { "Mon" 1 } { "Tue" 2 } { "Wed" 3 }
113 { "Thu" 4 } { "Fri" 5 } { "Sat" 6 }
116 CONSTANT: average-month 30+5/12
117 CONSTANT: months-per-year 12
118 CONSTANT: days-per-year 3652425/10000
119 CONSTANT: hours-per-year 876582/100
120 CONSTANT: minutes-per-year 5259492/10
121 CONSTANT: seconds-per-year 31556952
123 :: julian-day-number ( year month day -- n )
124 #! Returns a composite date number
125 #! Not valid before year -4800
126 14 month - 12 /i :> a
128 month 12 a * + 3 - :> m
130 day 153 m * 2 + 5 /i + 365 y * +
131 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
133 :: julian-day-number>date ( n -- year month day )
134 #! Inverse of julian-day-number
136 4 a * 3 + 146097 /i :> b
137 a 146097 b * 4 /i - :> c
138 4 c * 3 + 1461 /i :> d
139 c 1461 d * 4 /i - :> e
140 5 e * 2 + 153 /i :> m
145 e 153 m * 2 + 5 /i - 1 + ;
147 GENERIC: easter ( obj -- obj' )
149 :: easter-month-day ( year -- month day )
151 year 100 /mod :> ( b c )
155 19 a * b + d - g - 15 + 30 mod :> h
157 32 2 e * + 2 i * + h - k - 7 mod :> l
158 a 11 h * + 22 l * + 451 /i :> m
160 h l + 7 m * - 114 + 31 /mod 1 + ;
162 M: integer easter ( year -- timestamp )
163 dup easter-month-day <date> ;
165 M: timestamp easter ( timestamp -- timestamp )
167 dup year>> easter-month-day
168 swapd >>day swap >>month ;
170 : >date< ( timestamp -- year month day )
171 [ year>> ] [ month>> ] [ day>> ] tri ;
173 : >time< ( timestamp -- hour minute second )
174 [ hour>> ] [ minute>> ] [ second>> ] tri ;
176 : years ( x -- duration ) instant swap >>year ;
177 : months ( x -- duration ) instant swap >>month ;
178 : days ( x -- duration ) instant swap >>day ;
179 : weeks ( x -- duration ) 7 * days ;
180 : hours ( x -- duration ) instant swap >>hour ;
181 : minutes ( x -- duration ) instant swap >>minute ;
182 : seconds ( x -- duration ) instant swap >>second ;
183 : milliseconds ( x -- duration ) 1000 / seconds ;
184 : microseconds ( x -- duration ) 1000000 / seconds ;
185 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
187 GENERIC: leap-year? ( obj -- ? )
189 M: integer leap-year? ( year -- ? )
190 dup 100 divisor? 400 4 ? divisor? ;
192 M: timestamp leap-year? ( timestamp -- ? )
197 GENERIC: +year ( timestamp x -- timestamp )
198 GENERIC: +month ( timestamp x -- timestamp )
199 GENERIC: +day ( timestamp x -- timestamp )
200 GENERIC: +hour ( timestamp x -- timestamp )
201 GENERIC: +minute ( timestamp x -- timestamp )
202 GENERIC: +second ( timestamp x -- timestamp )
204 : /rem ( f n -- q r )
205 #! q is positive or negative, r is positive from 0 <= r < n
206 [ / floor >integer ] 2keep rem ;
208 : float>whole-part ( float -- int float )
209 [ floor >integer ] keep over - ;
211 : adjust-leap-year ( timestamp -- timestamp )
213 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
214 [ 3 >>month 1 >>day ] when ;
216 M: integer +year ( timestamp n -- timestamp )
217 [ + ] curry change-year adjust-leap-year ;
219 M: real +year ( timestamp n -- timestamp )
220 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
222 : months/years ( n -- months years )
223 12 /rem [ 1 - 12 ] when-zero swap ; inline
225 M: integer +month ( timestamp n -- timestamp )
226 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
228 M: real +month ( timestamp n -- timestamp )
229 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
231 M: integer +day ( timestamp n -- timestamp )
233 over >date< julian-day-number + julian-day-number>date
234 [ >>year ] [ >>month ] [ >>day ] tri*
237 M: real +day ( timestamp n -- timestamp )
238 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
240 : hours/days ( n -- hours days )
243 M: integer +hour ( timestamp n -- timestamp )
244 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
246 M: real +hour ( timestamp n -- timestamp )
247 float>whole-part swapd 60 * +minute swap +hour ;
249 : minutes/hours ( n -- minutes hours )
252 M: integer +minute ( timestamp n -- timestamp )
253 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
255 M: real +minute ( timestamp n -- timestamp )
256 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
258 : seconds/minutes ( n -- seconds minutes )
259 60 /rem swap >integer ;
261 M: number +second ( timestamp n -- timestamp )
262 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
264 : (time+) ( timestamp duration -- timestamp' duration )
265 [ second>> +second ] keep
266 [ minute>> +minute ] keep
267 [ hour>> +hour ] keep
269 [ month>> +month ] keep
270 [ year>> +year ] keep ; inline
272 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
273 [ bi@ + ] curry 2keep ; inline
277 GENERIC# time+ 1 ( time1 time2 -- time3 )
280 [ clone ] dip (time+) drop ;
295 : duration>years ( duration -- x )
296 #! Uses average month/year length since duration loses calendar
301 [ month>> months-per-year / + ]
302 [ day>> days-per-year / + ]
303 [ hour>> hours-per-year / + ]
304 [ minute>> minutes-per-year / + ]
305 [ second>> seconds-per-year / + ]
308 M: duration <=> [ duration>years ] compare ;
310 : duration>months ( duration -- x ) duration>years months-per-year * ;
311 : duration>days ( duration -- x ) duration>years days-per-year * ;
312 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
313 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
314 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
315 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
316 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
317 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
319 GENERIC: time- ( time1 time2 -- time3 )
321 : convert-timezone ( timestamp duration -- timestamp' )
322 over gmt-offset>> over = [ drop ] [
323 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
326 : >local-time ( timestamp -- timestamp' )
327 gmt-offset-duration convert-timezone ;
329 : >gmt ( timestamp -- timestamp' )
330 dup gmt-offset>> dup instant =
332 [ neg +second 0 ] change-second
333 [ neg +minute 0 ] change-minute
334 [ neg +hour 0 ] change-hour
335 [ neg +day 0 ] change-day
336 [ neg +month 0 ] change-month
337 [ neg +year 0 ] change-year drop
340 M: timestamp <=> ( ts1 ts2 -- n )
341 [ >gmt tuple-slots ] compare ;
343 : same-day? ( ts1 ts2 -- ? )
344 [ >gmt >date< <date> ] same? ;
346 : (time-) ( timestamp timestamp -- n )
348 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
349 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
352 #! Exact calendar-time difference
355 : time* ( obj1 obj2 -- obj3 )
356 dup real? [ swap ] when
368 : before ( duration -- -duration )
373 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
374 [ bi@ - ] curry 2keep ; inline
391 : <zero> ( -- timestamp )
392 0 0 0 <date-gmt> ; inline
394 : valid-timestamp? ( timestamp -- ? )
395 clone instant >>gmt-offset
396 dup <zero> time- <zero> time+ = ;
398 : unix-1970 ( -- timestamp )
399 1970 <year-gmt> ; inline
401 : millis>timestamp ( x -- timestamp )
402 [ unix-1970 ] dip 1000 / +second ;
404 : timestamp>millis ( timestamp -- n )
405 unix-1970 (time-) 1000 * >integer ;
407 : micros>timestamp ( x -- timestamp )
408 [ unix-1970 ] dip 1000000 / +second ;
410 : timestamp>micros ( timestamp -- n )
411 unix-1970 (time-) 1000000 * >integer ;
413 : now ( -- timestamp )
414 gmt gmt-offset-duration (time+) >>gmt-offset ;
416 : hence ( duration -- timestamp ) now swap time+ ;
418 : ago ( duration -- timestamp ) now swap time- ;
420 : zeller-congruence ( year month day -- n )
422 #! http://web.textfiles.com/computers/formulas.txt
423 #! good for any date since October 15, 1582
425 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
426 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
427 [ 1 + 3 * 5 /i + ] keep 2 * +
430 GENERIC: days-in-year ( obj -- n )
432 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
433 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
435 : (days-in-month) ( year month -- n )
436 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
438 : days-in-month ( timestamp -- n )
439 >date< drop (days-in-month) ;
441 : day-of-week ( timestamp -- n )
442 >date< zeller-congruence ;
444 GENERIC: day-name ( obj -- string )
445 M: integer day-name day-names nth ;
446 M: timestamp day-name day-of-week day-names nth ;
448 :: (day-of-year) ( year month day -- n )
449 day-counts month head-slice sum day +
451 year month day <date>
456 : day-of-year ( timestamp -- n )
457 >date< (day-of-year) ;
459 : midnight ( timestamp -- new-timestamp )
460 clone 0 >>hour 0 >>minute 0 >>second ; inline
462 : noon ( timestamp -- new-timestamp )
463 midnight 12 >>hour ; inline
465 : today ( -- timestamp )
466 now midnight ; inline
468 : tomorrow ( -- timestamp )
469 1 days hence midnight ; inline
471 : yesterday ( -- timestamp )
472 1 days ago midnight ; inline
474 : beginning-of-month ( timestamp -- new-timestamp )
475 midnight 1 >>day ; inline
477 : end-of-month ( timestamp -- new-timestamp )
478 [ midnight ] [ days-in-month ] bi >>day ;
482 : day-offset ( timestamp m -- new-timestamp n )
483 over day-of-week - ; inline
485 : day-this-week ( timestamp n -- new-timestamp )
486 day-offset days time+ ;
488 :: nth-day-this-month ( timestamp n day -- new-timestamp )
489 timestamp beginning-of-month day day-this-week
490 dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless
491 n 1 - [ weeks time+ ] unless-zero ;
493 : last-day-this-month ( timestamp day -- new-timestamp )
494 [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
498 GENERIC: january ( obj -- timestamp )
499 GENERIC: february ( obj -- timestamp )
500 GENERIC: march ( obj -- timestamp )
501 GENERIC: april ( obj -- timestamp )
502 GENERIC: may ( obj -- timestamp )
503 GENERIC: june ( obj -- timestamp )
504 GENERIC: july ( obj -- timestamp )
505 GENERIC: august ( obj -- timestamp )
506 GENERIC: september ( obj -- timestamp )
507 GENERIC: october ( obj -- timestamp )
508 GENERIC: november ( obj -- timestamp )
509 GENERIC: december ( obj -- timestamp )
511 M: integer january 1 1 <date> ;
512 M: integer february 2 1 <date> ;
513 M: integer march 3 1 <date> ;
514 M: integer april 4 1 <date> ;
515 M: integer may 5 1 <date> ;
516 M: integer june 6 1 <date> ;
517 M: integer july 7 1 <date> ;
518 M: integer august 8 1 <date> ;
519 M: integer september 9 1 <date> ;
520 M: integer october 10 1 <date> ;
521 M: integer november 11 1 <date> ;
522 M: integer december 12 1 <date> ;
524 M: timestamp january clone 1 >>month ;
525 M: timestamp february clone 2 >>month ;
526 M: timestamp march clone 3 >>month ;
527 M: timestamp april clone 4 >>month ;
528 M: timestamp may clone 5 >>month ;
529 M: timestamp june clone 6 >>month ;
530 M: timestamp july clone 7 >>month ;
531 M: timestamp august clone 8 >>month ;
532 M: timestamp september clone 9 >>month ;
533 M: timestamp october clone 10 >>month ;
534 M: timestamp november clone 11 >>month ;
535 M: timestamp december clone 12 >>month ;
537 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
538 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
539 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
540 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
541 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
542 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
543 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
545 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
546 : monday? ( timestamp -- ? ) day-of-week 1 = ;
547 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
548 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
549 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
550 : friday? ( timestamp -- ? ) day-of-week 5 = ;
551 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
553 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
554 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
555 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
556 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
557 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
558 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
559 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
561 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
562 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
563 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
564 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
565 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
566 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
567 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
569 CONSTANT: day-predicates
570 { sunday? monday? tuesday? wednesday? thursday? friday? saturday? }
572 : day-predicate ( string -- predicate )
575 : day-abbreviation3 ( n -- string )
576 day-abbreviations3 nth ; inline
578 ERROR: not-a-day-abbreviation string ;
580 : day-abbreviation3-index ( string -- n )
581 day-abbreviations3-hash ?at [ not-a-day-abbreviation ] unless ; inline
583 : day-abbreviation3-predicate ( string -- predicate )
584 day-abbreviation3-index day-predicates nth ;
586 : beginning-of-week ( timestamp -- new-timestamp )
589 : o'clock ( timestamp n -- new-timestamp )
590 [ midnight ] dip >>hour ;
592 ERROR: twelve-hour-expected n ;
594 : check-twelve-hour ( n -- n )
595 dup 0 12 between? [ twelve-hour-expected ] unless ;
597 : am ( timestamp n -- new-timestamp )
598 check-twelve-hour o'clock ;
600 : pm ( timestamp n -- new-timestamp )
601 check-twelve-hour 12 + o'clock ;
603 GENERIC: beginning-of-year ( object -- new-timestamp )
604 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
605 M: integer beginning-of-year <year> ;
607 GENERIC: end-of-year ( object -- new-timestamp )
608 M: timestamp end-of-year 12 >>month 31 >>day ;
609 M: integer end-of-year 12 31 <date> ;
611 : time-since-midnight ( timestamp -- duration )
612 dup midnight time- ; inline
614 : since-1970 ( duration -- timestamp )
615 unix-1970 time+ ; inline
617 : timestamp>unix-time ( timestamp -- seconds )
618 unix-1970 (time-) ; inline
620 : unix-time>timestamp ( seconds -- timestamp )
621 [ unix-1970 ] dip +second ; inline
624 { [ os unix? ] [ "calendar.unix" ] }
625 { [ os windows? ] [ "calendar.windows" ] }
628 { "threads" "calendar" } "calendar.threads" require-when