1
0

zoneinfo2lua.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";
  37. print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
  38. print O <<HEAD;
  39. -- Licensed to the public under the Apache License 2.0.
  40. module "luci.sys.zoneinfo.tzdata"
  41. TZ = {
  42. HEAD
  43. foreach my $zone ( sort keys %TZ ) {
  44. printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
  45. }
  46. print O "}\n";
  47. close O;
  48. print STDERR "done\n";
  49. open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
  50. print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
  51. print O <<HEAD;
  52. -- Licensed to the public under the Apache License 2.0.
  53. module "luci.sys.zoneinfo.tzoffset"
  54. OFFSET = {
  55. HEAD
  56. my %seen;
  57. foreach my $tz ( sort keys %TZ ) {
  58. my $zone = $TZ{$tz};
  59. if( $zone =~ /^
  60. ([A-Z]+)
  61. (?:
  62. ( -? \d+ (?: : \d+ )? )
  63. (?:
  64. ([A-Z]+)
  65. ( -? \d+ (?: : \d+ )? )?
  66. )?
  67. )?
  68. \b /xo ) {
  69. my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
  70. my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
  71. next if $seen{$std}; # and ( !$dst or $seen{$dst} );
  72. if ( $soffset ) {
  73. ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
  74. $s = $s ? 1 : -1;
  75. $h ||= 0;
  76. $m ||= 0;
  77. $offset = $s * $h * 60 * 60;
  78. $offset += $s * $m * 60;
  79. printf O "\t%-5s = %6d,\t-- %s\n",
  80. lc($std), $offset, $std;
  81. $seen{$std} = 1;
  82. if( $dst ) {
  83. if( $doffset ) {
  84. ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
  85. $s = $s ? 1 : -1;
  86. $h ||= 0;
  87. $m ||= 0;
  88. $offset = $s * $h * 60 * 60;
  89. $offset += $s * $m * 60;
  90. } else {
  91. $offset += 60 * 60;
  92. }
  93. printf O "\t%-5s = %6d,\t-- %s\n",
  94. lc($dst), $offset, $dst;
  95. $seen{$dst} = 1;
  96. }
  97. }
  98. else {
  99. printf O "\t%-5s = %6d,\t-- %s\n",
  100. lc($std), $offset, $std;
  101. $seen{$std} = 1;
  102. }
  103. }
  104. }
  105. print O "}\n";
  106. close O;
  107. print STDERR "done\n";