]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[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 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 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 : unless-zero ( n quot -- )
190     [ dup zero? [ drop ] ] dip if ; inline
191
192 M: integer +year ( timestamp n -- timestamp )
193     [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
194
195 M: real +year ( timestamp n -- timestamp )
196     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
197
198 : months/years ( n -- months years )
199     12 /rem dup zero? [ drop 1 - 12 ] when swap ; inline
200
201 M: integer +month ( timestamp n -- timestamp )
202     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
203
204 M: real +month ( timestamp n -- timestamp )
205     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
206
207 M: integer +day ( timestamp n -- timestamp )
208     [
209         over >date< julian-day-number + julian-day-number>date
210         [ >>year ] [ >>month ] [ >>day ] tri*
211     ] unless-zero ;
212
213 M: real +day ( timestamp n -- timestamp )
214     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
215
216 : hours/days ( n -- hours days )
217     24 /rem swap ;
218
219 M: integer +hour ( timestamp n -- timestamp )
220     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
221
222 M: real +hour ( timestamp n -- timestamp )
223     float>whole-part swapd 60 * +minute swap +hour ;
224
225 : minutes/hours ( n -- minutes hours )
226     60 /rem swap ;
227
228 M: integer +minute ( timestamp n -- timestamp )
229     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
230
231 M: real +minute ( timestamp n -- timestamp )
232     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
233
234 : seconds/minutes ( n -- seconds minutes )
235     60 /rem swap >integer ;
236
237 M: number +second ( timestamp n -- timestamp )
238     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
239
240 : (time+) ( timestamp duration -- timestamp' duration )
241     [ second>> +second ] keep
242     [ minute>> +minute ] keep
243     [ hour>>   +hour   ] keep
244     [ day>>    +day    ] keep
245     [ month>>  +month  ] keep
246     [ year>>   +year   ] keep ; inline
247
248 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
249     [ bi@ + ] curry 2keep ; inline
250
251 PRIVATE>
252
253 GENERIC# time+ 1 ( time1 time2 -- time3 )
254
255 M: timestamp time+
256     [ clone ] dip (time+) drop ;
257
258 M: duration time+
259     dup timestamp? [
260         swap time+
261     ] [
262         [ year>> ] +slots
263         [ month>> ] +slots
264         [ day>> ] +slots
265         [ hour>> ] +slots
266         [ minute>> ] +slots
267         [ second>> ] +slots
268         2drop <duration>
269     ] if ;
270
271 : duration>years ( duration -- x )
272     #! Uses average month/year length since duration loses calendar
273     #! data
274     0 swap
275     {
276         [ year>> + ]
277         [ month>> months-per-year / + ]
278         [ day>> days-per-year / + ]
279         [ hour>> hours-per-year / + ]
280         [ minute>> minutes-per-year / + ]
281         [ second>> seconds-per-year / + ]
282     } cleave ;
283
284 M: duration <=> [ duration>years ] compare ;
285
286 : duration>months ( duration -- x ) duration>years months-per-year * ;
287 : duration>days ( duration -- x ) duration>years days-per-year * ;
288 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
289 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
290 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
291 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
292 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
293 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
294
295 GENERIC: time- ( time1 time2 -- time3 )
296
297 : convert-timezone ( timestamp duration -- timestamp )
298     over gmt-offset>> over = [ drop ] [
299         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
300     ] if ;
301
302 : >local-time ( timestamp -- timestamp )
303     gmt-offset-duration convert-timezone ;
304
305 : >gmt ( timestamp -- timestamp )
306     instant convert-timezone ;
307
308 M: timestamp <=> ( ts1 ts2 -- n )
309     [ >gmt tuple-slots ] compare ;
310
311 : (time-) ( timestamp timestamp -- n )
312     [ >gmt ] bi@
313     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
314     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
315
316 M: timestamp time-
317     #! Exact calendar-time difference
318     (time-) seconds ;
319
320 : time* ( obj1 obj2 -- obj3 )
321     dup real? [ swap ] when
322     dup real? [ * ] [
323         {
324             [   year>> * ]
325             [  month>> * ]
326             [    day>> * ]
327             [   hour>> * ]
328             [ minute>> * ]
329             [ second>> * ]
330         } 2cleave <duration>
331     ] if ;
332
333 : before ( duration -- -duration )
334     -1 time* ;
335
336 M: duration time-
337     before time+ ;
338
339 : <zero> ( -- timestamp )
340     0 0 0 0 0 0 instant <timestamp> ;
341
342 : valid-timestamp? ( timestamp -- ? )
343     clone instant >>gmt-offset
344     dup <zero> time- <zero> time+ = ;
345
346 : unix-1970 ( -- timestamp )
347     1970 1 1 0 0 0 instant <timestamp> ;
348
349 : millis>timestamp ( x -- timestamp )
350     [ unix-1970 ] dip milliseconds time+ ;
351
352 : timestamp>millis ( timestamp -- n )
353     unix-1970 (time-) 1000 * >integer ;
354
355 : micros>timestamp ( x -- timestamp )
356     [ unix-1970 ] dip microseconds time+ ;
357
358 : timestamp>micros ( timestamp -- n )
359     unix-1970 (time-) 1000000 * >integer ;
360
361 : gmt ( -- timestamp )
362     #! GMT time, right now
363     unix-1970 micros microseconds time+ ;
364
365 : now ( -- timestamp ) gmt >local-time ;
366 : hence ( duration -- timestamp ) now swap time+ ;
367 : ago ( duration -- timestamp ) now swap time- ;
368
369 : zeller-congruence ( year month day -- n )
370     #! Zeller Congruence
371     #! http://web.textfiles.com/computers/formulas.txt
372     #! good for any date since October 15, 1582
373     [
374         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
375         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
376         [ 1 + 3 * 5 /i + ] keep 2 * +
377     ] dip 1 + + 7 mod ;
378
379 GENERIC: days-in-year ( obj -- n )
380
381 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
382 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
383
384 : (days-in-month) ( year month -- n )
385     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
386
387 : days-in-month ( timestamp -- n )
388     >date< drop (days-in-month) ;
389
390 : day-of-week ( timestamp -- n )
391     >date< zeller-congruence ;
392
393 :: (day-of-year) ( year month day -- n )
394     day-counts month head-slice sum day +
395     year leap-year? [
396         year month day <date>
397         year 3 1 <date>
398         after=? [ 1 + ] when
399     ] when ;
400
401 : day-of-year ( timestamp -- n )
402     >date< (day-of-year) ;
403
404 <PRIVATE
405 : day-offset ( timestamp m -- timestamp n )
406     over day-of-week - ; inline
407
408 : day-this-week ( timestamp n -- timestamp )
409     day-offset days time+ ;
410 PRIVATE>
411
412 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
413 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
414 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
415 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
416 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
417 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
418 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
419
420 : midnight ( timestamp -- new-timestamp )
421     clone 0 >>hour 0 >>minute 0 >>second ; inline
422
423 : noon ( timestamp -- new-timestamp )
424     midnight 12 >>hour ; inline
425
426 : beginning-of-month ( timestamp -- new-timestamp )
427     midnight 1 >>day ;
428
429 : beginning-of-week ( timestamp -- new-timestamp )
430     midnight sunday ;
431
432 : beginning-of-year ( timestamp -- new-timestamp )
433     beginning-of-month 1 >>month ;
434
435 : time-since-midnight ( timestamp -- duration )
436     dup midnight time- ;
437
438 : since-1970 ( duration -- timestamp )
439     unix-1970 time+ >local-time ;
440
441 M: timestamp sleep-until timestamp>micros sleep-until ;
442
443 M: duration sleep hence sleep-until ;
444
445 {
446     { [ os unix? ] [ "calendar.unix" ] }
447     { [ os windows? ] [ "calendar.windows" ] }
448 } cond require