]> gitweb.factorcode.org Git - factor.git/commitdiff
calendar.format: rfc3339, don't drop seconds decimals
authorJon Harper <jon.harper87@gmail.com>
Tue, 1 Jul 2014 21:42:25 +0000 (23:42 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 8 Jul 2014 22:53:52 +0000 (15:53 -0700)
basis/calendar/format/format-tests.factor
basis/calendar/format/format.factor

index c0768629f0fdce01f6f1e68511c4e7277f0d8cba..8b9885a63c378ccecbfaf7da5a907cb3320dbfd1 100644 (file)
@@ -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 }
index 8d8250e5e77092a8d5f3029e20e90d691ec1cacc..4b2601f4f2454ef3cc8797d951e434e781cd6b03 100644 (file)
@@ -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 ;