1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar.english combinators
4 combinators.smart io.encodings.utf8 io.files kernel math.parser
5 memoize namespaces sequences sorting splitting unicode ;
8 CONSTANT: zoneinfo-paths
10 "vocab:zoneinfo/africa"
11 "vocab:zoneinfo/antarctica"
13 "vocab:zoneinfo/australasia"
14 "vocab:zoneinfo/europe"
15 "vocab:zoneinfo/northamerica"
16 "vocab:zoneinfo/pacificnew"
17 "vocab:zoneinfo/southamerica"
18 "vocab:zoneinfo/etcetera"
19 "vocab:zoneinfo/factory"
20 "vocab:zoneinfo/leapseconds"
21 "vocab:zoneinfo/systemv"
26 TUPLE: raw-zone name gmt-offset rules/save format until ;
27 TUPLE: raw-rule name from to type in on at-time save letters ;
28 TUPLE: raw-link from to ;
29 TUPLE: raw-leap year month day hms corr r/s ;
32 TUPLE: rule name from to at-time ;
34 : rule-to ( m string -- m n )
41 : parse-rule ( seq -- rule )
55 ] input<sequence raw-rule boa ;
57 : parse-zone ( seq -- zone )
64 } cleave raw-zone boa ;
66 : parse-partial-zone ( seq -- zone )
67 [ last-zone get name>> ] dip
73 } cleave raw-zone boa ;
75 : parse-link ( seq -- link )
82 ] input<sequence raw-link boa ;
84 : parse-leap ( seq -- link )
95 ] input<sequence raw-leap boa ;
97 : parse-line ( seq -- tuple )
100 { "zone" [ parse-zone dup last-zone set ] }
101 { "rule" [ parse-rule ] }
102 { "link" [ parse-link ] }
103 { "leap" [ parse-leap ] }
104 [ drop harvest parse-partial-zone ]
107 : parse-zoneinfo-file ( path -- seq )
109 [ "#" split1 drop ] map harvest
110 [ "\t " split harvest ] map harvest
111 [ [ parse-line ] map ] with-scope ;
113 MEMO: zoneinfo-files ( -- seq )
114 zoneinfo-paths [ parse-zoneinfo-file ] map ;
116 MEMO: zoneinfo-array ( -- seq )
117 zoneinfo-files concat ;
119 : raw-rule-map ( -- assoc )
120 zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
122 : raw-zone-map ( -- assoc )
123 zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
125 : zoneinfo-zones ( -- seq )
127 [ "/" swap subseq? ] partition
128 [ natural-sort ] bi@ append ;
130 GENERIC: zone-matches? ( string rule -- ? )
132 M: raw-rule zone-matches? name>> = ;
133 M: raw-zone zone-matches? name>> = ;
134 M: raw-link zone-matches? from>> = ;
135 M: raw-leap zone-matches? 2drop f ;
137 : find-rules ( string -- rules )
139 [ [ to>> "max" = ] filter ] assoc-map at ;
141 ERROR: zone-not-found name ;
143 : find-zone ( string -- rules )
145 [ last ] assoc-map ?at [ zone-not-found ] unless ;
147 : find-zone-rules ( string -- zone rules )
148 find-zone dup rules/save>> find-rules ;
150 : number>value ( n -- n' )
158 : on>value ( n -- n' )
159 ! "3", "Thu>=8" always >=, "lastFri"
161 { [ dup 3 swap ?nth CHAR: > = ] [
162 3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
164 { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
168 : raw-rule>triple ( raw-rule -- quot )
170 [ from>> string>number ]
171 [ in>> month-abbreviation-index ]
175 ! "Europe/Helsinki" find-zone-rules
179 ! from - year or "min"
181 ! from "1938" or "min"
182 ! to "1945" or "max" or "only"
183 ! type "-" always "-"
184 ! in "Mar" -- 3-letter month name
185 ! on "26" or "Mon>=15" or lastSun lastFri
186 ! at "23:00s" "12:13:00s" "1:00s" "1:00u"
187 ! save "-0:00:05" "1:00" "0:14:15"
188 ! letters "S" or "-" or "AMT" "BDST"
191 ! name "Indian/Maldives"
192 ! gmt-offset "4:54:00" "9:55:56" "-9:55:56"
193 ! rules/save "-" "0:20" "0:30" "1:00" "AN" "W-Eur" "Winn" "Zion" "sol87" "sol88"
194 ! format "LMT" "%s" "%sT" "A%sT" "AC%sT" "ACT"
196 ! { "1847" "Dec" "1" "0:00s" }
197 ! { "1883" "Nov" "18" "12:12:57" }
198 ! { "1989" "Sep" "lastSun" "2:00s" }
201 ! T{ link { from "Asia/Riyadh88" } { to "Mideast/Riyadh88" } }