]> gitweb.factorcode.org Git - factor.git/blob - extra/time/time.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / time / time.factor
1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays calendar io kernel fry macros math
5 math.functions math.parser peg.ebnf sequences strings vectors ;
6
7 IN: time
8
9 : timestring ( timestamp -- string ) 
10     [ hour>> ] keep [ minute>> ] keep second>> 3array
11     [ number>string 2 CHAR: 0 pad-left ] map ":" join ; inline
12
13 : datestring ( timestamp -- string )
14     [ month>> ] keep [ day>> ] keep year>> 3array
15     [ number>string 2 CHAR: 0 pad-left ] map "/" join ; inline
16
17 <PRIVATE
18
19 EBNF: parse-format-string
20
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 ]]
44
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
48
49 formats   = "%" (formats_)       => [[ second '[ _ dip ] ]]
50
51 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
52
53 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
54
55 ;EBNF
56
57 PRIVATE>
58
59 MACRO: strftime ( format-string -- )
60     parse-format-string [ length ] keep [ ] join 
61     '[ _ <vector> @ reverse concat nip ] ;
62
63