1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays calendar calendar.english combinators
4 formatting io io.streams.string kernel make math math.parser
5 math.parser.private present quotations sequences words ;
8 MACRO: formatted ( spec -- quot )
11 { [ dup word? ] [ 1quotation ] }
12 { [ dup quotation? ] [ ] }
13 [ [ nip write ] curry [ ] like ]
15 ] map [ cleave ] curry ;
17 : pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-head ;
19 : pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-head ;
21 : write-00 ( n -- ) pad-00 write ;
23 : write-0000 ( n -- ) pad-0000 write ;
25 : hh ( timestamp -- ) hour>> write-00 ;
27 : mm ( timestamp -- ) minute>> write-00 ;
29 : ss ( timestamp -- ) second>> >integer write-00 ;
31 ! Should be enough for anyone, allows to not do a fancy
32 ! algorithm to detect infinite decimals (e.g 1/3)
33 : ss.SSSSSS ( timestamp -- )
34 second>> >float "0" 9 6 "f" "C" format-float write ;
36 : hhmm ( timestamp -- ) [ hh ] [ mm ] bi ;
38 : hh:mm ( timestamp -- ) { hh ":" mm } formatted ;
40 : hh:mm:ss ( timestamp -- ) { hh ":" mm ":" ss } formatted ;
42 : hh:mm:ss.SSSSSS ( timestamp -- ) { hh ":" mm ":" ss.SSSSSS } formatted ;
44 : D ( timestamp -- ) day>> number>string write ;
46 : DD ( timestamp -- ) day>> write-00 ;
48 : DAY ( timestamp -- ) day-of-week day-abbreviation3 write ;
50 : MM ( timestamp -- ) month>> write-00 ;
52 : MONTH ( timestamp -- ) month>> month-abbreviation write ;
54 : YYYY ( timestamp -- ) year>> write-0000 ;
56 : YYYY-MM-DD ( timestamp -- ) { YYYY "-" MM "-" DD } formatted ;
58 GENERIC: day. ( obj -- )
61 number>string dup length 2 < [ bl ] when write ;
66 GENERIC: month. ( obj -- )
70 [ month-name write bl number>string print ]
71 [ 1 zeller-congruence ]
72 [ (days-in-month) day-abbreviations2 " " join print ] 2tri
73 over " " <repetition> "" concat-as write
76 1 + + 7 mod zero? [ nl ] [ bl ] if
77 ] with each-integer nl ;
80 [ year>> ] [ month>> ] bi 2array month. ;
82 GENERIC: year. ( obj -- )
85 12 [ 1 + 2array month. nl ] with each-integer ;
87 M: timestamp year. year>> year. ;
89 : timestamp>mdtm ( timestamp -- str )
90 [ { YYYY MM DD hh mm ss } formatted ] with-string-writer ;
92 : timestamp>ymd ( timestamp -- str )
93 [ YYYY-MM-DD ] with-string-writer ;
95 : timestamp>hms ( timestamp -- str )
96 [ hh:mm:ss ] with-string-writer ;
98 : timestamp>ymdhms ( timestamp -- str )
99 [ >gmt YYYY-MM-DD " " hh:mm:ss ] with-string-writer ;
101 : write-gmt-offset-hhmm ( gmt-offset -- )
102 [ hour>> dup 0 < "-" "+" ? write abs write-00 ] [ mm ] bi ;
104 : write-gmt-offset-hh:mm ( gmt-offset -- )
105 [ hour>> dup 0 < "-" "+" ? write abs write-00 ":" write ] [ mm ] bi ;
107 : write-gmt-offset ( gmt-offset -- )
108 dup instant = [ drop "GMT" write ] [ write-gmt-offset-hhmm ] if ;
110 : write-gmt-offset-z ( gmt-offset -- )
111 dup instant = [ drop "Z" write ] [ write-gmt-offset-hh:mm ] if ;
113 : write-rfc1036 ( timestamp -- )
115 DAY ", " DD "-" MONTH "-" YYYY " " hh ":" mm ":" ss " "
116 [ gmt-offset>> write-gmt-offset ]
119 : timestamp>rfc1036 ( timestamp -- str )
120 [ write-rfc1036 ] with-string-writer ;
122 ! RFC850 obsoleted by RFC1036
123 ALIAS: write-rfc850 write-rfc1036
124 ALIAS: timestamp>rfc850 timestamp>rfc1036
126 : write-rfc2822 ( timestamp -- )
128 DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss " "
129 [ gmt-offset>> write-gmt-offset ]
132 : timestamp>rfc2822 ( timestamp -- str )
133 [ write-rfc2822 ] with-string-writer ;
135 ! RFC822 obsoleted by RFC2822
136 ALIAS: write-rfc822 write-rfc2822
137 ALIAS: timestamp>rfc822 timestamp>rfc2822
139 : write-rfc3339 ( timestamp -- )
141 YYYY "-" MM "-" DD "T" hh ":" mm ":" ss.SSSSSS
142 [ gmt-offset>> write-gmt-offset-z ]
145 : timestamp>rfc3339 ( timestamp -- str )
146 [ write-rfc3339 ] with-string-writer ;
148 : write-iso8601 ( timestamp -- )
150 YYYY "-" MM "-" DD "T" hh ":" mm ":" ss.SSSSSS
151 [ gmt-offset>> write-gmt-offset-hh:mm ]
154 : timestamp>iso8601 ( timestamp -- str )
155 [ write-iso8601 ] with-string-writer ;
157 : write-ctime ( timestamp -- )
159 DAY " " MONTH " " DD " " hh ":" mm ":" ss " " YYYY
162 : timestamp>ctime-string ( timestamp -- str )
163 [ write-ctime ] with-string-writer ;
165 : timestamp>git-string ( timestamp -- str )
168 DAY " " MONTH " " D " " hh ":" mm ":" ss " " YYYY " "
169 [ gmt-offset>> write-gmt-offset-hhmm ]
171 ] with-string-writer ;
173 : timestamp>http-string ( timestamp -- str )
174 >gmt timestamp>rfc2822 ;
176 : timestamp>cookie-string ( timestamp -- str )
177 >gmt timestamp>rfc1036 ;
179 : write-timestamp ( timestamp -- )
180 { DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss } formatted ;
182 : timestamp>string ( timestamp -- str )
183 [ write-timestamp ] with-string-writer ;
185 M: timestamp present timestamp>string ;
187 : duration>hm ( duration -- str )
188 [ duration>hours >integer 24 mod pad-00 ]
189 [ duration>minutes >integer 60 mod pad-00 ] bi ":" glue ;
191 : duration>hms ( duration -- str )
192 [ duration>hm ] [ second>> >integer 60 mod pad-00 ] bi ":" glue ;
194 : duration>human-readable ( duration -- string )
197 duration>years >integer
199 [ number>string write ]
200 [ 1 > " years, " " year, " ? write ] bi
203 duration>days >integer 365 mod
205 [ number>string write ]
206 [ 1 > " days, " " day, " ? write ] bi
208 ] [ duration>hms write ] tri
209 ] with-string-writer ;
211 GENERIC: elapsed-time ( seconds -- string )
213 M: integer elapsed-time
214 dup 0 < [ "negative seconds" throw ] when [
223 [ first [ /mod ] [ dup ] if* ] [ second ] bi swap
224 dup 0 > [ number>string prepend , ] [ 2drop ] if
226 ] { } make [ "0s" ] [ reverse " " join ] if-empty ;
229 >integer elapsed-time ;
231 M: duration elapsed-time
232 duration>seconds elapsed-time ;
234 M: timestamp elapsed-time
235 now swap time- elapsed-time ;
237 ! XXX: Anything up to 2 hours is "about an hour"
238 : relative-time-offset ( seconds -- string )
240 { [ dup 1 < ] [ drop "just now" ] }
241 { [ dup 60 < ] [ drop "less than a minute" ] }
242 { [ dup 120 < ] [ drop "about a minute" ] }
243 { [ dup 2700 < ] [ 60 /i "%d minutes" sprintf ] }
244 { [ dup 7200 < ] [ drop "about an hour" ] }
245 { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
246 { [ dup 172800 < ] [ drop "1 day" ] }
247 [ 86400 /i "%d days" sprintf ]
250 GENERIC: relative-time ( seconds -- string )
252 M: real relative-time
253 [ relative-time-offset ] [
257 0 < "hence" "ago" ? " " glue
261 M: duration relative-time
262 duration>seconds relative-time ;
264 M: timestamp relative-time
265 now swap time- relative-time ;