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