]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/calendar/format/format.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / calendar / format / format.factor
index b15da4240998ddd4ffeca4b9dbba53a347ec4a7c..6aa4126ff920f913ea4a7cd3e7b986793020c122 100644 (file)
@@ -1,14 +1,15 @@
-USING: math math.order math.parser math.functions kernel sequences io\r
-accessors arrays io.streams.string splitting\r
-combinators accessors debugger\r
-calendar calendar.format.macros ;\r
+! Copyright (C) 2008 Slava Pestov.\r
+! See http://factorcode.org/license.txt for BSD license.\r
+USING: math math.order math.parser math.functions kernel\r
+sequences io accessors arrays io.streams.string splitting\r
+combinators calendar calendar.format.macros present ;\r
 IN: calendar.format\r
 \r
-: pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-left ;\r
+: pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-head ;\r
 \r
-: pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-left ;\r
+: pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-head ;\r
 \r
-: pad-00000 ( n -- str ) number>string 5 CHAR: 0 pad-left ;\r
+: pad-00000 ( n -- str ) number>string 5 CHAR: 0 pad-head ;\r
 \r
 : write-00 ( n -- ) pad-00 write ;\r
 \r
@@ -45,6 +46,11 @@ IN: calendar.format
 \r
 : read-0000 ( -- n ) 4 read string>number ;\r
 \r
+: hhmm>timestamp ( hhmm -- timestamp )\r
+    [\r
+        0 0 0 read-00 read-00 0 instant <timestamp>\r
+    ] with-string-reader ;\r
+\r
 GENERIC: day. ( obj -- )\r
 \r
 M: integer day. ( n -- )\r
@@ -62,8 +68,8 @@ M: array month. ( pair -- )
     [ (days-in-month) day-abbreviations2 " " join print ] 2tri\r
     over "   " <repetition> concat write\r
     [\r
-        [ 1+ day. ] keep\r
-        1+ + 7 mod zero? [ nl ] [ bl ] if\r
+        [ 1 + day. ] keep\r
+        1 + + 7 mod zero? [ nl ] [ bl ] if\r
     ] with each nl ;\r
 \r
 M: timestamp month. ( timestamp -- )\r
@@ -72,11 +78,14 @@ M: timestamp month. ( timestamp -- )
 GENERIC: year. ( obj -- )\r
 \r
 M: integer year. ( n -- )\r
-    12 [ 1+ 2array month. nl ] with each ;\r
+    12 [ 1 + 2array month. nl ] with each ;\r
 \r
 M: timestamp year. ( timestamp -- )\r
     year>> year. ;\r
 \r
+: timestamp>mdtm ( timestamp -- str )\r
+    [ { YYYY MM DD hh mm ss } formatted ] with-string-writer ;\r
+\r
 : (timestamp>string) ( timestamp -- )\r
     { DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss } formatted ;\r
 \r
@@ -138,11 +147,11 @@ M: timestamp year. ( timestamp -- )
 \r
 : read-rfc3339-gmt-offset ( ch -- dt )\r
     dup CHAR: Z = [ drop instant ] [\r
-        >r\r
-        read-00 hours\r
-        read1 { { CHAR: : [ read-00 ] } { f [ 0 ] } } case minutes\r
-        time+\r
-        r> signed-gmt-offset\r
+        [\r
+            read-00 hours\r
+            read1 { { CHAR: : [ read-00 ] } { f [ 0 ] } } case minutes\r
+            time+\r
+        ] dip signed-gmt-offset\r
     ] if ;\r
 \r
 : read-ymd ( -- y m d )\r
@@ -152,8 +161,9 @@ M: timestamp year. ( timestamp -- )
     read-00 ":" expect read-00 ":" expect read-00 ;\r
 \r
 : read-rfc3339-seconds ( s -- s' ch )\r
-    "+-Z" read-until >r\r
-    [ string>number ] [ length 10 swap ^ ] bi / + r> ;\r
+    "+-Z" read-until [\r
+        [ string>number ] [ length 10^ ] bi / +\r
+    ] dip ;\r
 \r
 : (rfc3339>timestamp) ( -- timestamp )\r
     read-ymd\r
@@ -181,9 +191,9 @@ ERROR: invalid-timestamp-format ;
 \r
 : parse-rfc822-gmt-offset ( string -- dt )\r
     dup "GMT" = [ drop instant ] [\r
-        unclip >r\r
-        2 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+\r
-        r> signed-gmt-offset\r
+        unclip \r
+            2 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+\r
+        ] dip signed-gmt-offset\r
     ] if ;\r
 \r
 : (rfc822>timestamp) ( -- timestamp )\r
@@ -191,7 +201,7 @@ ERROR: invalid-timestamp-format ;
         "," read-token day-abbreviations3 member? check-timestamp drop\r
         read1 CHAR: \s assert=\r
         read-sp checked-number >>day\r
-        read-sp month-abbreviations index 1+ check-timestamp >>month\r
+        read-sp month-abbreviations index 1 + check-timestamp >>month\r
         read-sp checked-number >>year\r
         ":" read-token checked-number >>hour\r
         ":" read-token checked-number >>minute\r
@@ -210,7 +220,7 @@ ERROR: invalid-timestamp-format ;
         "," read-token check-day-name\r
         read1 CHAR: \s assert=\r
         "-" read-token checked-number >>day\r
-        "-" read-token month-abbreviations index 1+ check-timestamp >>month\r
+        "-" read-token month-abbreviations index 1 + check-timestamp >>month\r
         read-sp checked-number >>year\r
         ":" read-token checked-number >>hour\r
         ":" read-token checked-number >>minute\r
@@ -223,7 +233,7 @@ ERROR: invalid-timestamp-format ;
 : (cookie-string>timestamp-2) ( -- timestamp )\r
     timestamp new\r
         read-sp check-day-name\r
-        read-sp month-abbreviations index 1+ check-timestamp >>month\r
+        read-sp month-abbreviations index 1 + check-timestamp >>month\r
         read-sp checked-number >>day\r
         ":" read-token checked-number >>hour\r
         ":" read-token checked-number >>minute\r
@@ -287,3 +297,5 @@ ERROR: invalid-timestamp-format ;
             ]\r
         } formatted\r
     ] with-string-writer ;\r
+\r
+M: timestamp present timestamp>string ;\r