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