1331479Sphilip# Convert tzdata source into vanguard or rearguard form.
2331479Sphilip
3331479Sphilip# Contributed by Paul Eggert.  This file is in the public domain.
4331479Sphilip
5331479Sphilip# This is not a general-purpose converter; it is designed for current tzdata.
6331479Sphilip#
7331479Sphilip# When converting to vanguard form, the output can use negative SAVE
8331479Sphilip# values.
9331479Sphilip#
10331479Sphilip# When converting to rearguard form, the output uses only nonnegative
11331479Sphilip# SAVE values.  The idea is for the output data to simulate the behavior
12331479Sphilip# of the input data as best it can within the constraints of the
13331479Sphilip# rearguard format.
14331479Sphilip
15331479SphilipBEGIN {
16333313Sphilip  dataform_type["vanguard"] = 1
17333313Sphilip  dataform_type["main"] = 1
18333313Sphilip  dataform_type["rearguard"] = 1
19331479Sphilip
20333313Sphilip  # The command line should set DATAFORM.
21333313Sphilip  if (!dataform_type[DATAFORM]) exit 1
22333313Sphilip  vanguard = DATAFORM == "vanguard"
23331479Sphilip}
24331479Sphilip
25331479Sphilip/^Zone/ { zone = $2 }
26331479Sphilip
27333313SphilipDATAFORM != "main" {
28331479Sphilip  in_comment = /^#/
29333313Sphilip  uncomment = comment_out = 0
30331479Sphilip
31333313Sphilip  # If the line should differ due to Czechoslovakia using negative SAVE values,
32333313Sphilip  # uncomment the desired version and comment out the undesired one.
33333313Sphilip  if (zone == "Europe/Prague" && /1947 Feb 23/) {
34333313Sphilip    if (($(in_comment + 2) != "-") == vanguard) {
35333313Sphilip      uncomment = in_comment
36333313Sphilip    } else {
37333313Sphilip      comment_out = !in_comment
38333313Sphilip    }
39333313Sphilip  }
40333313Sphilip
41331479Sphilip  # If this line should differ due to Ireland using negative SAVE values,
42331479Sphilip  # uncomment the desired version and comment out the undesired one.
43331479Sphilip  Rule_Eire = /^#?Rule[\t ]+Eire[\t ]/
44331479Sphilip  Zone_Dublin_post_1968 \
45331479Sphilip    = (zone == "Europe/Dublin" && /^#?[\t ]+[01]:00[\t ]/ \
46331479Sphilip       && (!$(in_comment + 4) || 1968 < $(in_comment + 4)))
47331479Sphilip  if (Rule_Eire || Zone_Dublin_post_1968) {
48331479Sphilip    if ((Rule_Eire \
49331479Sphilip	 || (Zone_Dublin_post_1968 && $(in_comment + 3) == "IST/GMT"))	\
50331479Sphilip	== vanguard) {
51333313Sphilip      uncomment = in_comment
52333313Sphilip    } else {
53333313Sphilip      comment_out = !in_comment
54331479Sphilip    }
55331479Sphilip  }
56333313Sphilip
57342669Sphilip  # If this line should differ due to Namibia using negative SAVE values,
58333313Sphilip  # uncomment the desired version and comment out the undesired one.
59333313Sphilip  Rule_Namibia = /^#?Rule[\t ]+Namibia[\t ]/
60333313Sphilip  Zone_using_Namibia_rule \
61333313Sphilip    = (zone == "Africa/Windhoek" \
62333313Sphilip       && ($(in_comment + 2) == "Namibia" \
63333313Sphilip	   || (1994 <= $(in_comment + 4) && $(in_comment + 4) <= 2017) \
64333313Sphilip	   || in_comment + 3 == NF))
65333313Sphilip  if (Rule_Namibia || Zone_using_Namibia_rule) {
66333313Sphilip      if ((Rule_Namibia \
67333313Sphilip	   ? ($(in_comment + 9) ~ /^-/ \
68333313Sphilip	      || ($(in_comment + 9) == 0 && $(in_comment + 10) == "CAT")) \
69333313Sphilip	   : $(in_comment + 1) == "2:00" && $(in_comment + 2) == "Namibia") \
70333313Sphilip	  == vanguard) {
71333313Sphilip      uncomment = in_comment
72333313Sphilip    } else {
73333313Sphilip      comment_out = !in_comment
74333313Sphilip    }
75333313Sphilip  }
76333313Sphilip
77333313Sphilip  if (uncomment) {
78333313Sphilip    sub(/^#/, "")
79333313Sphilip  }
80333313Sphilip  if (comment_out) {
81333313Sphilip    sub(/^/, "#")
82333313Sphilip  }
83339940Sphilip
84339940Sphilip  # In rearguard format, change the Japan rule line with "Sat>=8 25:00"
85339940Sphilip  # to "Sun>=9 1:00", to cater to zic before 2007 and to older Java.
86339940Sphilip  if (!vanguard && $1 == "Rule" && $7 == "Sat>=8" && $8 == "25:00") {
87339940Sphilip    sub(/Sat>=8/, "Sun>=9")
88339940Sphilip    sub(/25:00/, " 1:00")
89339940Sphilip  }
90342669Sphilip
91342669Sphilip  # In rearguard format, change the Morocco lines with negative SAVE values
92342669Sphilip  # to use positive SAVE values.
93342669Sphilip  if (!vanguard && $1 == "Rule" && $2 == "Morocco" && $4 == 2018 \
94342669Sphilip      && $6 == "Oct") {
95342669Sphilip    sub(/\t2018\t/, "\t2017\t")
96342669Sphilip  }
97342669Sphilip  if (!vanguard && $1 == "Rule" && $2 == "Morocco" && 2019 <= $3) {
98342669Sphilip    if ($9 == "0") {
99342669Sphilip      sub(/\t0\t/, "\t1:00\t")
100342669Sphilip    } else {
101342669Sphilip      sub(/\t-1:00\t/, "\t0\t")
102342669Sphilip    }
103342669Sphilip  }
104342669Sphilip  if (!vanguard && $1 == "1:00" && $2 == "Morocco" && $3 == "+01/+00") {
105342669Sphilip    sub(/1:00\tMorocco\t\+01\/\+00$/, "0:00\tMorocco\t+00/+01")
106342669Sphilip  }
107331479Sphilip}
108331479Sphilip
109331479Sphilip# If a Link line is followed by a Zone line for the same data, comment
110331479Sphilip# out the Link line.  This can happen if backzone overrides a Link
111331479Sphilip# with a Zone.
112331479Sphilip/^Link/ {
113331479Sphilip  linkline[$3] = NR
114331479Sphilip}
115331479Sphilip/^Zone/ {
116331479Sphilip  sub(/^Link/, "#Link", line[linkline[$2]])
117331479Sphilip}
118331479Sphilip
119331479Sphilip{ line[NR] = $0 }
120331479Sphilip
121331479SphilipEND {
122331479Sphilip  for (i = 1; i <= NR; i++)
123331479Sphilip    print line[i]
124331479Sphilip}
125