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 grouping io io.streams.string kernel make math
5 math.order math.parser math.parser.private math.ranges present
6 quotations sequences splitting strings words ;
9 MACRO: formatted ( spec -- quot )
12 { [ dup word? ] [ 1quotation ] }
13 { [ dup quotation? ] [ ] }
14 [ [ nip write ] curry [ ] like ]
16 ] map [ cleave ] curry ;
18 : pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-head ;
20 : pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-head ;
22 : write-00 ( n -- ) pad-00 write ;
24 : write-0000 ( n -- ) pad-0000 write ;
26 : hh ( timestamp -- ) hour>> write-00 ;
28 : mm ( timestamp -- ) minute>> write-00 ;
30 : ss ( timestamp -- ) second>> >integer write-00 ;
32 ! Should be enough for anyone, allows to not do a fancy
33 ! algorithm to detect infinite decimals (e.g 1/3)
34 : ss.SSSSSS ( timestamp -- )
35 second>> >float "0" 9 6 "f" "C" format-float write ;
37 : hhmm ( timestamp -- ) [ hh ] [ mm ] bi ;
39 : hh:mm ( timestamp -- ) { hh ":" mm } formatted ;
41 : hh:mm:ss ( timestamp -- ) { hh ":" mm ":" ss } formatted ;
43 : hh:mm:ss.SSSSSS ( timestamp -- ) { hh ":" mm ":" ss.SSSSSS } formatted ;
45 : D ( timestamp -- ) day>> number>string write ;
47 : DD ( timestamp -- ) day>> write-00 ;
49 : DAY ( timestamp -- ) day-of-week day-abbreviation3 write ;
51 : MM ( timestamp -- ) month>> write-00 ;
53 : MONTH ( timestamp -- ) month>> month-abbreviation write ;
55 : YYYY ( timestamp -- ) year>> write-0000 ;
57 : YYYY-MM-DD ( timestamp -- ) { YYYY "-" MM "-" DD } formatted ;
59 GENERIC: day. ( obj -- )
62 number>string dup length 2 < [ bl ] when write ;
69 : center. ( str n -- )
70 over length [-] 2/ CHAR: \s <string> write print ;
72 : month-header. ( year month -- )
73 [ number>string ] [ month-name ] bi* swap " " glue 20 center. ;
76 day-abbreviations2 " " join print ;
78 : days. ( year month -- )
79 [ 1 zeller-congruence dup [ " " write ] times ]
80 [ (days-in-month) ] 2bi [1,b] [
81 [ day. ] [ + 7 mod zero? [ nl ] [ bl ] if ] bi
86 GENERIC: month. ( obj -- )
89 first2 [ month-header. ] [ days-header. days. ] 2bi ;
92 [ year>> ] [ month>> ] bi 2array month. ;
94 GENERIC: year. ( obj -- )
97 dup number>string 64 center. nl 12 [1,b] [
99 [ month-name 20 center. ]
100 [ days-header. days. nl nl ] bi
101 ] with-string-writer string-lines
102 ] with map 3 <groups>
103 [ first3 [ "%-20s %-20s %-20s\n" printf ] 3each ] each ;
105 M: timestamp year. year>> year. ;
107 : timestamp>mdtm ( timestamp -- str )
108 [ { YYYY MM DD hh mm ss } formatted ] with-string-writer ;
110 : timestamp>ymd ( timestamp -- str )
111 [ YYYY-MM-DD ] with-string-writer ;
113 : timestamp>hms ( timestamp -- str )
114 [ hh:mm:ss ] with-string-writer ;
116 : timestamp>ymdhms ( timestamp -- str )
117 [ >gmt YYYY-MM-DD " " hh:mm:ss ] with-string-writer ;
119 : write-gmt-offset-hhmm ( gmt-offset -- )
120 [ hour>> dup 0 < "-" "+" ? write abs write-00 ] [ mm ] bi ;
122 : write-gmt-offset-hh:mm ( gmt-offset -- )
123 [ hour>> dup 0 < "-" "+" ? write abs write-00 ":" write ] [ mm ] bi ;
125 : write-gmt-offset ( gmt-offset -- )
126 dup instant = [ drop "GMT" write ] [ write-gmt-offset-hhmm ] if ;
128 : write-gmt-offset-z ( gmt-offset -- )
129 dup instant = [ drop "Z" write ] [ write-gmt-offset-hh:mm ] if ;
131 : write-rfc1036 ( timestamp -- )
133 DAY ", " DD "-" MONTH "-" YYYY " " hh ":" mm ":" ss " "
134 [ gmt-offset>> write-gmt-offset ]
137 : timestamp>rfc1036 ( timestamp -- str )
138 [ write-rfc1036 ] with-string-writer ;
140 ! RFC850 obsoleted by RFC1036
141 ALIAS: write-rfc850 write-rfc1036
142 ALIAS: timestamp>rfc850 timestamp>rfc1036
144 : write-rfc2822 ( timestamp -- )
146 DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss " "
147 [ gmt-offset>> write-gmt-offset ]
150 : timestamp>rfc2822 ( timestamp -- str )
151 [ write-rfc2822 ] with-string-writer ;
153 ! RFC822 obsoleted by RFC2822
154 ALIAS: write-rfc822 write-rfc2822
155 ALIAS: timestamp>rfc822 timestamp>rfc2822
157 : write-rfc3339 ( timestamp -- )
159 YYYY "-" MM "-" DD "T" hh ":" mm ":" ss.SSSSSS
160 [ gmt-offset>> write-gmt-offset-z ]
163 : timestamp>rfc3339 ( timestamp -- str )
164 [ write-rfc3339 ] with-string-writer ;
166 : write-iso8601 ( timestamp -- )
168 YYYY "-" MM "-" DD "T" hh ":" mm ":" ss.SSSSSS
169 [ gmt-offset>> write-gmt-offset-hh:mm ]
172 : timestamp>iso8601 ( timestamp -- str )
173 [ write-iso8601 ] with-string-writer ;
175 : write-ctime ( timestamp -- )
177 DAY " " MONTH " " DD " " hh ":" mm ":" ss " " YYYY
180 : timestamp>ctime-string ( timestamp -- str )
181 [ write-ctime ] with-string-writer ;
183 : timestamp>git-string ( timestamp -- str )
186 DAY " " MONTH " " D " " hh ":" mm ":" ss " " YYYY " "
187 [ gmt-offset>> write-gmt-offset-hhmm ]
189 ] with-string-writer ;
191 : timestamp>http-string ( timestamp -- str )
192 >gmt timestamp>rfc2822 ;
194 : timestamp>cookie-string ( timestamp -- str )
195 >gmt timestamp>rfc1036 ;
197 : write-timestamp ( timestamp -- )
198 { DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss } formatted ;
200 : timestamp>string ( timestamp -- str )
201 [ write-timestamp ] with-string-writer ;
203 M: timestamp present timestamp>string ;
205 : duration>hm ( duration -- str )
206 [ duration>hours >integer 24 mod pad-00 ]
207 [ duration>minutes >integer 60 mod pad-00 ] bi ":" glue ;
209 : duration>hms ( duration -- str )
210 [ duration>hm ] [ second>> >integer 60 mod pad-00 ] bi ":" glue ;
212 : duration>human-readable ( duration -- string )
215 duration>years >integer
217 [ number>string write ]
218 [ 1 > " years, " " year, " ? write ] bi
221 duration>days >integer 365 mod
223 [ number>string write ]
224 [ 1 > " days, " " day, " ? write ] bi
226 ] [ duration>hms write ] tri
227 ] with-string-writer ;
229 GENERIC: elapsed-time ( seconds -- string )
231 M: integer elapsed-time
232 dup 0 < [ "negative seconds" throw ] when [
241 [ first [ /mod ] [ dup ] if* ] [ second ] bi swap
242 dup 0 > [ number>string prepend , ] [ 2drop ] if
244 ] { } make [ "0s" ] [ reverse " " join ] if-empty ;
247 >integer elapsed-time ;
249 M: duration elapsed-time
250 duration>seconds elapsed-time ;
252 M: timestamp elapsed-time
253 now swap time- elapsed-time ;
255 ! XXX: Anything up to 2 hours is "about an hour"
256 : relative-time-offset ( seconds -- string )
258 { [ dup 1 < ] [ drop "just now" ] }
259 { [ dup 60 < ] [ drop "less than a minute" ] }
260 { [ dup 120 < ] [ drop "about a minute" ] }
261 { [ dup 2700 < ] [ 60 /i "%d minutes" sprintf ] }
262 { [ dup 7200 < ] [ drop "about an hour" ] }
263 { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
264 { [ dup 172800 < ] [ drop "1 day" ] }
265 [ 86400 /i "%d days" sprintf ]
268 GENERIC: relative-time ( seconds -- string )
270 M: real relative-time
271 [ relative-time-offset ] [
275 0 < "hence" "ago" ? " " glue
279 M: duration relative-time
280 duration>seconds relative-time ;
282 M: timestamp relative-time
283 now swap time- relative-time ;