libs/core: adapt luci.model.network for wifi-iface instances which are members of...
[project/luci.git] / build / zoneinfo2lua.pl
1 #!/usr/bin/perl
2 # zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
3 # Execute from within /usr/share/zoneinfo
4 # $Id$
5
6 use strict;
7
8 my %TZ;
9
10 my $tzdin = $ARGV[0] || "/usr/share/zoneinfo";
11 my $tzdout = $ARGV[1] || "./libs/sys/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 --[[
55 LuCI - Autogenerated Zoneinfo Module
56
57 Licensed under the Apache License, Version 2.0 (the "License");
58 you may not use this file except in compliance with the License.
59 You may obtain a copy of the License at
60
61 http://www.apache.org/licenses/LICENSE-2.0
62
63 ]]--
64
65 module "luci.sys.zoneinfo.tzdata"
66
67 TZ = {
68 HEAD
69
70 foreach my $zone ( sort keys %TZ ) {
71 printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
72 }
73
74 print O "}\n";
75 close O;
76
77 print STDERR "done\n";
78
79
80 open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
81
82 print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
83 print O <<HEAD;
84 --[[
85 LuCI - Autogenerated Zoneinfo Module
86
87 Licensed under the Apache License, Version 2.0 (the "License");
88 you may not use this file except in compliance with the License.
89 You may obtain a copy of the License at
90
91 http://www.apache.org/licenses/LICENSE-2.0
92
93 ]]--
94
95 module "luci.sys.zoneinfo.tzoffset"
96
97 OFFSET = {
98 HEAD
99
100 my %seen;
101 foreach my $tz ( sort keys %TZ ) {
102 my $zone = $TZ{$tz};
103
104 if( $zone =~ /^
105 ([A-Z]+)
106 (?:
107 ( -? \d+ (?: : \d+ )? )
108 (?:
109 ([A-Z]+)
110 ( -? \d+ (?: : \d+ )? )?
111 )?
112 )?
113 \b /xo ) {
114 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
115 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
116
117 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
118
119 if ( $soffset ) {
120 ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
121
122 $s = $s ? 1 : -1;
123 $h ||= 0;
124 $m ||= 0;
125
126 $offset = $s * $h * 60 * 60;
127 $offset += $s * $m * 60;
128
129 printf O "\t%-5s = %6d,\t-- %s\n",
130 lc($std), $offset, $std;
131
132 $seen{$std} = 1;
133
134 if( $dst ) {
135 if( $doffset ) {
136 ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
137
138 $s = $s ? 1 : -1;
139 $h ||= 0;
140 $m ||= 0;
141
142 $offset = $s * $h * 60 * 60;
143 $offset += $s * $m * 60;
144 } else {
145 $offset += 60 * 60;
146 }
147
148 printf O "\t%-5s = %6d,\t-- %s\n",
149 lc($dst), $offset, $dst;
150
151 $seen{$dst} = 1;
152 }
153 }
154 else {
155 printf O "\t%-5s = %6d,\t-- %s\n",
156 lc($std), $offset, $std;
157
158 $seen{$std} = 1;
159 }
160
161 }
162 }
163
164 print O "}\n";
165 close O;
166
167 print STDERR "done\n";