}
} ;
-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"
}
} ;
{ $subsections
>local-time
>gmt
- convert-timezone
+ convert-timezone!
}
"Timestamps relative to each other:"
{ $subsections "relative-timestamps" }
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
}
} [
2020 december-gmt 5 <iota> [ monday-of-month ] with map
-] unit-test
\ No newline at end of file
+] unit-test
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
: 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 ;
: 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 ;