]> gitweb.factorcode.org Git - factor.git/blob - extra/zoneinfo/zoneinfo.factor
zoneinfo: Add updated files.
[factor.git] / extra / zoneinfo / zoneinfo.factor
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 ;
6 IN: zoneinfo
7
8 CONSTANT: zoneinfo-paths
9 {
10     "vocab:zoneinfo/africa"
11     "vocab:zoneinfo/antarctica"
12     "vocab:zoneinfo/asia"
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"
20 }
21
22 SYMBOL: last-zone
23
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 ;
28
29 TUPLE: zone name ;
30 TUPLE: rule name from to at-time ;
31
32 : rule-to ( m string -- m n )
33     {
34         { "only" [ dup ] }
35         { "max" [ 1/0. ] }
36         [ string>number ]
37     } case ;
38
39 : parse-rule ( seq -- rule )
40     [
41         {
42             [ drop ]
43             [ ]
44             [ ]
45             [ ]
46             [ ]
47             [ ]
48             [ ]
49             [ ]
50             [ ]
51             [ ]
52         } spread
53     ] input<sequence raw-rule boa ;
54
55 : parse-zone ( seq -- zone )
56     {
57         [ second ]
58         [ third ]
59         [ fourth ]
60         [ 4 swap nth ]
61         [ 5 tail harvest ]
62     } cleave raw-zone boa ;
63
64 : parse-partial-zone ( seq -- zone )
65     [ last-zone get name>> ] dip
66     {
67         [ first ]
68         [ second ]
69         [ 2 swap nth ]
70         [ 3 tail harvest ]
71     } cleave raw-zone boa ;
72
73 : parse-link ( seq -- link )
74     [
75         {
76             [ drop ]
77             [ ]
78             [ ]
79         } spread
80     ] input<sequence raw-link boa ;
81
82 : parse-leap ( seq -- link )
83     [
84         {
85             [ drop ]
86             [ ]
87             [ ]
88             [ ]
89             [ ]
90             [ ]
91             [ ]
92         } spread
93     ] input<sequence raw-leap boa ;
94
95 : parse-line ( seq -- tuple )
96     dup first >lower
97     {
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 ]
103     } case ;
104
105 : parse-zoneinfo-file ( path -- seq )
106     utf8 file-lines
107     [ "#" split1 drop ] map harvest
108     [ "\t " split harvest ] map harvest
109     [ [ parse-line ] map ] with-scope ;
110
111 MEMO: zoneinfo-files ( -- seq )
112     zoneinfo-paths [ parse-zoneinfo-file ] map ;
113
114 MEMO: zoneinfo-array ( -- seq )
115     zoneinfo-files concat ;
116
117 : raw-rule-map ( -- assoc )
118     zoneinfo-array [ raw-rule? ] filter [ name>> ] collect-by ;
119
120 : raw-zone-map ( -- assoc )
121     zoneinfo-array [ raw-zone? ] filter [ name>> ] collect-by ;
122
123 GENERIC: zone-matches? ( string rule -- ? )
124
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 ;
129
130 : find-rules ( string -- rules )
131     raw-rule-map
132     [ [ to>> "max" = ] filter ] assoc-map at ;
133
134 ERROR: zone-not-found name ;
135
136 : find-zone ( string -- rules )
137     raw-zone-map
138     [ last ] assoc-map ?at [ zone-not-found ] unless ;
139
140 : find-zone-rules ( string -- zone rules )
141     find-zone dup rules/save>> find-rules ;
142
143 : number>value ( n -- n' )
144     {
145         { "only" [ f ] }
146         { "min" [ f ] }
147         { "max" [ t ] }
148         [ string>number ]
149     } case ;
150
151 : on>value ( n -- n' )
152     ! "3", "Thu>=8" always >=, "lastFri"
153     {
154         { [ dup 3 swap ?nth CHAR: > = ] [
155             3 cut 2 tail [ day-abbreviation3-predicate ] [ string>number ] bi* 2array
156         ] }
157         { [ dup "last" head? ] [ 4 tail day-abbreviation3-index ] }
158         [ string>number ]
159     } cond ;
160
161 : raw-rule>triple ( raw-rule -- quot )
162     {
163         [ from>> string>number ]
164         [ in>> month-abbreviation-index ]
165         [ on>> on>value ]
166     } cleave>array ;
167
168 ! "Europe/Helsinki" find-zone-rules
169
170 ! Rule
171 ! name - string
172 ! from - year or "min"
173 ! name    "France"
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"
182
183 ! Zone
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"
188 ! until      { "1880" }
189     ! { "1847" "Dec" "1" "0:00s" }
190     ! { "1883" "Nov" "18" "12:12:57" }
191     ! { "1989" "Sep" "lastSun" "2:00s" }
192
193 ! Link
194 ! T{ link { from "Asia/Riyadh88" } { to "Mideast/Riyadh88" } }