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