summaryrefslogtreecommitdiffhomepage
path: root/build/zoneinfo2lua.pl
blob: 1a0bee1c68afb4722531538b046950c6c707e4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl
# zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
# Execute from within root of Luci feed, usually feeds/luci
# $Id$

use strict;

my %TZ;

my $tzdin  = $ARGV[0] || "/usr/share/zoneinfo";
my $tzdout = $ARGV[1] || "./modules/luci-base/luasrc/sys/zoneinfo";

local $/ = "\012";
open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";

while( ! eof ZTAB ) {
	chomp( my $line = readline ZTAB );
	next if $line =~ /^#/ || $line =~ /^\s+$/;

	my ( undef, undef, $zone, @comment ) = split /\s+/, $line;

	printf STDERR "%-40s", $zone;

	if( open ZONE, "< $tzdin/$zone" ) {
		seek ZONE, -2, 2;

		while( tell(ZONE) > 0 ) {
			read ZONE, my $char, 1;
			( $char eq "\012" ) ? last : seek ZONE, -2, 1;
		}

		chomp( my $tz = readline ZONE );
		print STDERR ( $tz || "(no tzinfo found)" ), "\n";
		close ZONE;

		if( $tz ) {
			$zone =~ s/_/ /g;
			$TZ{$zone} = $tz;
		}
	}
	else
	{
		print STDERR "open($tzdin/$zone): $!\n";
	}
}

close ZTAB;

# Add Etc/GMT zones from manually as they are not in zone.tab
$TZ{"Etc/GMT"} = "GMT0";
$TZ{"Etc/GMT-1"} = "<+01>-1";
$TZ{"Etc/GMT-2"} = "<+02>-2";
$TZ{"Etc/GMT-3"} = "<+03>-3";
$TZ{"Etc/GMT-4"} = "<+04>-4";
$TZ{"Etc/GMT-5"} = "<+05>-5";
$TZ{"Etc/GMT-6"} = "<+06>-6";
$TZ{"Etc/GMT-7"} = "<+07>-7";
$TZ{"Etc/GMT-8"} = "<+08>-8";
$TZ{"Etc/GMT-9"} = "<+09>-9";
$TZ{"Etc/GMT-10"} = "<+10>-10";
$TZ{"Etc/GMT-11"} = "<+11>-11";
$TZ{"Etc/GMT-12"} = "<+12>-12";
$TZ{"Etc/GMT-13"} = "<+13>-13";
$TZ{"Etc/GMT-14"} = "<+14>-14";
$TZ{"Etc/GMT+1"} = "<-01>1";
$TZ{"Etc/GMT+2"} = "<-02>2";
$TZ{"Etc/GMT+3"} = "<-03>3";
$TZ{"Etc/GMT+4"} = "<-04>4";
$TZ{"Etc/GMT+5"} = "<-05>5";
$TZ{"Etc/GMT+6"} = "<-06>6";
$TZ{"Etc/GMT+7"} = "<-07>7";
$TZ{"Etc/GMT+8"} = "<-08>8";
$TZ{"Etc/GMT+9"} = "<-09>9";
$TZ{"Etc/GMT+10"} = "<-10>10";
$TZ{"Etc/GMT+11"} = "<-11>11";
$TZ{"Etc/GMT+12"} = "<-12>12";

open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";

print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
print O <<HEAD;
-- Licensed to the public under the Apache License 2.0.

module "luci.sys.zoneinfo.tzdata"

TZ = {
HEAD

foreach my $zone ( sort keys %TZ ) {
	printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
}

print O "}\n";
close O;

print STDERR "done\n";


open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";

print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
print O <<HEAD;
-- Licensed to the public under the Apache License 2.0.

module "luci.sys.zoneinfo.tzoffset"

OFFSET = {
HEAD

my %seen;
foreach my $tz ( sort keys %TZ ) {
	my $zone = $TZ{$tz};

	if( $zone =~ /^
		([A-Z]+)
		(?:
			( -? \d+ (?: : \d+ )? )
			(?:
				([A-Z]+)
				( -? \d+ (?: : \d+ )? )? 
			)?
		)?
	\b /xo ) {
		my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
		my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );

		next if $seen{$std}; # and ( !$dst or $seen{$dst} );

		if ( $soffset ) {
			( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;

			$s = $s ? 1 : -1;
			$h ||= 0;
			$m ||= 0;

			$offset  = $s * $h * 60 * 60;
			$offset += $s * $m * 60;

			printf O "\t%-5s = %6d,\t-- %s\n",
				lc($std), $offset, $std;

			$seen{$std} = 1;

			if( $dst ) {
				if( $doffset ) {
					( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;

					$s = $s ? 1 : -1;
					$h ||= 0;
					$m ||= 0;

					$offset  = $s * $h * 60 * 60;
					$offset += $s * $m * 60;
				} else  {
					$offset += 60 * 60;
				}

				printf O "\t%-5s = %6d,\t-- %s\n",
					lc($dst), $offset, $dst;
	
				$seen{$dst} = 1;
			}
		}
		else {
			printf O "\t%-5s = %6d,\t-- %s\n",
				lc($std), $offset, $std;

			$seen{$std} = 1;
		}

	}
}

print O "}\n";
close O;

print STDERR "done\n";