]> gitweb.factorcode.org Git - factor.git/commitdiff
calendar: remove normalize-timestamp, cleanup convert-timezone.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 10 Dec 2020 23:11:04 +0000 (15:11 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 10 Dec 2020 23:11:04 +0000 (15:11 -0800)
basis/calendar/calendar-docs.factor
basis/calendar/calendar-tests.factor
basis/calendar/calendar.factor

index a48af67e8eaff5ae0a53e8c55778704ecf5213eb..b72f566293a4643cec5115554049882ff146b31a 100644 (file)
@@ -259,12 +259,12 @@ HELP: time-
     }
 } ;
 
-HELP: convert-timezone
-{ $values { "timestamp" timestamp } { "duration" duration } { "timestamp'" timestamp } }
+HELP: convert-timezone!
+{ $values { "timestamp" timestamp } { "duration" duration } }
 { $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>> ."
+               "gmt noon instant -5 >>hour convert-timezone! gmt-offset>> hour>> ."
                "-5"
     }
 } ;
@@ -500,7 +500,7 @@ ARTICLE: "calendar" "Calendar"
 { $subsections
     >local-time
     >gmt
-    convert-timezone
+    convert-timezone!
 }
 "Timestamps relative to each other:"
 { $subsections "relative-timestamps" }
index 02555a057e6cae2a6de846443599ab3221000aee..d5383e7c688250930e7eeb24bfff02a821fa0d10 100644 (file)
@@ -188,50 +188,6 @@ IN: calendar
     2008 2 29 <date> =
 ] unit-test
 
-{
-    T{ timestamp
-        { year 2020 }
-        { month 1 }
-        { day 1 }
-        { hour 2 }
-        { minute 46 }
-        { second 40 }
-    }
-} [
-    2020 <year-gmt> 10000 >>second normalize-timestamp
-] unit-test
-
-{
-    T{ timestamp
-        { year 2020 }
-        { month 1 }
-        { day 1 }
-        { hour 2 }
-        { minute 46 }
-        { second 40 }
-    }
-} [
-    2020 <year-gmt> 10000 >>second normalize-timestamp!
-] unit-test
-
-{ f } [
-    2020 <year-gmt> dup 10000 >>second normalize-timestamp eq?
-] unit-test
-
-{ t } [
-    2020 <year-gmt> dup 10000 >>second normalize-timestamp! eq?
-] unit-test
-
-{ +eq+ } [
-    2020 <year-gmt> 10000 >>second
-    dup normalize-timestamp <=>
-] unit-test
-
-{ +eq+ } [
-    2020 <year-gmt> 10000 >>second
-    dup normalize-timestamp <=>
-] unit-test
-
 { f } [
     2020 <year-gmt> dup 10000 >>second [ >gmt ] bi@ eq?
 ] unit-test
@@ -416,4 +372,4 @@ IN: calendar
     }
 } [
     2020 december-gmt 5 <iota> [ monday-of-month ] with map
-] unit-test
\ No newline at end of file
+] unit-test
index e5d821bddc3d0a0376151531814dfd03244fe6fb..b436345a2ea0eb9a171413d6fad2d4bf4004406b 100644 (file)
@@ -284,14 +284,6 @@ M: timestamp time+
 M: duration time+
     dup timestamp? [ swap time+ ] [ duration+ ] if ;
 
-GENERIC#: time+! 1 ( time1 time2 -- time3 )
-
-M: timestamp time+!
-    (time+) drop ;
-
-M: duration time+!
-    dup timestamp? [ swap time+! ] [ duration+ ] if ;
-
 : duration>years ( duration -- x )
     ! Uses average month/year length since duration loses calendar data
     0 swap
@@ -315,32 +307,19 @@ M: duration <=> [ duration>years ] compare ;
 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
 
-GENERIC: time- ( time1 time2 -- time3 )
+DEFER: time-
 
-: convert-timezone ( timestamp duration -- timestamp' )
-    over gmt-offset>> over = [ drop ] [
-        [ over gmt-offset>> time- time+ ] keep >>gmt-offset
-    ] if ;
+: convert-timezone! ( timestamp duration -- timestamp )
+    [ over gmt-offset>> time- (time+) drop ] [ >>gmt-offset ] bi ;
 
 : >local-time ( timestamp -- timestamp' )
-    clone gmt-offset-duration convert-timezone ;
-
-: normalize-timestamp! ( timestamp -- timestamp ) 0 seconds time+! ;
-: normalize-timestamp ( timestamp -- timestamp' ) 0 seconds time+ ;
+    clone gmt-offset-duration convert-timezone! ;
 
-: (>gmt) ( timestamp -- timestamp' )
-    dup gmt-offset>> dup instant =
-    [ drop ] [
-        [ neg +second 0 ] change-second
-        [ neg +minute 0 ] change-minute
-        [ neg +hour   0 ] change-hour
-        [ neg +day    0 ] change-day
-        [ neg +month  0 ] change-month
-        [ neg +year   0 ] change-year drop
-    ] if ; inline
+: >gmt! ( timestamp -- timestamp )
+    instant convert-timezone! ;
 
-: >gmt! ( timestamp -- timestamp ) normalize-timestamp! (>gmt) ;
-: >gmt ( timestamp -- timestamp' ) normalize-timestamp (>gmt) ;
+: >gmt ( timestamp -- timestamp' )
+    clone >gmt! ;
 
 M: timestamp <=> [ >gmt tuple-slots ] compare ;
 
@@ -406,11 +385,17 @@ DEFER: end-of-year
 : same-second? ( ts1 ts2 -- ? )
     [ >gmt slots{ year month day hour minute second } ] same? ;
 
+<PRIVATE
+
 : (time-) ( timestamp timestamp -- n )
     [ >gmt ] bi@
     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
 
+PRIVATE>
+
+GENERIC: time- ( time1 time2 -- time3 )
+
 M: timestamp time-
     ! Exact calendar-time difference
     (time-) seconds ;