timezone-gen.pl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use XML::Entities;
  6. use HTML::Entities;
  7. require "./fix-tzstr.pl";
  8. my $base = "/usr/share/zoneinfo";
  9. my $output = "timezones.conf.xml";
  10. my $debug;
  11. my $help;
  12. my %zones = ();
  13. my %name_to_file = ();
  14. my $res = GetOptions(
  15. "base=s" => \$base,
  16. "debug+" => \$debug,
  17. "help" => \$help,
  18. "output=s" => \$output
  19. );
  20. if ( !$res || $help ) {
  21. print "$0 [--base=/usr/share/zoneinfo] [--output=timezones.conf.xml] [--debug] [--help]\n";
  22. exit;
  23. }
  24. my @dirs = ($base);
  25. while (@dirs) {
  26. my $dir = shift @dirs;
  27. opendir( my $top, $dir );
  28. while ( my $file = readdir($top) ) {
  29. next if ( $file eq "." || $file eq ".." );
  30. if ( -f "$dir/$file" ) {
  31. $debug && print "Found $dir/$file\n";
  32. my $name = "$dir/$file";
  33. $name =~ s|^${base}/||o;
  34. $name_to_file{$name} = "$dir/$file";
  35. }
  36. elsif ( -d "$dir/$file" ) {
  37. $debug && print "Found subdir $dir/$file\n";
  38. push( @dirs, "$dir/$file" );
  39. }
  40. }
  41. closedir($top);
  42. }
  43. foreach my $name ( sort( keys(%name_to_file) ) ) {
  44. my $file = $name_to_file{$name};
  45. $debug && print "Processing $file...\n";
  46. open( my $in, "<$file" );
  47. my $data = join( "", <$in> );
  48. close($in);
  49. my @strings = $data =~ (m/[ -~]{4,}/g);
  50. if ( shift(@strings) !~ /^TZif/o ) {
  51. $debug && print "Skipped $file\n";
  52. next;
  53. }
  54. my $tzstr = fixTzstr( pop(@strings), $name );
  55. $zones{$name} = $tzstr;
  56. }
  57. open( my $out, ">$output" );
  58. print $out "<configuration name=\"timezones.conf\" description=\"Timezones\">\n";
  59. print $out " " x 4, "<timezones>\n";
  60. my $lastprefix = "";
  61. foreach my $zone ( sort( keys(%zones) ) ) {
  62. my $str = encode_entities($zones{$zone});
  63. next if ( !$str );
  64. my $newprefix = $zone;
  65. $newprefix =~ s|/.*||go;
  66. if ( $newprefix ne $lastprefix && $lastprefix ne "" ) {
  67. print $out "\n";
  68. }
  69. $lastprefix = $newprefix;
  70. print $out " " x 8, "<zone name=\"$zone\" value=\"$str\" />\n";
  71. }
  72. print $out " " x 4, "</timezones>\n";
  73. print $out "</configuration>\n";
  74. close($out);