build: make zoneinfo2lua.pl script more convenient
[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 my $tzdir = $ARGV[0] || "/usr/share/zoneinfo";
10
11 chdir($tzdir) || die "chdir($tzdir): $!\n";
12
13 local $/ = "\012";
14 open( ZTAB, "< ./zone.tab" ) || die "Unable to open 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, "< ./$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 "Unable to open $zone: $!\n";
44         }
45 }
46
47 close ZTAB;
48
49
50 print <<HEAD;
51 --[[
52 LuCI - Autogenerated Zoneinfo Module
53
54 Licensed under the Apache License, Version 2.0 (the "License");
55 you may not use this file except in compliance with the License.
56 You may obtain a copy of the License at
57
58         http://www.apache.org/licenses/LICENSE-2.0
59
60 ]]--
61
62 module "luci.sys.zoneinfo"
63
64 TZ = {
65 HEAD
66
67 foreach my $zone ( sort keys %TZ ) {
68         printf "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
69 }
70
71 print <<HEAD;
72 }
73
74 OFFSET = {
75 HEAD
76
77 my %seen;
78 foreach my $tz ( sort keys %TZ ) {
79         my $zone = $TZ{$tz};
80
81         if( $zone =~ /^
82                 ([A-Z]+)
83                 (?:
84                         ( -? \d+ (?: : \d+ )? )
85                         (?:
86                                 ([A-Z]+)
87                                 ( -? \d+ (?: : \d+ )? )? 
88                         )?
89                 )?
90         \b /xo ) {
91                 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
92                 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
93
94                 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
95
96                 if ( $soffset ) {
97                         ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
98
99                         $s = $s ? 1 : -1;
100                         $h ||= 0;
101                         $m ||= 0;
102
103                         $offset  = $s * $h * 60 * 60;
104                         $offset += $s * $m * 60;
105
106                         printf("\t%-5s = %6d,\t-- %s\n",
107                                 lc($std), $offset, $std);
108
109                         $seen{$std} = 1;
110
111                         if( $dst ) {
112                                 if( $doffset ) {
113                                         ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
114
115                                         $s = $s ? 1 : -1;
116                                         $h ||= 0;
117                                         $m ||= 0;
118
119                                         $offset  = $s * $h * 60 * 60;
120                                         $offset += $s * $m * 60;
121                                 } else  {
122                                         $offset += 60 * 60;
123                                 }
124
125                                 printf("\t%-5s = %6d,\t-- %s\n",
126                                         lc($dst), $offset, $dst);
127         
128                                 $seen{$dst} = 1;
129                         }
130                 }
131                 else {
132                         printf("\t%-5s = %6d,\t-- %s\n",
133                                 lc($std), $offset, $std);
134
135                         $seen{$std} = 1;
136                 }
137
138         }
139 }
140
141 print "}\n";