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