luci-app-https-dns-proxy: prepare migration to APK
[project/luci.git] / build / zoneinfo2lua.pl
1 #!/usr/bin/perl
2 # zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
3 # Execute from within root of Luci feed, usually feeds/luci
4 # $Id$
5
6 # NOTE: Darwin before 13.6.4 (22G513) may have had bugs in some TZinfo2 files.
7 # Some lacked the mandatory footers detailed in RFC 8536. A complete tzdata can
8 # not be built when this script is run on Darwin 13.6.3 and possibly other
9 # Darwin versions released prior to 13.6.4 (e.g. even Darwin (Sonoma) 14.2.1).
10
11 use strict;
12
13 my %TZ;
14
15 my $tzdin = $ARGV[0] || "/usr/share/zoneinfo";
16 my $tzdout = $ARGV[1] || "./modules/luci-base/luasrc/sys/zoneinfo";
17
18 local $/ = "\012";
19 open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";
20
21 while( ! eof ZTAB ) {
22 chomp( my $line = readline ZTAB );
23 next if $line =~ /^#/ || $line =~ /^\s+$/;
24
25 my ( undef, undef, $zone, @comment ) = split /\s+/, $line;
26
27 printf STDERR "%-40s", $zone;
28
29 if( open ZONE, "< $tzdin/$zone" ) {
30 seek ZONE, -2, 2;
31
32 while( tell(ZONE) > 0 ) {
33 read ZONE, my $char, 1;
34 ( $char eq "\012" ) ? last : seek ZONE, -2, 1;
35 }
36
37 chomp( my $tz = readline ZONE );
38 print STDERR ( $tz || "(no tzinfo found)" ), "\n";
39 close ZONE;
40
41 if( $tz ) {
42 $zone =~ s/_/ /g;
43 $TZ{$zone} = $tz;
44 }
45 }
46 else
47 {
48 print STDERR "open($tzdin/$zone): $!\n";
49 }
50 }
51
52 close ZTAB;
53
54 # Add Etc/GMT zones from manually as they are not in zone.tab
55 $TZ{"Etc/GMT"} = "GMT0";
56 $TZ{"Etc/GMT-1"} = "<+01>-1";
57 $TZ{"Etc/GMT-2"} = "<+02>-2";
58 $TZ{"Etc/GMT-3"} = "<+03>-3";
59 $TZ{"Etc/GMT-4"} = "<+04>-4";
60 $TZ{"Etc/GMT-5"} = "<+05>-5";
61 $TZ{"Etc/GMT-6"} = "<+06>-6";
62 $TZ{"Etc/GMT-7"} = "<+07>-7";
63 $TZ{"Etc/GMT-8"} = "<+08>-8";
64 $TZ{"Etc/GMT-9"} = "<+09>-9";
65 $TZ{"Etc/GMT-10"} = "<+10>-10";
66 $TZ{"Etc/GMT-11"} = "<+11>-11";
67 $TZ{"Etc/GMT-12"} = "<+12>-12";
68 $TZ{"Etc/GMT-13"} = "<+13>-13";
69 $TZ{"Etc/GMT-14"} = "<+14>-14";
70 $TZ{"Etc/GMT+1"} = "<-01>1";
71 $TZ{"Etc/GMT+2"} = "<-02>2";
72 $TZ{"Etc/GMT+3"} = "<-03>3";
73 $TZ{"Etc/GMT+4"} = "<-04>4";
74 $TZ{"Etc/GMT+5"} = "<-05>5";
75 $TZ{"Etc/GMT+6"} = "<-06>6";
76 $TZ{"Etc/GMT+7"} = "<-07>7";
77 $TZ{"Etc/GMT+8"} = "<-08>8";
78 $TZ{"Etc/GMT+9"} = "<-09>9";
79 $TZ{"Etc/GMT+10"} = "<-10>10";
80 $TZ{"Etc/GMT+11"} = "<-11>11";
81 $TZ{"Etc/GMT+12"} = "<-12>12";
82
83 open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";
84
85 print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
86 print O <<HEAD;
87 -- Licensed to the public under the Apache License 2.0.
88
89 module "luci.sys.zoneinfo.tzdata"
90
91 TZ = {
92 HEAD
93
94 foreach my $zone ( sort keys %TZ ) {
95 printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
96 }
97
98 print O "}\n";
99 close O;
100
101 print STDERR "done\n";
102
103
104 open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
105
106 print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
107 print O <<HEAD;
108 -- Licensed to the public under the Apache License 2.0.
109
110 module "luci.sys.zoneinfo.tzoffset"
111
112 OFFSET = {
113 HEAD
114
115 my %seen;
116 foreach my $tz ( sort keys %TZ ) {
117 my $zone = $TZ{$tz};
118
119 if( $zone =~ /^
120 ([A-Z]+)
121 (?:
122 ( -? \d+ (?: : \d+ )? )
123 (?:
124 ([A-Z]+)
125 ( -? \d+ (?: : \d+ )? )?
126 )?
127 )?
128 \b /xo ) {
129 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
130 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
131
132 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
133
134 if ( $soffset ) {
135 ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
136
137 $s = $s ? 1 : -1;
138 $h ||= 0;
139 $m ||= 0;
140
141 $offset = $s * $h * 60 * 60;
142 $offset += $s * $m * 60;
143
144 printf O "\t%-5s = %6d,\t-- %s\n",
145 lc($std), $offset, $std;
146
147 $seen{$std} = 1;
148
149 if( $dst ) {
150 if( $doffset ) {
151 ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
152
153 $s = $s ? 1 : -1;
154 $h ||= 0;
155 $m ||= 0;
156
157 $offset = $s * $h * 60 * 60;
158 $offset += $s * $m * 60;
159 } else {
160 $offset += 60 * 60;
161 }
162
163 printf O "\t%-5s = %6d,\t-- %s\n",
164 lc($dst), $offset, $dst;
165
166 $seen{$dst} = 1;
167 }
168 }
169 else {
170 printf O "\t%-5s = %6d,\t-- %s\n",
171 lc($std), $offset, $std;
172
173 $seen{$std} = 1;
174 }
175
176 }
177 }
178
179 print O "}\n";
180 close O;
181
182 print STDERR "done\n";