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