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