]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
calendar: fix nth-day-this-month to mutate.
[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 duration )
254     {
255         [ second>> +second ]
256         [ minute>> +minute ]
257         [ hour>>   +hour   ]
258         [ day>>    +day    ]
259         [ month>>  +month  ]
260         [ year>>   +year   ]
261         [ ]
262      } cleave ; inline
263
264 PRIVATE>
265
266 GENERIC#: time+ 1 ( time1 time2 -- time3 )
267
268 M: timestamp time+
269     [ clone ] dip (time+) drop ;
270
271 : duration+ ( duration1 duration2 -- duration3 )
272     {
273         [ [ year>> ] bi@ + ]
274         [ [ month>> ] bi@ + ]
275         [ [ day>> ] bi@ + ]
276         [ [ hour>> ] bi@ + ]
277         [ [ minute>> ] bi@ + ]
278         [ [ second>> ] bi@ + ]
279     } 2cleave <duration> ; inline
280
281 M: duration time+
282     dup timestamp? [ swap time+ ] [ duration+ ] if ;
283
284 : duration>years ( duration -- x )
285     ! Uses average month/year length since duration loses calendar data
286     0 swap
287     {
288         [ year>> + ]
289         [ month>> months-per-year / + ]
290         [ day>> days-per-year / + ]
291         [ hour>> hours-per-year / + ]
292         [ minute>> minutes-per-year / + ]
293         [ second>> seconds-per-year / + ]
294     } cleave ;
295
296 M: duration <=> [ duration>years ] compare ;
297
298 : duration>months ( duration -- x ) duration>years months-per-year * ;
299 : duration>days ( duration -- x ) duration>years days-per-year * ;
300 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
301 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
302 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
303 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
304 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
305 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
306
307 DEFER: time-
308
309 : gmt ( timestamp -- timestamp )
310     instant >>gmt-offset ; inline
311
312 : local-time ( timestamp -- timestamp )
313     gmt-offset-duration >>gmt-offset ; inline
314
315 : convert-timezone ( timestamp duration -- timestamp )
316     [ over gmt-offset>> time- (time+) drop ] [ >>gmt-offset ] bi ;
317
318 : >local-time ( timestamp -- timestamp )
319     gmt-offset-duration convert-timezone ;
320
321 : >gmt ( timestamp -- timestamp )
322     instant convert-timezone ;
323
324 M: timestamp <=> [ clone >gmt tuple-slots ] compare ;
325
326 <PRIVATE
327
328 : same-times? ( timestamp1 timestamp2 quot -- ? )
329     [ clone >gmt ] prepose same? ; inline
330
331 PRIVATE>
332
333 : same-year? ( ts1 ts2 -- ? )
334     [ slots{ year } ] same-times? ;
335
336 : quarter ( timestamp -- [1,4] )
337     month>> 3 /i 1 + ; inline
338
339 : same-quarter? ( ts1 ts2 -- ? )
340     [ [ year>> ] [ quarter ] bi 2array ] same-times? ;
341
342 : same-month? ( ts1 ts2 -- ? )
343     [ slots{ year month } ] same-times? ;
344
345 :: (day-of-year) ( year month day -- n )
346     month days-until nth day + {
347         [ year leap-year? ]
348         [ month 3 >= ]
349     } 0&& [ 1 + ] when ;
350
351 : day-of-year ( timestamp -- n )
352     >date< (day-of-year) ;
353
354 : same-day? ( ts1 ts2 -- ? )
355     [ slots{ year month day } ] same-times? ;
356
357 : (day-of-week) ( year month day -- n )
358     ! Zeller Congruence
359     ! http://web.textfiles.com/computers/formulas.txt
360     ! good for any date since October 15, 1582
361     [
362         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
363         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
364         [ 1 + 3 * 5 /i + ] keep 2 * +
365     ] dip 1 + + 7 mod ;
366
367 : day-of-week ( timestamp -- n )
368     >date< (day-of-week) ;
369
370 : (week-number) ( timestamp -- [0,53] )
371     [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
372
373 DEFER: end-of-year
374
375 : week-number ( timestamp -- [1,53] )
376     dup (week-number) {
377         {  0 [ year>> 1 - end-of-year (week-number) ] }
378         { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
379         [ nip ]
380     } case ;
381
382 : same-week? ( ts1 ts2 -- ? )
383     [ [ year>> ] [ week-number ] bi 2array ] same-times? ;
384
385 : same-hour? ( ts1 ts2 -- ? )
386     [ slots{ year month day hour } ] same-times? ;
387
388 : same-minute? ( ts1 ts2 -- ? )
389     [ slots{ year month day hour minute } ] same-times? ;
390
391 : same-second? ( ts1 ts2 -- ? )
392     [ slots{ year month day hour minute second } ] same-times? ;
393
394 <PRIVATE
395
396 : (time-) ( timestamp timestamp -- n )
397     [ clone >gmt ] bi@
398     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
399     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
400
401 PRIVATE>
402
403 GENERIC: time- ( time1 time2 -- time3 )
404
405 M: timestamp time-
406     ! Exact calendar-time difference
407     (time-) seconds ;
408
409 : time* ( obj1 obj2 -- obj3 )
410     dup real? [ swap ] when
411     dup real? [ * ] [
412         {
413             [   year>> * ]
414             [  month>> * ]
415             [    day>> * ]
416             [   hour>> * ]
417             [ minute>> * ]
418             [ second>> * ]
419         } 2cleave <duration>
420     ] if ;
421
422 : before ( duration -- -duration )
423     -1 time* ;
424
425 : duration- ( duration1 duration2 -- duration3 )
426     {
427         [ [ year>> ] bi@ - ]
428         [ [ month>> ] bi@ - ]
429         [ [ day>> ] bi@ - ]
430         [ [ hour>> ] bi@ - ]
431         [ [ minute>> ] bi@ - ]
432         [ [ second>> ] bi@ - ]
433     } 2cleave <duration> ; inline
434
435 M: duration time-
436     over timestamp? [ before time+ ] [ duration- ] if ;
437
438 : unix-1970 ( -- timestamp )
439     1970 <year-gmt> ; inline
440
441 : millis>timestamp ( x -- timestamp )
442     unix-1970 swap 1000 / +second ;
443
444 : timestamp>millis ( timestamp -- n )
445     unix-1970 (time-) 1000 * >integer ;
446
447 : micros>timestamp ( x -- timestamp )
448     unix-1970 swap 1000000 / +second ;
449
450 : timestamp>micros ( timestamp -- n )
451     unix-1970 (time-) 1000000 * >integer ;
452
453 : now ( -- timestamp )
454     now-gmt gmt-offset-duration (time+) >>gmt-offset ;
455
456 : hence ( duration -- timestamp ) now swap time+ ;
457 : ago ( duration -- timestamp ) now swap time- ;
458
459 GENERIC: days-in-year ( obj -- n )
460
461 M: integer days-in-year leap-year? 366 365 ? ;
462
463 M: timestamp days-in-year year>> days-in-year ;
464
465 : days-in-month ( timestamp -- n )
466     >date< drop (days-in-month) ;
467
468 : midnight ( timestamp -- timestamp ) 0 0 0 set-time ; inline
469 : noon ( timestamp -- timestamp ) 12 0 0 set-time ; inline
470
471 : today ( -- timestamp ) now midnight ; inline
472 : tomorrow ( -- timestamp ) 1 days hence midnight ; inline
473 : overtomorrow ( -- timestamp ) 2 days hence midnight ; inline
474 : yesterday ( -- timestamp ) 1 days ago midnight ; inline
475 : ereyesterday ( -- timestamp ) 2 days ago midnight ; inline
476
477 ALIAS: start-of-day midnight
478
479 : end-of-day ( timestamp -- timestamp )
480     23 >>hour 59 >>minute 59+999/1000 >>second ; inline
481
482 : start-of-month ( timestamp -- timestamp )
483     midnight 1 >>day ; inline
484
485 : end-of-month ( timestamp -- timestamp )
486     [ end-of-day ] [ days-in-month ] bi >>day ;
487
488 : start-of-quarter ( timestamp -- timestamp )
489     [ start-of-day ] [ quarter 1 - 3 * ] bi >>month ; inline
490
491 : end-of-quarter ( timestamp -- timestamp )
492     dup quarter 1 - 3 * 3 + >>month end-of-month ; inline
493
494 : first-day-of-month ( timestamp -- timestamp )
495     1 >>day ;
496
497 : last-day-of-month ( timestamp -- timestamp )
498     dup days-in-month >>day ; inline
499
500 GENERIC: first-day-of-year ( object -- timestamp )
501 M: timestamp first-day-of-year first-day-of-month 1 >>month ;
502 M: integer first-day-of-year <year> ;
503
504 GENERIC: last-day-of-year ( object -- timestamp )
505 M: timestamp last-day-of-year 12 >>month 31 >>day ;
506 M: integer last-day-of-year 12 31 <date> ;
507
508 : first-day-of-decade ( object -- timestamp )
509     first-day-of-year [ dup 10 mod - ] change-year ;
510
511 : last-day-of-decade ( object -- timestamp )
512     last-day-of-year [ dup 10 mod - 9 + ] change-year ;
513
514 : first-day-of-century ( object -- timestamp )
515     first-day-of-year [ dup 100 mod - ] change-year ;
516
517 : last-day-of-century ( object -- timestamp )
518     last-day-of-year [ dup 100 mod - 99 + ] change-year ;
519
520 : first-day-of-millennium ( object -- timestamp )
521     first-day-of-year [ dup 1000 mod - ] change-year ;
522
523 : last-day-of-millennium ( object -- timestamp )
524     last-day-of-year [ dup 1000 mod - 999 + ] change-year ;
525
526 : start-of-year ( object -- timestamp )
527     first-day-of-year start-of-day ;
528
529 : end-of-year ( object -- timestamp )
530     last-day-of-year end-of-day ;
531
532 : start-of-decade ( object -- timestamp )
533     first-day-of-decade start-of-day ;
534
535 : end-of-decade ( object -- timestamp )
536     last-day-of-decade end-of-day ;
537
538 : end-of-century ( object -- timestamp )
539     last-day-of-century end-of-day ;
540
541 : start-of-millennium ( object -- timestamp )
542     first-day-of-millennium start-of-day ;
543
544 : end-of-millennium ( object -- timestamp )
545     last-day-of-millennium end-of-day ;
546
547 : start-of-hour ( timestamp -- timestamp ) 0 >>minute 0 >>second ;
548 : end-of-hour ( timestamp -- timestamp ) 59 >>minute 59+999/1000 >>second ;
549
550 : start-of-minute ( timestamp -- timestamp ) 0 >>second ;
551 : end-of-minute ( timestamp -- timestamp ) 59+999/1000 >>second ;
552
553 : start-of-second ( timestamp -- timestamp ) [ floor ] change-second ;
554 : end-of-second ( timestamp -- timestamp ) [ floor 999/1000 + ] change-second ;
555
556 <PRIVATE
557
558 : day-offset ( timestamp m -- timestamp n )
559     over day-of-week - ; inline
560
561 : day-this-week ( timestamp n -- timestamp )
562     day-offset days (time+) drop ;
563
564 : closest-day ( timestamp n -- timestamp )
565     { 0 1 2 3 -3 -2 -1 } pick day-of-week 7 swap -
566     <rotated> nth days (time+) drop ;
567
568 :: nth-day-this-month ( timestamp n day -- timestamp )
569     timestamp clone
570     timestamp start-of-month day day-this-week
571     [ [ month>> ] same? ] keep swap
572     [ 1 weeks (time+) drop ] unless
573     n [ weeks (time+) drop ] unless-zero ;
574
575 PRIVATE>
576
577 GENERIC: january ( obj -- timestamp )
578 GENERIC: february ( obj -- timestamp )
579 GENERIC: march ( obj -- timestamp )
580 GENERIC: april ( obj -- timestamp )
581 GENERIC: may ( obj -- timestamp )
582 GENERIC: june ( obj -- timestamp )
583 GENERIC: july ( obj -- timestamp )
584 GENERIC: august ( obj -- timestamp )
585 GENERIC: september ( obj -- timestamp )
586 GENERIC: october ( obj -- timestamp )
587 GENERIC: november ( obj -- timestamp )
588 GENERIC: december ( obj -- timestamp )
589
590 M: integer january 1 1 <date> ;
591 M: integer february 2 1 <date> ;
592 M: integer march 3 1 <date> ;
593 M: integer april 4 1 <date> ;
594 M: integer may 5 1 <date> ;
595 M: integer june 6 1 <date> ;
596 M: integer july 7 1 <date> ;
597 M: integer august 8 1 <date> ;
598 M: integer september 9 1 <date> ;
599 M: integer october 10 1 <date> ;
600 M: integer november 11 1 <date> ;
601 M: integer december 12 1 <date> ;
602
603 M: timestamp january 1 >>month ;
604 M: timestamp february 2 >>month ;
605 M: timestamp march 3 >>month ;
606 M: timestamp april 4 >>month ;
607 M: timestamp may 5 >>month ;
608 M: timestamp june 6 >>month ;
609 M: timestamp july 7 >>month ;
610 M: timestamp august 8 >>month ;
611 M: timestamp september 9 >>month ;
612 M: timestamp october 10 >>month ;
613 M: timestamp november 11 >>month ;
614 M: timestamp december 12 >>month ;
615
616 : closest-sunday ( timestamp -- timestamp ) 0 closest-day ;
617 : closest-monday ( timestamp -- timestamp ) 1 closest-day ;
618 : closest-tuesday ( timestamp -- timestamp ) 2 closest-day ;
619 : closest-wednesday ( timestamp -- timestamp ) 3 closest-day ;
620 : closest-thursday ( timestamp -- timestamp ) 4 closest-day ;
621 : closest-friday ( timestamp -- timestamp ) 5 closest-day ;
622 : closest-saturday ( timestamp -- timestamp ) 6 closest-day ;
623
624 : sunday ( timestamp -- timestamp ) 0 day-this-week ;
625 : monday ( timestamp -- timestamp ) 1 day-this-week ;
626 : tuesday ( timestamp -- timestamp ) 2 day-this-week ;
627 : wednesday ( timestamp -- timestamp ) 3 day-this-week ;
628 : thursday ( timestamp -- timestamp ) 4 day-this-week ;
629 : friday ( timestamp -- timestamp ) 5 day-this-week ;
630 : saturday ( timestamp -- timestamp ) 6 day-this-week ;
631
632 ALIAS: first-day-of-week sunday
633 ALIAS: last-day-of-week saturday
634
635 : day< ( timestamp quot -- timestamp )
636     over clone [ call dup ] dip after=? [ -7 days (time+) drop ] when ; inline
637 : day<= ( timestamp quot -- timestamp )
638     over clone [ call dup ] dip after? [ -7 days (time+) drop ] when ; inline
639 : day> ( timestamp quot -- timestamp )
640     over clone [ call dup ] dip before=? [ 7 days (time+) drop ] when ; inline
641 : day>= ( timestamp quot -- timestamp )
642     over clone [ call dup ] dip before? [ 7 days (time+) drop ] when ; inline
643
644 : sunday< ( timestamp -- timestamp ) [ sunday ] day< ;
645 : monday< ( timestamp -- timestamp ) [ monday ] day< ;
646 : tuesday< ( timestamp -- timestamp ) [ tuesday ] day< ;
647 : wednesday< ( timestamp -- timestamp ) [ wednesday ] day< ;
648 : thursday< ( timestamp -- timestamp ) [ thursday ] day< ;
649 : friday< ( timestamp -- timestamp ) [ friday ] day< ;
650 : saturday< ( timestamp -- timestamp ) [ saturday ] day< ;
651
652 : sunday<= ( timestamp -- timestamp ) [ sunday ] day<= ;
653 : monday<= ( timestamp -- timestamp ) [ monday ] day<= ;
654 : tuesday<= ( timestamp -- timestamp ) [ tuesday ] day<= ;
655 : wednesday<= ( timestamp -- timestamp ) [ wednesday ] day<= ;
656 : thursday<= ( timestamp -- timestamp ) [ thursday ] day<= ;
657 : friday<= ( timestamp -- timestamp ) [ friday ] day<= ;
658 : saturday<= ( timestamp -- timestamp ) [ saturday ] day<= ;
659
660 : sunday> ( timestamp -- timestamp ) [ sunday ] day> ;
661 : monday> ( timestamp -- timestamp ) [ monday ] day> ;
662 : tuesday> ( timestamp -- timestamp ) [ tuesday ] day> ;
663 : wednesday> ( timestamp -- timestamp ) [ wednesday ] day> ;
664 : thursday> ( timestamp -- timestamp ) [ thursday ] day> ;
665 : friday> ( timestamp -- timestamp ) [ friday ] day> ;
666 : saturday> ( timestamp -- timestamp ) [ saturday ] day> ;
667
668 : sunday>= ( timestamp -- timestamp ) [ sunday ] day>= ;
669 : monday>= ( timestamp -- timestamp ) [ monday ] day>= ;
670 : tuesday>= ( timestamp -- timestamp ) [ tuesday ] day>= ;
671 : wednesday>= ( timestamp -- timestamp ) [ wednesday ] day>= ;
672 : thursday>= ( timestamp -- timestamp ) [ thursday ] day>= ;
673 : friday>= ( timestamp -- timestamp ) [ friday ] day>= ;
674 : saturday>= ( timestamp -- timestamp ) [ saturday ] day>= ;
675
676 : next-sunday ( timestamp -- timestamp ) closest-sunday sunday> ;
677 : next-monday ( timestamp -- timestamp ) closest-monday monday> ;
678 : next-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday> ;
679 : next-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday> ;
680 : next-thursday ( timestamp -- timestamp ) closest-thursday thursday> ;
681 : next-friday ( timestamp -- timestamp ) closest-friday friday> ;
682 : next-saturday ( timestamp -- timestamp ) closest-saturday saturday> ;
683
684 : last-sunday ( timestamp -- timestamp ) closest-sunday sunday< ;
685 : last-monday ( timestamp -- timestamp ) closest-monday monday< ;
686 : last-tuesday ( timestamp -- timestamp ) closest-tuesday tuesday< ;
687 : last-wednesday ( timestamp -- timestamp ) closest-wednesday wednesday< ;
688 : last-thursday ( timestamp -- timestamp ) closest-thursday thursday< ;
689 : last-friday ( timestamp -- timestamp ) closest-friday friday< ;
690 : last-saturday ( timestamp -- timestamp ) closest-saturday saturday< ;
691
692 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
693 : monday? ( timestamp -- ? ) day-of-week 1 = ;
694 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
695 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
696 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
697 : friday? ( timestamp -- ? ) day-of-week 5 = ;
698 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
699
700 : january? ( timestamp -- ? ) month>> 1 = ;
701 : february? ( timestamp -- ? ) month>> 2 = ;
702 : march? ( timestamp -- ? ) month>> 3  = ;
703 : april? ( timestamp -- ? ) month>> 4 = ;
704 : may? ( timestamp -- ? ) month>> 5 = ;
705 : june? ( timestamp -- ? ) month>> 6 = ;
706 : july? ( timestamp -- ? ) month>> 7 = ;
707 : august? ( timestamp -- ? ) month>> 8 = ;
708 : september? ( timestamp -- ? ) month>> 9 = ;
709 : october? ( timestamp -- ? ) month>> 10 = ;
710 : november? ( timestamp -- ? ) month>> 11 = ;
711 : december? ( timestamp -- ? ) month>> 12 = ;
712
713 : weekend? ( timestamp -- ? ) day-of-week { 0 6 } member? ;
714 : weekday? ( timestamp -- ? ) day-of-week weekend? not ;
715
716 : same-or-next-business-day ( timestamp -- timestamp )
717     dup day-of-week {
718         { 0 [ monday ] }
719         { 6 [ 2 days (time+) drop ] }
720         [ drop ]
721     } case ;
722
723 : same-or-previous-business-day ( timestamp -- timestamp )
724     dup day-of-week {
725         { 0 [ -2 days (time+) drop ] }
726         { 6 [ friday ] }
727         [ drop ]
728     } case ;
729
730 : weekdays-between ( date1 date2 -- n )
731     [
732         [ swap time- duration>days 5 * ]
733         [ [ day-of-week ] bi@ - 2 * ] 2bi - 7 /i 1 +
734     ] 2keep
735     day-of-week 6 = [ [ 1 - ] dip ] when
736     day-of-week 0 = [ 1 - ] when ;
737
738 CONSTANT: weekday-offsets { 0 0 1 2 3 4 5 }
739
740 : weekdays-between2 ( date1 date2 -- n )
741     [ swap time- duration>days 1 + ]
742     [ [ day-of-week ] bi@ 6 swap - ] 2bi
743
744     [ + + 1.4 /i ]
745     [ [ weekday-offsets nth ] bi@ + ] 2bi - ;
746
747 : sunday-of-month ( timestamp n -- timestamp ) 0 nth-day-this-month ;
748 : monday-of-month ( timestamp n -- timestamp ) 1 nth-day-this-month ;
749 : tuesday-of-month ( timestamp n -- timestamp ) 2 nth-day-this-month ;
750 : wednesday-of-month ( timestamp n -- timestamp ) 3 nth-day-this-month ;
751 : thursday-of-month ( timestamp n -- timestamp ) 4 nth-day-this-month ;
752 : friday-of-month ( timestamp n -- timestamp ) 5 nth-day-this-month ;
753 : saturday-of-month ( timestamp n -- timestamp ) 6 nth-day-this-month ;
754
755 : last-sunday-of-month ( timestamp -- timestamp ) last-day-of-month sunday<= ;
756 : last-monday-of-month ( timestamp -- timestamp ) last-day-of-month monday<= ;
757 : last-tuesday-of-month ( timestamp -- timestamp ) last-day-of-month tuesday<= ;
758 : last-wednesday-of-month ( timestamp -- timestamp ) last-day-of-month wednesday<= ;
759 : last-thursday-of-month ( timestamp -- timestamp ) last-day-of-month thursday<= ;
760 : last-friday-of-month ( timestamp -- timestamp ) last-day-of-month friday<= ;
761 : last-saturday-of-month ( timestamp -- timestamp ) last-day-of-month saturday<= ;
762
763 : start-of-week ( timestamp -- timestamp )
764     sunday midnight ;
765
766 : end-of-week ( timestamp -- timestamp )
767     saturday end-of-day ;
768
769 : o'clock ( timestamp n -- timestamp )
770     [ midnight ] dip >>hour ;
771
772 : am ( timestamp n -- timestamp )
773     0 12 [a,b] check-interval o'clock ;
774
775 : pm ( timestamp n -- timestamp )
776     0 12 [a,b] check-interval 12 + o'clock ;
777
778 : time-since-midnight ( timestamp -- duration )
779     dup clone midnight time- ; inline
780
781 : since-1970 ( duration -- timestamp )
782     unix-1970 time+ ; inline
783
784 : timestamp>unix-time ( timestamp -- seconds )
785     unix-1970 (time-) ; inline
786
787 : unix-time>timestamp ( seconds -- timestamp )
788     [ unix-1970 ] dip +second ; inline
789
790 ! January and February need a fixup with this algorithm.
791 ! Find a better algorithm.
792 : ymd>ordinal ( year month day -- ordinal )
793     [ leap-year? dup -2 -3 ? ]
794     [ dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
795     [ ] tri* + + >integer
796     swap 367 366 ? mod ;
797
798 : timestamp>year-dates-gmt ( timestamp -- seq )
799     [ start-of-year >date< julian-day-number ]
800     [ days-in-year ] bi
801     [ drop ] [ + ] 2bi
802     [a..b) [ julian-day-number>date <date-gmt> ] map ;
803
804 : year-ordinal>timestamp ( year ordinal -- timestamp )
805     [ 1 1 julian-day-number ] dip
806     + 1 - julian-day-number>date <date> ;
807
808 GENERIC: weeks-in-week-year ( obj -- n )
809
810 M: integer weeks-in-week-year
811     { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
812
813 M: timestamp weeks-in-week-year
814     { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
815
816 {
817     { [ os unix? ] [ "calendar.unix" ] }
818     { [ os windows? ] [ "calendar.windows" ] }
819 } cond require
820
821 { "threads" "calendar" } "calendar.threads" require-when