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 ;
8 HOOK: gmt-offset os ( -- hours minutes seconds )
18 C: <duration> duration
27 { gmt-offset duration } ;
29 C: <timestamp> timestamp
31 : gmt-offset-duration ( -- duration )
32 0 0 0 gmt-offset <duration> ;
34 : <date> ( year month day -- timestamp )
35 0 0 0 gmt-offset-duration <timestamp> ;
37 ERROR: not-a-month n ;
38 M: not-a-month summary
39 drop "Months are indexed starting at 1" ;
43 : check-month ( n -- n )
44 dup zero? [ not-a-month ] when ;
48 : month-names ( -- array )
50 "January" "February" "March" "April" "May" "June"
51 "July" "August" "September" "October" "November" "December"
54 : month-name ( n -- string )
55 check-month 1- month-names nth ;
57 CONSTANT: month-abbreviations
59 "Jan" "Feb" "Mar" "Apr" "May" "Jun"
60 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
63 : month-abbreviation ( n -- string )
64 check-month 1- month-abbreviations nth ;
66 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
68 : day-names ( -- array )
70 "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
73 : day-name ( n -- string ) day-names nth ;
75 CONSTANT: day-abbreviations2
76 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
78 : day-abbreviation2 ( n -- string )
79 day-abbreviations2 nth ; inline
81 CONSTANT: day-abbreviations3
82 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
84 : day-abbreviation3 ( n -- string )
85 day-abbreviations3 nth ; inline
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
94 :: julian-day-number ( year month day -- n )
95 #! Returns a composite date number
96 #! Not valid before year -4800
99 month 12 a * + 3 - :> m
101 day 153 m * 2 + 5 /i + 365 y * +
102 y 4 /i + y 100 /i - y 400 /i + 32045 - ;
104 :: julian-day-number>date ( n -- year month day )
105 #! Inverse of julian-day-number
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
116 e 153 m * 2 + 5 /i - 1+ ;
118 GENERIC: easter ( obj -- obj' )
120 :: easter-month-day ( year -- month day )
122 year 100 /mod :> c :> b
126 19 a * b + d - g - 15 + 30 mod :> h
128 32 2 e * + 2 i * + h - k - 7 mod :> l
129 a 11 h * + 22 l * + 451 /i :> m
131 h l + 7 m * - 114 + 31 /mod 1 + :> day :> month
134 M: integer easter ( year -- timestamp )
135 dup easter-month-day <date> ;
137 M: timestamp easter ( timestamp -- timestamp )
139 dup year>> easter-month-day
140 swapd >>day swap >>month ;
142 : >date< ( timestamp -- year month day )
143 [ year>> ] [ month>> ] [ day>> ] tri ;
145 : >time< ( timestamp -- hour minute second )
146 [ hour>> ] [ minute>> ] [ second>> ] tri ;
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 ;
160 GENERIC: leap-year? ( obj -- ? )
162 M: integer leap-year? ( year -- ? )
163 dup 100 divisor? 400 4 ? divisor? ;
165 M: timestamp leap-year? ( timestamp -- ? )
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 )
177 : /rem ( f n -- q r )
178 #! q is positive or negative, r is positive from 0 <= r < n
179 [ / floor >integer ] 2keep rem ;
181 : float>whole-part ( float -- int float )
182 [ floor >integer ] keep over - ;
184 : adjust-leap-year ( timestamp -- timestamp )
186 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
187 [ 3 >>month 1 >>day ] when ;
189 : unless-zero ( n quot -- )
190 [ dup zero? [ drop ] ] dip if ; inline
192 M: integer +year ( timestamp n -- timestamp )
193 [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
195 M: real +year ( timestamp n -- timestamp )
196 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
198 : months/years ( n -- months years )
199 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
201 M: integer +month ( timestamp n -- timestamp )
202 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
204 M: real +month ( timestamp n -- timestamp )
205 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
207 M: integer +day ( timestamp n -- timestamp )
209 over >date< julian-day-number + julian-day-number>date
210 [ >>year ] [ >>month ] [ >>day ] tri*
213 M: real +day ( timestamp n -- timestamp )
214 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
216 : hours/days ( n -- hours days )
219 M: integer +hour ( timestamp n -- timestamp )
220 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
222 M: real +hour ( timestamp n -- timestamp )
223 float>whole-part swapd 60 * +minute swap +hour ;
225 : minutes/hours ( n -- minutes hours )
228 M: integer +minute ( timestamp n -- timestamp )
229 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
231 M: real +minute ( timestamp n -- timestamp )
232 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
234 : seconds/minutes ( n -- seconds minutes )
235 60 /rem swap >integer ;
237 M: number +second ( timestamp n -- timestamp )
238 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
240 : (time+) ( timestamp duration -- timestamp' duration )
241 [ second>> +second ] keep
242 [ minute>> +minute ] keep
243 [ hour>> +hour ] keep
245 [ month>> +month ] keep
246 [ year>> +year ] keep ; inline
248 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
249 [ bi@ + ] curry 2keep ; inline
253 GENERIC# time+ 1 ( time1 time2 -- time3 )
256 [ clone ] dip (time+) drop ;
271 : duration>years ( duration -- x )
272 #! Uses average month/year length since duration loses calendar
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 / + ]
284 M: duration <=> [ duration>years ] compare ;
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 * ;
295 GENERIC: time- ( time1 time2 -- time3 )
297 : convert-timezone ( timestamp duration -- timestamp )
298 over gmt-offset>> over = [ drop ] [
299 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
302 : >local-time ( timestamp -- timestamp )
303 gmt-offset-duration convert-timezone ;
305 : >gmt ( timestamp -- timestamp )
306 instant convert-timezone ;
308 M: timestamp <=> ( ts1 ts2 -- n )
309 [ >gmt tuple-slots ] compare ;
311 : (time-) ( timestamp timestamp -- n )
313 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
314 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
317 #! Exact calendar-time difference
320 : time* ( obj1 obj2 -- obj3 )
321 dup real? [ swap ] when
333 : before ( duration -- -duration )
339 : <zero> ( -- timestamp )
340 0 0 0 0 0 0 instant <timestamp> ;
342 : valid-timestamp? ( timestamp -- ? )
343 clone instant >>gmt-offset
344 dup <zero> time- <zero> time+ = ;
346 : unix-1970 ( -- timestamp )
347 1970 1 1 0 0 0 instant <timestamp> ;
349 : millis>timestamp ( x -- timestamp )
350 [ unix-1970 ] dip milliseconds time+ ;
352 : timestamp>millis ( timestamp -- n )
353 unix-1970 (time-) 1000 * >integer ;
355 : micros>timestamp ( x -- timestamp )
356 [ unix-1970 ] dip microseconds time+ ;
358 : timestamp>micros ( timestamp -- n )
359 unix-1970 (time-) 1000000 * >integer ;
361 : gmt ( -- timestamp )
362 #! GMT time, right now
363 unix-1970 micros microseconds time+ ;
365 : now ( -- timestamp ) gmt >local-time ;
366 : hence ( duration -- timestamp ) now swap time+ ;
367 : ago ( duration -- timestamp ) now swap time- ;
369 : zeller-congruence ( year month day -- n )
371 #! http://web.textfiles.com/computers/formulas.txt
372 #! good for any date since October 15, 1582
374 dup 2 <= [ [ 1- ] [ 12 + ] bi* ] when
375 [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
376 [ 1+ 3 * 5 /i + ] keep 2 * +
379 GENERIC: days-in-year ( obj -- n )
381 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
382 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
384 : (days-in-month) ( year month -- n )
385 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
387 : days-in-month ( timestamp -- n )
388 >date< drop (days-in-month) ;
390 : day-of-week ( timestamp -- n )
391 >date< zeller-congruence ;
393 :: (day-of-year) ( year month day -- n )
394 day-counts month head-slice sum day +
396 year month day <date>
401 : day-of-year ( timestamp -- n )
402 >date< (day-of-year) ;
405 : day-offset ( timestamp m -- timestamp n )
406 over day-of-week - ; inline
408 : day-this-week ( timestamp n -- timestamp )
409 day-offset days time+ ;
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 ;
420 : midnight ( timestamp -- new-timestamp )
421 clone 0 >>hour 0 >>minute 0 >>second ; inline
423 : noon ( timestamp -- new-timestamp )
424 midnight 12 >>hour ; inline
426 : beginning-of-month ( timestamp -- new-timestamp )
429 : beginning-of-week ( timestamp -- new-timestamp )
432 : beginning-of-year ( timestamp -- new-timestamp )
433 beginning-of-month 1 >>month ;
435 : time-since-midnight ( timestamp -- duration )
438 : since-1970 ( duration -- timestamp )
439 unix-1970 time+ >local-time ;
441 M: timestamp sleep-until timestamp>micros sleep-until ;
443 M: duration sleep hence sleep-until ;
446 { [ os unix? ] [ "calendar.unix" ] }
447 { [ os windows? ] [ "calendar.windows" ] }