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.
13#
14# When converting to vanguard form, the output can use negative SAVE
15# values.
16#
17# When converting to rearguard form, the output uses only nonnegative
18# SAVE values.  The idea is for the output data to simulate the behavior
19# of the input data as best it can within the constraints of the
20# rearguard format.
21
22BEGIN {
23  dataform_type["vanguard"] = 1
24  dataform_type["main"] = 1
25  dataform_type["rearguard"] = 1
26
27  # The command line should set DATAFORM.
28  if (!dataform_type[DATAFORM]) exit 1
29  vanguard = DATAFORM == "vanguard"
30}
31
32/^Zone/ { zone = $2 }
33
34DATAFORM != "main" {
35  in_comment = /^#/
36  uncomment = comment_out = 0
37
38  # If this line should differ due to Czechoslovakia using negative SAVE values,
39  # uncomment the desired version and comment out the undesired one.
40  if (zone == "Europe/Prague" && /^#?[\t ]+[01]:00[\t ]/ && /1947 Feb 23/) {
41    if (($(in_comment + 2) != "-") == vanguard) {
42      uncomment = in_comment
43    } else {
44      comment_out = !in_comment
45    }
46  }
47
48  # If this line should differ due to Ireland using negative SAVE values,
49  # uncomment the desired version and comment out the undesired one.
50  Rule_Eire = /^#?Rule[\t ]+Eire[\t ]/
51  Zone_Dublin_post_1968 \
52    = (zone == "Europe/Dublin" && /^#?[\t ]+[01]:00[\t ]/ \
53       && (!$(in_comment + 4) || 1968 < $(in_comment + 4)))
54  if (Rule_Eire || Zone_Dublin_post_1968) {
55    if ((Rule_Eire \
56	 || (Zone_Dublin_post_1968 && $(in_comment + 3) == "IST/GMT"))	\
57	== vanguard) {
58      uncomment = in_comment
59    } else {
60      comment_out = !in_comment
61    }
62  }
63
64  # If this line should differ due to Namibia using negative SAVE values,
65  # uncomment the desired version and comment out the undesired one.
66  Rule_Namibia = /^#?Rule[\t ]+Namibia[\t ]/
67  Zone_using_Namibia_rule \
68    = (zone == "Africa/Windhoek" && /^#?[\t ]+[12]:00[\t ]/ \
69       && ($(in_comment + 2) == "Namibia" \
70	   || ($(in_comment + 2) == "-" && $(in_comment + 3) == "CAT" \
71	       && ((1994 <= $(in_comment + 4) && $(in_comment + 4) <= 2017) \
72		   || in_comment + 3 == NF))))
73  if (Rule_Namibia || Zone_using_Namibia_rule) {
74      if ((Rule_Namibia \
75	   ? ($(in_comment + 9) ~ /^-/ \
76	      || ($(in_comment + 9) == 0 && $(in_comment + 10) == "CAT")) \
77	   : $(in_comment + 1) == "2:00" && $(in_comment + 2) == "Namibia") \
78	  == vanguard) {
79      uncomment = in_comment
80    } else {
81      comment_out = !in_comment
82    }
83  }
84
85  if (uncomment) {
86    sub(/^#/, "")
87  }
88  if (comment_out) {
89    sub(/^/, "#")
90  }
91
92  # In rearguard format, change the Japan rule line with "Sat>=8 25:00"
93  # to "Sun>=9 1:00", to cater to zic before 2007 and to older Java.
94  if (!vanguard && $1 == "Rule" && $7 == "Sat>=8" && $8 == "25:00") {
95    sub(/Sat>=8/, "Sun>=9")
96    sub(/25:00/, " 1:00")
97  }
98
99  # In rearguard format, change the Morocco lines with negative SAVE values
100  # to use positive SAVE values.
101  if (!vanguard && $1 == "Rule" && $2 == "Morocco" && $4 == 2018 \
102      && $6 == "Oct") {
103    sub(/\t2018\t/, "\t2017\t")
104  }
105  if (!vanguard && $1 == "Rule" && $2 == "Morocco" && 2019 <= $3) {
106    if ($9 == "0") {
107      sub(/\t0\t/, "\t1:00\t")
108    } else {
109      sub(/\t-1:00\t/, "\t0\t")
110    }
111  }
112  if (!vanguard && $1 == "1:00" && $2 == "Morocco" && $3 == "+01/+00") {
113    sub(/1:00\tMorocco\t\+01\/\+00$/, "0:00\tMorocco\t+00/+01")
114  }
115}
116
117# If a Link line is followed by a Zone line for the same data, comment
118# out the Link line.  This can happen if backzone overrides a Link
119# with a Zone.
120/^Link/ {
121  linkline[$3] = NR
122}
123/^Zone/ {
124  sub(/^Link/, "#Link", line[linkline[$2]])
125}
126
127{ line[NR] = $0 }
128
129END {
130  for (i = 1; i <= NR; i++)
131    print line[i]
132}
133