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 ;
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" ;
42 : check-month ( n -- n )
43 dup zero? [ not-a-month ] when ;
46 : month-names ( -- array )
48 "January" "February" "March" "April" "May" "June"
49 "July" "August" "September" "October" "November" "December"
52 : month-name ( n -- string )
53 check-month 1- month-names nth ;
55 : month-abbreviations ( -- array )
57 "Jan" "Feb" "Mar" "Apr" "May" "Jun"
58 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
61 : month-abbreviation ( n -- string )
62 check-month 1- month-abbreviations nth ;
64 : day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline
66 : day-names ( -- array )
68 "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
71 : day-name ( n -- string ) day-names nth ;
73 : day-abbreviations2 ( -- array )
74 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" } ;
76 : day-abbreviation2 ( n -- string )
77 day-abbreviations2 nth ;
79 : day-abbreviations3 ( -- array )
80 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" } ;
82 : day-abbreviation3 ( n -- string )
83 day-abbreviations3 nth ;
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
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
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 -
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 ] |
120 e 153 m * 2 + 5 /i - 1+
123 : >date< ( timestamp -- year month day )
124 [ year>> ] [ month>> ] [ day>> ] tri ;
126 : >time< ( timestamp -- hour minute second )
127 [ hour>> ] [ minute>> ] [ second>> ] tri ;
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 ;
141 GENERIC: leap-year? ( obj -- ? )
143 M: integer leap-year? ( year -- ? )
144 dup 100 mod zero? 400 4 ? mod zero? ;
146 M: timestamp leap-year? ( timestamp -- ? )
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 )
158 : /rem ( f n -- q r )
159 #! q is positive or negative, r is positive from 0 <= r < n
160 [ / floor >integer ] 2keep rem ;
162 : float>whole-part ( float -- int float )
163 [ floor >integer ] keep over - ;
165 : adjust-leap-year ( timestamp -- timestamp )
167 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
168 [ 3 >>month 1 >>day ] when ;
170 : unless-zero ( n quot -- )
171 [ dup zero? [ drop ] ] dip if ; inline
173 M: integer +year ( timestamp n -- timestamp )
174 [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
176 M: real +year ( timestamp n -- timestamp )
177 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
179 : months/years ( n -- months years )
180 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
182 M: integer +month ( timestamp n -- timestamp )
183 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
185 M: real +month ( timestamp n -- timestamp )
186 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
188 M: integer +day ( timestamp n -- timestamp )
190 over >date< julian-day-number + julian-day-number>date
191 [ >>year ] [ >>month ] [ >>day ] tri*
194 M: real +day ( timestamp n -- timestamp )
195 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
197 : hours/days ( n -- hours days )
200 M: integer +hour ( timestamp n -- timestamp )
201 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
203 M: real +hour ( timestamp n -- timestamp )
204 float>whole-part swapd 60 * +minute swap +hour ;
206 : minutes/hours ( n -- minutes hours )
209 M: integer +minute ( timestamp n -- timestamp )
210 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
212 M: real +minute ( timestamp n -- timestamp )
213 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
215 : seconds/minutes ( n -- seconds minutes )
216 60 /rem swap >integer ;
218 M: number +second ( timestamp n -- timestamp )
219 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
222 [ second>> +second ] keep
223 [ minute>> +minute ] keep
224 [ hour>> +hour ] keep
226 [ month>> +month ] keep
227 [ year>> +year ] keep ; inline
229 : +slots [ bi@ + ] curry 2keep ; inline
233 GENERIC# time+ 1 ( time1 time2 -- time3 )
236 [ clone ] dip (time+) drop ;
251 : duration>years ( duration -- x )
252 #! Uses average month/year length since duration loses calendar
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 / + ]
264 M: duration <=> [ duration>years ] compare ;
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 * ;
275 GENERIC: time- ( time1 time2 -- time3 )
277 : convert-timezone ( timestamp duration -- timestamp )
278 over gmt-offset>> over = [ drop ] [
279 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
282 : >local-time ( timestamp -- timestamp )
283 gmt-offset-duration convert-timezone ;
285 : >gmt ( timestamp -- timestamp )
286 instant convert-timezone ;
288 M: timestamp <=> ( ts1 ts2 -- n )
289 [ >gmt tuple-slots ] compare ;
291 : (time-) ( timestamp timestamp -- n )
293 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
294 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
297 #! Exact calendar-time difference
300 : time* ( obj1 obj2 -- obj3 )
301 dup real? [ swap ] when
313 : before ( duration -- -duration )
319 : <zero> ( -- timestamp )
320 0 0 0 0 0 0 instant <timestamp> ;
322 : valid-timestamp? ( timestamp -- ? )
323 clone instant >>gmt-offset
324 dup <zero> time- <zero> time+ = ;
326 : unix-1970 ( -- timestamp )
327 1970 1 1 0 0 0 instant <timestamp> ;
329 : millis>timestamp ( x -- timestamp )
330 [ unix-1970 ] dip milliseconds time+ ;
332 : timestamp>millis ( timestamp -- n )
333 unix-1970 (time-) 1000 * >integer ;
335 : micros>timestamp ( x -- timestamp )
336 [ unix-1970 ] dip microseconds time+ ;
338 : timestamp>micros ( timestamp -- n )
339 unix-1970 (time-) 1000000 * >integer ;
341 : gmt ( -- timestamp )
342 #! GMT time, right now
343 unix-1970 micros microseconds time+ ;
345 : now ( -- timestamp ) gmt >local-time ;
346 : hence ( duration -- timestamp ) now swap time+ ;
347 : ago ( duration -- timestamp ) now swap time- ;
349 : zeller-congruence ( year month day -- n )
351 #! http://web.textfiles.com/computers/formulas.txt
352 #! good for any date since October 15, 1582
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 * +
359 GENERIC: days-in-year ( obj -- n )
361 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
362 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
364 : (days-in-month) ( year month -- n )
365 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
367 : days-in-month ( timestamp -- n )
368 >date< drop (days-in-month) ;
370 : day-of-week ( timestamp -- n )
371 >date< zeller-congruence ;
373 :: (day-of-year) ( year month day -- n )
374 day-counts month head-slice sum day +
376 year month day <date>
381 : day-of-year ( timestamp -- n )
382 >date< (day-of-year) ;
385 : day-offset ( timestamp m -- timestamp n )
386 over day-of-week - ; inline
388 : day-this-week ( timestamp n -- timestamp )
389 day-offset days time+ ;
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 ;
400 : midnight ( timestamp -- new-timestamp )
401 clone 0 >>hour 0 >>minute 0 >>second ; inline
403 : noon ( timestamp -- new-timestamp )
404 midnight 12 >>hour ; inline
406 : beginning-of-month ( timestamp -- new-timestamp )
409 : beginning-of-week ( timestamp -- new-timestamp )
412 : beginning-of-year ( timestamp -- new-timestamp )
413 beginning-of-month 1 >>month ;
415 : time-since-midnight ( timestamp -- duration )
418 : since-1970 ( duration -- timestamp )
419 unix-1970 time+ >local-time ;
421 M: timestamp sleep-until timestamp>micros sleep-until ;
423 M: duration sleep hence sleep-until ;
426 { [ os unix? ] [ "calendar.unix" ] }
427 { [ os windows? ] [ "calendar.windows" ] }