1 # Convert tzdata source into vanguard or rearguard form.
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 # It just converts from current source to main, vanguard, and rearguard forms.
7 # Although it might be nice for it to be idempotent, or to be useful
8 # for converting back and forth between vanguard and rearguard formats,
9 # it does not do these nonessential tasks now.
11 # Although main and vanguard forms are currently equivalent,
12 # this need not always be the case. When the two forms differ,
13 # this script can convert either from main to vanguard form (needed then),
14 # or from vanguard to main form (this conversion would be needed later,
15 # after main became rearguard and vanguard became main).
16 # There is no need to convert rearguard to other forms.
18 # When converting to vanguard form, the output can use negative SAVE
21 # When converting to rearguard form, the output uses only nonnegative
22 # SAVE values. The idea is for the output data to simulate the behavior
23 # of the input data as best it can within the constraints of the
26 # Given a FIELD like "-0:30", return a minute count like -30.
27 function get_minutes(field, \
30 sign = field ~ /^-/ ? -1 : 1
34 sub(/[^:]*:/, "", minutes)
36 return 60 * hours + sign * minutes
39 # Given an OFFSET, which is a minute count like 300 or 330,
40 # return a %z-style abbreviation like "+05" or "+0530".
41 function offset_abbr(offset, \
44 hours = int(offset / 60)
47 return sprintf("%+.4d", hours * 100 + minutes);
49 return sprintf("%+.2d", hours)
53 # Round TIMESTAMP (a +-hh:mm:ss.dddd string) to the nearest second.
54 function round_to_second(timestamp, \
55 hh, mm, ss, seconds, dot_dddd, subseconds)
58 if (!sub(/^[+-]?[0-9]+:[0-9]+:[0-9]+\./, ".", dot_dddd))
60 hh = mm = ss = timestamp
61 sub(/^[-+]?[0-9]+:[0-9]+:/, "", ss)
62 sub(/^[-+]?[0-9]+:/, "", mm)
64 seconds = 3600 * hh + 60 * mm + ss
65 subseconds = +dot_dddd
66 seconds += 0.5 < subseconds || ((subseconds == 0.5) && (seconds % 2));
67 return sprintf("%s%d:%.2d:%.2d", timestamp ~ /^-/ ? "-" : "", \
68 seconds / 3600, seconds / 60 % 60, seconds % 60)
72 dataform_type["vanguard"] = 1
73 dataform_type["main"] = 1
74 dataform_type["rearguard"] = 1
77 while (getline <PACKRATLIST) {
78 if ($0 ~ /^#/) continue
83 # The command line should set DATAFORM.
84 if (!dataform_type[DATAFORM]) exit 1
87 $1 == "#PACKRATLIST" && $2 == PACKRATLIST {
88 sub(/^#PACKRATLIST[\t ]+[^\t ]+[\t ]+/, "")
95 uncomment = comment_out = 0
97 # If this line should differ due to Czechoslovakia using negative SAVE values,
98 # uncomment the desired version and comment out the undesired one.
99 if (zone == "Europe/Prague" && /^#?[\t ]+[01]:00[\t ]/ && /1947 Feb 23/) {
100 if (($(in_comment + 2) != "-") == (DATAFORM != "rearguard")) {
101 uncomment = in_comment
103 comment_out = !in_comment
107 # If this line should differ due to Ireland using negative SAVE values,
108 # uncomment the desired version and comment out the undesired one.
109 Rule_Eire = /^#?Rule[\t ]+Eire[\t ]/
110 Zone_Dublin_post_1968 \
111 = (zone == "Europe/Dublin" && /^#?[\t ]+[01]:00[\t ]/ \
112 && (!$(in_comment + 4) || 1968 < $(in_comment + 4)))
113 if (Rule_Eire || Zone_Dublin_post_1968) {
115 || (Zone_Dublin_post_1968 && $(in_comment + 3) == "IST/GMT")) \
116 == (DATAFORM != "rearguard")) {
117 uncomment = in_comment
119 comment_out = !in_comment
123 # If this line should differ due to Namibia using negative SAVE values,
124 # uncomment the desired version and comment out the undesired one.
125 Rule_Namibia = /^#?Rule[\t ]+Namibia[\t ]/
126 Zone_using_Namibia_rule \
127 = (zone == "Africa/Windhoek" && /^#?[\t ]+[12]:00[\t ]/ \
128 && ($(in_comment + 2) == "Namibia" \
129 || ($(in_comment + 2) == "-" && $(in_comment + 3) == "CAT" \
130 && ((1994 <= $(in_comment + 4) && $(in_comment + 4) <= 2017) \
131 || in_comment + 3 == NF))))
132 if (Rule_Namibia || Zone_using_Namibia_rule) {
134 ? ($9 ~ /^-/ || ($9 == 0 && $10 == "CAT")) \
135 : $(in_comment + 1) == "2:00" && $(in_comment + 2) == "Namibia") \
136 == (DATAFORM != "rearguard")) {
137 uncomment = in_comment
139 comment_out = !in_comment
143 # If this line should differ due to Portugal benefiting from %z if supported,
144 # uncomment the desired version and comment out the undesired one.
145 if (/^#?[\t ]+-[12]:00[\t ]+Port[\t ]+[%+-]/) {
146 if (/%z/ == (DATAFORM == "vanguard")) {
147 uncomment = in_comment
149 comment_out = !in_comment
160 # Prefer %z in vanguard form, explicit abbreviations otherwise.
161 if (DATAFORM == "vanguard") {
162 sub(/^(Zone[\t ]+[^\t ]+)?[\t ]+[^\t ]+[\t ]+[^\t ]+[\t ]+[-+][^\t ]+/, \
164 sub(/-00CHANGE-TO-%z/, "-00")
165 sub(/[-+][^\t ]+CHANGE-TO-/, "")
168 stdoff_column = 2 * /^Zone/ + 1
169 rules_column = stdoff_column + 1
170 stdoff = get_minutes($stdoff_column)
171 rules = $rules_column
172 stdabbr = offset_abbr(stdoff)
176 dstabbr_only = rules ~ /^[+0-9-]/
178 dstoff = get_minutes(rules)
180 # The DST offset is normally an hour, but there are special cases.
181 if (rules == "Morocco" && NF == 3) {
183 } else if (rules == "NBorneo") {
185 } else if (((rules == "Cook" || rules == "LH") && NF == 3) \
186 || (rules == "Uruguay" \
187 && /[\t ](1942 Dec 14|1960|1970|1974 Dec 22)$/)) {
189 } else if (rules == "Uruguay" && /[\t ]1974 Mar 10$/) {
195 dstabbr = offset_abbr(stdoff + dstoff)
199 abbr = stdabbr "/" dstabbr
206 # Normally, prefer whole seconds. However, prefer subseconds
207 # if generating vanguard form and the otherwise-undocumented
208 # VANGUARD_SUBSECONDS environment variable is set.
209 # This relies on #STDOFF comment lines in the data.
210 # It is for hypothetical clients that support UT offsets that are
211 # not integer multiples of one second (e.g., Europe/Lisbon, 1884 to 1912).
212 # No known clients need this currently, and this experimental
213 # feature may be changed or withdrawn in future releases.
214 if ($1 == "#STDOFF") {
216 rounded_stdoff = round_to_second(stdoff)
217 if (DATAFORM == "vanguard" && ENVIRON["VANGUARD_SUBSECONDS"]) {
218 stdoff_subst[0] = rounded_stdoff
219 stdoff_subst[1] = stdoff
221 stdoff_subst[0] = stdoff
222 stdoff_subst[1] = rounded_stdoff
224 } else if (stdoff_subst[0]) {
225 stdoff_column = 2 * /^Zone/ + 1
226 stdoff_column_val = $stdoff_column
227 if (stdoff_column_val == stdoff_subst[0]) {
228 sub(stdoff_subst[0], stdoff_subst[1])
229 } else if (stdoff_column_val != stdoff_subst[1]) {
234 # In rearguard form, change the Japan rule line with "Sat>=8 25:00"
235 # to "Sun>=9 1:00", to cater to zic before 2007 and to older Java.
236 if (/^Rule/ && $2 == "Japan") {
237 if (DATAFORM == "rearguard") {
238 if ($7 == "Sat>=8" && $8 == "25:00") {
239 sub(/Sat>=8/, "Sun>=9")
240 sub(/25:00/, " 1:00")
243 if ($7 == "Sun>=9" && $8 == "1:00") {
244 sub(/Sun>=9/, "Sat>=8")
245 sub(/ 1:00/, "25:00")
250 # In rearguard form, change the Morocco lines with negative SAVE values
251 # to use positive SAVE values.
252 if ($2 == "Morocco") {
254 if ($4 ~ /^201[78]$/ && $6 == "Oct") {
255 if (DATAFORM == "rearguard") {
256 sub(/\t2018\t/, "\t2017\t")
258 sub(/\t2017\t/, "\t2018\t")
264 if (DATAFORM == "rearguard") {
265 sub(/\t0\t/, "\t1:00\t")
267 sub(/\t1:00\t/, "\t0\t")
270 if (DATAFORM == "rearguard") {
271 sub(/\t-1:00\t/, "\t0\t")
273 sub(/\t0\t/, "\t-1:00\t")
278 if ($1 ~ /^[+0-9-]/ && NF == 3) {
279 if (DATAFORM == "rearguard") {
280 sub(/1:00\tMorocco/, "0:00\tMorocco")
281 sub(/\t\+01\/\+00$/, "\t+00/+01")
283 sub(/0:00\tMorocco/, "1:00\tMorocco")
284 sub(/\t\+00\/+01$/, "\t+01/+00")
291 packrat_ignored = FILENAME == PACKRATDATA && PACKRATLIST && !packratlist[$2];
293 packrat_ignored && !/^Rule/ {
297 # If a Link line is followed by a Link or Zone line for the same data, comment
298 # out the Link line. This can happen if backzone overrides a Link
299 # with a Zone or a different Link.
301 sub(/^Link/, "#Link", line[linkline[$2]])
304 sub(/^Link/, "#Link", line[linkline[$3]])
311 for (i = 1; i <= NR; i++)