]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
use CONSTANT: in calendar
[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
43 : check-month ( n -- n )
44     dup zero? [ not-a-month ] when ;
45
46 PRIVATE>
47
48 : month-names ( -- array )
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     [let* | a [ 14 month - 12 /i ]
98             y [ year 4800 + a - ]
99             m [ month 12 a * + 3 - ] |
100         day 153 m * 2 + 5 /i + 365 y * +
101         y 4 /i + y 100 /i - y 400 /i + 32045 -
102     ] ;
103
104 :: julian-day-number>date ( n -- year month day )
105     #! Inverse of julian-day-number
106     [let* | a [ n 32044 + ]
107             b [ 4 a * 3 + 146097 /i ]
108             c [ a 146097 b * 4 /i - ]
109             d [ 4 c * 3 + 1461 /i ]
110             e [ c 1461 d * 4 /i - ]
111             m [ 5 e * 2 + 153 /i ] |
112         100 b * d + 4800 -
113         m 10 /i + m 3 +
114         12 m 10 /i * -
115         e 153 m * 2 + 5 /i - 1+
116     ] ;
117
118 : >date< ( timestamp -- year month day )
119     [ year>> ] [ month>> ] [ day>> ] tri ;
120
121 : >time< ( timestamp -- hour minute second )
122     [ hour>> ] [ minute>> ] [ second>> ] tri ;
123
124 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
125 : years ( x -- duration ) instant clone swap >>year ;
126 : months ( x -- duration ) instant clone swap >>month ;
127 : days ( x -- duration ) instant clone swap >>day ;
128 : weeks ( x -- duration ) 7 * days ;
129 : hours ( x -- duration ) instant clone swap >>hour ;
130 : minutes ( x -- duration ) instant clone swap >>minute ;
131 : seconds ( x -- duration ) instant clone swap >>second ;
132 : milliseconds ( x -- duration ) 1000 / seconds ;
133 : microseconds ( x -- duration ) 1000000 / seconds ;
134 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
135
136 GENERIC: leap-year? ( obj -- ? )
137
138 M: integer leap-year? ( year -- ? )
139     dup 100 mod zero? 400 4 ? mod zero? ;
140
141 M: timestamp leap-year? ( timestamp -- ? )
142     year>> leap-year? ;
143
144 <PRIVATE
145
146 GENERIC: +year ( timestamp x -- timestamp )
147 GENERIC: +month ( timestamp x -- timestamp )
148 GENERIC: +day ( timestamp x -- timestamp )
149 GENERIC: +hour ( timestamp x -- timestamp )
150 GENERIC: +minute ( timestamp x -- timestamp )
151 GENERIC: +second ( timestamp x -- timestamp )
152
153 : /rem ( f n -- q r )
154     #! q is positive or negative, r is positive from 0 <= r < n
155     [ / floor >integer ] 2keep rem ;
156
157 : float>whole-part ( float -- int float )
158     [ floor >integer ] keep over - ;
159
160 : adjust-leap-year ( timestamp -- timestamp )
161     dup
162     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
163     [ 3 >>month 1 >>day ] when ;
164
165 : unless-zero ( n quot -- )
166     [ dup zero? [ drop ] ] dip if ; inline
167
168 M: integer +year ( timestamp n -- timestamp )
169     [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
170
171 M: real +year ( timestamp n -- timestamp )
172     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
173
174 : months/years ( n -- months years )
175     12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
176
177 M: integer +month ( timestamp n -- timestamp )
178     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
179
180 M: real +month ( timestamp n -- timestamp )
181     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
182
183 M: integer +day ( timestamp n -- timestamp )
184     [
185         over >date< julian-day-number + julian-day-number>date
186         [ >>year ] [ >>month ] [ >>day ] tri*
187     ] unless-zero ;
188
189 M: real +day ( timestamp n -- timestamp )
190     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
191
192 : hours/days ( n -- hours days )
193     24 /rem swap ;
194
195 M: integer +hour ( timestamp n -- timestamp )
196     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
197
198 M: real +hour ( timestamp n -- timestamp )
199     float>whole-part swapd 60 * +minute swap +hour ;
200
201 : minutes/hours ( n -- minutes hours )
202     60 /rem swap ;
203
204 M: integer +minute ( timestamp n -- timestamp )
205     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
206
207 M: real +minute ( timestamp n -- timestamp )
208     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
209
210 : seconds/minutes ( n -- seconds minutes )
211     60 /rem swap >integer ;
212
213 M: number +second ( timestamp n -- timestamp )
214     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
215
216 : (time+) ( timestamp duration -- timestamp' duration )
217     [ second>> +second ] keep
218     [ minute>> +minute ] keep
219     [ hour>>   +hour   ] keep
220     [ day>>    +day    ] keep
221     [ month>>  +month  ] keep
222     [ year>>   +year   ] keep ; inline
223
224 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
225     [ bi@ + ] curry 2keep ; inline
226
227 PRIVATE>
228
229 GENERIC# time+ 1 ( time1 time2 -- time3 )
230
231 M: timestamp time+
232     [ clone ] dip (time+) drop ;
233
234 M: duration time+
235     dup timestamp? [
236         swap time+
237     ] [
238         [ year>> ] +slots
239         [ month>> ] +slots
240         [ day>> ] +slots
241         [ hour>> ] +slots
242         [ minute>> ] +slots
243         [ second>> ] +slots
244         2drop <duration>
245     ] if ;
246
247 : duration>years ( duration -- x )
248     #! Uses average month/year length since duration loses calendar
249     #! data
250     0 swap
251     {
252         [ year>> + ]
253         [ month>> months-per-year / + ]
254         [ day>> days-per-year / + ]
255         [ hour>> hours-per-year / + ]
256         [ minute>> minutes-per-year / + ]
257         [ second>> seconds-per-year / + ]
258     } cleave ;
259
260 M: duration <=> [ duration>years ] compare ;
261
262 : duration>months ( duration -- x ) duration>years months-per-year * ;
263 : duration>days ( duration -- x ) duration>years days-per-year * ;
264 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
265 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
266 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
267 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
268 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
269 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
270
271 GENERIC: time- ( time1 time2 -- time3 )
272
273 : convert-timezone ( timestamp duration -- timestamp )
274     over gmt-offset>> over = [ drop ] [
275         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
276     ] if ;
277
278 : >local-time ( timestamp -- timestamp )
279     gmt-offset-duration convert-timezone ;
280
281 : >gmt ( timestamp -- timestamp )
282     instant convert-timezone ;
283
284 M: timestamp <=> ( ts1 ts2 -- n )
285     [ >gmt tuple-slots ] compare ;
286
287 : (time-) ( timestamp timestamp -- n )
288     [ >gmt ] bi@
289     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
290     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
291
292 M: timestamp time-
293     #! Exact calendar-time difference
294     (time-) seconds ;
295
296 : time* ( obj1 obj2 -- obj3 )
297     dup real? [ swap ] when
298     dup real? [ * ] [
299         {
300             [   year>> * ]
301             [  month>> * ]
302             [    day>> * ]
303             [   hour>> * ]
304             [ minute>> * ]
305             [ second>> * ]
306         } 2cleave <duration>
307     ] if ;
308
309 : before ( duration -- -duration )
310     -1 time* ;
311
312 M: duration time-
313     before time+ ;
314
315 : <zero> ( -- timestamp )
316     0 0 0 0 0 0 instant <timestamp> ;
317
318 : valid-timestamp? ( timestamp -- ? )
319     clone instant >>gmt-offset
320     dup <zero> time- <zero> time+ = ;
321
322 : unix-1970 ( -- timestamp )
323     1970 1 1 0 0 0 instant <timestamp> ;
324
325 : millis>timestamp ( x -- timestamp )
326     [ unix-1970 ] dip milliseconds time+ ;
327
328 : timestamp>millis ( timestamp -- n )
329     unix-1970 (time-) 1000 * >integer ;
330
331 : micros>timestamp ( x -- timestamp )
332     [ unix-1970 ] dip microseconds time+ ;
333
334 : timestamp>micros ( timestamp -- n )
335     unix-1970 (time-) 1000000 * >integer ;
336
337 : gmt ( -- timestamp )
338     #! GMT time, right now
339     unix-1970 micros microseconds time+ ;
340
341 : now ( -- timestamp ) gmt >local-time ;
342 : hence ( duration -- timestamp ) now swap time+ ;
343 : ago ( duration -- timestamp ) now swap time- ;
344
345 : zeller-congruence ( year month day -- n )
346     #! Zeller Congruence
347     #! http://web.textfiles.com/computers/formulas.txt
348     #! good for any date since October 15, 1582
349     [
350         dup 2 <= [ [ 1- ] [ 12 + ] bi* ] when
351         [ dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + ] dip
352         [ 1+ 3 * 5 /i + ] keep 2 * +
353     ] dip 1+ + 7 mod ;
354
355 GENERIC: days-in-year ( obj -- n )
356
357 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
358 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
359
360 : (days-in-month) ( year month -- n )
361     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
362
363 : days-in-month ( timestamp -- n )
364     >date< drop (days-in-month) ;
365
366 : day-of-week ( timestamp -- n )
367     >date< zeller-congruence ;
368
369 :: (day-of-year) ( year month day -- n )
370     day-counts month head-slice sum day +
371     year leap-year? [
372         year month day <date>
373         year 3 1 <date>
374         after=? [ 1+ ] when
375     ] when ;
376
377 : day-of-year ( timestamp -- n )
378     >date< (day-of-year) ;
379
380 <PRIVATE
381 : day-offset ( timestamp m -- timestamp n )
382     over day-of-week - ; inline
383
384 : day-this-week ( timestamp n -- timestamp )
385     day-offset days time+ ;
386 PRIVATE>
387
388 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
389 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
390 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
391 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
392 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
393 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
394 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
395
396 : midnight ( timestamp -- new-timestamp )
397     clone 0 >>hour 0 >>minute 0 >>second ; inline
398
399 : noon ( timestamp -- new-timestamp )
400     midnight 12 >>hour ; inline
401
402 : beginning-of-month ( timestamp -- new-timestamp )
403     midnight 1 >>day ;
404
405 : beginning-of-week ( timestamp -- new-timestamp )
406     midnight sunday ;
407
408 : beginning-of-year ( timestamp -- new-timestamp )
409     beginning-of-month 1 >>month ;
410
411 : time-since-midnight ( timestamp -- duration )
412     dup midnight time- ;
413
414 : since-1970 ( duration -- timestamp )
415     unix-1970 time+ >local-time ;
416
417 M: timestamp sleep-until timestamp>micros sleep-until ;
418
419 M: duration sleep hence sleep-until ;
420
421 {
422     { [ os unix? ] [ "calendar.unix" ] }
423     { [ os windows? ] [ "calendar.windows" ] }
424 } cond require