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
6 combinators.short-circuit ;
9 HOOK: gmt-offset os ( -- hours minutes seconds )
19 C: <duration> duration
28 { gmt-offset duration } ;
30 C: <timestamp> timestamp
32 : gmt-offset-duration ( -- duration )
33 0 0 0 gmt-offset <duration> ;
35 : <date> ( year month day -- timestamp )
36 0 0 0 gmt-offset-duration <timestamp> ;
38 ERROR: not-a-month n ;
39 M: not-a-month summary
40 drop "Months are indexed starting at 1" ;
43 : check-month ( n -- n )
44 dup zero? [ not-a-month ] when ;
47 : month-names ( -- array )
49 "January" "February" "March" "April" "May" "June"
50 "July" "August" "September" "October" "November" "December"
53 : month-name ( n -- string )
54 check-month 1- month-names nth ;
56 : month-abbreviations ( -- array )
58 "Jan" "Feb" "Mar" "Apr" "May" "Jun"
59 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
62 : month-abbreviation ( n -- string )
63 check-month 1- month-abbreviations nth ;
65 : day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline
67 : day-names ( -- array )
69 "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
72 : day-name ( n -- string ) day-names nth ;
74 : day-abbreviations2 ( -- array )
75 { "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" } ;
77 : day-abbreviation2 ( n -- string )
78 day-abbreviations2 nth ;
80 : day-abbreviations3 ( -- array )
81 { "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" } ;
83 : day-abbreviation3 ( n -- string )
84 day-abbreviations3 nth ;
86 : average-month ( -- ratio ) 30+5/12 ; inline
87 : months-per-year ( -- integer ) 12 ; inline
88 : days-per-year ( -- ratio ) 3652425/10000 ; inline
89 : hours-per-year ( -- ratio ) 876582/100 ; inline
90 : minutes-per-year ( -- ratio ) 5259492/10 ; inline
91 : seconds-per-year ( -- integer ) 31556952 ; inline
93 :: julian-day-number ( year month day -- n )
94 #! Returns a composite date number
95 #! Not valid before year -4800
96 [let* | a [ 14 month - 12 /i ]
98 m [ month 12 a * + 3 - ] |
99 day 153 m * 2 + 5 /i + 365 y * +
100 y 4 /i + y 100 /i - y 400 /i + 32045 -
103 :: julian-day-number>date ( n -- year month day )
104 #! Inverse of julian-day-number
105 [let* | a [ n 32044 + ]
106 b [ 4 a * 3 + 146097 /i ]
107 c [ a 146097 b * 4 /i - ]
108 d [ 4 c * 3 + 1461 /i ]
109 e [ c 1461 d * 4 /i - ]
110 m [ 5 e * 2 + 153 /i ] |
114 e 153 m * 2 + 5 /i - 1+
117 : >date< ( timestamp -- year month day )
118 [ year>> ] [ month>> ] [ day>> ] tri ;
120 : >time< ( timestamp -- hour minute second )
121 [ hour>> ] [ minute>> ] [ second>> ] tri ;
123 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
124 : years ( x -- duration ) instant clone swap >>year ;
125 : months ( x -- duration ) instant clone swap >>month ;
126 : days ( x -- duration ) instant clone swap >>day ;
127 : weeks ( x -- duration ) 7 * days ;
128 : hours ( x -- duration ) instant clone swap >>hour ;
129 : minutes ( x -- duration ) instant clone swap >>minute ;
130 : seconds ( x -- duration ) instant clone swap >>second ;
131 : milliseconds ( x -- duration ) 1000 / seconds ;
133 GENERIC: leap-year? ( obj -- ? )
135 M: integer leap-year? ( year -- ? )
136 dup 100 mod zero? 400 4 ? mod zero? ;
138 M: timestamp leap-year? ( timestamp -- ? )
143 GENERIC: +year ( timestamp x -- timestamp )
144 GENERIC: +month ( timestamp x -- timestamp )
145 GENERIC: +day ( timestamp x -- timestamp )
146 GENERIC: +hour ( timestamp x -- timestamp )
147 GENERIC: +minute ( timestamp x -- timestamp )
148 GENERIC: +second ( timestamp x -- timestamp )
150 : /rem ( f n -- q r )
151 #! q is positive or negative, r is positive from 0 <= r < n
152 [ / floor >integer ] 2keep rem ;
154 : float>whole-part ( float -- int float )
155 [ floor >integer ] keep over - ;
157 : adjust-leap-year ( timestamp -- timestamp )
159 { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
160 [ 3 >>month 1 >>day ] when ;
162 : unless-zero ( n quot -- )
163 [ dup zero? [ drop ] ] dip if ; inline
165 M: integer +year ( timestamp n -- timestamp )
166 [ [ + ] curry change-year adjust-leap-year ] unless-zero ;
168 M: real +year ( timestamp n -- timestamp )
169 [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
171 : months/years ( n -- months years )
172 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
174 M: integer +month ( timestamp n -- timestamp )
175 [ over month>> + months/years >r >>month r> +year ] unless-zero ;
177 M: real +month ( timestamp n -- timestamp )
178 [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
180 M: integer +day ( timestamp n -- timestamp )
182 over >date< julian-day-number + julian-day-number>date
183 >r >r >>year r> >>month r> >>day
186 M: real +day ( timestamp n -- timestamp )
187 [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
189 : hours/days ( n -- hours days )
192 M: integer +hour ( timestamp n -- timestamp )
193 [ over hour>> + hours/days >r >>hour r> +day ] unless-zero ;
195 M: real +hour ( timestamp n -- timestamp )
196 float>whole-part swapd 60 * +minute swap +hour ;
198 : minutes/hours ( n -- minutes hours )
201 M: integer +minute ( timestamp n -- timestamp )
202 [ over minute>> + minutes/hours >r >>minute r> +hour ] unless-zero ;
204 M: real +minute ( timestamp n -- timestamp )
205 [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
207 : seconds/minutes ( n -- seconds minutes )
208 60 /rem swap >integer ;
210 M: number +second ( timestamp n -- timestamp )
211 [ over second>> + seconds/minutes >r >>second r> +minute ] unless-zero ;
214 [ second>> +second ] keep
215 [ minute>> +minute ] keep
216 [ hour>> +hour ] keep
218 [ month>> +month ] keep
219 [ year>> +year ] keep ; inline
221 : +slots [ bi@ + ] curry 2keep ; inline
225 GENERIC# time+ 1 ( time1 time2 -- time3 )
228 >r clone r> (time+) drop ;
243 : duration>years ( duration -- x )
244 #! Uses average month/year length since duration loses calendar
249 [ month>> months-per-year / + ]
250 [ day>> days-per-year / + ]
251 [ hour>> hours-per-year / + ]
252 [ minute>> minutes-per-year / + ]
253 [ second>> seconds-per-year / + ]
256 M: duration <=> [ duration>years ] compare ;
258 : duration>months ( duration -- x ) duration>years months-per-year * ;
259 : duration>days ( duration -- x ) duration>years days-per-year * ;
260 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
261 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
262 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
263 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
265 GENERIC: time- ( time1 time2 -- time3 )
267 : convert-timezone ( timestamp duration -- timestamp )
268 over gmt-offset>> over = [ drop ] [
269 [ over gmt-offset>> time- time+ ] keep >>gmt-offset
272 : >local-time ( timestamp -- timestamp )
273 gmt-offset-duration convert-timezone ;
275 : >gmt ( timestamp -- timestamp )
276 instant convert-timezone ;
278 M: timestamp <=> ( ts1 ts2 -- n )
279 [ >gmt tuple-slots ] compare ;
281 : (time-) ( timestamp timestamp -- n )
283 [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
284 [ >time< >r >r 3600 * r> 60 * r> + + ] bi@ - + ;
287 #! Exact calendar-time difference
290 : time* ( obj1 obj2 -- obj3 )
291 dup real? [ swap ] when
303 : before ( duration -- -duration )
309 : <zero> ( -- timestamp )
310 0 0 0 0 0 0 instant <timestamp> ;
312 : valid-timestamp? ( timestamp -- ? )
313 clone instant >>gmt-offset
314 dup <zero> time- <zero> time+ = ;
316 : unix-1970 ( -- timestamp )
317 1970 1 1 0 0 0 instant <timestamp> ;
319 : millis>timestamp ( x -- timestamp )
320 >r unix-1970 r> milliseconds time+ ;
322 : timestamp>millis ( timestamp -- n )
323 unix-1970 (time-) 1000 * >integer ;
325 : gmt ( -- timestamp )
326 #! GMT time, right now
327 unix-1970 millis milliseconds time+ ;
329 : now ( -- timestamp ) gmt >local-time ;
330 : hence ( duration -- timestamp ) now swap time+ ;
331 : ago ( duration -- timestamp ) now swap time- ;
333 : zeller-congruence ( year month day -- n )
335 #! http://web.textfiles.com/computers/formulas.txt
336 #! good for any date since October 15, 1582
337 >r dup 2 <= [ 12 + >r 1- r> ] when
338 >r dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + r>
339 [ 1+ 3 * 5 /i + ] keep 2 * + r>
342 GENERIC: days-in-year ( obj -- n )
344 M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
345 M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
347 : (days-in-month) ( year month -- n )
348 dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
350 : days-in-month ( timestamp -- n )
351 >date< drop (days-in-month) ;
353 : day-of-week ( timestamp -- n )
354 >date< zeller-congruence ;
356 :: (day-of-year) ( year month day -- n )
357 day-counts month head-slice sum day +
359 year month day <date>
364 : day-of-year ( timestamp -- n )
365 >date< (day-of-year) ;
368 : day-offset ( timestamp m -- timestamp n )
369 over day-of-week - ; inline
371 : day-this-week ( timestamp n -- timestamp )
372 day-offset days time+ ;
375 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
376 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
377 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
378 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
379 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
380 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
381 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
383 : midnight ( timestamp -- new-timestamp )
384 clone 0 >>hour 0 >>minute 0 >>second ; inline
386 : noon ( timestamp -- new-timestamp )
387 midnight 12 >>hour ; inline
389 : beginning-of-month ( timestamp -- new-timestamp )
392 : beginning-of-week ( timestamp -- new-timestamp )
395 : beginning-of-year ( timestamp -- new-timestamp )
396 beginning-of-month 1 >>month ;
398 : time-since-midnight ( timestamp -- duration )
401 M: timestamp sleep-until timestamp>millis sleep-until ;
403 M: duration sleep hence sleep-until ;
406 { [ os unix? ] [ "calendar.unix" ] }
407 { [ os windows? ] [ "calendar.windows" ] }