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