PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/makejson.pl

https://bitbucket.org/haraki/mash-time-zone-converter
Perl | 141 lines | 83 code | 23 blank | 35 comment | 10 complexity | 53597c51f99d64ae1d96c2edd2535bdd MD5 | raw file
  1. #!/usr/bin/perl
  2. #
  3. # MASH Time Zone Converter v1.0
  4. #
  5. # https://bitbucket.org/haraki/mash-time-zone-converter
  6. #
  7. #
  8. # The MIT License (MIT)
  9. #
  10. # Copyright (c) 2013 Masashi Haraki (masa.haraki@gmail.com)
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documentation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furnished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. my $iso3166_tab_file = "tz/iso3166.tab";
  31. my $zone_tab_file = "tz/zone.tab";
  32. my $timezone_json_file = "timezone.json";
  33. my $country_json_file = "country.json";
  34. # read 'iso3166.tab'
  35. if(!open(FILE, $iso3166_tab_file))
  36. {
  37. print $iso3166_tab_file . " open error.\n";
  38. exit(-1);
  39. }
  40. my @iso3166_tab_list = <FILE>;
  41. close(FILE);
  42. # parse 'iso3166.tab'
  43. my %country_hash;
  44. foreach my $iso3166_tab_str (@iso3166_tab_list)
  45. {
  46. if($iso3166_tab_str !~ /#/)
  47. {
  48. chomp($iso3166_tab_str);
  49. my @iso3166_tab = split(/\t/, $iso3166_tab_str);
  50. $country_hash{$iso3166_tab[0]} = $iso3166_tab[1]; # key = country code, param = country name
  51. }
  52. }
  53. # read 'zone.tab'
  54. if(!open(FILE, $zone_tab_file))
  55. {
  56. print $zone_tab_file . " open error.\n";
  57. exit(-1);
  58. }
  59. my @zone_tab_list = <FILE>;
  60. close(FILE);
  61. # parse 'zone.tab'
  62. my @zone_cc_list;
  63. my @zone_tz_list;
  64. foreach my $zone_tab_str (@zone_tab_list)
  65. {
  66. if($zone_tab_str !~ /#/)
  67. {
  68. chomp($zone_tab_str);
  69. my @zone_tab = split(/\t/, $zone_tab_str);
  70. push(@zone_cc_list, $zone_tab[0]);
  71. push(@zone_tz_list, $zone_tab[2]);
  72. }
  73. }
  74. # open 'timezone.json'
  75. if(!open(TIMEZONE_OUT_FILE, ">$timezone_json_file"))
  76. {
  77. print $timezone_json_file . " open error.\n";
  78. exit(-1);
  79. }
  80. print TIMEZONE_OUT_FILE "[\n";
  81. for(my $i = 0;$i < @zone_tz_list;$i++)
  82. {
  83. if($i > 0)
  84. {
  85. print TIMEZONE_OUT_FILE ",\n";
  86. }
  87. my $zone_tz_str = $zone_tz_list[$i];
  88. my @zone_tz = split(/\//, $zone_tz_str);
  89. my $zone_city = pop(@zone_tz);
  90. my $zone_country = $country_hash{$zone_cc_list[$i]};
  91. $zone_country =~ s/\s/_/g;
  92. $zone_country =~ s/\&/and/g;
  93. print TIMEZONE_OUT_FILE "\t{ \"tz\":\"$zone_tz_str\", \"country\":\"$zone_country\", \"city_name\":\"$zone_city\" }";
  94. }
  95. print TIMEZONE_OUT_FILE "\n]\n";
  96. close(TIMEZONE_OUT_FILE);
  97. # open 'country.json'
  98. if(!open(COUNTRY_OUT_FILE, ">$country_json_file"))
  99. {
  100. print $country_json_file . " open error.\n";
  101. exit(-1);
  102. }
  103. my @country_list = values(%country_hash);
  104. print COUNTRY_OUT_FILE "[\n";
  105. for(my $i = 0;$i < @country_list;$i++)
  106. {
  107. if($i > 0)
  108. {
  109. print COUNTRY_OUT_FILE ",\n";
  110. }
  111. my $country_name = $country_list[$i];
  112. my $country = $country_list[$i];
  113. $country =~ s/\s/_/g;
  114. $country =~ s/\&/and/g;
  115. print COUNTRY_OUT_FILE "\t{ \"country\":\"$country\", \"country_name\":\"$country_name\" }";
  116. }
  117. print COUNTRY_OUT_FILE "\n]\n";
  118. close(COUNTRY_OUT_FILE);