1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays calendar io kernel fry macros math
5 math.functions math.parser peg.ebnf sequences strings vectors ;
9 : timestring ( timestamp -- string )
10 [ hour>> ] keep [ minute>> ] keep second>> 3array
11 [ number>string 2 CHAR: 0 pad-left ] map ":" join ; inline
13 : datestring ( timestamp -- string )
14 [ month>> ] keep [ day>> ] keep year>> 3array
15 [ number>string 2 CHAR: 0 pad-left ] map "/" join ; inline
19 EBNF: parse-format-string
21 fmt-% = "%" => [[ [ "%" ] ]]
22 fmt-a = "a" => [[ [ dup day-of-week day-abbreviation3 ] ]]
23 fmt-A = "A" => [[ [ dup day-of-week day-name ] ]]
24 fmt-b = "b" => [[ [ dup month>> month-abbreviation ] ]]
25 fmt-B = "B" => [[ [ dup month>> month-name ] ]]
26 fmt-c = "c" => [[ [ "Not yet implemented" throw ] ]]
27 fmt-d = "d" => [[ [ dup day>> number>string 2 CHAR: 0 pad-left ] ]]
28 fmt-H = "H" => [[ [ dup hour>> number>string 2 CHAR: 0 pad-left ] ]]
29 fmt-I = "I" => [[ [ dup hour>> 12 > [ 12 - ] when number>string 2 CHAR: 0 pad-left ] ]]
30 fmt-j = "j" => [[ [ dup day-of-year number>string ] ]]
31 fmt-m = "m" => [[ [ dup month>> number>string 2 CHAR: 0 pad-left ] ]]
32 fmt-M = "M" => [[ [ dup minute>> number>string 2 CHAR: 0 pad-left ] ]]
33 fmt-p = "p" => [[ [ dup hour>> 12 < [ "AM" ] [ "PM" ] ? ] ]]
34 fmt-S = "S" => [[ [ dup second>> round number>string 2 CHAR: 0 pad-left ] ]]
35 fmt-U = "U" => [[ [ "Not yet implemented" throw ] ]]
36 fmt-w = "w" => [[ [ dup day-of-week number>string ] ]]
37 fmt-W = "W" => [[ [ "Not yet implemented" throw ] ]]
38 fmt-x = "x" => [[ [ dup datestring ] ]]
39 fmt-X = "X" => [[ [ dup timestring ] ]]
40 fmt-y = "y" => [[ [ dup year>> 100 mod number>string ] ]]
41 fmt-Y = "Y" => [[ [ dup year>> number>string ] ]]
42 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
43 unknown = (.)* => [[ "Unknown directive" throw ]]
45 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
46 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
47 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
49 formats = "%" (formats_) => [[ second '[ _ dip ] ]]
51 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
53 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
59 MACRO: strftime ( format-string -- )
60 parse-format-string [ length ] keep [ ] join
61 '[ _ <vector> @ reverse concat nip ] ;