1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar combinators
4 combinators.smart io.encodings.utf8 io.files kernel math.parser
5 memoize namespaces sequences 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/solar87"
18 "vocab:zoneinfo/solar88"
19 "vocab:zoneinfo/solar89"
20 "vocab:zoneinfo/southamerica"
21 "vocab:zoneinfo/systemv"
22 "vocab:zoneinfo/leapseconds"
27 TUPLE: raw-zone name gmt-offset rules/save format until ;
28 TUPLE: raw-rule name from to type in on at-time save letters ;
29 TUPLE: raw-link from to ;
30 TUPLE: raw-leap year month day hms corr r/s ;
33 TUPLE: rule name from to at-time ;
35 : rule-to ( m string -- m n )
42 : parse-rule ( seq -- rule )
56 ] input<sequence raw-rule boa ;
58 : parse-zone ( seq -- zone )
65 } cleave raw-zone boa ;
67 : parse-partial-zone ( seq -- zone )
68 [ last-zone get name>> ] dip
74 } cleave raw-zone boa ;
76 : parse-link ( seq -- link )
83 ] input<sequence raw-link boa ;
85 : parse-leap ( seq -- link )
96 ] input<sequence raw-leap boa ;
98 : parse-line ( seq -- tuple )
101 { "zone" [ parse-zone dup last-zone set ] }
102 { "rule" [ parse-rule ] }
103 { "link" [ parse-link ] }
104 { "leap" [ parse-leap ] }
105 [ drop harvest parse-partial-zone ]
108 : parse-zoneinfo-file ( path -- seq )
110 [ "#" split1 drop ] map harvest
111 [ "\t " split harvest ] map harvest
112 [ [ parse-line ] map ] with-scope ;
114 MEMO: zoneinfo-files ( -- seq )
115 zoneinfo-paths [ parse-zoneinfo-file ] map ;
117 MEMO: zoneinfo-array ( -- seq )
118 zoneinfo-files concat ;
120 : raw-rule-map ( -- assoc )
121 zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
123 : raw-zone-map ( -- assoc )
124 zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
126 GENERIC: zone-matches? ( string rule -- ? )
128 M: raw-rule zone-matches? name>> = ;
129 M: raw-zone zone-matches? name>> = ;
130 M: raw-link zone-matches? from>> = ;
131 M: raw-leap zone-matches? 2drop f ;
133 : find-rules ( string -- rules )
135 [ [ to>> "max" = ] filter ] assoc-map at ;
137 ERROR: zone-not-found name ;
139 : find-zone ( string -- rules )
141 [ last ] assoc-map ?at [ zone-not-found ] unless ;
143 : find-zone-rules ( string -- zone rules )
144 find-zone dup rules/save>> find-rules ;
146 : number>value ( n -- n' )
154 : on>value ( n -- n' )
155 ! "3", "Thu>=8" always >=, "lastFri"
157 { [ dup 3 swap ?nth CHAR: > = ] [
158 3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
160 { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
164 : raw-rule>triple ( raw-rule -- quot )
166 [ from>> string>number ]
167 [ in>> month-abbreviation-index ]
171 ! "Europe/Helsinki" find-zone-rules
175 ! from - year or "min"
177 ! from "1938" or "min"
178 ! to "1945" or "max" or "only"
179 ! type "-" always "-"
180 ! in "Mar" -- 3-letter month name
181 ! on "26" or "Mon>=15" or lastSun lastFri
182 ! at "23:00s" "12:13:00s" "1:00s" "1:00u"
183 ! save "-0:00:05" "1:00" "0:14:15"
184 ! letters "S" or "-" or "AMT" "BDST"
187 ! name "Indian/Maldives"
188 ! gmt-offset "4:54:00" "9:55:56" "-9:55:56"
189 ! rules/save "-" "0:20" "0:30" "1:00" "AN" "W-Eur" "Winn" "Zion" "sol87" "sol88"
190 ! format "LMT" "%s" "%sT" "A%sT" "AC%sT" "ACT"
192 ! { "1847" "Dec" "1" "0:00s" }
193 ! { "1883" "Nov" "18" "12:12:57" }
194 ! { "1989" "Sep" "lastSun" "2:00s" }
197 ! T{ link { from "Asia/Riyadh88" } { to "Mideast/Riyadh88" } }