factor/basis/calendar/calendar.factor

409 lines
11 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2007 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2008-02-26 18:22:48 -05:00
USING: arrays kernel math math.functions namespaces sequences
2008-09-01 12:48:22 -04:00
strings system vocabs.loader threads accessors combinators
locals classes.tuple math.order summary
combinators.short-circuit ;
2007-09-20 18:09:08 -04:00
IN: calendar
2008-09-01 12:48:22 -04:00
HOOK: gmt-offset os ( -- hours minutes seconds )
TUPLE: duration
{ year real }
{ month real }
{ day real }
{ hour real }
{ minute real }
{ second real } ;
2007-09-20 18:09:08 -04:00
C: <duration> duration
2007-09-20 18:09:08 -04:00
TUPLE: timestamp
{ year integer }
{ month integer }
{ day integer }
{ hour integer }
{ minute integer }
{ second real }
{ gmt-offset duration } ;
2008-02-26 18:22:48 -05:00
C: <timestamp> timestamp
2007-09-20 18:09:08 -04:00
: gmt-offset-duration ( -- duration )
0 0 0 gmt-offset <duration> ;
: <date> ( year month day -- timestamp )
0 0 0 gmt-offset-duration <timestamp> ;
ERROR: not-a-month n ;
M: not-a-month summary
drop "Months are indexed starting at 1" ;
<PRIVATE
: check-month ( n -- n )
dup zero? [ not-a-month ] when ;
PRIVATE>
: month-names ( -- array )
2007-09-20 18:09:08 -04:00
{
"January" "February" "March" "April" "May" "June"
2007-09-20 18:09:08 -04:00
"July" "August" "September" "October" "November" "December"
} ;
: month-name ( n -- string )
check-month 1- month-names nth ;
: month-abbreviations ( -- array )
2007-09-20 18:09:08 -04:00
{
"Jan" "Feb" "Mar" "Apr" "May" "Jun"
"Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
2007-09-20 18:09:08 -04:00
} ;
: month-abbreviation ( n -- string )
check-month 1- month-abbreviations nth ;
2008-08-31 22:20:56 -04:00
: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline
: day-names ( -- array )
2007-09-20 18:09:08 -04:00
{
"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
} ;
: day-name ( n -- string ) day-names nth ;
: day-abbreviations2 ( -- array )
{ "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" } ;
: day-abbreviation2 ( n -- string )
day-abbreviations2 nth ;
: day-abbreviations3 ( -- array )
{ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" } ;
: day-abbreviation3 ( n -- string )
day-abbreviations3 nth ;
2007-09-20 18:09:08 -04:00
: average-month ( -- ratio ) 30+5/12 ; inline
: months-per-year ( -- integer ) 12 ; inline
: days-per-year ( -- ratio ) 3652425/10000 ; inline
: hours-per-year ( -- ratio ) 876582/100 ; inline
: minutes-per-year ( -- ratio ) 5259492/10 ; inline
: seconds-per-year ( -- integer ) 31556952 ; inline
2007-09-20 18:09:08 -04:00
2008-03-19 22:41:39 -04:00
:: julian-day-number ( year month day -- n )
2007-09-20 18:09:08 -04:00
#! Returns a composite date number
#! Not valid before year -4800
2008-03-19 22:41:39 -04:00
[let* | a [ 14 month - 12 /i ]
y [ year 4800 + a - ]
m [ month 12 a * + 3 - ] |
day 153 m * 2 + 5 /i + 365 y * +
y 4 /i + y 100 /i - y 400 /i + 32045 -
] ;
:: julian-day-number>date ( n -- year month day )
2007-09-20 18:09:08 -04:00
#! Inverse of julian-day-number
2008-03-19 22:41:39 -04:00
[let* | a [ n 32044 + ]
b [ 4 a * 3 + 146097 /i ]
c [ a 146097 b * 4 /i - ]
d [ 4 c * 3 + 1461 /i ]
e [ c 1461 d * 4 /i - ]
m [ 5 e * 2 + 153 /i ] |
100 b * d + 4800 -
m 10 /i + m 3 +
12 m 10 /i * -
e 153 m * 2 + 5 /i - 1+
] ;
2007-09-20 18:09:08 -04:00
: >date< ( timestamp -- year month day )
[ year>> ] [ month>> ] [ day>> ] tri ;
2007-09-20 18:09:08 -04:00
: >time< ( timestamp -- hour minute second )
[ hour>> ] [ minute>> ] [ second>> ] tri ;
2008-02-26 18:22:48 -05:00
2008-08-31 22:20:56 -04:00
: instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
2008-08-31 16:54:00 -04:00
: years ( x -- duration ) instant clone swap >>year ;
: months ( x -- duration ) instant clone swap >>month ;
: days ( x -- duration ) instant clone swap >>day ;
: weeks ( x -- duration ) 7 * days ;
: hours ( x -- duration ) instant clone swap >>hour ;
: minutes ( x -- duration ) instant clone swap >>minute ;
: seconds ( x -- duration ) instant clone swap >>second ;
: milliseconds ( x -- duration ) 1000 / seconds ;
2008-02-26 18:22:48 -05:00
GENERIC: leap-year? ( obj -- ? )
M: integer leap-year? ( year -- ? )
dup 100 mod zero? 400 4 ? mod zero? ;
2007-09-20 18:09:08 -04:00
2008-02-26 18:22:48 -05:00
M: timestamp leap-year? ( timestamp -- ? )
year>> leap-year? ;
<PRIVATE
2007-09-20 18:09:08 -04:00
GENERIC: +year ( timestamp x -- timestamp )
GENERIC: +month ( timestamp x -- timestamp )
GENERIC: +day ( timestamp x -- timestamp )
GENERIC: +hour ( timestamp x -- timestamp )
GENERIC: +minute ( timestamp x -- timestamp )
GENERIC: +second ( timestamp x -- timestamp )
: /rem ( f n -- q r )
#! q is positive or negative, r is positive from 0 <= r < n
2008-02-26 18:22:48 -05:00
[ / floor >integer ] 2keep rem ;
2007-09-20 18:09:08 -04:00
: float>whole-part ( float -- int float )
[ floor >integer ] keep over - ;
2007-09-20 18:09:08 -04:00
: adjust-leap-year ( timestamp -- timestamp )
dup
{ [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
2008-02-26 18:22:48 -05:00
[ 3 >>month 1 >>day ] when ;
: unless-zero ( n quot -- )
[ dup zero? [ drop ] ] dip if ; inline
2007-09-20 18:09:08 -04:00
M: integer +year ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ [ + ] curry change-year adjust-leap-year ] unless-zero ;
2007-09-20 18:09:08 -04:00
M: real +year ( timestamp n -- timestamp )
2008-02-26 21:03:35 -05:00
[ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
2008-02-26 18:22:48 -05:00
: months/years ( n -- months years )
12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
2007-09-20 18:09:08 -04:00
M: integer +month ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ over month>> + months/years >r >>month r> +year ] unless-zero ;
2007-09-20 18:09:08 -04:00
M: real +month ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
2007-09-20 18:09:08 -04:00
M: integer +day ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[
over >date< julian-day-number + julian-day-number>date
>r >r >>year r> >>month r> >>day
] unless-zero ;
2007-09-20 18:09:08 -04:00
M: real +day ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
: hours/days ( n -- hours days )
24 /rem swap ;
2007-09-20 18:09:08 -04:00
M: integer +hour ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ over hour>> + hours/days >r >>hour r> +day ] unless-zero ;
2007-09-20 18:09:08 -04:00
M: real +hour ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
float>whole-part swapd 60 * +minute swap +hour ;
: minutes/hours ( n -- minutes hours )
60 /rem swap ;
2007-09-20 18:09:08 -04:00
M: integer +minute ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ over minute>> + minutes/hours >r >>minute r> +hour ] unless-zero ;
2007-09-20 18:09:08 -04:00
M: real +minute ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
: seconds/minutes ( n -- seconds minutes )
60 /rem swap >integer ;
2007-09-20 18:09:08 -04:00
M: number +second ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
[ over second>> + seconds/minutes >r >>second r> +minute ] unless-zero ;
: (time+)
[ second>> +second ] keep
[ minute>> +minute ] keep
[ hour>> +hour ] keep
[ day>> +day ] keep
[ month>> +month ] keep
[ year>> +year ] keep ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 21:36:58 -04:00
: +slots [ bi@ + ] curry 2keep ; inline
2007-09-20 18:09:08 -04:00
2008-02-26 18:22:48 -05:00
PRIVATE>
2007-09-20 18:09:08 -04:00
2008-08-31 16:54:00 -04:00
GENERIC# time+ 1 ( time1 time2 -- time3 )
2008-02-26 18:22:48 -05:00
M: timestamp time+
>r clone r> (time+) drop ;
M: duration time+
2008-02-26 21:03:35 -05:00
dup timestamp? [
swap time+
] [
[ year>> ] +slots
[ month>> ] +slots
[ day>> ] +slots
[ hour>> ] +slots
[ minute>> ] +slots
[ second>> ] +slots
2drop <duration>
] if ;
2007-09-20 18:09:08 -04:00
2008-09-01 21:10:10 -04:00
: duration>years ( duration -- x )
2008-08-31 16:54:00 -04:00
#! Uses average month/year length since duration loses calendar
2007-09-20 18:09:08 -04:00
#! data
2008-02-26 18:22:48 -05:00
0 swap
2008-04-22 21:19:54 -04:00
{
[ year>> + ]
[ month>> months-per-year / + ]
[ day>> days-per-year / + ]
[ hour>> hours-per-year / + ]
[ minute>> minutes-per-year / + ]
[ second>> seconds-per-year / + ]
} cleave ;
2008-02-26 18:22:48 -05:00
2008-09-01 21:10:10 -04:00
M: duration <=> [ duration>years ] compare ;
2008-01-12 17:23:34 -05:00
2008-09-01 21:10:10 -04:00
: duration>months ( duration -- x ) duration>years months-per-year * ;
: duration>days ( duration -- x ) duration>years days-per-year * ;
: duration>hours ( duration -- x ) duration>years hours-per-year * ;
: duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
: duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
: duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
2007-09-20 18:09:08 -04:00
2008-08-31 22:20:56 -04:00
GENERIC: time- ( time1 time2 -- time3 )
: convert-timezone ( timestamp duration -- timestamp )
2008-02-26 18:22:48 -05:00
over gmt-offset>> over = [ drop ] [
[ over gmt-offset>> time- time+ ] keep >>gmt-offset
2008-02-26 18:22:48 -05:00
] if ;
2007-09-20 18:09:08 -04:00
: >local-time ( timestamp -- timestamp )
gmt-offset-duration convert-timezone ;
2007-09-20 18:09:08 -04:00
: >gmt ( timestamp -- timestamp )
instant convert-timezone ;
2007-09-20 18:09:08 -04:00
2007-10-31 15:37:29 -04:00
M: timestamp <=> ( ts1 ts2 -- n )
2007-09-20 18:09:08 -04:00
[ >gmt tuple-slots ] compare ;
2008-02-26 21:03:35 -05:00
: (time-) ( timestamp timestamp -- n )
2008-03-29 21:36:58 -04:00
[ >gmt ] bi@
[ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
[ >time< >r >r 3600 * r> 60 * r> + + ] bi@ - + ;
2007-09-20 18:09:08 -04:00
2008-02-26 21:03:35 -05:00
M: timestamp time-
#! Exact calendar-time difference
(time-) seconds ;
2008-04-22 21:19:54 -04:00
: time* ( obj1 obj2 -- obj3 )
dup real? [ swap ] when
dup real? [ * ] [
{
[ year>> * ]
[ month>> * ]
[ day>> * ]
[ hour>> * ]
[ minute>> * ]
[ second>> * ]
} 2cleave <duration>
] if ;
2008-08-31 16:54:00 -04:00
: before ( duration -- -duration )
2008-04-22 21:19:54 -04:00
-1 time* ;
2008-02-26 21:03:35 -05:00
M: duration time-
before time+ ;
: <zero> ( -- timestamp )
2008-09-01 12:48:22 -04:00
0 0 0 0 0 0 instant <timestamp> ;
2008-02-26 21:03:35 -05:00
: valid-timestamp? ( timestamp -- ? )
clone instant >>gmt-offset
2008-02-26 21:03:35 -05:00
dup <zero> time- <zero> time+ = ;
: unix-1970 ( -- timestamp )
2008-06-08 17:47:20 -04:00
1970 1 1 0 0 0 instant <timestamp> ;
2008-02-21 21:57:41 -05:00
2008-09-01 19:06:40 -04:00
: millis>timestamp ( x -- timestamp )
2008-02-26 18:22:48 -05:00
>r unix-1970 r> milliseconds time+ ;
2008-02-21 21:57:41 -05:00
: timestamp>millis ( timestamp -- n )
2008-02-26 21:03:35 -05:00
unix-1970 (time-) 1000 * >integer ;
2007-09-20 18:09:08 -04:00
: gmt ( -- timestamp )
#! GMT time, right now
2008-02-26 18:22:48 -05:00
unix-1970 millis milliseconds time+ ;
2007-09-20 18:09:08 -04:00
: now ( -- timestamp ) gmt >local-time ;
2008-08-31 16:54:00 -04:00
: hence ( duration -- timestamp ) now swap time+ ;
: ago ( duration -- timestamp ) now swap time- ;
2007-09-20 18:09:08 -04:00
: zeller-congruence ( year month day -- n )
#! Zeller Congruence
#! http://web.textfiles.com/computers/formulas.txt
#! good for any date since October 15, 1582
>r dup 2 <= [ 12 + >r 1- r> ] when
>r dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + r>
[ 1+ 3 * 5 /i + ] keep 2 * + r>
1+ + 7 mod ;
GENERIC: days-in-year ( obj -- n )
M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
2008-02-26 18:22:48 -05:00
M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
2008-07-08 14:33:08 -04:00
: (days-in-month) ( year month -- n )
dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
2008-07-08 14:33:08 -04:00
: days-in-month ( timestamp -- n )
>date< drop (days-in-month) ;
2008-07-08 14:33:08 -04:00
: day-of-week ( timestamp -- n )
2008-01-13 13:29:04 -05:00
>date< zeller-congruence ;
2007-09-20 18:09:08 -04:00
2008-07-08 14:33:08 -04:00
:: (day-of-year) ( year month day -- n )
day-counts month head-slice sum day +
year leap-year? [
year month day <date>
year 3 1 <date>
2008-02-26 18:22:48 -05:00
after=? [ 1+ ] when
2008-07-08 14:33:08 -04:00
] when ;
2008-07-08 14:33:08 -04:00
: day-of-year ( timestamp -- n )
>date< (day-of-year) ;
2007-09-20 18:09:08 -04:00
2008-09-01 23:16:51 -04:00
<PRIVATE
: day-offset ( timestamp m -- timestamp n )
over day-of-week - ; inline
2008-01-10 23:06:23 -05:00
: day-this-week ( timestamp n -- timestamp )
2008-02-26 18:22:48 -05:00
day-offset days time+ ;
2008-09-01 23:16:51 -04:00
PRIVATE>
2008-01-10 23:06:23 -05:00
2008-09-01 19:06:40 -04:00
: sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
: monday ( timestamp -- new-timestamp ) 1 day-this-week ;
: tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
: wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
: thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
: friday ( timestamp -- new-timestamp ) 5 day-this-week ;
: saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
: midnight ( timestamp -- new-timestamp )
clone 0 >>hour 0 >>minute 0 >>second ; inline
: noon ( timestamp -- new-timestamp )
midnight 12 >>hour ; inline
: beginning-of-month ( timestamp -- new-timestamp )
midnight 1 >>day ;
: beginning-of-week ( timestamp -- new-timestamp )
midnight sunday ;
: beginning-of-year ( timestamp -- new-timestamp )
2008-02-26 18:22:48 -05:00
beginning-of-month 1 >>month ;
2008-02-26 21:03:35 -05:00
: time-since-midnight ( timestamp -- duration )
dup midnight time- ;
M: timestamp sleep-until timestamp>millis sleep-until ;
2008-07-08 16:50:38 -04:00
M: duration sleep hence sleep-until ;
{
2008-04-02 19:25:33 -04:00
{ [ os unix? ] [ "calendar.unix" ] }
{ [ os windows? ] [ "calendar.windows" ] }
} cond require