1# Convert tzdata source into a smaller version of itself. 2 3# Contributed by Paul Eggert. This file is in the public domain. 4 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 7# this script's input. 8 9# Record a hash N for the new name NAME, checking for collisions. 10 11function record_hash(n, name) 12{ 13 if (used_hashes[n]) { 14 printf "# ! collision: %s %s\n", used_hashes[n], name 15 exit 1 16 } 17 used_hashes[n] = name 18} 19 20# Return a shortened rule name representing NAME, 21# and record this relationship to the hash table. 22 23function gen_rule_name(name, \ 24 n) 25{ 26 # Use a simple memonic: the first two letters. 27 n = substr(name, 1, 2) 28 record_hash(n, name) 29 # printf "# %s = %s\n", n, name 30 return n 31} 32 33function prehash_rule_names( \ 34 name) 35{ 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. 42 43 # Abbreviating these rules names to one letter saved the most space 44 # circa 2018e. 45 rule["Arg"] = "A" 46 rule["Brazil"] = "B" 47 rule["Canada"] = "C" 48 rule["Denmark"] = "D" 49 rule["EU"] = "E" 50 rule["France"] = "F" 51 rule["GB-Eire"] = "G" 52 rule["Halifax"] = "H" 53 rule["Italy"] = "I" 54 rule["Jordan"] = "J" 55 rule["Egypt"] = "K" # "Kemet" in ancient Egyptian 56 rule["Libya"] = "L" 57 rule["Morocco"] = "M" 58 rule["Neth"] = "N" 59 rule["Poland"] = "O" # arbitrary 60 rule["Palestine"] = "P" 61 rule["Cuba"] = "Q" # Its start sounds like "Q". 62 rule["Russia"] = "R" 63 rule["Syria"] = "S" 64 rule["Turkey"] = "T" 65 rule["Uruguay"] = "U" 66 rule["Vincennes"] = "V" 67 rule["Winn"] = "W" 68 rule["Mongol"] = "X" # arbitrary 69 rule["NT_YK"] = "Y" 70 rule["Zion"] = "Z" 71 rule["Austria"] = "a" 72 rule["Belgium"] = "b" 73 rule["C-Eur"] = "c" 74 rule["Algeria"] = "d" # country code DZ 75 rule["E-Eur"] = "e" 76 rule["Taiwan"] = "f" # Formosa 77 rule["Greece"] = "g" 78 rule["Hungary"] = "h" 79 rule["Iran"] = "i" 80 rule["StJohns"] = "j" 81 rule["Chatham"] = "k" # arbitrary 82 rule["Lebanon"] = "l" 83 rule["Mexico"] = "m" 84 rule["Tunisia"] = "n" # country code TN 85 rule["Moncton"] = "o" # arbitrary 86 rule["Port"] = "p" 87 rule["Albania"] = "q" # arbitrary 88 rule["Regina"] = "r" 89 rule["Spain"] = "s" 90 rule["Toronto"] = "t" 91 rule["US"] = "u" 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 98 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" 102 rule["Aus"] = "AU" 103 rule["Azer"] = "AZ" 104 rule["Barb"] = "BB" 105 rule["Dhaka"] = "BD" 106 rule["Bulg"] = "BG" 107 rule["Bahamas"] = "BS" 108 rule["Belize"] = "BZ" 109 rule["Swiss"] = "CH" 110 rule["Cook"] = "CK" 111 rule["PRC"] = "CN" 112 rule["Cyprus"] = "CY" 113 rule["Czech"] = "CZ" 114 rule["Germany"] = "DE" 115 rule["DR"] = "DO" 116 rule["Ecuador"] = "EC" 117 rule["Finland"] = "FI" 118 rule["Fiji"] = "FJ" 119 rule["Falk"] = "FK" 120 rule["Ghana"] = "GH" 121 rule["Guat"] = "GT" 122 rule["Hond"] = "HN" 123 rule["Haiti"] = "HT" 124 rule["Eire"] = "IE" 125 rule["Iraq"] = "IQ" 126 rule["Japan"] = "JP" 127 rule["Kyrgyz"] = "KG" 128 rule["ROK"] = "KR" 129 rule["Latvia"] = "LV" 130 rule["Lux"] = "LX" 131 rule["Moldova"] = "MD" 132 rule["Malta"] = "MT" 133 rule["Mauritius"] = "MU" 134 rule["Namibia"] = "NA" 135 rule["Nic"] = "NI" 136 rule["Norway"] = "NO" 137 rule["Peru"] = "PE" 138 rule["Phil"] = "PH" 139 rule["Pakistan"] = "PK" 140 rule["Sudan"] = "SD" 141 rule["Salv"] = "SV" 142 rule["Tonga"] = "TO" 143 rule["Vanuatu"] = "VU" 144 145 # Avoid collisions. 146 rule["Detroit"] = "Dt" # De = Denver 147 148 for (name in rule) { 149 record_hash(rule[name], name) 150 } 151} 152 153# Process the input line LINE and save it for later output. 154 155function process_input_line(line, \ 156 field, end, i, n, startdef, \ 157 linkline, ruleline, zoneline) 158{ 159 # Remove comments, normalize spaces, and append a space to each line. 160 sub(/#.*/, "", line) 161 line = line " " 162 gsub(/[\t ]+/, " ", line) 163 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) 168 169 # SystemV rules are not needed. 170 if (line ~ /^R SystemV /) return 171 172 # Replace FooAsia rules with the same rules without "Asia", as they 173 # are duplicates. 174 if (match(line, /[^ ]Asia /)) { 175 if (ruleline) return 176 line = substr(line, 1, RSTART) substr(line, RSTART + 5) 177 } 178 179 # Abbreviate times. 180 while (match(line, /[: ]0+[0-9]/)) 181 line = substr(line, 1, RSTART) substr(line, RSTART + RLENGTH - 1) 182 while (match(line, /:0[^:]/)) 183 line = substr(line, 1, RSTART - 1) substr(line, RSTART + 2) 184 185 # Abbreviate weekday names. 186 while (match(line, / (last)?(Mon|Wed|Fri)[ <>]/)) { 187 end = RSTART + RLENGTH 188 line = substr(line, 1, end - 4) substr(line, end - 1) 189 } 190 while (match(line, / (last)?(Sun|Tue|Thu|Sat)[ <>]/)) { 191 end = RSTART + RLENGTH 192 line = substr(line, 1, end - 3) substr(line, end - 1) 193 } 194 195 # Abbreviate "max", "min", "only" and month names. 196 gsub(/ max /, " ma ", line) 197 gsub(/ min /, " mi ", line) 198 gsub(/ only /, " o ", line) 199 gsub(/ Jan /, " Ja ", line) 200 gsub(/ Feb /, " F ", line) 201 gsub(/ Apr /, " Ap ", line) 202 gsub(/ Aug /, " Au ", line) 203 gsub(/ Sep /, " S ", line) 204 gsub(/ Oct /, " O ", line) 205 gsub(/ Nov /, " N ", line) 206 gsub(/ Dec /, " D ", line) 207 208 # Strip leading and trailing space. 209 sub(/^ /, "", line) 210 sub(/ $/, "", line) 211 212 # Remove unnecessary trailing zero fields. 213 sub(/ 0+$/, "", line) 214 215 # Remove unnecessary trailing days-of-month "1". 216 if (match(line, /[A-Za-z] 1$/)) 217 line = substr(line, 1, RSTART) 218 219 # Remove unnecessary trailing " Ja" (for January). 220 sub(/ Ja$/, "", line) 221 222 n = split(line, field) 223 224 # Abbreviate rule names. 225 i = zoneline ? 4 : linkline ? 0 : 2 226 if (i && field[i] ~ /^[^-+0-9]/) { 227 if (!rule[field[i]]) 228 rule[field[i]] = gen_rule_name(field[i]) 229 field[i] = rule[field[i]] 230 } 231 232 # If this zone supersedes an earlier one, delete the earlier one 233 # from the saved output lines. 234 startdef = "" 235 if (zoneline) 236 zonename = startdef = field[2] 237 else if (linkline) 238 zonename = startdef = field[3] 239 else if (ruleline) 240 zonename = "" 241 if (startdef) { 242 i = zonedef[startdef] 243 if (i) { 244 do 245 output_line[i - 1] = "" 246 while (output_line[i++] ~ /^[-+0-9]/); 247 } 248 } 249 zonedef[zonename] = nout + 1 250 251 # Save the line for later output. 252 line = field[1] 253 for (i = 2; i <= n; i++) 254 line = line " " field[i] 255 output_line[nout++] = line 256} 257 258function output_saved_lines( \ 259 i) 260{ 261 for (i = 0; i < nout; i++) 262 if (output_line[i]) 263 print output_line[i] 264} 265 266BEGIN { 267 # Files that the output normally depends on. 268 default_dep["africa"] = 1 269 default_dep["antarctica"] = 1 270 default_dep["asia"] = 1 271 default_dep["australasia"] = 1 272 default_dep["backward"] = 1 273 default_dep["etcetera"] = 1 274 default_dep["europe"] = 1 275 default_dep["factory"] = 1 276 default_dep["northamerica"] = 1 277 default_dep["southamerica"] = 1 278 default_dep["systemv"] = 1 279 default_dep["ziguard.awk"] = 1 280 default_dep["zishrink.awk"] = 1 281 282 # Output a version string from 'version' and related configuration variables 283 # supported by tzdb's Makefile. If you change the makefile or any other files 284 # that affect the output of this script, you should append '-SOMETHING' 285 # to the contents of 'version', where SOMETHING identifies what was changed. 286 287 ndeps = split(deps, dep) 288 ddeps = "" 289 for (i = 1; i <= ndeps; i++) { 290 if (default_dep[dep[i]]) { 291 default_dep[dep[i]]++ 292 } else { 293 ddeps = ddeps " " dep[i] 294 } 295 } 296 for (d in default_dep) { 297 if (default_dep[d] == 1) { 298 ddeps = ddeps " !" d 299 } 300 } 301 print "# version", version 302 if (dataform != "main") { 303 print "# dataform", dataform 304 } 305 if (redo != "posix_right") { 306 print "# redo " redo 307 } 308 if (ddeps) { 309 print "# ddeps" ddeps 310 } 311 print "# This zic input file is in the public domain." 312 313 prehash_rule_names() 314} 315 316/^[\t ]*[^#\t ]/ { 317 process_input_line($0) 318} 319 320END { 321 output_saved_lines() 322} 323