]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
calendar: since-1970 shouldn't convert to >local-time; this causes words like "gmt...
[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.order sequences summary system threads vocabs.loader ;
6 IN: calendar
7
8 HOOK: gmt-offset os ( -- hours minutes seconds )
9
10 HOOK: gmt os ( -- timestamp )
11
12 TUPLE: duration
13     { year real }
14     { month real }
15     { day real }
16     { hour real }
17     { minute real }
18     { second real } ;
19
20 C: <duration> duration
21
22 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
23
24 TUPLE: timestamp
25     { year integer }
26     { month integer }
27     { day integer }
28     { hour integer }
29     { minute integer }
30     { second real }
31     { gmt-offset duration } ;
32
33 C: <timestamp> timestamp
34
35 : gmt-offset-duration ( -- duration )
36     0 0 0 gmt-offset <duration> ;
37
38 : <date> ( year month day -- timestamp )
39     0 0 0 gmt-offset-duration <timestamp> ;
40
41 : <date-gmt> ( year month day -- timestamp )
42     0 0 0 instant <timestamp> ;
43
44 : <year> ( year -- timestamp )
45     1 1 <date> ;
46
47 : <year-gmt> ( year -- timestamp )
48     1 1 <date-gmt> ;
49
50 ERROR: not-a-month ;
51 M: not-a-month summary
52     drop "Months are indexed starting at 1" ;
53
54 <PRIVATE
55
56 : check-month ( n -- n )
57     [ not-a-month ] when-zero ;
58
59 PRIVATE>
60
61 CONSTANT: month-names 
62     {
63         "January" "February" "March" "April" "May" "June"
64         "July" "August" "September" "October" "November" "December"
65     }
66
67 <PRIVATE
68
69 : (month-name) ( n -- string ) 1 - month-names nth ;
70
71 PRIVATE>
72
73 GENERIC: month-name ( obj -- string )
74
75 M: integer month-name check-month 1 - month-names nth ;
76 M: timestamp month-name month>> 1 - month-names nth ;
77
78 CONSTANT: month-abbreviations
79     {
80         "Jan" "Feb" "Mar" "Apr" "May" "Jun"
81         "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
82     }
83
84 : month-abbreviation ( n -- string )
85     check-month 1 - month-abbreviations nth ;
86
87 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
88
89 CONSTANT: day-names
90     { "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" }
91
92 CONSTANT: day-abbreviations2
93     { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
94
95 : day-abbreviation2 ( n -- string )
96     day-abbreviations2 nth ; inline
97
98 CONSTANT: day-abbreviations3
99     { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
100
101 : day-abbreviation3 ( n -- string )
102     day-abbreviations3 nth ; inline
103
104 CONSTANT: average-month 30+5/12
105 CONSTANT: months-per-year 12
106 CONSTANT: days-per-year 3652425/10000
107 CONSTANT: hours-per-year 876582/100
108 CONSTANT: minutes-per-year 5259492/10
109 CONSTANT: seconds-per-year 31556952
110
111 :: julian-day-number ( year month day -- n )
112     #! Returns a composite date number
113     #! Not valid before year -4800
114     14 month - 12 /i :> a
115     year 4800 + a - :> y
116     month 12 a * + 3 - :> m
117
118     day 153 m * 2 + 5 /i + 365 y * +
119     y 4 /i + y 100 /i - y 400 /i + 32045 - ;
120
121 :: julian-day-number>date ( n -- year month day )
122     #! Inverse of julian-day-number
123     n 32044 + :> a
124     4 a * 3 + 146097 /i :> b
125     a 146097 b * 4 /i - :> c
126     4 c * 3 + 1461 /i :> d
127     c 1461 d * 4 /i - :> e
128     5 e * 2 + 153 /i :> m
129
130     100 b * d + 4800 -
131     m 10 /i + m 3 +
132     12 m 10 /i * -
133     e 153 m * 2 + 5 /i - 1 + ;
134
135 GENERIC: easter ( obj -- obj' )
136
137 :: easter-month-day ( year -- month day )
138     year 19 mod :> a
139     year 100 /mod :> ( b c )
140     b 4 /mod :> ( d e )
141     b 8 + 25 /i :> f
142     b f - 1 + 3 /i :> g
143     19 a * b + d - g - 15 + 30 mod :> h
144     c 4 /mod :> ( i k )
145     32 2 e * + 2 i * + h - k - 7 mod :> l
146     a 11 h * + 22 l * + 451 /i :> m
147
148     h l + 7 m * - 114 + 31 /mod 1 + ;
149
150 M: integer easter ( year -- timestamp )
151     dup easter-month-day <date> ;
152
153 M: timestamp easter ( timestamp -- timestamp )
154     clone
155     dup year>> easter-month-day
156     swapd >>day swap >>month ;
157
158 : >date< ( timestamp -- year month day )
159     [ year>> ] [ month>> ] [ day>> ] tri ;
160
161 : >time< ( timestamp -- hour minute second )
162     [ hour>> ] [ minute>> ] [ second>> ] tri ;
163
164 : years ( x -- duration ) instant clone swap >>year ;
165 : months ( x -- duration ) instant clone swap >>month ;
166 : days ( x -- duration ) instant clone swap >>day ;
167 : weeks ( x -- duration ) 7 * days ;
168 : hours ( x -- duration ) instant clone swap >>hour ;
169 : minutes ( x -- duration ) instant clone swap >>minute ;
170 : seconds ( x -- duration ) instant clone swap >>second ;
171 : milliseconds ( x -- duration ) 1000 / seconds ;
172 : microseconds ( x -- duration ) 1000000 / seconds ;
173 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
174
175 GENERIC: leap-year? ( obj -- ? )
176
177 M: integer leap-year? ( year -- ? )
178     dup 100 divisor? 400 4 ? divisor? ;
179
180 M: timestamp leap-year? ( timestamp -- ? )
181     year>> leap-year? ;
182
183 <PRIVATE
184
185 GENERIC: +year ( timestamp x -- timestamp )
186 GENERIC: +month ( timestamp x -- timestamp )
187 GENERIC: +day ( timestamp x -- timestamp )
188 GENERIC: +hour ( timestamp x -- timestamp )
189 GENERIC: +minute ( timestamp x -- timestamp )
190 GENERIC: +second ( timestamp x -- timestamp )
191
192 : /rem ( f n -- q r )
193     #! q is positive or negative, r is positive from 0 <= r < n
194     [ / floor >integer ] 2keep rem ;
195
196 : float>whole-part ( float -- int float )
197     [ floor >integer ] keep over - ;
198
199 : adjust-leap-year ( timestamp -- timestamp )
200     dup
201     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
202     [ 3 >>month 1 >>day ] when ;
203
204 M: integer +year ( timestamp n -- timestamp )
205     [ + ] curry change-year adjust-leap-year ;
206
207 M: real +year ( timestamp n -- timestamp )
208     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
209
210 : months/years ( n -- months years )
211     12 /rem [ 1 - 12 ] when-zero swap ; inline
212
213 M: integer +month ( timestamp n -- timestamp )
214     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
215
216 M: real +month ( timestamp n -- timestamp )
217     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
218
219 M: integer +day ( timestamp n -- timestamp )
220     [
221         over >date< julian-day-number + julian-day-number>date
222         [ >>year ] [ >>month ] [ >>day ] tri*
223     ] unless-zero ;
224
225 M: real +day ( timestamp n -- timestamp )
226     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
227
228 : hours/days ( n -- hours days )
229     24 /rem swap ;
230
231 M: integer +hour ( timestamp n -- timestamp )
232     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
233
234 M: real +hour ( timestamp n -- timestamp )
235     float>whole-part swapd 60 * +minute swap +hour ;
236
237 : minutes/hours ( n -- minutes hours )
238     60 /rem swap ;
239
240 M: integer +minute ( timestamp n -- timestamp )
241     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
242
243 M: real +minute ( timestamp n -- timestamp )
244     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
245
246 : seconds/minutes ( n -- seconds minutes )
247     60 /rem swap >integer ;
248
249 M: number +second ( timestamp n -- timestamp )
250     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
251
252 : (time+) ( timestamp duration -- timestamp' duration )
253     [ second>> +second ] keep
254     [ minute>> +minute ] keep
255     [ hour>>   +hour   ] keep
256     [ day>>    +day    ] keep
257     [ month>>  +month  ] keep
258     [ year>>   +year   ] keep ; inline
259
260 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
261     [ bi@ + ] curry 2keep ; inline
262
263 PRIVATE>
264
265 GENERIC# time+ 1 ( time1 time2 -- time3 )
266
267 M: timestamp time+
268     [ clone ] dip (time+) drop ;
269
270 M: duration time+
271     dup timestamp? [
272         swap time+
273     ] [
274         [ year>> ] +slots
275         [ month>> ] +slots
276         [ day>> ] +slots
277         [ hour>> ] +slots
278         [ minute>> ] +slots
279         [ second>> ] +slots
280         2drop <duration>
281     ] if ;
282
283 : duration>years ( duration -- x )
284     #! Uses average month/year length since duration loses calendar
285     #! data
286     0 swap
287     {
288         [ year>> + ]
289         [ month>> months-per-year / + ]
290         [ day>> days-per-year / + ]
291         [ hour>> hours-per-year / + ]
292         [ minute>> minutes-per-year / + ]
293         [ second>> seconds-per-year / + ]
294     } cleave ;
295
296 M: duration <=> [ duration>years ] compare ;
297
298 : duration>months ( duration -- x ) duration>years months-per-year * ;
299 : duration>days ( duration -- x ) duration>years days-per-year * ;
300 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
301 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
302 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
303 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
304 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
305 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
306
307 GENERIC: time- ( time1 time2 -- time3 )
308
309 : convert-timezone ( timestamp duration -- timestamp' )
310     over gmt-offset>> over = [ drop ] [
311         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
312     ] if ;
313
314 : >local-time ( timestamp -- timestamp' )
315     gmt-offset-duration convert-timezone ;
316
317 : >gmt ( timestamp -- timestamp' )
318     instant convert-timezone ;
319
320 M: timestamp <=> ( ts1 ts2 -- n )
321     [ >gmt tuple-slots ] compare ;
322
323 : same-day? ( ts1 ts2 -- ? )
324     [ >gmt >date< <date> ] bi@ = ;
325
326 : (time-) ( timestamp timestamp -- n )
327     [ >gmt ] bi@
328     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
329     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
330
331 M: timestamp time-
332     #! Exact calendar-time difference
333     (time-) seconds ;
334
335 : time* ( obj1 obj2 -- obj3 )
336     dup real? [ swap ] when
337     dup real? [ * ] [
338         {
339             [   year>> * ]
340             [  month>> * ]
341             [    day>> * ]
342             [   hour>> * ]
343             [ minute>> * ]
344             [ second>> * ]
345         } 2cleave <duration>
346     ] if ;
347
348 : before ( duration -- -duration )
349     -1 time* ;
350
351 M: duration time-
352     before time+ ;
353
354 : <zero> ( -- timestamp )
355     0 0 0 0 0 0 instant <timestamp> ;
356
357 : valid-timestamp? ( timestamp -- ? )
358     clone instant >>gmt-offset
359     dup <zero> time- <zero> time+ = ;
360
361 : unix-1970 ( -- timestamp )
362     1970 1 1 0 0 0 instant <timestamp> ;
363
364 : millis>timestamp ( x -- timestamp )
365     [ unix-1970 ] dip milliseconds time+ ;
366
367 : timestamp>millis ( timestamp -- n )
368     unix-1970 (time-) 1000 * >integer ;
369
370 : micros>timestamp ( x -- timestamp )
371     [ unix-1970 ] dip microseconds time+ ;
372
373 : timestamp>micros ( timestamp -- n )
374     unix-1970 (time-) 1000000 * >integer ;
375
376 : now ( -- timestamp ) gmt >local-time ;
377 : hence ( duration -- timestamp ) now swap time+ ;
378 : ago ( duration -- timestamp ) now swap time- ;
379
380 : zeller-congruence ( year month day -- n )
381     #! Zeller Congruence
382     #! http://web.textfiles.com/computers/formulas.txt
383     #! good for any date since October 15, 1582
384     [
385         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
386         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
387         [ 1 + 3 * 5 /i + ] keep 2 * +
388     ] dip 1 + + 7 mod ;
389
390 GENERIC: days-in-year ( obj -- n )
391
392 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
393 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
394
395 : (days-in-month) ( year month -- n )
396     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
397
398 : days-in-month ( timestamp -- n )
399     >date< drop (days-in-month) ;
400
401 : day-of-week ( timestamp -- n )
402     >date< zeller-congruence ;
403
404 GENERIC: day-name ( obj -- string )
405 M: integer day-name day-names nth ;
406 M: timestamp day-name day-of-week day-names nth ;
407
408 :: (day-of-year) ( year month day -- n )
409     day-counts month head-slice sum day +
410     year leap-year? [
411         year month day <date>
412         year 3 1 <date>
413         after=? [ 1 + ] when
414     ] when ;
415
416 : day-of-year ( timestamp -- n )
417     >date< (day-of-year) ;
418
419 : midnight ( timestamp -- new-timestamp )
420     clone 0 >>hour 0 >>minute 0 >>second ; inline
421
422 : noon ( timestamp -- new-timestamp )
423     midnight 12 >>hour ; inline
424
425 : beginning-of-month ( timestamp -- new-timestamp )
426     midnight 1 >>day ;
427
428 : end-of-month ( timestamp -- new-timestamp )
429     [ midnight ] [ days-in-month ] bi >>day ;
430
431 <PRIVATE
432
433 : day-offset ( timestamp m -- new-timestamp n )
434     over day-of-week - ; inline
435
436 : day-this-week ( timestamp n -- new-timestamp )
437     day-offset days time+ ;
438
439 :: nth-day-this-month ( timestamp n day -- new-timestamp )
440     timestamp beginning-of-month day day-this-week
441     dup timestamp [ month>> ] bi@ = [ 1 weeks time+ ] unless
442     n 1 - [ weeks time+ ] unless-zero ;
443
444 : last-day-this-month ( timestamp day -- new-timestamp )
445     [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
446
447 PRIVATE>
448
449 GENERIC: january ( obj -- timestamp )
450 GENERIC: february ( obj -- timestamp )
451 GENERIC: march ( obj -- timestamp )
452 GENERIC: april ( obj -- timestamp )
453 GENERIC: may ( obj -- timestamp )
454 GENERIC: june ( obj -- timestamp )
455 GENERIC: july ( obj -- timestamp )
456 GENERIC: august ( obj -- timestamp )
457 GENERIC: september ( obj -- timestamp )
458 GENERIC: october ( obj -- timestamp )
459 GENERIC: november ( obj -- timestamp )
460 GENERIC: december ( obj -- timestamp )
461
462 M: integer january 1 1 <date> ;
463 M: integer february 2 1 <date> ;
464 M: integer march 3 1 <date> ;
465 M: integer april 4 1 <date> ;
466 M: integer may 5 1 <date> ;
467 M: integer june 6 1 <date> ;
468 M: integer july 7 1 <date> ;
469 M: integer august 8 1 <date> ;
470 M: integer september 9 1 <date> ;
471 M: integer october 10 1 <date> ;
472 M: integer november 11 1 <date> ;
473 M: integer december 12 1 <date> ;
474
475 M: timestamp january clone 1 >>month ;
476 M: timestamp february clone 2 >>month ;
477 M: timestamp march clone 3 >>month ;
478 M: timestamp april clone 4 >>month ;
479 M: timestamp may clone 5 >>month ;
480 M: timestamp june clone 6 >>month ;
481 M: timestamp july clone 7 >>month ;
482 M: timestamp august clone 8 >>month ;
483 M: timestamp september clone 9 >>month ;
484 M: timestamp october clone 10 >>month ;
485 M: timestamp november clone 11 >>month ;
486 M: timestamp december clone 12 >>month ;
487
488 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
489 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
490 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
491 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
492 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
493 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
494 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
495
496 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
497 : monday? ( timestamp -- ? ) day-of-week 1 = ;
498 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
499 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
500 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
501 : friday? ( timestamp -- ? ) day-of-week 5 = ;
502 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
503
504 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
505 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
506 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
507 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
508 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
509 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
510 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
511
512 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
513 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
514 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
515 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
516 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
517 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
518 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
519
520 : beginning-of-week ( timestamp -- new-timestamp )
521     midnight sunday ;
522
523 GENERIC: beginning-of-year ( object -- new-timestamp )
524 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
525 M: integer beginning-of-year <year> ;
526
527 GENERIC: end-of-year ( object -- new-timestamp )
528 M: timestamp end-of-year 12 >>month 31 >>day ;
529 M: integer end-of-year 12 31 <date> ;
530
531 : time-since-midnight ( timestamp -- duration )
532     dup midnight time- ;
533
534 : since-1970 ( duration -- timestamp )
535     unix-1970 time+ ;
536
537 : timestamp>unix-time ( timestamp -- seconds )
538     unix-1970 time- second>> ;
539
540 : unix-time>timestamp ( seconds -- timestamp )
541     seconds unix-1970 time+ ;
542
543 M: duration sleep
544     duration>nanoseconds >integer nano-count + sleep-until ;
545
546 {
547     { [ os unix? ] [ "calendar.unix" ] }
548     { [ os windows? ] [ "calendar.windows" ] }
549 } cond require