zoneinfo2lua.pl 3.9 KB

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