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