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 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/systemv"
19 "vocab:zoneinfo/leapseconds"
24 TUPLE: raw-zone name gmt-offset rules/save format until ;
25 TUPLE: raw-rule name from to type in on at-time save letters ;
26 TUPLE: raw-link from to ;
27 TUPLE: raw-leap year month day hms corr r/s ;
30 TUPLE: rule name from to at-time ;
32 : rule-to ( m string -- m n )
39 : parse-rule ( seq -- rule )
53 ] input<sequence raw-rule boa ;
55 : parse-zone ( seq -- zone )
62 } cleave raw-zone boa ;
64 : parse-partial-zone ( seq -- zone )
65 [ last-zone get name>> ] dip
71 } cleave raw-zone boa ;
73 : parse-link ( seq -- link )
80 ] input<sequence raw-link boa ;
82 : parse-leap ( seq -- link )
93 ] input<sequence raw-leap boa ;
95 : parse-line ( seq -- tuple )
98 { "zone" [ parse-zone dup last-zone set ] }
99 { "rule" [ parse-rule ] }
100 { "link" [ parse-link ] }
101 { "leap" [ parse-leap ] }
102 [ drop harvest parse-partial-zone ]
105 : parse-zoneinfo-file ( path -- seq )
107 [ "#" split1 drop ] map harvest
108 [ "\t " split harvest ] map harvest
109 [ [ parse-line ] map ] with-scope ;
111 MEMO: zoneinfo-files ( -- seq )
112 zoneinfo-paths [ parse-zoneinfo-file ] map ;
114 MEMO: zoneinfo-array ( -- seq )
115 zoneinfo-files concat ;
117 : raw-rule-map ( -- assoc )
118 zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
120 : raw-zone-map ( -- assoc )
121 zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
123 GENERIC: zone-matches? ( string rule -- ? )
125 M: raw-rule zone-matches? name>> = ;
126 M: raw-zone zone-matches? name>> = ;
127 M: raw-link zone-matches? from>> = ;
128 M: raw-leap zone-matches? 2drop f ;
130 : find-rules ( string -- rules )
132 [ [ to>> "max" = ] filter ] assoc-map at ;
134 ERROR: zone-not-found name ;
136 : find-zone ( string -- rules )
138 [ last ] assoc-map ?at [ zone-not-found ] unless ;
140 : find-zone-rules ( string -- zone rules )
141 find-zone dup rules/save>> find-rules ;
143 : number>value ( n -- n' )
151 : on>value ( n -- n' )
152 ! "3", "Thu>=8" always >=, "lastFri"
154 { [ dup 3 swap ?nth CHAR: > = ] [
155 3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
157 { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
161 : raw-rule>triple ( raw-rule -- quot )
163 [ from>> string>number ]
164 [ in>> month-abbreviation-index ]
168 ! "Europe/Helsinki" find-zone-rules
172 ! from - year or "min"
174 ! from "1938" or "min"
175 ! to "1945" or "max" or "only"
176 ! type "-" always "-"
177 ! in "Mar" -- 3-letter month name
178 ! on "26" or "Mon>=15" or lastSun lastFri
179 ! at "23:00s" "12:13:00s" "1:00s" "1:00u"
180 ! save "-0:00:05" "1:00" "0:14:15"
181 ! letters "S" or "-" or "AMT" "BDST"
184 ! name "Indian/Maldives"
185 ! gmt-offset "4:54:00" "9:55:56" "-9:55:56"
186 ! rules/save "-" "0:20" "0:30" "1:00" "AN" "W-Eur" "Winn" "Zion" "sol87" "sol88"
187 ! format "LMT" "%s" "%sT" "A%sT" "AC%sT" "ACT"
189 ! { "1847" "Dec" "1" "0:00s" }
190 ! { "1883" "Nov" "18" "12:12:57" }
191 ! { "1989" "Sep" "lastSun" "2:00s" }
194 ! T{ link { from "Asia/Riyadh88" } { to "Mideast/Riyadh88" } }