zoneinfo2lua.pl 3.6 KB

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