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
53 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
55 CONSTANT: average-month 30+5/12
56 CONSTANT: months-per-year 12
57 CONSTANT: days-per-year 3652425/10000
58 CONSTANT: hours-per-year 876582/100
59 CONSTANT: minutes-per-year 5259492/10
60 CONSTANT: seconds-per-year 31556952
62 :: julian-day-number ( year month day -- n )
63 ! Returns a composite date number
64 ! Not valid before year -4800
67 month 12 a * + 3 - :> m
69 day 153 m * 2 + 5 /i + 365 y * +
70 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
72 :: julian-day-number>date ( n -- year month day )
73 ! Inverse of julian-day-number
75 4 a * 3 + 146097 /i :> b
76 a 146097 b * 4 /i - :> c
77 4 c * 3 + 1461 /i :> d
78 c 1461 d * 4 /i - :> e
84 e 153 m * 2 + 5 /i - 1 + ;
86 GENERIC: easter ( obj -- obj' )
88 :: easter-month-day ( year -- month day )
90 year 100 /mod :> ( b c )
94 19 a * b + d - g - 15 + 30 mod :> h
96 32 2 e * + 2 i * + h - k - 7 mod :> l
97 a 11 h * + 22 l * + 451 /i :> m
99 h l + 7 m * - 114 + 31 /mod 1 + ;
101 M: integer easter ( year -- timestamp )
102 dup easter-month-day <date> ;
104 M: timestamp easter ( timestamp -- timestamp )
106 dup year>> easter-month-day
107 swapd >>day swap >>month ;
109 : >date< ( timestamp -- year month day )
110 [ year>> ] [ month>> ] [ day>> ] tri ;
112 : >time< ( timestamp -- hour minute second )
113 [ hour>> ] [ minute>> ] [ second>> ] tri ;
115 : years ( x -- duration ) instant swap >>year ;
116 : months ( x -- duration ) instant swap >>month ;
117 : days ( x -- duration ) instant swap >>day ;
118 : weeks ( x -- duration ) 7 * days ;
119 : hours ( x -- duration ) instant swap >>hour ;
120 : minutes ( x -- duration ) instant swap >>minute ;
121 : seconds ( x -- duration ) instant swap >>second ;
122 : milliseconds ( x -- duration ) 1000 / seconds ;
123 : microseconds ( x -- duration ) 1000000 / seconds ;
124 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
126 GENERIC: leap-year? ( obj -- ? )
128 M: integer leap-year? ( year -- ? )
129 dup 100 divisor? 400 4 ? divisor? ;
131 M: timestamp leap-year? ( timestamp -- ? )
136 GENERIC: +year ( timestamp x -- timestamp )
137 GENERIC: +month ( timestamp x -- timestamp )
138 GENERIC: +day ( timestamp x -- timestamp )
139 GENERIC: +hour ( timestamp x -- timestamp )
140 GENERIC: +minute ( timestamp x -- timestamp )
141 GENERIC: +second ( timestamp x -- timestamp )
143 : /rem ( f n -- q r )
144 ! q is positive or negative, r is positive from 0 <= r < n
145 [ / floor >integer ] 2keep rem ;
147 : float>whole-part ( float -- int float )
148 [ floor >integer ] keep over - ;
150 : adjust-leap-year ( timestamp -- timestamp )
152 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
153 [ 3 >>month 1 >>day ] when ;
155 M: integer +year ( timestamp n -- timestamp )
156 [ + ] curry change-year adjust-leap-year ;
158 M: real +year ( timestamp n -- timestamp )
159 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
161 : months/years ( n -- months years )
162 12 /rem [ 1 - 12 ] when-zero swap ; inline
164 M: integer +month ( timestamp n -- timestamp )
165 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
167 M: real +month ( timestamp n -- timestamp )
168 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
170 M: integer +day ( timestamp n -- timestamp )
172 over >date< julian-day-number + julian-day-number>date
173 [ >>year ] [ >>month ] [ >>day ] tri*
176 M: real +day ( timestamp n -- timestamp )
177 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
179 : hours/days ( n -- hours days )
182 M: integer +hour ( timestamp n -- timestamp )
183 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
185 M: real +hour ( timestamp n -- timestamp )
186 float>whole-part swapd 60 * +minute swap +hour ;
188 : minutes/hours ( n -- minutes hours )
191 M: integer +minute ( timestamp n -- timestamp )
192 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
194 M: real +minute ( timestamp n -- timestamp )
195 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
197 : seconds/minutes ( n -- seconds minutes )
198 60 /rem swap >integer ;
200 M: number +second ( timestamp n -- timestamp )
201 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
203 : (time+) ( timestamp duration -- timestamp' duration )
204 [ second>> +second ] keep
205 [ minute>> +minute ] keep
206 [ hour>> +hour ] keep
208 [ month>> +month ] keep
209 [ year>> +year ] keep ; inline
211 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
212 [ bi@ + ] curry 2keep ; inline
216 GENERIC# time+ 1 ( time1 time2 -- time3 )
219 [ clone ] dip (time+) drop ;
234 : duration>years ( duration -- x )
235 ! Uses average month/year length since duration loses calendar
240 [ month>> months-per-year / + ]
241 [ day>> days-per-year / + ]
242 [ hour>> hours-per-year / + ]
243 [ minute>> minutes-per-year / + ]
244 [ second>> seconds-per-year / + ]
247 M: duration <=> [ duration>years ] compare ;
249 : duration>months ( duration -- x ) duration>years months-per-year * ;
250 : duration>days ( duration -- x ) duration>years days-per-year * ;
251 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
252 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
253 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
254 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
255 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
256 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
258 GENERIC: time- ( time1 time2 -- time3 )
260 : convert-timezone ( timestamp duration -- timestamp' )
261 over gmt-offset>> over = [ drop ] [
262 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
265 : >local-time ( timestamp -- timestamp' )
266 clone gmt-offset-duration convert-timezone ;
268 : >gmt ( timestamp -- timestamp' )
269 clone dup gmt-offset>> dup instant =
271 [ neg +second 0 ] change-second
272 [ neg +minute 0 ] change-minute
273 [ neg +hour 0 ] change-hour
274 [ neg +day 0 ] change-day
275 [ neg +month 0 ] change-month
276 [ neg +year 0 ] change-year drop
279 M: timestamp <=> ( ts1 ts2 -- n )
280 [ >gmt tuple-slots ] compare ;
282 : same-day? ( ts1 ts2 -- ? )
283 [ >gmt >date< <date> ] same? ;
285 : (time-) ( timestamp timestamp -- n )
287 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
288 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
291 ! Exact calendar-time difference
294 : time* ( obj1 obj2 -- obj3 )
295 dup real? [ swap ] when
307 : before ( duration -- -duration )
312 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
313 [ bi@ - ] curry 2keep ; inline
330 : <zero> ( -- timestamp )
331 0 0 0 <date-gmt> ; inline
333 : valid-timestamp? ( timestamp -- ? )
334 clone instant >>gmt-offset
335 dup <zero> time- <zero> time+ = ;
337 : unix-1970 ( -- timestamp )
338 1970 <year-gmt> ; inline
340 : millis>timestamp ( x -- timestamp )
341 [ unix-1970 ] dip 1000 / +second ;
343 : timestamp>millis ( timestamp -- n )
344 unix-1970 (time-) 1000 * >integer ;
346 : micros>timestamp ( x -- timestamp )
347 [ unix-1970 ] dip 1000000 / +second ;
349 : timestamp>micros ( timestamp -- n )
350 unix-1970 (time-) 1000000 * >integer ;
352 : now ( -- timestamp )
353 gmt gmt-offset-duration (time+) >>gmt-offset ;
355 : hence ( duration -- timestamp ) now swap time+ ;
357 : ago ( duration -- timestamp ) now swap time- ;
359 : zeller-congruence ( year month day -- n )
361 ! http://web.textfiles.com/computers/formulas.txt
362 ! good for any date since October 15, 1582
364 dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
365 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
366 [ 1 + 3 * 5 /i + ] keep 2 * +
369 GENERIC: days-in-year ( obj -- n )
371 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
372 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
374 : (days-in-month) ( year month -- n )
375 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
377 : days-in-month ( timestamp -- n )
378 >date< drop (days-in-month) ;
380 : day-of-week ( timestamp -- n )
381 >date< zeller-congruence ;
383 :: (day-of-year) ( year month day -- n )
384 day-counts month head-slice sum day +
386 year month day <date>
391 : day-of-year ( timestamp -- n )
392 >date< (day-of-year) ;
394 : midnight ( timestamp -- new-timestamp )
395 clone 0 >>hour 0 >>minute 0 >>second ; inline
397 : noon ( timestamp -- new-timestamp )
398 midnight 12 >>hour ; inline
400 : today ( -- timestamp )
401 now midnight ; inline
403 : tomorrow ( -- timestamp )
404 1 days hence midnight ; inline
406 : yesterday ( -- timestamp )
407 1 days ago midnight ; inline
409 : beginning-of-month ( timestamp -- new-timestamp )
410 midnight 1 >>day ; inline
412 : end-of-month ( timestamp -- new-timestamp )
413 [ midnight ] [ days-in-month ] bi >>day ;
417 : day-offset ( timestamp m -- new-timestamp n )
418 over day-of-week - ; inline
420 : day-this-week ( timestamp n -- new-timestamp )
421 day-offset days time+ ;
423 :: nth-day-this-month ( timestamp n day -- new-timestamp )
424 timestamp beginning-of-month day day-this-week
425 dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless
426 n 1 - [ weeks time+ ] unless-zero ;
428 : last-day-this-month ( timestamp day -- new-timestamp )
429 [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
433 GENERIC: january ( obj -- timestamp )
434 GENERIC: february ( obj -- timestamp )
435 GENERIC: march ( obj -- timestamp )
436 GENERIC: april ( obj -- timestamp )
437 GENERIC: may ( obj -- timestamp )
438 GENERIC: june ( obj -- timestamp )
439 GENERIC: july ( obj -- timestamp )
440 GENERIC: august ( obj -- timestamp )
441 GENERIC: september ( obj -- timestamp )
442 GENERIC: october ( obj -- timestamp )
443 GENERIC: november ( obj -- timestamp )
444 GENERIC: december ( obj -- timestamp )
446 M: integer january 1 1 <date> ;
447 M: integer february 2 1 <date> ;
448 M: integer march 3 1 <date> ;
449 M: integer april 4 1 <date> ;
450 M: integer may 5 1 <date> ;
451 M: integer june 6 1 <date> ;
452 M: integer july 7 1 <date> ;
453 M: integer august 8 1 <date> ;
454 M: integer september 9 1 <date> ;
455 M: integer october 10 1 <date> ;
456 M: integer november 11 1 <date> ;
457 M: integer december 12 1 <date> ;
459 M: timestamp january clone 1 >>month ;
460 M: timestamp february clone 2 >>month ;
461 M: timestamp march clone 3 >>month ;
462 M: timestamp april clone 4 >>month ;
463 M: timestamp may clone 5 >>month ;
464 M: timestamp june clone 6 >>month ;
465 M: timestamp july clone 7 >>month ;
466 M: timestamp august clone 8 >>month ;
467 M: timestamp september clone 9 >>month ;
468 M: timestamp october clone 10 >>month ;
469 M: timestamp november clone 11 >>month ;
470 M: timestamp december clone 12 >>month ;
472 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
473 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
474 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
475 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
476 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
477 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
478 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
480 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
481 : monday? ( timestamp -- ? ) day-of-week 1 = ;
482 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
483 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
484 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
485 : friday? ( timestamp -- ? ) day-of-week 5 = ;
486 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
488 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
489 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
490 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
491 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
492 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
493 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
494 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
496 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
497 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
498 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
499 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
500 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
501 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
502 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
504 : beginning-of-week ( timestamp -- new-timestamp )
507 : o'clock ( timestamp n -- new-timestamp )
508 [ midnight ] dip >>hour ;
510 ERROR: twelve-hour-expected n ;
512 : check-twelve-hour ( n -- n )
513 dup 0 12 between? [ twelve-hour-expected ] unless ;
515 : am ( timestamp n -- new-timestamp )
516 check-twelve-hour o'clock ;
518 : pm ( timestamp n -- new-timestamp )
519 check-twelve-hour 12 + o'clock ;
521 GENERIC: beginning-of-year ( object -- new-timestamp )
522 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
523 M: integer beginning-of-year <year> ;
525 GENERIC: end-of-year ( object -- new-timestamp )
526 M: timestamp end-of-year 12 >>month 31 >>day ;
527 M: integer end-of-year 12 31 <date> ;
529 : time-since-midnight ( timestamp -- duration )
530 dup midnight time- ; inline
532 : since-1970 ( duration -- timestamp )
533 unix-1970 time+ ; inline
535 : timestamp>unix-time ( timestamp -- seconds )
536 unix-1970 (time-) ; inline
538 : unix-time>timestamp ( seconds -- timestamp )
539 [ unix-1970 ] dip +second ; inline
542 { [ os unix? ] [ "calendar.unix" ] }
543 { [ os windows? ] [ "calendar.windows" ] }
546 { "threads" "calendar" } "calendar.threads" require-when