]> gitweb.factorcode.org Git - factor.git/blob - extra/zoneinfo/zoneinfo.factor
Merge branch 'master' into simd-cleanup
[factor.git] / extra / zoneinfo / zoneinfo.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators combinators.smart io.encodings.utf8 io.files
4 kernel namespaces sequences splitting unicode.case accessors
5 math.parser calendar ;
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 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 ;
34 TUPLE: link ;
35 TUPLE: leap ;
36
37 : rule-to ( m string -- m n )
38     {
39         { "only" [ dup ] }
40         { "max" [ 1/0. ] }
41         [ string>number ]
42     } case ;
43
44 : raw-rule>rule ( raw-rule -- rule )
45     ;
46
47 : parse-rule ( seq -- rule )
48     [
49         {
50             [ drop ]
51             [ ]
52             [ ]
53             [ ]
54             [ ]
55             [ ]
56             [ ]
57             [ ]
58             [ ]
59             [ ]
60         } spread
61     ] input<sequence raw-rule boa ;
62
63 : raw-zone>zone ( raw-zone -- zone )
64     ;
65
66 : parse-zone ( seq -- zone )
67     {
68         [ second ]
69         [ third ]
70         [ fourth ]
71         [ 4 swap nth ]
72         [ 5 tail harvest ]
73     } cleave raw-zone boa ;
74
75 : parse-partial-zone ( seq -- zone )
76     [ last-zone get name>> ] dip
77     {
78         [ first ]
79         [ second ]
80         [ 2 swap nth ]
81         [ 3 tail harvest ]
82     } cleave raw-zone boa ;
83
84 : raw-link>link ( raw-link -- link )
85     ;
86
87 : parse-link ( seq -- link )
88     [
89         {
90             [ drop ]
91             [ ]
92             [ ]
93         } spread
94     ] input<sequence raw-link boa ;
95
96 : raw-leap>leap ( raw-leap -- leap )
97     ;
98
99 : parse-leap ( seq -- link )
100     [
101         {
102             [ drop ]
103             [ ]
104             [ ]
105             [ ]
106             [ ]
107             [ ]
108             [ ]
109         } spread
110     ] input<sequence raw-leap boa ;
111
112 : parse-line ( seq -- tuple )
113     dup first >lower
114     {
115         { "zone" [ parse-zone dup last-zone set raw-zone>zone ] }
116         { "rule" [ parse-rule raw-rule>rule ] }
117         { "link" [ parse-link raw-link>link ] }
118         { "leap" [ parse-leap raw-leap>leap ] }
119         [ drop harvest parse-partial-zone ]
120     } case ;
121
122 : parse-zoneinfo-file ( path -- seq )
123     utf8 file-lines
124     [ "#" split1 drop ] map harvest
125     [ "\t " split harvest ] map harvest
126     [ [ parse-line ] map ] with-scope ;
127
128 : load-zoneinfo-files ( -- seq )
129     zoneinfo-paths [ parse-zoneinfo-file ] map ;
130
131
132
133 ! Rule
134 ! name - string
135 ! from - year or "min"
136 ! name    "France"
137 ! from    "1938"  or "min"
138 ! to      "1945" or "max" or "only"
139 ! type    "-"  always "-"
140 ! in      "Mar"  -- 3-letter month name
141 ! on      "26"  or "Mon>=15"  or lastSun lastFri
142 ! at      "23:00s"  "12:13:00s" "1:00s" "1:00u"
143 ! save    "-0:00:05" "1:00" "0:14:15"
144 ! letters "S" or "-" or "AMT" "BDST"
145
146 ! Zone
147 ! name       "Indian/Maldives"
148 ! gmt-offset "4:54:00" "9:55:56" "-9:55:56"
149 ! rules/save "-" "0:20" "0:30" "1:00" "AN" "W-Eur" "Winn" "Zion" "sol87" "sol88"
150 ! format     "LMT" "%s" "%sT" "A%sT" "AC%sT" "ACT"
151 ! until      { "1880" }
152     ! { "1847" "Dec" "1" "0:00s" }
153     ! { "1883" "Nov" "18" "12:12:57" }
154     ! { "1989" "Sep" "lastSun" "2:00s" }
155
156 ! Link
157 ! T{ link { from "Asia/Riyadh88" } { to "Mideast/Riyadh88" } }