diff --git a/basis/calendar/format/format-tests.factor b/basis/calendar/format/format-tests.factor index c0768629f0..8b9885a63c 100644 --- a/basis/calendar/format/format-tests.factor +++ b/basis/calendar/format/format-tests.factor @@ -95,6 +95,8 @@ IN: calendar.format.tests } ] [ "2013-04-23T13:50:24" rfc3339>timestamp ] unit-test +{ "2001-12-14T21:59:43.1-05:00" } [ "2001-12-14T21:59:43.1-05:00" rfc3339>timestamp timestamp>rfc3339 ] unit-test + [ T{ timestamp { year 2001 } diff --git a/basis/calendar/format/format.factor b/basis/calendar/format/format.factor index 8d8250e5e7..4b2601f4f2 100644 --- a/basis/calendar/format/format.factor +++ b/basis/calendar/format/format.factor @@ -133,9 +133,18 @@ M: timestamp year. ( timestamp -- ) { +gt+ [ "+" write (write-rfc3339-gmt-offset) ] } } case ; +! Should be enough for anyone, allows to not do a fancy +! algorithm to detect infinite decimals (e.g 1/3) +CONSTANT: rfc3339-precision 5 +: write-rfc3339-seconds ( timestamp -- ) + second>> 1 mod rfc3339-precision 10^ * round >integer + number>string rfc3339-precision CHAR: 0 pad-head + [ CHAR: 0 = ] trim-tail [ "." write write ] unless-empty ; + : (timestamp>rfc3339) ( timestamp -- ) { YYYY "-" MM "-" DD "T" hh ":" mm ":" ss + write-rfc3339-seconds [ gmt-offset>> write-rfc3339-gmt-offset ] } formatted ;