-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
\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
[ (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
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
\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
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
\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
"," 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
"," 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
: (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
]\r
} formatted\r
] with-string-writer ;\r
+\r
+M: timestamp present timestamp>string ;\r