1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
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
10 TUPLE: timestamp year month day hour minute second gmt-offset ;
12 C: <timestamp> timestamp
14 TUPLE: duration year month day hour minute second ;
16 C: <duration> duration
18 : gmt-offset-duration ( -- duration )
19 0 0 0 gmt-offset <duration> ;
21 : <date> ( year month day -- timestamp )
22 0 0 0 gmt-offset-duration <timestamp> ;
26 "Not a month" "January" "February" "March" "April" "May" "June"
27 "July" "August" "September" "October" "November" "December"
33 "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
38 "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
41 : day-abbreviations2 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" } ;
42 : day-abbreviations3 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" } ;
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
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 ]
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 -
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 ] |
72 e 153 m * 2 + 5 /i - 1+
75 : >date< ( timestamp -- year month day )
76 [ year>> ] [ month>> ] [ day>> ] tri ;
78 : >time< ( timestamp -- hour minute second )
79 [ hour>> ] [ minute>> ] [ second>> ] tri ;
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 ;
91 GENERIC: leap-year? ( obj -- ? )
93 M: integer leap-year? ( year -- ? )
94 dup 100 mod zero? 400 4 ? mod zero? ;
96 M: timestamp leap-year? ( timestamp -- ? )
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 )
108 : /rem ( f n -- q r )
109 #! q is positive or negative, r is positive from 0 <= r < n
110 [ / floor >integer ] 2keep rem ;
112 : float>whole-part ( float -- int float )
113 [ floor >integer ] keep over - ;
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 ;
119 : unless-zero >r dup zero? [ drop ] r> if ; inline
121 M: integer +year ( timestamp n -- timestamp )
122 [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
124 M: real +year ( timestamp n -- timestamp )
125 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
127 : months/years ( n -- months years )
128 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
130 M: integer +month ( timestamp n -- timestamp )
131 [ over month>> + months/years >r >>month r> +year ] unless-zero ;
133 M: real +month ( timestamp n -- timestamp )
134 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
136 M: integer +day ( timestamp n -- timestamp )
138 over >date< julian-day-number + julian-day-number>date
139 >r >r >>year r> >>month r> >>day
142 M: real +day ( timestamp n -- timestamp )
143 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
145 : hours/days ( n -- hours days )
148 M: integer +hour ( timestamp n -- timestamp )
149 [ over hour>> + hours/days >r >>hour r> +day ] unless-zero ;
151 M: real +hour ( timestamp n -- timestamp )
152 float>whole-part swapd 60 * +minute swap +hour ;
154 : minutes/hours ( n -- minutes hours )
157 M: integer +minute ( timestamp n -- timestamp )
158 [ over minute>> + minutes/hours >r >>minute r> +hour ] unless-zero ;
160 M: real +minute ( timestamp n -- timestamp )
161 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
163 : seconds/minutes ( n -- seconds minutes )
164 60 /rem swap >integer ;
166 M: number +second ( timestamp n -- timestamp )
167 [ over second>> + seconds/minutes >r >>second r> +minute ] unless-zero ;
170 [ second>> +second ] keep
171 [ minute>> +minute ] keep
172 [ hour>> +hour ] keep
174 [ month>> +month ] keep
175 [ year>> +year ] keep ; inline
177 : +slots [ bi@ + ] curry 2keep ; inline
181 GENERIC# time+ 1 ( time dt -- time )
184 >r clone r> (time+) drop ;
199 : dt>years ( dt -- x )
200 #! Uses average month/year length since dt loses calendar
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 / + ]
212 M: duration <=> [ dt>years ] compare ;
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 * ;
221 GENERIC: time- ( time1 time2 -- time )
223 : convert-timezone ( timestamp duration -- timestamp )
224 over gmt-offset>> over = [ drop ] [
225 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
228 : >local-time ( timestamp -- timestamp )
229 gmt-offset-duration convert-timezone ;
231 : >gmt ( timestamp -- timestamp )
232 instant convert-timezone ;
234 M: timestamp <=> ( ts1 ts2 -- n )
235 [ >gmt tuple-slots ] compare ;
237 : (time-) ( timestamp timestamp -- n )
239 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
240 [ >time< >r >r 3600 * r> 60 * r> + + ] bi@ - + ;
243 #! Exact calendar-time difference
246 : time* ( obj1 obj2 -- obj3 )
247 dup real? [ swap ] when
259 : before ( dt -- -dt )
265 MEMO: <zero> ( -- timestamp )
266 0 0 0 0 0 0 instant <timestamp> ;
268 : valid-timestamp? ( timestamp -- ? )
269 clone instant >>gmt-offset
270 dup <zero> time- <zero> time+ = ;
272 MEMO: unix-1970 ( -- timestamp )
273 1970 1 1 0 0 0 instant <timestamp> ;
275 : millis>timestamp ( n -- timestamp )
276 >r unix-1970 r> milliseconds time+ ;
278 : timestamp>millis ( timestamp -- n )
279 unix-1970 (time-) 1000 * >integer ;
281 : gmt ( -- timestamp )
282 #! GMT time, right now
283 unix-1970 millis milliseconds time+ ;
285 : now ( -- timestamp ) gmt >local-time ;
287 : hence ( dt -- timestamp ) now swap time+ ;
288 : ago ( dt -- timestamp ) now swap time- ;
290 : day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline
292 : zeller-congruence ( year month day -- n )
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>
301 GENERIC: days-in-year ( obj -- n )
303 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
304 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
306 : (days-in-month) ( year month -- n )
307 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
309 : days-in-month ( timestamp -- n )
310 >date< drop (days-in-month) ;
312 : day-of-week ( timestamp -- n )
313 >date< zeller-congruence ;
315 :: (day-of-year) ( year month day -- n )
316 day-counts month head-slice sum day +
318 year month day <date>
323 : day-of-year ( timestamp -- n )
324 >date< (day-of-year) ;
326 : day-offset ( timestamp m -- timestamp n )
327 over day-of-week - ; inline
329 : day-this-week ( timestamp n -- timestamp )
330 day-offset days time+ ;
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 ;
340 : beginning-of-day ( timestamp -- new-timestamp )
346 : beginning-of-month ( timestamp -- new-timestamp )
347 beginning-of-day 1 >>day ;
349 : beginning-of-week ( timestamp -- new-timestamp )
350 beginning-of-day sunday ;
352 : beginning-of-year ( timestamp -- new-timestamp )
353 beginning-of-month 1 >>month ;
355 : time-since-midnight ( timestamp -- duration )
356 dup beginning-of-day time- ;
358 M: timestamp sleep-until timestamp>millis sleep-until ;
360 M: duration sleep hence sleep-until ;
363 { [ os unix? ] [ "calendar.unix" ] }
364 { [ os windows? ] [ "calendar.windows" ] }