1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs assocs.extras calendar
4 calendar.english combinators combinators.short-circuit
5 combinators.smart csv grouping interval-maps io.encodings.utf8
6 io.files kernel math math.parser memoize namespaces sequences
7 sequences.extras sorting splitting splitting.extras ;
11 CONSTANT: zoneinfo-paths
13 "vocab:zoneinfo/africa"
14 "vocab:zoneinfo/antarctica"
16 "vocab:zoneinfo/australasia"
17 "vocab:zoneinfo/europe"
18 "vocab:zoneinfo/northamerica"
19 "vocab:zoneinfo/pacificnew"
20 "vocab:zoneinfo/southamerica"
21 "vocab:zoneinfo/backzone"
24 CONSTANT: zoneinfo-extra-paths
26 "vocab:zoneinfo/backward"
27 "vocab:zoneinfo/etcetera"
28 "vocab:zoneinfo/factory"
29 "vocab:zoneinfo/leapseconds"
30 "vocab:zoneinfo/systemv"
33 : zoneinfo-lines ( path -- seq )
36 { [ length 0 = ] [ "#" head? ] } 1||
39 TUPLE: zonetab codes lat lng tz comments ;
41 MEMO: zoneinfo-country-zones ( -- seq )
42 "vocab:zoneinfo/zone1970.tab" zoneinfo-lines
46 [ "-+" split* first4 [ append ] 2dip append ] 2dip
50 : parse-zonetabs ( -- seq )
51 zoneinfo-country-zones
52 [ [ codes>> ] [ tz>> ] bi [ 2array ] curry map ] map concat ;
54 MEMO: tz>country-map ( -- alist )
55 parse-zonetabs [ first ] collect-value-by ;
57 MEMO: country>tzs-map ( -- alist )
58 parse-zonetabs [ second ] collect-key-by ;
62 TUPLE: raw-zone name gmt-offset rules/save format until ;
63 TUPLE: raw-rule name from to type in on at-time save letters ;
64 TUPLE: raw-link from to ;
65 TUPLE: raw-leap year month day hms corr r/s ;
68 TUPLE: rule name from to at-time ;
70 : rule-to ( m string -- m n )
77 : parse-rule ( seq -- rule )
79 { [ drop ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] } spread
80 ] input<sequence raw-rule boa ;
82 : parse-link ( seq -- link )
84 { [ drop ] [ ] [ ] } spread
85 ] input<sequence raw-link boa ;
87 : parse-leap ( seq -- link )
89 { [ drop ] [ ] [ ] [ ] [ ] [ ] [ ] } spread
90 ] input<sequence raw-leap boa ;
92 : parse-zone ( seq -- zone )
99 } cleave raw-zone boa ;
101 : parse-partial-zone ( seq -- zone )
102 [ last-zone get name>> ] dip
108 } cleave raw-zone boa ;
110 : parse-zoneinfo-line ( seq -- tuple )
113 { "rule" [ parse-rule ] }
114 { "link" [ parse-link ] }
115 { "leap" [ parse-leap ] }
116 { "zone" [ parse-zone dup last-zone set ] }
117 [ drop harvest parse-partial-zone ]
120 : parse-zoneinfo-file ( path -- seq )
123 [ parse-zoneinfo-line ] map ;
125 MEMO: zoneinfo-files ( -- seq )
126 zoneinfo-paths [ parse-zoneinfo-file ] map ;
128 MEMO: zoneinfo-array ( -- seq )
129 zoneinfo-files concat ;
131 MEMO: zoneinfo-assoc ( -- assoc )
132 zoneinfo-paths [ dup parse-zoneinfo-file ] { } map>assoc ;
134 : raw-rule-map ( -- assoc )
135 zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
137 : current-rule-map ( -- assoc )
139 [ [ to>> "max" = ] filter ] assoc-map
142 : raw-zone-map ( -- assoc )
143 zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
145 : zoneinfo-zones ( -- seq )
147 [ "/" swap subseq? ] partition
148 [ natural-sort ] bi@ append ;
150 GENERIC: zone-matches? ( string rule -- ? )
152 M: raw-rule zone-matches? name>> = ;
153 M: raw-link zone-matches? from>> = ;
154 M: raw-leap zone-matches? 2drop f ;
155 M: raw-zone zone-matches? name>> = ;
157 : find-rules ( string -- rules )
159 [ [ to>> "max" = ] filter ] assoc-map at ;
161 ERROR: zone-not-found name ;
163 : find-zone ( string -- zone )
165 [ last ] assoc-map ?at [ zone-not-found ] unless ;
167 : find-zone-rules ( string -- zone rules )
168 find-zone dup rules/save>> find-rules ;
170 : zone-abbrevs ( -- assoc )
175 letters>> dup { "D" "S" } member? [ drop "" ] unless
177 [ 1 tail surround ] [ nip ] if*
178 ] with V{ } map-as sets:members
181 : number>value ( n -- n' )
189 : on>value ( n -- n' )
190 ! "3", "Thu>=8" always >=, "lastFri"
192 { [ dup 3 swap ?nth CHAR: > = ] [
193 3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
195 { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
199 : zone-month ( timestamp month -- timestamp' )
200 month-abbreviation-index >>month ;
202 ERROR: unknown-day-abbrev day ;
203 : day-abbrev>= ( timestamp day -- timestamp' )
205 { "Sun" [ sunday>= ] }
206 { "Mon" [ monday>= ] }
207 { "Tue" [ tuesday>= ] }
208 { "Wed" [ wednesday>= ] }
209 { "Thu" [ thursday>= ] }
210 { "Fri" [ friday>= ] }
211 { "Sat" [ saturday>= ] }
212 [ unknown-day-abbrev ]
215 : day-abbrev<= ( timestamp day -- timestamp' )
217 { "Sun" [ sunday<= ] }
218 { "Mon" [ monday<= ] }
219 { "Tue" [ tuesday<= ] }
220 { "Wed" [ wednesday<= ] }
221 { "Thu" [ thursday<= ] }
222 { "Fri" [ friday<= ] }
223 { "Sat" [ saturday<= ] }
224 [ unknown-day-abbrev ]
227 : comparison-day-string ( timestamp string -- timestamp )
229 { [ ">=" over subseq? ] [ ">=" split1 swap [ string>number >>day ] dip day-abbrev>= ] }
230 { [ "<=" over subseq? ] [ "<=" split1 swap [ string>number >>day ] dip day-abbrev<= ] }
231 [ string>number >>day ]
234 ERROR: unknown-last-day string ;
236 : last-day-string ( timestamp string -- timestamp )
238 { "lastSun" [ last-sunday-of-month ] }
239 { "lastMon" [ last-monday-of-month ] }
240 { "lastTue" [ last-tuesday-of-month ] }
241 { "lastWed" [ last-wednesday-of-month ] }
242 { "lastThu" [ last-thursday-of-month ] }
243 { "lastFri" [ last-friday-of-month ] }
244 { "lastSat" [ last-saturday-of-month ] }
248 ! "lastFri" | "Fri<=1" | "Sat>=2" | "15"
249 : zone-day ( timestamp text -- timestamp' )
253 comparison-day-string
256 : string>year ( str -- year )
257 string>number <year-gmt> ;
259 : rule-year>years ( rule -- from to )
260 [ from>> ] [ to>> ] bi
262 { [ over "min" = ] [ [ drop -1/0. ] [ string>year ] bi* ] }
263 { [ dup "max" = ] [ [ string>year ] [ drop 1/0. ] bi* ] }
264 { [ dup "only" = ] [ drop dup [ string>year ] bi@ ] }
265 [ [ string>year ] bi@ ]
268 : parse-hms ( str -- hms-seq )
269 ":" split [ string>number ] map 3 0 pad-tail ;
271 : parse-offset ( str -- hms-seq )
272 "-" ?head [ parse-hms ] dip [ [ neg ] map ] when ;
274 ! XXX: Don't just drop the s/u, e.g. 2:00:00s
275 : zone-time ( timestamp time -- timestamp' )
276 [ Letter? ] split-tail drop
277 parse-offset first3 set-time ;
279 : hm>duration ( str -- duration )
280 ":" split1 "0" or [ string>number ] bi@
281 [ instant ] 2dip 0 set-time ;
283 : rule>timestamp-rest ( timestamp zone -- from )
285 [ over fp-infinity? [ drop ] [ in>> month-abbreviation-index >>month ] if ]
286 [ over fp-infinity? [ drop ] [ on>> zone-day ] if ]
287 [ over fp-infinity? [ drop ] [ at-time>> zone-time ] if ]
290 : rule>timestamps ( zone -- from to )
291 [ rule-year>years ] keep
292 [ nip rule>timestamp-rest ]
293 [ nipd rule>timestamp-rest ] 3bi ;
295 : until>timestamp ( seq -- unix-time )
297 4 f pad-tail first4 {
298 [ string>number <year-gmt> ]
299 [ [ zone-month ] when* ]
300 [ [ zone-day ] when* ]
301 [ [ zone-time ] when* ]
302 } spread timestamp>unix-time
305 : zones>interval-map ( zones -- interval-map )
307 [ until>> until>timestamp ] map
308 -1/0. prefix 2 <clumps> [ >array ] map
310 [ first2 1 - 2array ] map-keys <interval-map> ;
312 : name>zones ( name -- interval-map )
313 raw-zone-map at zones>interval-map ;
315 : gmt-offset ( timestamp name -- gmt-offset )
316 [ timestamp>unix-time ]
317 [ zones>interval-map ] bi* interval-at ;
319 : name>rules ( name -- rules )
322 [ rule>timestamps [ dup fp-infinity? [ timestamp>unix-time ] unless ] bi@ 2array ]
323 [ [ save>> hm>duration ] [ letters>> ] bi 2array ] bi 2array
327 : chicago-zones ( -- interval-map ) "America/Chicago" name>zones ;
328 : us-rules ( -- rules ) "US" name>rules ;