testtz_database.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (c) 2003-2005 CrystalClear Software, Inc.
  2. * Subject to the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  4. * Author: Jeff Garland, Bart Garst
  5. * $Date$
  6. */
  7. #include "../testfrmwk.hpp"
  8. #include "boost/date_time/gregorian/gregorian.hpp"
  9. #include "boost/date_time/posix_time/posix_time.hpp"
  10. #include "boost/date_time/local_time/custom_time_zone.hpp"
  11. #include "boost/date_time/local_time/local_time_types.hpp"
  12. #include "boost/date_time/local_time/tz_database.hpp"
  13. #include "boost/date_time/local_time/posix_time_zone.hpp"
  14. #include <iostream>
  15. bool run_bad_field_count_test(char const* fn);
  16. int main(int /* argc */, char const* argv[]){
  17. using namespace boost::gregorian;
  18. using namespace boost::posix_time;
  19. using namespace boost::local_time;
  20. /* NOTE: The testlocal_time_facet tests required full names
  21. * be added to some of the date_time_zonespec.csv entries. The
  22. * tests here also use those full names. Those entries are:
  23. * Chicago, Denver, Los_Angeles, New_York, and Phoenix
  24. * have all had full names added */
  25. // run the exception tests first
  26. try{
  27. tz_database tz_db;
  28. tz_db.load_from_file("missing_file.csv"); // file does not exist
  29. }catch(data_not_accessible&){
  30. check("Caught Missing data file exception", true);
  31. }catch(...){
  32. check("Caught first unexpected exception", false);
  33. }
  34. check("Caught Bad field count exception", run_bad_field_count_test(argv[2]));
  35. tz_database tz_db;
  36. try {
  37. tz_db.load_from_file(argv[1]);
  38. }catch(...) {
  39. check("Cannot locate data file - aborting.", false);
  40. return printTestStats();
  41. }
  42. time_zone_ptr bad_tz = tz_db.time_zone_from_region("Invalid/name");
  43. check("Expected null pointer return", bad_tz == time_zone_ptr());
  44. time_zone_ptr nyc_test = tz_db.time_zone_from_region("America/New_York");
  45. check("nyc Valid pointer", nyc_test != time_zone_ptr() );
  46. check("nyc Abbreviations",nyc_test->std_zone_abbrev() == std::string("EST"));
  47. check("nyc Full Name", nyc_test->std_zone_name() == std::string("Eastern Standard Time"));
  48. check("nyc Abbreviations",nyc_test->dst_zone_abbrev() == std::string("EDT"));
  49. //std::cout << nyc_test->std_zone_name() << std::endl;
  50. check("nyc Full Name", nyc_test->dst_zone_name() == std::string("Eastern Daylight Time"));
  51. check("nyc GMT Offset", nyc_test->base_utc_offset() == hours(-5));
  52. check("nyc DST Offset", nyc_test->dst_offset() == hours(1));
  53. //std::cout << nyc_test->dst_local_start_time(2004) << std::endl;
  54. check("nyc dst start", nyc_test->dst_local_start_time(2007) == ptime(date(2007, Mar, 11), hours(2)));
  55. check("nyc dst end", nyc_test->dst_local_end_time(2007) == ptime(date(2007, Nov, 4), hours(2)));
  56. check("nyc has dst", nyc_test->has_dst());
  57. time_zone_ptr phx_test = tz_db.time_zone_from_region("America/Phoenix");
  58. check("az Valid pointer", phx_test != time_zone_ptr() );
  59. check("az Abbreviations",phx_test->std_zone_abbrev() == std::string("MST"));
  60. check("az Full Name", phx_test->std_zone_name() == std::string("Mountain Standard Time"));
  61. check("az Abbreviations", phx_test->dst_zone_abbrev() == std::string(""));
  62. check("az Full Name", phx_test->dst_zone_name() == std::string(""));
  63. check("az GMT Offset", phx_test->base_utc_offset() == hours(-7));
  64. check("az DST Offset", phx_test->dst_offset() == hours(0));
  65. //std::cout << phx_test->dst_local_start_time(2004) << std::endl;
  66. check("az has dst", phx_test->has_dst() == false);
  67. //Now add and retrieve a Posix tz spec from the database
  68. time_zone_ptr eastern(new posix_time_zone("EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"));
  69. tz_db.add_record("United States/Eastern", eastern);
  70. time_zone_ptr eastern_test = tz_db.time_zone_from_region("United States/Eastern");
  71. check("eastern Valid pointer", eastern_test != time_zone_ptr() );
  72. check("eastern Abbreviations",
  73. eastern_test->std_zone_abbrev() == std::string("EST"));
  74. check("eastern Abbreviations",
  75. eastern_test->std_zone_name() == std::string("EST"));
  76. check("eastern Abbreviations",
  77. eastern_test->dst_zone_abbrev() == std::string("EDT"));
  78. check("eastern Abbreviations",
  79. eastern_test->dst_zone_name() == std::string("EDT"));
  80. check("eastern GMT Offset", eastern_test->base_utc_offset() == hours(-5));
  81. check("eastern dst start", eastern_test->dst_local_start_time(2004) == ptime(date(2004, Apr, 4), hours(2)));
  82. check("eastern dst end", eastern_test->dst_local_end_time(2004) == ptime(date(2004, Oct, 31), hours(2)));
  83. check("eastern has dst", eastern_test->has_dst() == true);
  84. return printTestStats();
  85. }
  86. /* This test only checks to make sure the bad_field_count exception
  87. * is properly thrown. It does not pay any attention to any other
  88. * exception, those are tested elsewhere. */
  89. bool run_bad_field_count_test(char const* fn)
  90. {
  91. using namespace boost::local_time;
  92. bool caught_bfc = false;
  93. tz_database other_db;
  94. try{
  95. other_db.load_from_file(fn);
  96. }catch(bad_field_count&){
  97. caught_bfc = true;
  98. }catch(...) {
  99. // do nothing (file not found)
  100. }
  101. return caught_bfc;
  102. }