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 :: julian-day-number ( year month day -- n )
93 #! Returns a composite date number
94 #! Not valid before year -4800
95 [let* | a [ 14 month - 12 /i ]
97 m [ month 12 a * + 3 - ] |
98 day 153 m * 2 + 5 /i + 365 y * +
99 y 4 /i + y 100 /i - y 400 /i + 32045 -
102 :: julian-day-number>date ( n -- year month day )
103 #! Inverse of julian-day-number
104 [let* | a [ n 32044 + ]
105 b [ 4 a * 3 + 146097 /i ]
106 c [ a 146097 b * 4 /i - ]
107 d [ 4 c * 3 + 1461 /i ]
108 e [ c 1461 d * 4 /i - ]
109 m [ 5 e * 2 + 153 /i ] |
113 e 153 m * 2 + 5 /i - 1+
116 : >date< ( timestamp -- year month day )
117 [ year>> ] [ month>> ] [ day>> ] tri ;
119 : >time< ( timestamp -- hour minute second )
120 [ hour>> ] [ minute>> ] [ second>> ] tri ;
122 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
123 : years ( x -- duration ) instant clone swap >>year ;
124 : months ( x -- duration ) instant clone swap >>month ;
125 : days ( x -- duration ) instant clone swap >>day ;
126 : weeks ( x -- duration ) 7 * days ;
127 : hours ( x -- duration ) instant clone swap >>hour ;
128 : minutes ( x -- duration ) instant clone swap >>minute ;
129 : seconds ( x -- duration ) instant clone swap >>second ;
130 : milliseconds ( x -- duration ) 1000 / seconds ;
131 : microseconds ( x -- duration ) 1000000 / seconds ;
132 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
134 GENERIC: leap-year? ( obj -- ? )
136 M: integer leap-year? ( year -- ? )
137 dup 100 mod zero? 400 4 ? mod zero? ;
139 M: timestamp leap-year? ( timestamp -- ? )
144 GENERIC: +year ( timestamp x -- timestamp )
145 GENERIC: +month ( timestamp x -- timestamp )
146 GENERIC: +day ( timestamp x -- timestamp )
147 GENERIC: +hour ( timestamp x -- timestamp )
148 GENERIC: +minute ( timestamp x -- timestamp )
149 GENERIC: +second ( timestamp x -- timestamp )
151 : /rem ( f n -- q r )
152 #! q is positive or negative, r is positive from 0 <= r < n
153 [ / floor >integer ] 2keep rem ;
155 : float>whole-part ( float -- int float )
156 [ floor >integer ] keep over - ;
158 : adjust-leap-year ( timestamp -- timestamp )
160 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
161 [ 3 >>month 1 >>day ] when ;
163 : unless-zero ( n quot -- )
164 [ dup zero? [ drop ] ] dip if ; inline
166 M: integer +year ( timestamp n -- timestamp )
167 [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
169 M: real +year ( timestamp n -- timestamp )
170 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
172 : months/years ( n -- months years )
173 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
175 M: integer +month ( timestamp n -- timestamp )
176 [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
178 M: real +month ( timestamp n -- timestamp )
179 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
181 M: integer +day ( timestamp n -- timestamp )
183 over >date< julian-day-number + julian-day-number>date
184 [ >>year ] [ >>month ] [ >>day ] tri*
187 M: real +day ( timestamp n -- timestamp )
188 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
190 : hours/days ( n -- hours days )
193 M: integer +hour ( timestamp n -- timestamp )
194 [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
196 M: real +hour ( timestamp n -- timestamp )
197 float>whole-part swapd 60 * +minute swap +hour ;
199 : minutes/hours ( n -- minutes hours )
202 M: integer +minute ( timestamp n -- timestamp )
203 [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
205 M: real +minute ( timestamp n -- timestamp )
206 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
208 : seconds/minutes ( n -- seconds minutes )
209 60 /rem swap >integer ;
211 M: number +second ( timestamp n -- timestamp )
212 [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
214 : (time+) ( timestamp duration -- timestamp' duration )
215 [ second>> +second ] keep
216 [ minute>> +minute ] keep
217 [ hour>> +hour ] keep
219 [ month>> +month ] keep
220 [ year>> +year ] keep ; inline
222 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
223 [ bi@ + ] curry 2keep ; inline
227 GENERIC# time+ 1 ( time1 time2 -- time3 )
230 [ clone ] dip (time+) drop ;
245 : duration>years ( duration -- x )
246 #! Uses average month/year length since duration loses calendar
251 [ month>> months-per-year / + ]
252 [ day>> days-per-year / + ]
253 [ hour>> hours-per-year / + ]
254 [ minute>> minutes-per-year / + ]
255 [ second>> seconds-per-year / + ]
258 M: duration <=> [ duration>years ] compare ;
260 : duration>months ( duration -- x ) duration>years months-per-year * ;
261 : duration>days ( duration -- x ) duration>years days-per-year * ;
262 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
263 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
264 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
265 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
266 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
267 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
269 GENERIC: time- ( time1 time2 -- time3 )
271 : convert-timezone ( timestamp duration -- timestamp )
272 over gmt-offset>> over = [ drop ] [
273 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
276 : >local-time ( timestamp -- timestamp )
277 gmt-offset-duration convert-timezone ;
279 : >gmt ( timestamp -- timestamp )
280 instant convert-timezone ;
282 M: timestamp <=> ( ts1 ts2 -- n )
283 [ >gmt tuple-slots ] compare ;
285 : (time-) ( timestamp timestamp -- n )
287 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
288 [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
291 #! Exact calendar-time difference
294 : time* ( obj1 obj2 -- obj3 )
295 dup real? [ swap ] when
307 : before ( duration -- -duration )
313 : <zero> ( -- timestamp )
314 0 0 0 0 0 0 instant <timestamp> ;
316 : valid-timestamp? ( timestamp -- ? )
317 clone instant >>gmt-offset
318 dup <zero> time- <zero> time+ = ;
320 : unix-1970 ( -- timestamp )
321 1970 1 1 0 0 0 instant <timestamp> ;
323 : millis>timestamp ( x -- timestamp )
324 [ unix-1970 ] dip milliseconds time+ ;
326 : timestamp>millis ( timestamp -- n )
327 unix-1970 (time-) 1000 * >integer ;
329 : micros>timestamp ( x -- timestamp )
330 [ unix-1970 ] dip microseconds time+ ;
332 : timestamp>micros ( timestamp -- n )
333 unix-1970 (time-) 1000000 * >integer ;
335 : gmt ( -- timestamp )
336 #! GMT time, right now
337 unix-1970 micros microseconds time+ ;
339 : now ( -- timestamp ) gmt >local-time ;
340 : hence ( duration -- timestamp ) now swap time+ ;
341 : ago ( duration -- timestamp ) now swap time- ;
343 : zeller-congruence ( year month day -- n )
345 #! http://web.textfiles.com/computers/formulas.txt
346 #! good for any date since October 15, 1582
348 dup 2 <= [ [ 1- ] [ 12 + ] bi* ] when
349 [ dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + ] dip
350 [ 1+ 3 * 5 /i + ] keep 2 * +
353 GENERIC: days-in-year ( obj -- n )
355 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
356 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
358 : (days-in-month) ( year month -- n )
359 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
361 : days-in-month ( timestamp -- n )
362 >date< drop (days-in-month) ;
364 : day-of-week ( timestamp -- n )
365 >date< zeller-congruence ;
367 :: (day-of-year) ( year month day -- n )
368 day-counts month head-slice sum day +
370 year month day <date>
375 : day-of-year ( timestamp -- n )
376 >date< (day-of-year) ;
379 : day-offset ( timestamp m -- timestamp n )
380 over day-of-week - ; inline
382 : day-this-week ( timestamp n -- timestamp )
383 day-offset days time+ ;
386 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
387 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
388 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
389 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
390 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
391 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
392 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
394 : midnight ( timestamp -- new-timestamp )
395 clone 0 >>hour 0 >>minute 0 >>second ; inline
397 : noon ( timestamp -- new-timestamp )
398 midnight 12 >>hour ; inline
400 : beginning-of-month ( timestamp -- new-timestamp )
403 : beginning-of-week ( timestamp -- new-timestamp )
406 : beginning-of-year ( timestamp -- new-timestamp )
407 beginning-of-month 1 >>month ;
409 : time-since-midnight ( timestamp -- duration )
412 : since-1970 ( duration -- timestamp )
413 unix-1970 time+ >local-time ;
415 M: timestamp sleep-until timestamp>micros sleep-until ;
417 M: duration sleep hence sleep-until ;
420 { [ os unix? ] [ "calendar.unix" ] }
421 { [ os windows? ] [ "calendar.windows" ] }