1 ! Copyright (C) 2009 Doug Coleman.
2 ! See https://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 countries grouping interval-maps
6 io.encodings.utf8 io.files kernel math math.parser namespaces
7 sequences 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/southamerica"
20 "vocab:zoneinfo/backzone"
21 "vocab:zoneinfo/etcetera"
24 CONSTANT: zoneinfo-extra-paths
26 "vocab:zoneinfo/backward"
27 "vocab:zoneinfo/factory"
28 "vocab:zoneinfo/leapseconds"
31 : zoneinfo-lines ( path -- seq )
33 [ { [ length 0 = ] [ "#" head? ] } 1|| ] reject
34 [ "#" split1-last drop ] map ;
36 TUPLE: zonetab codes lat lng tz comments ;
39 MEMO: zoneinfo-country-zones ( -- seq )
40 "vocab:zoneinfo/zone1970.tab" zoneinfo-lines
44 [ "-+" split* first4 [ append ] 2dip append ] 2dip
48 : parse-zonetabs ( -- seq )
49 zoneinfo-country-zones
50 [ [ codes>> ] [ tz>> ] bi [ 2array ] curry map ] map concat ;
52 : lookup-country-name ( seq -- seq' ) alpha-2 ?at drop ; inline
53 : lookup-country-names ( seq -- seq' ) [ lookup-country-name ] map ;
55 : timezone>country-map ( -- alist )
56 parse-zonetabs [ nip ] collect-key-by ;
58 : country>timezones-map ( -- alist )
59 parse-zonetabs [ drop ] collect-value-by ;
61 : country-timezones-map ( -- alist )
62 country>timezones-map [ dup lookup-country-names zip ] map-values ;
64 TUPLE: raw-zone name gmt-offset rules/save format until ;
65 TUPLE: raw-rule name from to type in on at-time save letters ;
66 TUPLE: raw-link from to ;
67 TUPLE: raw-leap year month day hms corr r/s ;
70 ! TUPLE: rule name from to at-time ;
72 ! : rule-to ( m string -- m n )
79 : parse-rule ( seq -- rule )
81 { [ drop ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] } spread
82 ] input<sequence raw-rule boa ;
84 : parse-link ( seq -- link )
86 { [ drop ] [ ] [ ] } spread
87 ] input<sequence raw-link boa ;
89 : parse-leap ( seq -- link )
91 { [ drop ] [ ] [ ] [ ] [ ] [ ] [ ] } spread
92 ] input<sequence raw-leap boa ;
94 : parse-zone ( seq -- zone )
101 } cleave raw-zone boa ;
103 : parse-rest-of-zone ( prev seq -- zone )
109 } cleave raw-zone boa ;
111 : parse-zoneinfo-line ( prev/f seq -- tuple )
114 { "rule" [ nip parse-rule ] }
115 { "link" [ nip parse-link ] }
116 { "leap" [ nip parse-leap ] }
117 { "zone" [ nip parse-zone ] }
118 [ drop harvest parse-rest-of-zone ]
121 : parse-zoneinfo-file ( path -- seq )
123 [ "\t " split harvest ] map harvest
124 [ parse-zoneinfo-line ] map-with-previous ;
126 MEMO: zoneinfo-files ( -- seq )
127 zoneinfo-paths [ parse-zoneinfo-file ] map ;
129 MEMO: zoneinfo-array ( -- seq )
130 zoneinfo-files concat ;
132 MEMO: zoneinfo-assoc ( -- assoc )
133 zoneinfo-paths [ dup parse-zoneinfo-file ] { } map>assoc ;
135 : raw-rule-map ( -- assoc )
136 zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
138 : current-rule-map ( -- assoc )
140 [ [ to>> "max" = ] filter ] assoc-map
143 : raw-zone-map ( -- assoc )
144 zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
146 : zoneinfo-zones ( -- seq )
148 [ "/" subseq-of? ] partition
149 [ sort ] bi@ append ;
151 GENERIC: zone-matches? ( string rule -- ? )
153 M: raw-rule zone-matches? name>> = ;
154 M: raw-link zone-matches? from>> = ;
155 M: raw-leap zone-matches? 2drop f ;
156 M: raw-zone zone-matches? name>> = ;
158 : find-rules ( country -- rules )
160 [ [ to>> "max" = ] filter ] assoc-map at ;
162 ERROR: zone-not-found name ;
164 : find-zone ( timezone -- zone )
166 [ last ] assoc-map ?at [ zone-not-found ] unless ;
168 : timezone>rules ( timezone -- rules )
171 : find-zone-rules ( timezone -- zone rules )
172 find-zone dup rules/save>> find-rules ;
174 : zone-abbrevs ( -- assoc )
179 letters>> dup { "D" "S" } member? [ drop "" ] unless
181 [ 1 tail surround ] [ nip ] if*
182 ] with V{ } map-as sets:members
185 : number>value ( n -- n' )
193 : on>value ( n -- n' )
194 ! "3", "Thu>=8" always >=, "lastFri"
196 { [ dup 3 swap ?nth CHAR: > = ] [
197 3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
199 { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
203 : zone-month ( timestamp month -- timestamp' )
204 month-abbreviation-index >>month ;
206 ERROR: unknown-day-abbrev day ;
207 : day-abbrev>= ( timestamp day -- timestamp' )
209 { "Sun" [ sunday>= ] }
210 { "Mon" [ monday>= ] }
211 { "Tue" [ tuesday>= ] }
212 { "Wed" [ wednesday>= ] }
213 { "Thu" [ thursday>= ] }
214 { "Fri" [ friday>= ] }
215 { "Sat" [ saturday>= ] }
216 [ unknown-day-abbrev ]
219 : day-abbrev<= ( timestamp day -- timestamp' )
221 { "Sun" [ sunday<= ] }
222 { "Mon" [ monday<= ] }
223 { "Tue" [ tuesday<= ] }
224 { "Wed" [ wednesday<= ] }
225 { "Thu" [ thursday<= ] }
226 { "Fri" [ friday<= ] }
227 { "Sat" [ saturday<= ] }
228 [ unknown-day-abbrev ]
231 : comparison-day-string ( timestamp string -- timestamp )
233 { [ dup ">=" subseq-of? ] [ ">=" split1 swap [ string>number >>day ] dip day-abbrev>= ] }
234 { [ dup "<=" subseq-of? ] [ "<=" split1 swap [ string>number >>day ] dip day-abbrev<= ] }
235 [ string>number >>day ]
238 ERROR: unknown-last-day string ;
240 : last-day-string ( timestamp string -- timestamp )
242 { "lastSun" [ last-sunday-of-month ] }
243 { "lastMon" [ last-monday-of-month ] }
244 { "lastTue" [ last-tuesday-of-month ] }
245 { "lastWed" [ last-wednesday-of-month ] }
246 { "lastThu" [ last-thursday-of-month ] }
247 { "lastFri" [ last-friday-of-month ] }
248 { "lastSat" [ last-saturday-of-month ] }
252 ! "lastFri" | "Fri<=1" | "Sat>=2" | "15"
253 : zone-day ( timestamp text -- timestamp' )
257 comparison-day-string
260 : string>year ( str -- year )
261 string>number <year-gmt> ;
263 : rule-year>years ( raw-rule -- from to )
264 [ from>> ] [ to>> ] bi
266 { [ over "min" = ] [ [ drop -1/0. ] [ string>year ] bi* ] }
267 { [ dup "max" = ] [ [ string>year ] [ drop 1/0. ] bi* ] }
268 { [ dup "only" = ] [ drop dup [ string>year ] bi@ ] }
269 [ [ string>year ] bi@ ]
272 : parse-hms ( str -- hms-seq )
273 ":" split [ string>number ] map 3 0 pad-tail ;
275 : parse-offset ( str -- hms-seq )
276 "-" ?head [ parse-hms ] dip [ [ neg ] map ] when ;
278 ! XXX: Don't just drop the s/u, e.g. 2:00:00s
279 : zone-time ( timestamp time -- timestamp' )
280 [ Letter? ] split-tail drop
281 parse-offset first3 set-time ;
283 : hm>duration ( str -- duration )
284 ":" split1 "0" or [ string>number ] bi@
285 [ instant ] 2dip 0 set-time ;
287 : hms>duration ( str -- duration )
288 ":" split 3 "0" pad-tail
289 [ string>number ] map first3
290 [ instant ] 3dip set-time ;
292 : rule>timestamp-rest ( timestamp zone -- from )
294 [ over fp-infinity? [ drop ] [ in>> month-abbreviation-index >>month ] if ]
295 [ over fp-infinity? [ drop ] [ on>> zone-day ] if ]
296 [ over fp-infinity? [ drop ] [ at-time>> zone-time ] if ]
299 : rule>timestamps ( zone -- from to )
300 [ rule-year>years ] keep
301 [ nip rule>timestamp-rest ]
302 [ nipd rule>timestamp-rest ] 3bi ;
304 : until>timestamp ( seq -- unix-time )
306 4 f pad-tail first4 {
307 [ string>number <year-gmt> ]
308 [ [ zone-month ] when* ]
309 [ [ zone-day ] when* ]
310 [ [ zone-time ] when* ]
311 } spread timestamp>unix-time
314 : raw-zones>interval-map ( raw-zones -- interval-map )
316 [ until>> until>timestamp ] map
317 -1/0. prefix 2 <clumps> [ >array ] map
319 [ first2 1 - 2array ] map-keys <interval-map> ;
321 : timezone>interval-map ( timezone-name -- interval-map )
322 raw-zone-map at raw-zones>interval-map ;
324 : gmt-offset ( timestamp timezone-name -- gmt-offset )
325 [ timestamp>unix-time ]
326 [ raw-zones>interval-map ] bi* interval-at ;
328 : name>rules ( name -- rules )
331 [ rule>timestamps [ dup fp-infinity? [ timestamp>unix-time ] unless ] bi@ 2array ]
332 [ [ save>> hms>duration ] [ letters>> ] bi 2array ] bi 2array
336 : chicago-zones ( -- interval-map ) "America/Chicago" timezone>interval-map ;
337 : us-rules ( -- rules ) "US" name>rules ;
338 : us-timezones ( -- timezones )
339 country>timezones-map "US" of ;
341 : puerto-rico-timezone-countries ( -- countries )
342 timezone>country-map "America/Puerto_Rico" of lookup-country-names ;