zoneinfo2ucode.pl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/perl
  2. # zoneinfo2ucode.pl - Make ucode module from /usr/share/zoneinfo
  3. # Execute from within root of Luci feed, usually feeds/luci
  4. use strict;
  5. my %TZ;
  6. my $tzdin = $ARGV[0] || "/usr/share/zoneinfo";
  7. my $tzdout = $ARGV[1] || "./modules/luci-base/ucode/zoneinfo.uc";
  8. local $/ = "\012";
  9. open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";
  10. while( ! eof ZTAB ) {
  11. chomp( my $line = readline ZTAB );
  12. next if $line =~ /^#/ || $line =~ /^\s+$/;
  13. my ( undef, undef, $zone, @comment ) = split /\s+/, $line;
  14. printf STDERR "%-40s", $zone;
  15. if( open ZONE, "< $tzdin/$zone" ) {
  16. seek ZONE, -2, 2;
  17. while( tell(ZONE) > 0 ) {
  18. read ZONE, my $char, 1;
  19. ( $char eq "\012" ) ? last : seek ZONE, -2, 1;
  20. }
  21. chomp( my $tz = readline ZONE );
  22. print STDERR ( $tz || "(no tzinfo found)" ), "\n";
  23. close ZONE;
  24. if( $tz ) {
  25. $zone =~ s/_/ /g;
  26. $TZ{$zone} = $tz;
  27. }
  28. }
  29. else
  30. {
  31. print STDERR "open($tzdin/$zone): $!\n";
  32. }
  33. }
  34. close ZTAB;
  35. # Add Etc/GMT zones from manually as they are not in zone.tab
  36. $TZ{"Etc/GMT"} = "GMT0";
  37. $TZ{"Etc/GMT-1"} = "<+01>-1";
  38. $TZ{"Etc/GMT-2"} = "<+02>-2";
  39. $TZ{"Etc/GMT-3"} = "<+03>-3";
  40. $TZ{"Etc/GMT-4"} = "<+04>-4";
  41. $TZ{"Etc/GMT-5"} = "<+05>-5";
  42. $TZ{"Etc/GMT-6"} = "<+06>-6";
  43. $TZ{"Etc/GMT-7"} = "<+07>-7";
  44. $TZ{"Etc/GMT-8"} = "<+08>-8";
  45. $TZ{"Etc/GMT-9"} = "<+09>-9";
  46. $TZ{"Etc/GMT-10"} = "<+10>-10";
  47. $TZ{"Etc/GMT-11"} = "<+11>-11";
  48. $TZ{"Etc/GMT-12"} = "<+12>-12";
  49. $TZ{"Etc/GMT-13"} = "<+13>-13";
  50. $TZ{"Etc/GMT-14"} = "<+14>-14";
  51. $TZ{"Etc/GMT+1"} = "<-01>1";
  52. $TZ{"Etc/GMT+2"} = "<-02>2";
  53. $TZ{"Etc/GMT+3"} = "<-03>3";
  54. $TZ{"Etc/GMT+4"} = "<-04>4";
  55. $TZ{"Etc/GMT+5"} = "<-05>5";
  56. $TZ{"Etc/GMT+6"} = "<-06>6";
  57. $TZ{"Etc/GMT+7"} = "<-07>7";
  58. $TZ{"Etc/GMT+8"} = "<-08>8";
  59. $TZ{"Etc/GMT+9"} = "<-09>9";
  60. $TZ{"Etc/GMT+10"} = "<-10>10";
  61. $TZ{"Etc/GMT+11"} = "<-11>11";
  62. $TZ{"Etc/GMT+12"} = "<-12>12";
  63. open(O, "> $tzdout") || die "open($tzdout): $!\n";
  64. print STDERR "Writing time zones to $tzdout ... ";
  65. print O <<HEAD;
  66. // Autogenerated by zoneinfo2ucode.pl
  67. export default {
  68. HEAD
  69. foreach my $zone ( sort keys %TZ ) {
  70. printf O "\t'%s': '%s',\n", $zone, $TZ{$zone}
  71. }
  72. print O "};\n";
  73. close O;
  74. print STDERR "done\n";