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