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