From 7025ebd7ee5f856751af7b205195fd828e808f91 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 31 Aug 2008 20:19:16 -0500 Subject: [PATCH 1/2] docs --- basis/calendar/calendar-docs.factor | 68 ++++++++++++++++++++++++++++- basis/calendar/calendar.factor | 1 - 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/basis/calendar/calendar-docs.factor b/basis/calendar/calendar-docs.factor index 2c23ae95c1..d3bfa7bcb1 100644 --- a/basis/calendar/calendar-docs.factor +++ b/basis/calendar/calendar-docs.factor @@ -184,7 +184,7 @@ HELP: time+ { $description "Adds two durations to produce a duration or adds a timestamp and a duration to produce a timestamp. The calculation takes timezones into account." } { $examples { $example "USING: calendar math.order prettyprint ;" - "10 months 2 months time+ 1 year <=> ." + "10 months 2 months time+ 1 years <=> ." "+eq+" } { $example "USING: accessors calendar math.order prettyprint ;" @@ -193,3 +193,69 @@ HELP: time+ } } ; +HELP: dt>years +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in years." } +{ $examples + { $example "USING: calendar prettyprint ;" + "6 months dt>years ." + "1/2" + } +} ; + +HELP: dt>months +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in months." } +{ $examples + { $example "USING: calendar prettyprint ;" + "30 days dt>months ." + "16000/16233" + } +} ; + +HELP: dt>days +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in days." } +{ $examples + { $example "USING: calendar prettyprint ;" + "6 hours dt>days ." + "1/4" + } +} ; + +HELP: dt>hours +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in hours." } +{ $examples + { $example "USING: calendar prettyprint ;" + "3/4 days dt>hours ." + "18" + } +} ; +HELP: dt>minutes +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in minutes." } +{ $examples + { $example "USING: calendar prettyprint ;" + "6 hours dt>minutes ." + "360" + } +} ; +HELP: dt>seconds +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in seconds." } +{ $examples + { $example "USING: calendar prettyprint ;" + "6 minutes dt>seconds ." + "360" + } +} ; +HELP: dt>milliseconds +{ $values { "duration" duration } { "x" number } } +{ $description "Calculates the length of a duration in milliseconds." } +{ $examples + { $example "USING: calendar prettyprint ;" + "6 seconds dt>milliseconds ." + "6000" + } +} ; diff --git a/basis/calendar/calendar.factor b/basis/calendar/calendar.factor index d9284573c4..36b3cf3250 100755 --- a/basis/calendar/calendar.factor +++ b/basis/calendar/calendar.factor @@ -395,7 +395,6 @@ M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ; : time-since-midnight ( timestamp -- duration ) dup midnight time- ; - M: timestamp sleep-until timestamp>millis sleep-until ; M: duration sleep hence sleep-until ; From 261fc87dca04101790b2148ef8edd6df1fde9fda Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 31 Aug 2008 21:20:56 -0500 Subject: [PATCH 2/2] wtf instant was MEMO: oops. docs --- basis/calendar/calendar-docs.factor | 40 +++++++++++++++++++++++++++++ basis/calendar/calendar.factor | 9 +++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/basis/calendar/calendar-docs.factor b/basis/calendar/calendar-docs.factor index d3bfa7bcb1..8ee104d16e 100644 --- a/basis/calendar/calendar-docs.factor +++ b/basis/calendar/calendar-docs.factor @@ -250,6 +250,7 @@ HELP: dt>seconds "360" } } ; + HELP: dt>milliseconds { $values { "duration" duration } { "x" number } } { $description "Calculates the length of a duration in milliseconds." } @@ -259,3 +260,42 @@ HELP: dt>milliseconds "6000" } } ; + +{ dt>years dt>months dt>days dt>hours dt>minutes dt>seconds dt>milliseconds } related-words + + +HELP: time- +{ $values { "time1" "timestamp or duration" } { "time2" "timestamp or duration" } { "time3" "timestamp or duration" } } +{ $description "Subtracts two durations to produce a duration or subtracts a duration from a timestamp to produce a timestamp. The calculation takes timezones into account." } +{ $examples + { $example "USING: calendar math.order prettyprint ;" + "10 months 2 months time- 8 months <=> ." + "+eq+" + } + { $example "USING: accessors calendar math.order prettyprint ;" + "2010 1 1 3 days time- day>> ." + "29" + } +} ; + +{ time+ time- } related-words + +HELP: convert-timezone +{ $values { "timestamp" timestamp } { "duration" duration } { "timestamp" timestamp } } +{ $description "Converts the " { $snippet "timestamp" } "'s " { $snippet "gmt-offset" } " to the GMT offset represented by the " { $snippet "duration" } "." } +{ $examples + { $example "USING: accessors calendar prettyprint ;" + "gmt noon instant -5 >>hour convert-timezone gmt-offset>> hour>> ." + "-5" + } +} ; + +HELP: >local-time +{ $values { "timestamp" timestamp } { "timestamp" timestamp } } +{ $description "Converts the " { $snippet "timestamp" } " to the timezone of your computer." } +{ $examples + { $example "USING: accessors calendar kernel prettyprint ;" + "now gmt >local-time [ gmt-offset>> ] bi@ = ." + "t" + } +} ; diff --git a/basis/calendar/calendar.factor b/basis/calendar/calendar.factor index 36b3cf3250..ff002bb16c 100755 --- a/basis/calendar/calendar.factor +++ b/basis/calendar/calendar.factor @@ -60,6 +60,8 @@ PRIVATE> : month-abbreviation ( n -- string ) check-month 1- month-abbreviations nth ; +: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline + : day-names ( -- array ) { "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" @@ -116,7 +118,7 @@ PRIVATE> : >time< ( timestamp -- hour minute second ) [ hour>> ] [ minute>> ] [ second>> ] tri ; -MEMO: instant ( -- duration ) 0 0 0 0 0 0 ; +: instant ( -- duration ) 0 0 0 0 0 0 ; : years ( x -- duration ) instant clone swap >>year ; : months ( x -- duration ) instant clone swap >>month ; : days ( x -- duration ) instant clone swap >>day ; @@ -258,7 +260,7 @@ M: duration <=> [ dt>years ] compare ; : dt>seconds ( duration -- x ) dt>years seconds-per-year * ; : dt>milliseconds ( duration -- x ) dt>seconds 1000 * ; -GENERIC: time- ( time1 time2 -- time ) +GENERIC: time- ( time1 time2 -- time3 ) : convert-timezone ( timestamp duration -- timestamp ) over gmt-offset>> over = [ drop ] [ @@ -323,12 +325,9 @@ MEMO: unix-1970 ( -- timestamp ) unix-1970 millis milliseconds time+ ; : now ( -- timestamp ) gmt >local-time ; - : hence ( duration -- timestamp ) now swap time+ ; : ago ( duration -- timestamp ) now swap time- ; -: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 } ; inline - : zeller-congruence ( year month day -- n ) #! Zeller Congruence #! http://web.textfiles.com/computers/formulas.txt