]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/format/format.factor
calendar.format: change rfc3339 to always print microseconds.
[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 arrays calendar calendar.format.macros
4 combinators io io.streams.string kernel math math.functions
5 math.order math.parser math.parser.private present sequences
6 typed ;
7 IN: calendar.format
8
9 : pad-00 ( n -- str ) number>string 2 CHAR: 0 pad-head ;
10
11 : pad-0000 ( n -- str ) number>string 4 CHAR: 0 pad-head ;
12
13 : pad-00000 ( n -- str ) number>string 5 CHAR: 0 pad-head ;
14
15 : write-00 ( n -- ) pad-00 write ;
16
17 : write-0000 ( n -- ) pad-0000 write ;
18
19 : write-00000 ( n -- ) pad-00000 write ;
20
21 : hh ( time -- ) hour>> write-00 ;
22
23 : mm ( time -- ) minute>> write-00 ;
24
25 : ss ( time -- ) second>> >integer write-00 ;
26
27 : D ( time -- ) day>> number>string write ;
28
29 : DD ( time -- ) day>> write-00 ;
30
31 : DAY ( time -- ) day-of-week day-abbreviation3 write ;
32
33 : MM ( time -- ) month>> write-00 ;
34
35 : MONTH ( time -- ) month>> month-abbreviation write ;
36
37 : YYYY ( time -- ) year>> write-0000 ;
38
39 : YYYYY ( time -- ) year>> write-00000 ;
40
41 : expect ( str -- )
42     read1 swap member? [ "Parse error" throw ] unless ;
43
44 : read-00 ( -- n ) 2 read string>number ;
45
46 : read-000 ( -- n ) 3 read string>number ;
47
48 : read-0000 ( -- n ) 4 read string>number ;
49
50 : hhmm>timestamp ( hhmm -- timestamp )
51     [
52         0 0 0 read-00 read-00 0 instant <timestamp>
53     ] with-string-reader ;
54
55 GENERIC: day. ( obj -- )
56
57 M: integer day. ( n -- )
58     number>string dup length 2 < [ bl ] when write ;
59
60 M: timestamp day. ( timestamp -- )
61     day>> day. ;
62
63 GENERIC: month. ( obj -- )
64
65 M: array month. ( pair -- )
66     first2
67     [ month-name write bl number>string print ]
68     [ 1 zeller-congruence ]
69     [ (days-in-month) day-abbreviations2 " " join print ] 2tri
70     over "   " <repetition> "" concat-as write
71     [
72         [ 1 + day. ] keep
73         1 + + 7 mod zero? [ nl ] [ bl ] if
74     ] with each-integer nl ;
75
76 M: timestamp month. ( timestamp -- )
77     [ year>> ] [ month>> ] bi 2array month. ;
78
79 GENERIC: year. ( obj -- )
80
81 M: integer year. ( n -- )
82     12 [ 1 + 2array month. nl ] with each-integer ;
83
84 M: timestamp year. ( timestamp -- )
85     year>> year. ;
86
87 : timestamp>mdtm ( timestamp -- str )
88     [ { YYYY MM DD hh mm ss } formatted ] with-string-writer ;
89
90 : (timestamp>string) ( timestamp -- )
91     { DAY ", " D " " MONTH " " YYYY " " hh ":" mm ":" ss } formatted ;
92
93 : timestamp>string ( timestamp -- str )
94     [ (timestamp>string) ] with-string-writer ;
95
96 : (write-gmt-offset) ( duration -- )
97     [ hh ] [ mm ] bi ;
98
99 : write-gmt-offset ( gmt-offset -- )
100     dup instant <=> {
101         { +eq+ [ drop "GMT" write ] }
102         { +lt+ [ "-" write before (write-gmt-offset) ] }
103         { +gt+ [ "+" write (write-gmt-offset) ] }
104     } case ;
105
106 : timestamp>rfc822 ( timestamp -- str )
107     #! RFC822 timestamp format
108     #! Example: Tue, 15 Nov 1994 08:12:31 +0200
109     [
110         [ (timestamp>string) bl ]
111         [ gmt-offset>> write-gmt-offset ]
112         bi
113     ] with-string-writer ;
114
115 : timestamp>http-string ( timestamp -- str )
116     #! http timestamp format
117     #! Example: Tue, 15 Nov 1994 08:12:31 GMT
118     >gmt timestamp>rfc822 ;
119
120 : (timestamp>cookie-string) ( timestamp -- )
121     >gmt
122     { DAY ", " DD "-" MONTH "-" YYYY " " hh ":" mm ":" ss " GMT" } formatted ;
123
124 : timestamp>cookie-string ( timestamp -- str )
125     [ (timestamp>cookie-string) ] with-string-writer ;
126
127 : (write-rfc3339-gmt-offset) ( duration -- )
128     [ hh ":" write ] [ mm ] bi ;
129
130 : write-rfc3339-gmt-offset ( duration -- )
131     dup instant <=> {
132         { +eq+ [ drop "Z" write ] }
133         { +lt+ [ "-" write before (write-rfc3339-gmt-offset) ] }
134         { +gt+ [ "+" write (write-rfc3339-gmt-offset) ] }
135     } case ;
136
137 ! Should be enough for anyone, allows to not do a fancy
138 ! algorithm to detect infinite decimals (e.g 1/3)
139 : ss.SSSSSS ( timestamp -- )
140     second>> >float "%.6f" format-float 9 CHAR: 0 pad-head write ;
141
142 : (timestamp>rfc3339) ( timestamp -- )
143     {
144         YYYY "-" MM "-" DD "T" hh ":" mm ":" ss.SSSSSS
145         [ gmt-offset>> write-rfc3339-gmt-offset ]
146     } formatted ;
147
148 : timestamp>rfc3339 ( timestamp -- str )
149     [ (timestamp>rfc3339) ] with-string-writer ;
150
151 : signed-gmt-offset ( dt ch -- dt' )
152     { { CHAR: + [ 1 ] } { CHAR: - [ -1 ] } } case time* ;
153
154 : read-rfc3339-gmt-offset ( ch -- dt )
155     {
156         { f [ instant ] }
157         { CHAR: Z [ instant ] }
158         [
159             [
160                 read-00 hours
161                 read1 { { CHAR: : [ read-00 ] } { f [ 0 ] } } case minutes
162                 time+
163             ] dip signed-gmt-offset
164         ]
165     } case ;
166
167 : read-ymd ( -- y m d )
168     read-0000 "-" expect read-00 "-" expect read-00 ;
169
170 : read-hms ( -- h m s )
171     read-00 ":" expect read-00 ":" expect read-00 ;
172
173 : read-rfc3339-seconds ( s -- s' ch )
174     "+-Z" read-until [
175         [ string>number ] [ length 10^ ] bi / +
176     ] dip ;
177
178 : (rfc3339>timestamp) ( -- timestamp )
179     read-ymd
180     "Tt \t" expect
181     read-hms
182     read1 { { CHAR: . [ read-rfc3339-seconds ] } [ ] } case
183     read-rfc3339-gmt-offset
184     <timestamp> ;
185
186 : rfc3339>timestamp ( str -- timestamp )
187     [ (rfc3339>timestamp) ] with-string-reader ;
188
189 ERROR: invalid-timestamp-format ;
190
191 : check-timestamp ( obj/f -- obj )
192     [ invalid-timestamp-format ] unless* ;
193
194 : read-token ( seps -- token )
195     [ read-until ] keep member? check-timestamp drop ;
196
197 : read-sp ( -- token ) " " read-token ;
198
199 : checked-number ( str -- n )
200     string>number check-timestamp ;
201
202 : parse-rfc822-gmt-offset ( string -- dt )
203     dup "GMT" = [ drop instant ] [
204         unclip [
205             2 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+
206         ] dip signed-gmt-offset
207     ] if ;
208
209 : (rfc822>timestamp) ( -- timestamp )
210     timestamp new
211         "," read-token day-abbreviations3 member? check-timestamp drop
212         read1 CHAR: \s assert=
213         read-sp checked-number >>day
214         read-sp month-abbreviations index 1 + check-timestamp >>month
215         read-sp checked-number >>year
216         ":" read-token checked-number >>hour
217         ":" read-token checked-number >>minute
218         read-sp checked-number >>second
219         readln parse-rfc822-gmt-offset >>gmt-offset ;
220
221 : rfc822>timestamp ( str -- timestamp )
222     [ (rfc822>timestamp) ] with-string-reader ;
223
224 : check-day-name ( str -- )
225     [ day-abbreviations3 member? ] [ day-names member? ] bi or
226     check-timestamp drop ;
227
228 : (cookie-string>timestamp-1) ( -- timestamp )
229     timestamp new
230         "," read-token check-day-name
231         read1 CHAR: \s assert=
232         "-" read-token checked-number >>day
233         "-" read-token month-abbreviations index 1 + check-timestamp >>month
234         read-sp checked-number >>year
235         ":" read-token checked-number >>hour
236         ":" read-token checked-number >>minute
237         read-sp checked-number >>second
238         readln parse-rfc822-gmt-offset >>gmt-offset ;
239
240 : cookie-string>timestamp-1 ( str -- timestamp )
241     [ (cookie-string>timestamp-1) ] with-string-reader ;
242
243 : (cookie-string>timestamp-2) ( -- timestamp )
244     timestamp new
245         read-sp check-day-name
246         read-sp month-abbreviations index 1 + check-timestamp >>month
247         read-sp checked-number >>day
248         ":" read-token checked-number >>hour
249         ":" read-token checked-number >>minute
250         read-sp checked-number >>second
251         read-sp checked-number >>year
252         readln parse-rfc822-gmt-offset >>gmt-offset ;
253
254 : cookie-string>timestamp-2 ( str -- timestamp )
255     [ (cookie-string>timestamp-2) ] with-string-reader ;
256
257 : cookie-string>timestamp ( str -- timestamp )
258     {
259         [ cookie-string>timestamp-1 ]
260         [ cookie-string>timestamp-2 ]
261         [ rfc822>timestamp ]
262     } attempt-all-quots ;
263
264 : (ymdhms>timestamp) ( -- timestamp )
265     read-ymd " " expect read-hms instant <timestamp> ;
266
267 : ymdhms>timestamp ( str -- timestamp )
268     [ (ymdhms>timestamp) ] with-string-reader ;
269
270 : (hms>timestamp) ( -- timestamp )
271     0 0 0 read-hms instant <timestamp> ;
272
273 : hms>timestamp ( str -- timestamp )
274     [ (hms>timestamp) ] with-string-reader ;
275
276 : (ymd>timestamp) ( -- timestamp )
277     read-ymd <date-gmt> ;
278
279 : ymd>timestamp ( str -- timestamp )
280     [ (ymd>timestamp) ] with-string-reader ;
281
282 : (timestamp>ymd) ( timestamp -- )
283     { YYYY "-" MM "-" DD } formatted ;
284
285 TYPED: timestamp>ymd ( timestamp: timestamp -- str )
286     [ (timestamp>ymd) ] with-string-writer ;
287
288 : (timestamp>hms) ( timestamp -- )
289     { hh ":" mm ":" ss } formatted ;
290
291 TYPED: timestamp>hms ( timestamp: timestamp -- str )
292     [ (timestamp>hms) ] with-string-writer ;
293
294 : (timestamp>hm) ( timestamp -- )
295     { hh ":" mm } formatted ;
296
297 TYPED: timestamp>hm ( timestamp: timestamp -- str )
298     [ (timestamp>hm) ] with-string-writer ;
299
300 TYPED: timestamp>ymdhms ( timestamp: timestamp -- str )
301     [
302         >gmt
303         { (timestamp>ymd) " " (timestamp>hms) } formatted
304     ] with-string-writer ;
305
306 : file-time-string ( timestamp -- string )
307     [
308         {
309             MONTH " " DD " "
310             [
311                 dup now [ year>> ] same?
312                 [ [ hh ":" write ] [ mm ] bi ] [ YYYYY ] if
313             ]
314         } formatted
315     ] with-string-writer ;
316
317 M: timestamp present timestamp>string ;
318
319 TYPED: duration>hm ( duration: duration -- string )
320     [ duration>hours >integer 24 mod pad-00 ]
321     [ duration>minutes >integer 60 mod pad-00 ] bi ":" glue ;
322
323 TYPED: duration>human-readable ( duration: duration -- string )
324     [
325         [
326             duration>years >integer
327             [
328                 [ number>string write ]
329                 [ 1 > " years, " " year, " ? write ] bi
330             ] unless-zero
331         ] [
332             duration>days >integer 365 mod
333             [
334                 [ number>string write ]
335                 [ 1 > " days, " " day, " ? write ] bi
336             ] unless-zero
337         ] [ duration>hm write ] tri
338     ] with-string-writer ;