1 # Convert tzdata source into a smaller version of itself.
3 # Contributed by Paul Eggert. This file is in the public domain.
5 # This is not a general-purpose converter; it is designed for current tzdata.
6 # 'zic' should treat this script's output as if it were identical to
9 # Record a hash N for the new name NAME, checking for collisions.
11 function record_hash(n, name)
14 printf "# ! collision: %s %s\n", used_hashes[n], name
20 # Return a shortened rule name representing NAME,
21 # and record this relationship to the hash table.
23 function gen_rule_name(name, \
26 # Use a simple memonic: the first two letters.
27 n = substr(name, 1, 2)
29 # printf "# %s = %s\n", n, name
33 function prehash_rule_names( \
36 # Rule names are not part of the tzdb API, so substitute shorter
37 # ones. Shortening them consistently from one release to the next
38 # simplifies comparison of the output. That being said, the
39 # 1-letter names below are not standardized in any way, and can
40 # change arbitrarily from one release to the next, as the main goal
41 # here is compression not comparison.
43 # Abbreviating these rules names to one letter saved the most space
55 rule["Egypt"] = "K" # "Kemet" in ancient Egyptian
59 rule["Poland"] = "O" # arbitrary
60 rule["Palestine"] = "P"
61 rule["Cuba"] = "Q" # Its start sounds like "Q".
66 rule["Vincennes"] = "V"
68 rule["Mongol"] = "X" # arbitrary
74 rule["Algeria"] = "d" # country code DZ
76 rule["Taiwan"] = "f" # Formosa
81 rule["Chatham"] = "k" # arbitrary
84 rule["Tunisia"] = "n" # country code TN
85 rule["Moncton"] = "o" # arbitrary
87 rule["Albania"] = "q" # arbitrary
92 rule["Louisville"] = "v" # ville
93 rule["Iceland"] = "w" # arbitrary
94 rule["Chile"] = "x" # arbitrary
95 rule["Para"] = "y" # country code PY
96 rule["Romania"] = "z" # arbitrary
97 rule["Macau"] = "_" # arbitrary
99 # Use ISO 3166 alpha-2 country codes for remaining names that are countries.
100 # This is more systematic, and avoids collisions (e.g., Malta and Moldova).
101 rule["Armenia"] = "AM"
107 rule["Bahamas"] = "BS"
108 rule["Belize"] = "BZ"
112 rule["Cyprus"] = "CY"
114 rule["Germany"] = "DE"
116 rule["Ecuador"] = "EC"
117 rule["Finland"] = "FI"
127 rule["Kyrgyz"] = "KG"
129 rule["Latvia"] = "LV"
131 rule["Moldova"] = "MD"
133 rule["Mauritius"] = "MU"
134 rule["Namibia"] = "NA"
136 rule["Norway"] = "NO"
139 rule["Pakistan"] = "PK"
143 rule["Vanuatu"] = "VU"
146 rule["Detroit"] = "Dt" # De = Denver
149 record_hash(rule[name], name)
153 # Process the input line LINE and save it for later output.
155 function process_input_line(line, \
156 field, end, i, n, startdef, \
157 linkline, ruleline, zoneline)
159 # Remove comments, normalize spaces, and append a space to each line.
162 gsub(/[\t ]+/, " ", line)
164 # Abbreviate keywords and determine line type.
165 linkline = sub(/^Link /, "L ", line)
166 ruleline = sub(/^Rule /, "R ", line)
167 zoneline = sub(/^Zone /, "Z ", line)
169 # Replace FooAsia rules with the same rules without "Asia", as they
171 if (match(line, /[^ ]Asia /)) {
173 line = substr(line, 1, RSTART) substr(line, RSTART + 5)
177 while (match(line, /[: ]0+[0-9]/))
178 line = substr(line, 1, RSTART) substr(line, RSTART + RLENGTH - 1)
179 while (match(line, /:0[^:]/))
180 line = substr(line, 1, RSTART - 1) substr(line, RSTART + 2)
182 # Abbreviate weekday names.
183 while (match(line, / (last)?(Mon|Wed|Fri)[ <>]/)) {
184 end = RSTART + RLENGTH
185 line = substr(line, 1, end - 4) substr(line, end - 1)
187 while (match(line, / (last)?(Sun|Tue|Thu|Sat)[ <>]/)) {
188 end = RSTART + RLENGTH
189 line = substr(line, 1, end - 3) substr(line, end - 1)
192 # Abbreviate "max", "min", "only" and month names.
193 gsub(/ max /, " ma ", line)
194 gsub(/ min /, " mi ", line)
195 gsub(/ only /, " o ", line)
196 gsub(/ Jan /, " Ja ", line)
197 gsub(/ Feb /, " F ", line)
198 gsub(/ Apr /, " Ap ", line)
199 gsub(/ Aug /, " Au ", line)
200 gsub(/ Sep /, " S ", line)
201 gsub(/ Oct /, " O ", line)
202 gsub(/ Nov /, " N ", line)
203 gsub(/ Dec /, " D ", line)
205 # Strip leading and trailing space.
209 # Remove unnecessary trailing zero fields.
210 sub(/ 0+$/, "", line)
212 # Remove unnecessary trailing days-of-month "1".
213 if (match(line, /[A-Za-z] 1$/))
214 line = substr(line, 1, RSTART)
216 # Remove unnecessary trailing " Ja" (for January).
217 sub(/ Ja$/, "", line)
219 n = split(line, field)
221 # Abbreviate rule names.
222 i = zoneline ? 4 : linkline ? 0 : 2
223 if (i && field[i] ~ /^[^-+0-9]/) {
225 rule[field[i]] = gen_rule_name(field[i])
226 field[i] = rule[field[i]]
229 # If this zone supersedes an earlier one, delete the earlier one
230 # from the saved output lines.
233 zonename = startdef = field[2]
235 zonename = startdef = field[3]
239 i = zonedef[startdef]
242 output_line[i - 1] = ""
243 while (output_line[i++] ~ /^[-+0-9]/);
246 zonedef[zonename] = nout + 1
248 # Save the line for later output.
250 for (i = 2; i <= n; i++)
251 line = line " " field[i]
252 output_line[nout++] = line
255 function output_saved_lines( \
258 for (i = 0; i < nout; i++)
264 # Files that the output normally depends on.
265 default_dep["africa"] = 1
266 default_dep["antarctica"] = 1
267 default_dep["asia"] = 1
268 default_dep["australasia"] = 1
269 default_dep["backward"] = 1
270 default_dep["etcetera"] = 1
271 default_dep["europe"] = 1
272 default_dep["factory"] = 1
273 default_dep["northamerica"] = 1
274 default_dep["southamerica"] = 1
275 default_dep["ziguard.awk"] = 1
276 default_dep["zishrink.awk"] = 1
278 # Output a version string from 'version' and related configuration variables
279 # supported by tzdb's Makefile. If you change the makefile or any other files
280 # that affect the output of this script, you should append '-SOMETHING'
281 # to the contents of 'version', where SOMETHING identifies what was changed.
283 ndeps = split(deps, dep)
285 for (i = 1; i <= ndeps; i++) {
286 if (default_dep[dep[i]]) {
287 default_dep[dep[i]]++
289 ddeps = ddeps " " dep[i]
292 for (d in default_dep) {
293 if (default_dep[d] == 1) {
297 print "# version", version
298 if (dataform != "main") {
299 print "# dataform", dataform
301 if (redo != "posix_right") {
305 print "# ddeps" ddeps
307 print "# This zic input file is in the public domain."
313 process_input_line($0)