]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/format/format.factor
factor: trim using lists
[factor.git] / basis / calendar / format / format.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar calendar.english combinators
4 formatting grouping io io.streams.string kernel make math
5 math.order math.parser math.parser.private ranges present
6 quotations sequences splitting strings words ;
7 IN: calendar.format
8
9 MACRO: formatted ( spec -- quot )
10     [
11         {
12             { [ dup word? ] [ 1quotation ] }
13             { [ dup quotation? ] [ ] }
14             [ [ nip write ] curry [ ] like ]
15         } cond
16     ] map [ cleave ] curry ;
17
18 : pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-head ;
19
20 : pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-head ;
21
22 : write-00 ( n -- ) pad-00 write ;
23
24 : write-0000 ( n -- ) pad-0000 write ;
25
26 : hh ( timestamp -- ) hour>> write-00 ;
27
28 : mm ( timestamp -- ) minute>> write-00 ;
29
30 : ss ( timestamp -- ) second>> >integer write-00 ;
31
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 ;
36
37 : hhmm ( timestamp -- ) [ hh ] [ mm ] bi ;
38
39 : hh:mm ( timestamp -- ) { hh ":" mm } formatted ;
40
41 : hh:mm:ss ( timestamp -- ) { hh ":" mm ":" ss } formatted ;
42
43 : hh:mm:ss.SSSSSS ( timestamp -- ) { hh ":" mm ":" ss.SSSSSS } formatted ;
44
45 : D ( timestamp -- ) day>> number>string write ;
46
47 : DD ( timestamp -- ) day>> write-00 ;
48
49 : DAY ( timestamp -- ) day-of-week day-abbreviation3 write ;
50
51 : MM ( timestamp -- ) month>> write-00 ;
52
53 : MONTH ( timestamp -- ) month>> month-abbreviation write ;
54
55 : YYYY ( timestamp -- ) year>> write-0000 ;
56
57 : YYYY-MM-DD ( timestamp -- ) { YYYY "-" MM "-" DD } formatted ;
58
59 GENERIC: day. ( obj -- )
60
61 M: integer day.
62     number>string dup length 2 < [ bl ] when write ;
63
64 M: timestamp day.
65     day>> day. ;
66
67 <PRIVATE
68
69 : center. ( str n -- )
70     over length [-] 2/ CHAR: \s <string> write print ;
71
72 : month-header. ( year month -- )
73     [ number>string ] [ month-name ] bi* swap " " glue 20 center. ;
74
75 : days-header. ( -- )
76     day-abbreviations2 join-words print ;
77
78 : days. ( year month -- )
79     [ 1 (day-of-week) dup [ "   " write ] times ]
80     [ (days-in-month) ] 2bi [1..b] [
81         [ day. ] [ + 7 mod zero? [ nl ] [ bl ] if ] bi
82     ] with each nl ;
83
84 PRIVATE>
85
86 : month. ( timestamp -- )
87     [ year>> ] [ month>> ] bi
88     [ month-header. ] [ days-header. days. ] 2bi ;
89
90 GENERIC: year. ( obj -- )
91
92 M: integer year.
93     dup number>string 64 center. nl 12 [1..b] [
94         [
95             [ month-name 20 center. ]
96             [ days-header. days. nl nl ] bi
97         ] with-string-writer split-lines
98     ] with map 3 <groups>
99     [ first3 [ "%-20s  %-20s  %-20s\n" printf ] 3each ] each ;
100
101 M: timestamp year. year>> year. ;
102
103 : timestamp>mdtm ( timestamp -- str )
104     [ { YYYY MM DD hh mm ss } formatted ] with-string-writer ;
105
106 : timestamp>ymd ( timestamp -- str )
107     [ YYYY-MM-DD ] with-string-writer ;
108
109 : timestamp>hms ( timestamp -- str )
110     [ hh:mm:ss ] with-string-writer ;
111
112 : timestamp>ymdhms ( timestamp -- str )
113     [ >gmt { YYYY-MM-DD " " hh:mm:ss } formatted ] with-string-writer ;
114
115 : write-gmt-offset-hhmm ( gmt-offset -- )
116     [ hour>> dup 0 < "-" "+" ? write abs write-00 ] [ mm ] bi ;
117
118 : write-gmt-offset-hh:mm ( gmt-offset -- )
119     [ hour>> dup 0 < "-" "+" ? write abs write-00 ":" write ] [ mm ] bi ;
120
121 : write-gmt-offset ( gmt-offset -- )
122     dup instant = [ drop "GMT" write ] [ write-gmt-offset-hhmm ] if ;
123
124 : write-gmt-offset-z ( gmt-offset -- )
125     dup instant = [ drop "Z" write ] [ write-gmt-offset-hh:mm ] if ;
126
127 : write-rfc1036 ( timestamp -- )
128     {
129         DAY ", " DD "-" MONTH "-" YYYY " " hh:mm:ss " "
130         [ gmt-offset>> write-gmt-offset ]
131     } formatted ;
132
133 : timestamp>rfc1036 ( timestamp -- str )
134     [ write-rfc1036 ] with-string-writer ;
135
136 ! RFC850 obsoleted by RFC1036
137 ALIAS: write-rfc850 write-rfc1036
138 ALIAS: timestamp>rfc850 timestamp>rfc1036
139
140 : write-rfc2822 ( timestamp -- )
141     {
142         DAY ", " D " " MONTH " " YYYY " " hh:mm:ss " "
143         [ gmt-offset>> write-gmt-offset ]
144     } formatted ;
145
146 : timestamp>rfc2822 ( timestamp -- str )
147     [ write-rfc2822 ] with-string-writer ;
148
149 ! RFC822 obsoleted by RFC2822
150 ALIAS: write-rfc822 write-rfc2822
151 ALIAS: timestamp>rfc822 timestamp>rfc2822
152
153 : write-rfc3339 ( timestamp -- )
154     {
155         YYYY-MM-DD "T" hh:mm:ss.SSSSSS
156         [ gmt-offset>> write-gmt-offset-z ]
157     } formatted ;
158
159 : timestamp>rfc3339 ( timestamp -- str )
160     [ write-rfc3339 ] with-string-writer ;
161
162 : write-iso8601 ( timestamp -- )
163     {
164         YYYY-MM-DD "T" hh:mm:ss.SSSSSS
165         [ gmt-offset>> write-gmt-offset-hh:mm ]
166     } formatted ;
167
168 : timestamp>iso8601 ( timestamp -- str )
169     [ write-iso8601 ] with-string-writer ;
170
171 : write-ctime ( timestamp -- )
172     {
173         DAY " " MONTH " " DD " " hh:mm:ss " " YYYY
174     } formatted ;
175
176 : timestamp>ctime-string ( timestamp -- str )
177     [ write-ctime ] with-string-writer ;
178
179 : timestamp>git-string ( timestamp -- str )
180     [
181         {
182             DAY " " MONTH " " D " " hh:mm:ss " " YYYY " "
183             [ gmt-offset>> write-gmt-offset-hhmm ]
184         } formatted
185     ] with-string-writer ;
186
187 : timestamp>http-string ( timestamp -- str )
188     >gmt timestamp>rfc2822 ;
189
190 : timestamp>cookie-string ( timestamp -- str )
191     >gmt timestamp>rfc1036 ;
192
193 : write-timestamp ( timestamp -- )
194     { DAY ", " D " " MONTH " " YYYY " " hh:mm:ss } formatted ;
195
196 : timestamp>string ( timestamp -- str )
197     [ write-timestamp ] with-string-writer ;
198
199 M: timestamp present timestamp>string ;
200
201 : duration>hm ( duration -- str )
202     [ duration>hours >integer 24 mod pad-00 ]
203     [ duration>minutes >integer 60 mod pad-00 ] bi ":" glue ;
204
205 : duration>hms ( duration -- str )
206     [ duration>hm ]
207     [ duration>seconds >integer 60 mod pad-00 ] bi ":" glue ;
208
209 : duration>human-readable ( duration -- string )
210     [
211         [
212             duration>years >integer
213             [
214                 [ number>string write ]
215                 [ 1 > " years, " " year, " ? write ] bi
216             ] unless-zero
217         ] [
218             duration>days >integer 365 mod
219             [
220                 [ number>string write ]
221                 [ 1 > " days, " " day, " ? write ] bi
222             ] unless-zero
223         ] [ duration>hms write ] tri
224     ] with-string-writer ;
225
226 GENERIC: elapsed-time ( seconds -- string )
227
228 M: integer elapsed-time
229     dup 0 < [ "negative seconds" throw ] when [
230         {
231             { 60 "s" }
232             { 60 "m" }
233             { 24 "h" }
234             {  7 "d" }
235             { 52 "w" }
236             {  f "y" }
237         } [
238             [ first [ /mod ] [ dup ] if* ] [ second ] bi swap
239             dup 0 > [ number>string prepend , ] [ 2drop ] if
240         ] each drop
241     ] { } make [ "0s" ] [ reverse join-words ] if-empty ;
242
243 M: real elapsed-time
244     >integer elapsed-time ;
245
246 M: duration elapsed-time
247     duration>seconds elapsed-time ;
248
249 M: timestamp elapsed-time
250     now swap time- elapsed-time ;
251
252 ! XXX: Anything up to 2 hours is "about an hour"
253 : relative-time-offset ( seconds -- string )
254     abs {
255         { [ dup 1 < ] [ drop "just now" ] }
256         { [ dup 60 < ] [ drop "less than a minute" ] }
257         { [ dup 120 < ] [ drop "about a minute" ] }
258         { [ dup 2700 < ] [ 60 /i "%d minutes" sprintf ] }
259         { [ dup 7200 < ] [ drop "about an hour" ] }
260         { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
261         { [ dup 172800 < ] [ drop "1 day" ] }
262         [ 86400 /i "%d days" sprintf ]
263     } cond ;
264
265 GENERIC: relative-time ( seconds -- string )
266
267 M: real relative-time
268     [ relative-time-offset ] [
269         dup abs 1 < [
270             drop
271         ] [
272             0 < "hence" "ago" ? " " glue
273         ] if
274     ] bi ;
275
276 M: duration relative-time
277     duration>seconds relative-time ;
278
279 M: timestamp relative-time
280     now swap time- relative-time ;