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