]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
calendar: Always do calculation for datetimes.
[factor.git] / basis / calendar / calendar.factor
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 slots.syntax splitting system vocabs vocabs.loader ;
7 FROM: math.ranges => [a..b) ;
8 IN: calendar
9
10 ERROR: not-in-interval value interval ;
11
12 : check-interval ( value interval -- value )
13     2dup interval-contains? [ drop ] [ not-in-interval ] if ;
14
15 HOOK: gmt-offset os ( -- hours minutes seconds )
16
17 HOOK: gmt os ( -- timestamp )
18
19 TUPLE: duration
20     { year real }
21     { month real }
22     { day real }
23     { hour real }
24     { minute real }
25     { second real } ;
26
27 C: <duration> duration
28
29 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
30
31 TUPLE: timestamp
32     { year integer }
33     { month integer }
34     { day integer }
35     { hour integer }
36     { minute integer }
37     { second real }
38     { gmt-offset duration } ;
39
40 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
41
42 GENERIC: leap-year? ( obj -- ? )
43
44 M: integer leap-year?
45     dup 100 divisor? 400 4 ? divisor? ;
46
47 M: timestamp leap-year?
48     year>> leap-year? ;
49
50 : (days-in-month) ( year month -- n )
51     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
52
53 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
54     year
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 ;
61
62 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
63
64 : gmt-offset-duration ( -- duration )
65     0 0 0 gmt-offset <duration> ; inline
66
67 : <date> ( year month day -- timestamp )
68     0 0 0 gmt-offset-duration <timestamp> ; inline
69
70 : <date-gmt> ( year month day -- timestamp )
71     0 0 0 instant <timestamp> ; inline
72
73 : <year> ( year -- timestamp )
74     1 1 <date> ; inline
75
76 : <year-gmt> ( year -- timestamp )
77     1 1 <date-gmt> ; inline
78
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
85
86 :: julian-day-number ( year month day -- n )
87     ! Returns a composite date number
88     ! Not valid before year -4800
89     14 month - 12 /i :> a
90     year 4800 + a - :> y
91     month 12 a * + 3 - :> m
92
93     day 153 m * 2 + 5 /i + 365 y * +
94     y 4 /i + y 100 /i - y 400 /i + 32045 - ;
95
96 :: julian-day-number>date ( n -- year month day )
97     ! Inverse of julian-day-number
98     n 32044 + :> a
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
104
105     100 b * d + 4800 -
106     m 10 /i + m 3 +
107     12 m 10 /i * -
108     e 153 m * 2 + 5 /i - 1 + ;
109
110 GENERIC: easter ( obj -- obj' )
111
112 :: easter-month-day ( year -- month day )
113     year 19 mod :> a
114     year 100 /mod :> ( b c )
115     b 4 /mod :> ( d e )
116     b 8 + 25 /i :> f
117     b f - 1 + 3 /i :> g
118     19 a * b + d - g - 15 + 30 mod :> h
119     c 4 /mod :> ( i k )
120     32 2 e * + 2 i * + h - k - 7 mod :> l
121     a 11 h * + 22 l * + 451 /i :> m
122
123     h l + 7 m * - 114 + 31 /mod 1 + ;
124
125 M: integer easter
126     dup easter-month-day <date> ;
127
128 M: timestamp easter
129     clone
130     dup year>> easter-month-day
131     swapd >>day swap >>month ;
132
133 : >date< ( timestamp -- year month day )
134     [ year>> ] [ month>> ] [ day>> ] tri ;
135
136 : >time< ( timestamp -- hour minute second )
137     [ hour>> ] [ minute>> ] [ second>> ] tri ;
138
139 : set-time! ( timestamp hours minutes seconds -- timestamp )
140     [ >>hour ] [ >>minute ] [ >>second ] tri* ;
141
142 : set-time ( timestamp hours minutes seconds -- timestamp )
143     clone set-time! ;
144
145 : time>hms ( str -- hms-seq )
146     ":" split [ string>number ] map
147     3 0 pad-tail ;
148
149 : time>offset ( str -- hms-seq )
150     "-" ?head [ time>hms ] dip
151     [ [ neg ] map ] when ;
152
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 ;
163
164 <PRIVATE
165
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 )
172
173 : /rem ( f n -- q r )
174     ! q is positive or negative, r is positive from 0 <= r < n
175     [ / floor >integer ] 2keep rem ;
176
177 : float>whole-part ( float -- int float )
178     [ floor >integer ] keep over - ;
179
180 : adjust-leap-year ( timestamp -- timestamp )
181     dup
182     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
183     [ 3 >>month 1 >>day ] when ;
184
185 M: integer +year
186     [ + ] curry change-year adjust-leap-year ;
187
188 M: real +year
189     float>whole-part swapd days-per-year * +day swap +year ;
190
191 : months/years ( n -- months years )
192     12 /rem [ 1 - 12 ] when-zero swap ; inline
193
194 M: integer +month
195     over month>> + months/years [ >>month ] dip +year ;
196
197 M: real +month
198     float>whole-part swapd average-month * +day swap +month ;
199
200 M: integer +day
201     over >date< julian-day-number + julian-day-number>date
202     [ >>year ] [ >>month ] [ >>day ] tri* ;
203
204 M: real +day
205     float>whole-part swapd 24 * +hour swap +day ;
206
207 : hours/days ( n -- hours days )
208     24 /rem swap ;
209
210 M: integer +hour
211     over hour>> + hours/days [ >>hour ] dip +day ;
212
213 M: real +hour
214     float>whole-part swapd 60 * +minute swap +hour ;
215
216 : minutes/hours ( n -- minutes hours )
217     60 /rem swap ;
218
219 M: integer +minute
220     over minute>> + minutes/hours [ >>minute ] dip +hour ;
221
222 M: real +minute
223     float>whole-part swapd 60 * +second swap +minute ;
224
225 : seconds/minutes ( n -- seconds minutes )
226     60 /rem swap >integer ;
227
228 M: number +second
229     over second>> + seconds/minutes [ >>second ] dip +minute ;
230
231 : (time+) ( timestamp duration -- timestamp' duration )
232     [ second>> +second ] keep
233     [ minute>> +minute ] keep
234     [ hour>>   +hour   ] keep
235     [ day>>    +day    ] keep
236     [ month>>  +month  ] keep
237     [ year>>   +year   ] keep ; inline
238
239 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
240     [ bi@ + ] curry 2keep ; inline
241
242 PRIVATE>
243
244 GENERIC#: time+ 1 ( time1 time2 -- time3 )
245
246 M: timestamp time+
247     [ clone ] dip (time+) drop ;
248
249 M: duration time+
250     dup timestamp? [
251         swap time+
252     ] [
253         [ year>> ] +slots
254         [ month>> ] +slots
255         [ day>> ] +slots
256         [ hour>> ] +slots
257         [ minute>> ] +slots
258         [ second>> ] +slots
259         2drop <duration>
260     ] if ;
261
262 : duration>years ( duration -- x )
263     ! Uses average month/year length since duration loses calendar
264     ! data
265     0 swap
266     {
267         [ year>> + ]
268         [ month>> months-per-year / + ]
269         [ day>> days-per-year / + ]
270         [ hour>> hours-per-year / + ]
271         [ minute>> minutes-per-year / + ]
272         [ second>> seconds-per-year / + ]
273     } cleave ;
274
275 M: duration <=> [ duration>years ] compare ;
276
277 : duration>months ( duration -- x ) duration>years months-per-year * ;
278 : duration>days ( duration -- x ) duration>years days-per-year * ;
279 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
280 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
281 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
282 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
283 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
284 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
285
286 GENERIC: time- ( time1 time2 -- time3 )
287
288 : convert-timezone ( timestamp duration -- timestamp' )
289     over gmt-offset>> over = [ drop ] [
290         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
291     ] if ;
292
293 : >local-time ( timestamp -- timestamp' )
294     clone gmt-offset-duration convert-timezone ;
295
296 : >gmt ( timestamp -- timestamp' )
297     clone dup gmt-offset>> dup instant =
298     [ drop ] [
299         [ neg +second 0 ] change-second
300         [ neg +minute 0 ] change-minute
301         [ neg +hour   0 ] change-hour
302         [ neg +day    0 ] change-day
303         [ neg +month  0 ] change-month
304         [ neg +year   0 ] change-year drop
305     ] if ;
306
307 M: timestamp <=> [ >gmt tuple-slots ] compare ;
308
309 : same-year? ( ts1 ts2 -- ? )
310     [ >gmt slots{ year } ] same? ;
311
312 : quarter ( timestamp -- [1,4] )
313     month>> 3 /mod [ drop 1 + ] unless-zero ; inline
314
315 : same-quarter? ( ts1 ts2 -- ? )
316     [ >gmt [ year>> ] [ quarter ] bi 2array ] same? ;
317
318 : same-month? ( ts1 ts2 -- ? )
319     [ >gmt slots{ year month } ] same? ;
320
321 :: (day-of-year) ( year month day -- n )
322     day-counts month head-slice sum day +
323     year leap-year? [
324         year month day <date>
325         year 3 1 <date>
326         after=? [ 1 + ] when
327     ] when ;
328
329 : day-of-year ( timestamp -- n )
330     >date< (day-of-year) ;
331
332 : same-day? ( ts1 ts2 -- ? )
333     [ >gmt slots{ year month day } ] same? ;
334
335 : zeller-congruence ( year month day -- n )
336     ! Zeller Congruence
337     ! http://web.textfiles.com/computers/formulas.txt
338     ! good for any date since October 15, 1582
339     [
340         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
341         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
342         [ 1 + 3 * 5 /i + ] keep 2 * +
343     ] dip 1 + + 7 mod ;
344
345 : day-of-week ( timestamp -- n )
346     >date< zeller-congruence ;
347
348 : (week-number) ( timestamp -- [0,53] )
349     [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
350
351 DEFER: end-of-year
352 : week-number ( timestamp -- [1,53] )
353     dup (week-number) {
354         {  0 [ year>> 1 - end-of-year (week-number) ] }
355         { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
356         [ nip ]
357     } case ;
358
359 : same-week? ( ts1 ts2 -- ? )
360     [ >gmt [ year>> ] [ week-number ] bi 2array ] same? ;
361
362 : same-hour? ( ts1 ts2 -- ? )
363     [ >gmt slots{ year month day hour } ] same? ;
364
365 : same-minute? ( ts1 ts2 -- ? )
366     [ >gmt slots{ year month day hour minute } ] same? ;
367
368 : same-second? ( ts1 ts2 -- ? )
369     [ >gmt slots{ year month day hour minute second } ] same? ;
370
371 : (time-) ( timestamp timestamp -- n )
372     [ >gmt ] bi@
373     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
374     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
375
376 M: timestamp time-
377     ! Exact calendar-time difference
378     (time-) seconds ;
379
380 : time* ( obj1 obj2 -- obj3 )
381     dup real? [ swap ] when
382     dup real? [ * ] [
383         {
384             [   year>> * ]
385             [  month>> * ]
386             [    day>> * ]
387             [   hour>> * ]
388             [ minute>> * ]
389             [ second>> * ]
390         } 2cleave <duration>
391     ] if ;
392
393 : before ( duration -- -duration )
394     -1 time* ;
395
396 <PRIVATE
397
398 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
399     [ bi@ - ] curry 2keep ; inline
400
401 PRIVATE>
402
403 M: duration time-
404     over timestamp? [
405         before time+
406     ] [
407         [ year>> ] -slots
408         [ month>> ] -slots
409         [ day>> ] -slots
410         [ hour>> ] -slots
411         [ minute>> ] -slots
412         [ second>> ] -slots
413         2drop <duration>
414     ] if ;
415
416 : unix-1970 ( -- timestamp )
417     1970 <year-gmt> ; inline
418
419 : millis>timestamp ( x -- timestamp )
420     [ unix-1970 ] dip 1000 / +second ;
421
422 : timestamp>millis ( timestamp -- n )
423     unix-1970 (time-) 1000 * >integer ;
424
425 : micros>timestamp ( x -- timestamp )
426     [ unix-1970 ] dip 1000000 / +second ;
427
428 : timestamp>micros ( timestamp -- n )
429     unix-1970 (time-) 1000000 * >integer ;
430
431 : now ( -- timestamp )
432     gmt gmt-offset-duration (time+) >>gmt-offset ;
433
434 : hence ( duration -- timestamp ) now swap time+ ;
435
436 : ago ( duration -- timestamp ) now swap time- ;
437
438 GENERIC: days-in-year ( obj -- n )
439
440 M: integer days-in-year leap-year? 366 365 ? ;
441
442 M: timestamp days-in-year year>> days-in-year ;
443
444 : days-in-month ( timestamp -- n )
445     >date< drop (days-in-month) ;
446
447 : midnight! ( timestamp -- new-timestamp )
448     0 >>hour 0 >>minute 0 >>second ; inline
449
450 : midnight ( timestamp -- new-timestamp )
451     clone midnight! ; inline
452
453 : noon ( timestamp -- new-timestamp )
454     midnight 12 >>hour ; inline
455
456 : today ( -- timestamp )
457     now midnight! ; inline
458
459 : tomorrow ( -- timestamp )
460     1 days hence midnight! ; inline
461
462 : yesterday ( -- timestamp )
463     1 days ago midnight! ; inline
464
465 GENERIC: start-of-day ( object -- new-timestamp )
466 M: timestamp start-of-day midnight ;
467
468 : end-of-day! ( timestamp -- timestamp )
469     23 >>hour 59 >>minute 59+999/1000 >>second ;
470
471 GENERIC: end-of-day ( object -- new-timestamp )
472 M: timestamp end-of-day clone end-of-day! ;
473
474 : start-of-month ( timestamp -- new-timestamp )
475     midnight 1 >>day ; inline
476
477 : end-of-month ( timestamp -- new-timestamp )
478     [ end-of-day ] [ days-in-month ] bi >>day ;
479
480 : start-of-quarter ( timestamp -- new-timestamp )
481     [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
482
483 : end-of-quarter ( timestamp -- new-timestamp )
484     [ clone ] [ quarter 1 - 3 * 3 + ] bi >>month end-of-month ; inline
485
486 GENERIC: start-of-year ( object -- new-timestamp )
487 M: timestamp start-of-year start-of-month 1 >>month ;
488 M: integer start-of-year <year> ;
489
490 GENERIC: end-of-year ( object -- new-timestamp )
491 M: timestamp end-of-year end-of-day 12 >>month 31 >>day ;
492 M: integer end-of-year 12 31 <date> end-of-day! ;
493
494 GENERIC: start-of-decade ( object -- new-timestamp )
495 M: timestamp start-of-decade start-of-year [ dup 10 mod - ] change-year ;
496 M: integer start-of-decade start-of-year [ dup 10 mod - ] change-year ;
497
498 GENERIC: end-of-decade ( object -- new-timestamp )
499 M: timestamp end-of-decade end-of-year [ dup 10 mod - 9 + ] change-year ;
500 M: integer end-of-decade end-of-year [ dup 10 mod - 9 + ] change-year ;
501
502 : last-day-of-decade ( object -- new-timestamp )
503     end-of-decade end-of-decade midnight ;
504
505 : last-day-of-year ( object -- new-timestamp )
506     end-of-year midnight ;
507
508 : start-of-hour ( timestamp -- new-timestamp ) clone 0 >>minute 0 >>second ;
509 : end-of-hour ( timestamp -- new-timestamp ) clone 59 >>minute 59+999/1000 >>second ;
510
511 : start-of-minute ( timestamp -- new-timestamp ) clone 0 >>second ;
512 : end-of-minute ( timestamp -- new-timestamp ) clone 59+999/1000 >>second ;
513
514 GENERIC: start-of-second ( object -- new-timestamp )
515 M: timestamp start-of-second clone [ floor ] change-second ;
516 M: real start-of-second floor ;
517
518 GENERIC: end-of-second ( object -- new-timestamp )
519 M: timestamp end-of-second clone [ floor 999/1000 + ] change-second ;
520 M: real end-of-second floor 999/1000 + ;
521
522 <PRIVATE
523
524 : day-offset ( timestamp m -- new-timestamp n )
525     over day-of-week - ; inline
526
527 : day-this-week ( timestamp n -- new-timestamp )
528     day-offset days time+ ;
529
530 :: nth-day-this-month ( timestamp n day -- new-timestamp )
531     timestamp start-of-month day day-this-week
532     dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless ;
533
534 : last-day-this-month ( timestamp day -- new-timestamp )
535     [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
536
537 PRIVATE>
538
539 GENERIC: january ( obj -- timestamp )
540 GENERIC: february ( obj -- timestamp )
541 GENERIC: march ( obj -- timestamp )
542 GENERIC: april ( obj -- timestamp )
543 GENERIC: may ( obj -- timestamp )
544 GENERIC: june ( obj -- timestamp )
545 GENERIC: july ( obj -- timestamp )
546 GENERIC: august ( obj -- timestamp )
547 GENERIC: september ( obj -- timestamp )
548 GENERIC: october ( obj -- timestamp )
549 GENERIC: november ( obj -- timestamp )
550 GENERIC: december ( obj -- timestamp )
551
552 M: integer january 1 1 <date> ;
553 M: integer february 2 1 <date> ;
554 M: integer march 3 1 <date> ;
555 M: integer april 4 1 <date> ;
556 M: integer may 5 1 <date> ;
557 M: integer june 6 1 <date> ;
558 M: integer july 7 1 <date> ;
559 M: integer august 8 1 <date> ;
560 M: integer september 9 1 <date> ;
561 M: integer october 10 1 <date> ;
562 M: integer november 11 1 <date> ;
563 M: integer december 12 1 <date> ;
564
565 M: timestamp january clone 1 >>month ;
566 M: timestamp february clone 2 >>month ;
567 M: timestamp march clone 3 >>month ;
568 M: timestamp april clone 4 >>month ;
569 M: timestamp may clone 5 >>month ;
570 M: timestamp june clone 6 >>month ;
571 M: timestamp july clone 7 >>month ;
572 M: timestamp august clone 8 >>month ;
573 M: timestamp september clone 9 >>month ;
574 M: timestamp october clone 10 >>month ;
575 M: timestamp november clone 11 >>month ;
576 M: timestamp december clone 12 >>month ;
577
578 : <january> ( year day -- timestamp ) 1 swap <date> ; inline
579 : <february> ( year day -- timestamp ) 2 swap <date> ; inline
580 : <march> ( year day -- timestamp ) 3 swap <date> ; inline
581 : <april> ( year day -- timestamp ) 4 swap <date> ; inline
582 : <may> ( year day -- timestamp ) 5 swap <date> ; inline
583 : <june> ( year day -- timestamp ) 6 swap <date> ; inline
584 : <july> ( year day -- timestamp ) 7 swap <date> ; inline
585 : <august> ( year day -- timestamp ) 8 swap <date> ; inline
586 : <september> ( year day -- timestamp ) 9 swap <date> ; inline
587 : <october> ( year day -- timestamp ) 10 swap <date> ; inline
588 : <november> ( year day -- timestamp ) 11 swap <date> ; inline
589 : <december> ( year day -- timestamp ) 12 swap <date> ; inline
590
591 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
592 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
593 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
594 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
595 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
596 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
597 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
598
599 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
600 : monday? ( timestamp -- ? ) day-of-week 1 = ;
601 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
602 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
603 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
604 : friday? ( timestamp -- ? ) day-of-week 5 = ;
605 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
606
607 GENERIC: weekend? ( obj -- ? )
608 M: timestamp weekend? day-of-week weekend? ;
609 M: integer weekend? { 0 6 } member? ;
610
611 GENERIC: weekday? ( obj -- ? )
612 M: timestamp weekday? day-of-week weekday? ;
613 M: integer weekday? weekend? not ;
614
615 : same-or-next-business-day ( timestamp -- timestamp' )
616     dup day-of-week {
617         { 0 [ monday ] }
618         { 6 [ 2 days time+ ] }
619         [ drop ]
620     } case ;
621
622 : same-or-previous-business-day ( timestamp -- timestamp' )
623     dup day-of-week {
624         { 0 [ 2 days time- ] }
625         { 6 [ friday ] }
626         [ drop ]
627     } case ;
628
629 : weekdays-between ( date1 date2 -- n )
630     [
631         [ swap time- duration>days 5 * ]
632         [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
633     ] 2keep
634     day-of-week 6 = [ [ 1 - ] dip ] when
635     day-of-week 0 = [ 1 - ] when ;
636
637 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
638
639 : weekdays-between2 ( date1 date2 -- n )
640     [ swap time- duration>days 1 + ]
641     [ [ day-of-week ] bi@ 6 swap - ] 2bi
642
643     [ + + 1.4 /i ]
644     [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
645
646
647 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
648 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
649 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
650 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
651 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
652 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
653 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
654
655 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
656 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
657 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
658 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
659 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
660 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
661 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
662
663 : start-of-week ( timestamp -- new-timestamp )
664     midnight sunday ;
665
666 : o'clock ( timestamp n -- new-timestamp )
667     [ midnight ] dip >>hour ;
668
669 : am ( timestamp n -- new-timestamp )
670     0 12 [a,b] check-interval o'clock ;
671
672 : pm ( timestamp n -- new-timestamp )
673     0 12 [a,b] check-interval 12 + o'clock ;
674
675 : time-since-midnight ( timestamp -- duration )
676     dup midnight time- ; inline
677
678 : since-1970 ( duration -- timestamp )
679     unix-1970 time+ ; inline
680
681 : timestamp>unix-time ( timestamp -- seconds )
682     unix-1970 (time-) ; inline
683
684 : unix-time>timestamp ( seconds -- timestamp )
685     [ unix-1970 ] dip +second ; inline
686
687 ! January and February need a fixup with this algorithm.
688 ! Find a better algorithm.
689 : ymd>ordinal ( year month day -- ordinal )
690     [ leap-year? dup -2 -3 ? ]
691     [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
692     [ ] tri* + + >integer
693     swap 367 366 ? mod ;
694
695 : timestamp>year-dates ( timestamp -- seq )
696     [ start-of-year >date< julian-day-number ]
697     [ days-in-year ] bi
698     [ drop ] [ + ] 2bi
699     [a..b) [ julian-day-number>date <date> ] map ;
700
701 : year-ordinal>timestamp ( year ordinal -- timestamp )
702     [ 1 1 julian-day-number ] dip
703     + 1 - julian-day-number>date <date> ;
704
705 GENERIC: weeks-in-week-year ( obj -- n )
706 M: integer weeks-in-week-year
707     { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
708
709 M: timestamp weeks-in-week-year
710     { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
711
712 {
713     { [ os unix? ] [ "calendar.unix" ] }
714     { [ os windows? ] [ "calendar.windows" ] }
715 } cond require
716
717 { "threads" "calendar" } "calendar.threads" require-when