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