add word to convert timevals to unix time, add utility words

db4
Doug Coleman 2008-10-05 15:21:23 -05:00
parent 9a036b2463
commit 02bb3063ff
2 changed files with 44 additions and 4 deletions

View File

@ -165,7 +165,15 @@ HELP: milliseconds
{ $values { "x" number } { "duration" duration } }
{ $description "Creates a duration object with the specified number of milliseconds." } ;
{ years months days hours minutes seconds milliseconds } related-words
HELP: microseconds
{ $values { "x" number } { "duration" duration } }
{ $description "Creates a duration object with the specified number of microseconds." } ;
HELP: nanoseconds
{ $values { "x" number } { "duration" duration } }
{ $description "Creates a duration object with the specified number of nanoseconds." } ;
{ years months days hours minutes seconds milliseconds microseconds nanoseconds } related-words
HELP: leap-year?
{ $values { "obj" object } { "?" "a boolean" } }
@ -263,7 +271,27 @@ HELP: duration>milliseconds
}
} ;
{ duration>years duration>months duration>days duration>hours duration>minutes duration>seconds duration>milliseconds } related-words
HELP: duration>microseconds
{ $values { "duration" duration } { "x" number } }
{ $description "Calculates the length of a duration in microseconds." }
{ $examples
{ $example "USING: calendar prettyprint ;"
"6 seconds duration>microseconds ."
"6000000"
}
} ;
HELP: duration>nanoseconds
{ $values { "duration" duration } { "x" number } }
{ $description "Calculates the length of a duration in nanoseconds." }
{ $examples
{ $example "USING: calendar prettyprint ;"
"6 seconds duration>nanoseconds ."
"6000000000"
}
} ;
{ duration>years duration>months duration>days duration>hours duration>minutes duration>seconds duration>milliseconds duration>microseconds duration>nanoseconds } related-words
HELP: time-
@ -528,6 +556,8 @@ ARTICLE: "using-durations" "Using durations"
{ $subsection minutes }
{ $subsection seconds }
{ $subsection milliseconds }
{ $subsection microseconds }
{ $subsection nanoseconds }
{ $subsection instant }
"Converting a duration to a number:"
{ $subsection duration>years }
@ -536,7 +566,9 @@ ARTICLE: "using-durations" "Using durations"
{ $subsection duration>hours }
{ $subsection duration>minutes }
{ $subsection duration>seconds }
{ $subsection duration>milliseconds } ;
{ $subsection duration>milliseconds }
{ $subsection duration>microseconds }
{ $subsection duration>nanoseconds } ;
ARTICLE: "relative-timestamps" "Relative timestamps"
"In the future:"

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel math math.functions namespaces sequences
strings system vocabs.loader threads accessors combinators
locals classes.tuple math.order summary
locals classes.tuple math.order summary structs
combinators.short-circuit ;
IN: calendar
@ -129,6 +129,8 @@ PRIVATE>
: 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 ;
GENERIC: leap-year? ( obj -- ? )
@ -261,6 +263,8 @@ M: duration <=> [ duration>years ] compare ;
: 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 * ;
GENERIC: time- ( time1 time2 -- time3 )
@ -398,6 +402,10 @@ PRIVATE>
: time-since-midnight ( timestamp -- duration )
dup midnight time- ;
: timeval>unix-time ( timeval -- timestamp )
[ timeval-sec seconds ] [ timeval-usec microseconds ] bi
time+ unix-1970 time+ >local-time ;
M: timestamp sleep-until timestamp>millis sleep-until ;
M: duration sleep hence sleep-until ;