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