xref: /freebsd/contrib/tzdata/ziguard.awk (revision 271171e0)
1# Convert tzdata source into vanguard or rearguard form.
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# 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.
10#
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.
17#
18# When converting to vanguard form, the output can use negative SAVE
19# values.
20#
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
24# rearguard format.
25
26# Given a FIELD like "-0:30", return a minute count like -30.
27function get_minutes(field, \
28		     sign, hours, minutes)
29{
30  sign = field ~ /^-/ ? -1 : 1
31  hours = +field
32  if (field ~ /:/) {
33    minutes = field
34    sub(/[^:]*:/, "", minutes)
35  }
36  return 60 * hours + sign * minutes
37}
38
39# Given an OFFSET, which is a minute count like 300 or 330,
40# return a %z-style abbreviation like "+05" or "+0530".
41function offset_abbr(offset, \
42		     hours, minutes, sign)
43{
44  hours = int(offset / 60)
45  minutes = offset % 60
46  if (minutes) {
47    return sprintf("%+.4d", hours * 100 + minutes);
48  } else {
49    return sprintf("%+.2d", hours)
50  }
51}
52
53# Round TIMESTAMP (a +-hh:mm:ss.dddd string) to the nearest second.
54function round_to_second(timestamp, \
55			 hh, mm, ss, seconds, dot_dddd, subseconds)
56{
57  dot_dddd = timestamp
58  if (!sub(/^[+-]?[0-9]+:[0-9]+:[0-9]+\./, ".", dot_dddd))
59    return timestamp
60  hh = mm = ss = timestamp
61  sub(/^[-+]?[0-9]+:[0-9]+:/, "", ss)
62  sub(/^[-+]?[0-9]+:/, "", mm)
63  sub(/^[-+]?/, "", hh)
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)
69}
70
71BEGIN {
72  dataform_type["vanguard"] = 1
73  dataform_type["main"] = 1
74  dataform_type["rearguard"] = 1
75
76  if (PACKRATLIST) {
77    while (getline <PACKRATLIST) {
78      if ($0 ~ /^#/) continue
79      packratlist[$3] = 1
80    }
81  }
82
83  # The command line should set DATAFORM.
84  if (!dataform_type[DATAFORM]) exit 1
85}
86
87$1 == "#PACKRATLIST" && $2 == PACKRATLIST {
88  sub(/^#PACKRATLIST[\t ]+[^\t ]+[\t ]+/, "")
89}
90
91/^Zone/ { zone = $2 }
92
93DATAFORM != "main" {
94  in_comment = /^#/
95  uncomment = comment_out = 0
96
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
102    } else {
103      comment_out = !in_comment
104    }
105  }
106
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) {
114    if ((Rule_Eire \
115	 || (Zone_Dublin_post_1968 && $(in_comment + 3) == "IST/GMT"))	\
116	== (DATAFORM != "rearguard")) {
117      uncomment = in_comment
118    } else {
119      comment_out = !in_comment
120    }
121  }
122
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) {
133    if ((Rule_Namibia \
134	 ? ($9 ~ /^-/ || ($9 == 0 && $10 == "CAT")) \
135	 : $(in_comment + 1) == "2:00" && $(in_comment + 2) == "Namibia") \
136	== (DATAFORM != "rearguard")) {
137      uncomment = in_comment
138    } else {
139      comment_out = !in_comment
140    }
141  }
142
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
148    } else {
149      comment_out = !in_comment
150    }
151  }
152
153  if (uncomment) {
154    sub(/^#/, "")
155  }
156  if (comment_out) {
157    sub(/^/, "#")
158  }
159
160  # Prefer %z in vanguard form, explicit abbreviations otherwise.
161  if (DATAFORM == "vanguard") {
162    sub(/^(Zone[\t ]+[^\t ]+)?[\t ]+[^\t ]+[\t ]+[^\t ]+[\t ]+[-+][^\t ]+/, \
163	"&CHANGE-TO-%z")
164    sub(/-00CHANGE-TO-%z/, "-00")
165    sub(/[-+][^\t ]+CHANGE-TO-/, "")
166  } else {
167    if (/^[^#]*%z/) {
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)
173      if (rules == "-") {
174	abbr = stdabbr
175      } else {
176	dstabbr_only = rules ~ /^[+0-9-]/
177	if (dstabbr_only) {
178	  dstoff = get_minutes(rules)
179	} else {
180	  # The DST offset is normally an hour, but there are special cases.
181	  if (rules == "Morocco" && NF == 3) {
182	    dstoff = -60
183	  } else if (rules == "NBorneo") {
184	    dstoff = 20
185	  } else if (((rules == "Cook" || rules == "LH") && NF == 3) \
186		     || (rules == "Uruguay" \
187			 && /[\t ](1942 Dec 14|1960|1970|1974 Dec 22)$/)) {
188	    dstoff = 30
189	  } else if (rules == "Uruguay" && /[\t ]1974 Mar 10$/) {
190	    dstoff = 90
191	  } else {
192	    dstoff = 60
193	  }
194	}
195	dstabbr = offset_abbr(stdoff + dstoff)
196	if (dstabbr_only) {
197	  abbr = dstabbr
198	} else {
199	  abbr = stdabbr "/" dstabbr
200	}
201      }
202      sub(/%z/, abbr)
203    }
204  }
205
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") {
215    stdoff = $2
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
220    } else {
221      stdoff_subst[0] = stdoff
222      stdoff_subst[1] = rounded_stdoff
223    }
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]) {
230      stdoff_subst[0] = 0
231    }
232  }
233
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")
241      }
242    } else {
243      if ($7 == "Sun>=9" && $8 == "1:00") {
244	sub(/Sun>=9/, "Sat>=8")
245	sub(/ 1:00/, "25:00")
246      }
247    }
248  }
249
250  # In rearguard form, change the Morocco lines with negative SAVE values
251  # to use positive SAVE values.
252  if ($2 == "Morocco") {
253    if (/^Rule/) {
254      if ($4 ~ /^201[78]$/ && $6 == "Oct") {
255	if (DATAFORM == "rearguard") {
256	  sub(/\t2018\t/, "\t2017\t")
257	} else {
258	  sub(/\t2017\t/, "\t2018\t")
259	}
260      }
261
262      if (2019 <= $3) {
263	if ($8 == "2:00") {
264	  if (DATAFORM == "rearguard") {
265	    sub(/\t0\t/, "\t1:00\t")
266	  } else {
267	    sub(/\t1:00\t/, "\t0\t")
268	  }
269	} else {
270	  if (DATAFORM == "rearguard") {
271	    sub(/\t-1:00\t/, "\t0\t")
272	  } else {
273	    sub(/\t0\t/, "\t-1:00\t")
274	  }
275	}
276      }
277    }
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")
282      } else {
283	sub(/0:00\tMorocco/, "1:00\tMorocco")
284	sub(/\t\+00\/+01$/, "\t+01/+00")
285      }
286    }
287  }
288}
289
290/^Zone/ {
291  packrat_ignored = FILENAME == PACKRATDATA && PACKRATLIST && !packratlist[$2];
292}
293packrat_ignored && !/^Rule/ {
294  sub(/^/, "#")
295}
296
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.
300/^Zone/ {
301  sub(/^Link/, "#Link", line[linkline[$2]])
302}
303/^Link/ {
304  sub(/^Link/, "#Link", line[linkline[$3]])
305  linkline[$3] = NR
306}
307
308{ line[NR] = $0 }
309
310END {
311  for (i = 1; i <= NR; i++)
312    print line[i]
313}
314