Merge pull request #681 from cshore/pull-request-wifi-interface-name
[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 use strict;
7
8 my %TZ;
9
10 my $tzdin = $ARGV[0] || "/usr/share/zoneinfo";
11 my $tzdout = $ARGV[1] || "./modules/luci-base/luasrc/sys/zoneinfo";
12
13 local $/ = "\012";
14 open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";
15
16 while( ! eof ZTAB ) {
17 chomp( my $line = readline ZTAB );
18 next if $line =~ /^#/ || $line =~ /^\s+$/;
19
20 my ( undef, undef, $zone, @comment ) = split /\s+/, $line;
21
22 printf STDERR "%-40s", $zone;
23
24 if( open ZONE, "< $tzdin/$zone" ) {
25 seek ZONE, -2, 2;
26
27 while( tell(ZONE) > 0 ) {
28 read ZONE, my $char, 1;
29 ( $char eq "\012" ) ? last : seek ZONE, -2, 1;
30 }
31
32 chomp( my $tz = readline ZONE );
33 print STDERR ( $tz || "(no tzinfo found)" ), "\n";
34 close ZONE;
35
36 if( $tz ) {
37 $zone =~ s/_/ /g;
38 $TZ{$zone} = $tz;
39 }
40 }
41 else
42 {
43 print STDERR "open($tzdin/$zone): $!\n";
44 }
45 }
46
47 close ZTAB;
48
49
50 open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";
51
52 print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
53 print O <<HEAD;
54 -- Licensed to the public under the Apache License 2.0.
55
56 module "luci.sys.zoneinfo.tzdata"
57
58 TZ = {
59 HEAD
60
61 foreach my $zone ( sort keys %TZ ) {
62 printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
63 }
64
65 print O "}\n";
66 close O;
67
68 print STDERR "done\n";
69
70
71 open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
72
73 print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
74 print O <<HEAD;
75 -- Licensed to the public under the Apache License 2.0.
76
77 module "luci.sys.zoneinfo.tzoffset"
78
79 OFFSET = {
80 HEAD
81
82 my %seen;
83 foreach my $tz ( sort keys %TZ ) {
84 my $zone = $TZ{$tz};
85
86 if( $zone =~ /^
87 ([A-Z]+)
88 (?:
89 ( -? \d+ (?: : \d+ )? )
90 (?:
91 ([A-Z]+)
92 ( -? \d+ (?: : \d+ )? )?
93 )?
94 )?
95 \b /xo ) {
96 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
97 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
98
99 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
100
101 if ( $soffset ) {
102 ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
103
104 $s = $s ? 1 : -1;
105 $h ||= 0;
106 $m ||= 0;
107
108 $offset = $s * $h * 60 * 60;
109 $offset += $s * $m * 60;
110
111 printf O "\t%-5s = %6d,\t-- %s\n",
112 lc($std), $offset, $std;
113
114 $seen{$std} = 1;
115
116 if( $dst ) {
117 if( $doffset ) {
118 ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
119
120 $s = $s ? 1 : -1;
121 $h ||= 0;
122 $m ||= 0;
123
124 $offset = $s * $h * 60 * 60;
125 $offset += $s * $m * 60;
126 } else {
127 $offset += 60 * 60;
128 }
129
130 printf O "\t%-5s = %6d,\t-- %s\n",
131 lc($dst), $offset, $dst;
132
133 $seen{$dst} = 1;
134 }
135 }
136 else {
137 printf O "\t%-5s = %6d,\t-- %s\n",
138 lc($std), $offset, $std;
139
140 $seen{$std} = 1;
141 }
142
143 }
144 }
145
146 print O "}\n";
147 close O;
148
149 print STDERR "done\n";