factor/basis/calendar/calendar.factor

542 lines
16 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.
USING: accessors arrays classes.tuple combinators
combinators.short-circuit kernel locals math math.functions
math.order sequences summary system threads vocabs.loader ;
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 ;
M: not-a-month summary
drop "Months are indexed starting at 1" ;
<PRIVATE
2009-03-12 17:03:10 -04:00
: check-month ( n -- n )
[ not-a-month ] when-zero ;
2009-03-12 17:03:10 -04:00
PRIVATE>
2009-08-11 19:15:53 -04:00
CONSTANT: month-names
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"
2009-08-11 19:15:53 -04:00
}
2007-09-20 18:09:08 -04:00
2009-11-12 15:42:41 -05:00
<PRIVATE
: (month-name) ( n -- string ) 1 - month-names nth ;
PRIVATE>
GENERIC: month-name ( obj -- string )
M: integer month-name check-month 1 - month-names nth ;
M: timestamp month-name month>> 1 - month-names nth ;
2009-03-12 17:03:10 -04:00
CONSTANT: month-abbreviations
2007-09-20 18:09:08 -04:00
{
"Jan" "Feb" "Mar" "Apr" "May" "Jun"
"Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
2009-03-12 17:03:10 -04:00
}
2007-09-20 18:09:08 -04:00
: month-abbreviation ( n -- string )
check-month 1 - month-abbreviations nth ;
2009-02-22 20:13:08 -05:00
CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
2008-08-31 22:20:56 -04:00
2009-11-12 15:42:41 -05:00
CONSTANT: day-names
{ "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" }
2009-03-12 17:03:10 -04:00
CONSTANT: day-abbreviations2
{ "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" }
: day-abbreviation2 ( n -- string )
2009-03-12 17:03:10 -04:00
day-abbreviations2 nth ; inline
2009-03-12 17:03:10 -04:00
CONSTANT: day-abbreviations3
{ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" }
: day-abbreviation3 ( n -- string )
2009-03-12 17:03:10 -04:00
day-abbreviations3 nth ; inline
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
14 month - 12 /i :> a
year 4800 + a - :> y
month 12 a * + 3 - :> m
day 153 m * 2 + 5 /i + 365 y * +
y 4 /i + y 100 /i - y 400 /i + 32045 - ;
2008-03-19 22:41:39 -04:00
:: julian-day-number>date ( n -- year month day )
2007-09-20 18:09:08 -04:00
#! Inverse of julian-day-number
n 32044 + :> a
4 a * 3 + 146097 /i :> b
a 146097 b * 4 /i - :> c
4 c * 3 + 1461 /i :> d
c 1461 d * 4 /i - :> e
5 e * 2 + 153 /i :> m
100 b * d + 4800 -
m 10 /i + m 3 +
12 m 10 /i * -
e 153 m * 2 + 5 /i - 1 + ;
GENERIC: easter ( obj -- obj' )
:: easter-month-day ( year -- month day )
year 19 mod :> a
year 100 /mod :> ( b c )
b 4 /mod :> ( d e )
b 8 + 25 /i :> f
b f - 1 + 3 /i :> g
19 a * b + d - g - 15 + 30 mod :> h
c 4 /mod :> ( i k )
32 2 e * + 2 i * + h - k - 7 mod :> l
a 11 h * + 22 l * + 451 /i :> m
h l + 7 m * - 114 + 31 /mod 1 + :> ( month day )
month day ;
M: integer easter ( year -- timestamp )
dup easter-month-day <date> ;
M: timestamp easter ( timestamp -- timestamp )
clone
dup year>> easter-month-day
swapd >>day swap >>month ;
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 ;
: microseconds ( x -- duration ) 1000000 / seconds ;
: nanoseconds ( x -- duration ) 1000000000 / seconds ;
2008-02-26 18:22:48 -05:00
2009-11-11 18:19:14 -05:00
GENERIC: year ( obj -- n )
M: integer year ;
M: timestamp year year>> ;
GENERIC: month ( obj -- n )
M: integer month ;
M: timestamp month month>> ;
GENERIC: day ( obj -- n )
M: integer day ;
M: timestamp day day>> ;
2008-02-26 18:22:48 -05:00
GENERIC: leap-year? ( obj -- ? )
M: integer leap-year? ( year -- ? )
dup 100 divisor? 400 4 ? divisor? ;
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 ;
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 [ 1 - 12 ] when-zero swap ; inline
2007-09-20 18:09:08 -04:00
M: integer +month ( timestamp n -- timestamp )
2008-11-29 13:51:47 -05:00
[ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
2008-02-26 18:22:48 -05:00
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
2008-11-29 13:51:47 -05:00
[ >>year ] [ >>month ] [ >>day ] tri*
2008-02-26 18:22:48 -05:00
] 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-11-29 13:51:47 -05:00
[ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
2008-02-26 18:22:48 -05:00
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-11-29 13:51:47 -05:00
[ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
2008-02-26 18:22:48 -05:00
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-11-29 13:51:47 -05:00
[ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
2008-02-26 18:22:48 -05:00
2008-12-15 23:21:56 -05:00
: (time+) ( timestamp duration -- timestamp' duration )
2008-02-26 18:22:48 -05:00
[ 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
: +slots ( obj1 obj2 quot -- n obj1 obj2 )
[ 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+
2008-11-29 13:51:47 -05:00
[ clone ] dip (time+) drop ;
2008-02-26 18:22:48 -05:00
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 * ;
: duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
: duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
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 ;
2009-11-12 15:42:41 -05:00
: same-day? ( ts1 ts2 -- ? )
[ >gmt >date< <date> ] bi@ = ;
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
2008-11-29 13:51:47 -05:00
[ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] 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-11-29 13:51:47 -05:00
[ unix-1970 ] dip 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 ;
: micros>timestamp ( x -- timestamp )
2008-11-29 13:51:47 -05:00
[ unix-1970 ] dip microseconds time+ ;
: timestamp>micros ( timestamp -- n )
unix-1970 (time-) 1000000 * >integer ;
2007-09-20 18:09:08 -04:00
: gmt ( -- timestamp )
#! GMT time, right now
unix-1970 micros microseconds 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
2008-11-29 13:51:47 -05:00
[
dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
[ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
[ 1 + 3 * 5 /i + ] keep 2 * +
] dip 1 + + 7 mod ;
2007-09-20 18:09:08 -04:00
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
2009-11-12 15:42:41 -05:00
GENERIC: day-name ( obj -- string )
M: integer day-name day-names nth ;
M: timestamp day-name day-of-week day-names nth ;
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>
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
2009-11-11 16:52:30 -05:00
: 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 ;
2008-09-01 23:16:51 -04:00
<PRIVATE
2009-11-11 16:52:30 -05:00
: day-offset ( timestamp m -- new-timestamp n )
over day-of-week - ; inline
2008-01-10 23:06:23 -05:00
2009-11-11 16:52:30 -05:00
: day-this-week ( timestamp n -- new-timestamp )
2008-02-26 18:22:48 -05:00
day-offset days time+ ;
2009-11-11 16:52:30 -05:00
:: nth-day-this-month ( timestamp n day -- new-timestamp )
timestamp beginning-of-month day day-this-week
dup timestamp [ month>> ] bi@ = [ 1 weeks time+ ] unless
n 1 - [ weeks time+ ] unless-zero ;
2009-11-11 18:19:14 -05:00
: last-day-this-month ( timestamp day -- new-timestamp )
[ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
2008-09-01 23:16:51 -04:00
PRIVATE>
2008-01-10 23:06:23 -05:00
2009-11-11 18:19:14 -05:00
GENERIC: january ( obj -- timestamp )
GENERIC: february ( obj -- timestamp )
GENERIC: march ( obj -- timestamp )
GENERIC: april ( obj -- timestamp )
GENERIC: may ( obj -- timestamp )
GENERIC: june ( obj -- timestamp )
GENERIC: july ( obj -- timestamp )
GENERIC: august ( obj -- timestamp )
GENERIC: september ( obj -- timestamp )
GENERIC: october ( obj -- timestamp )
GENERIC: november ( obj -- timestamp )
GENERIC: december ( obj -- timestamp )
M: integer january 1 1 <date> ;
M: integer february 2 1 <date> ;
M: integer march 3 1 <date> ;
M: integer april 4 1 <date> ;
M: integer may 5 1 <date> ;
M: integer june 6 1 <date> ;
M: integer july 7 1 <date> ;
M: integer august 8 1 <date> ;
M: integer september 9 1 <date> ;
M: integer october 10 1 <date> ;
M: integer november 11 1 <date> ;
M: integer december 12 1 <date> ;
M: timestamp january clone 1 >>month ;
M: timestamp february clone 2 >>month ;
M: timestamp march clone 3 >>month ;
M: timestamp april clone 4 >>month ;
M: timestamp may clone 5 >>month ;
M: timestamp june clone 6 >>month ;
M: timestamp july clone 7 >>month ;
M: timestamp august clone 8 >>month ;
M: timestamp september clone 9 >>month ;
M: timestamp october clone 10 >>month ;
M: timestamp november clone 11 >>month ;
M: timestamp december clone 12 >>month ;
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 ;
2009-11-12 15:42:41 -05:00
: sunday? ( timestamp -- ? ) day-of-week 0 = ;
: monday? ( timestamp -- ? ) day-of-week 1 = ;
: tuesday? ( timestamp -- ? ) day-of-week 2 = ;
: wednesday? ( timestamp -- ? ) day-of-week 3 = ;
: thursday? ( timestamp -- ? ) day-of-week 4 = ;
: friday? ( timestamp -- ? ) day-of-week 5 = ;
: saturday? ( timestamp -- ? ) day-of-week 6 = ;
2009-11-11 16:52:30 -05:00
: sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
: monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
: tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
: wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
: thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
: friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
: saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
2009-11-11 18:19:14 -05:00
: last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
: last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
: last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
: last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
: last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
: last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
: last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
: 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- ;
2008-10-06 22:04:30 -04:00
: since-1970 ( duration -- timestamp )
2008-10-06 18:17:49 -04:00
unix-1970 time+ >local-time ;
M: timestamp sleep-until timestamp>micros 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