testcustom_time_zone.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "boost/date_time/local_time/local_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. #include <iostream>
  10. int
  11. main()
  12. {
  13. using namespace boost::gregorian;
  14. using namespace boost::posix_time;
  15. using namespace boost::local_time;
  16. boost::shared_ptr<dst_calc_rule>
  17. rule1(new partial_date_dst_rule(partial_date(30,Apr),
  18. partial_date(30,Oct)));
  19. boost::shared_ptr<dst_calc_rule>
  20. rule2(new first_last_dst_rule(first_last_dst_rule::start_rule(Sunday,Apr),
  21. first_last_dst_rule::end_rule(Sunday,Oct)));
  22. boost::shared_ptr<dst_calc_rule>
  23. rule3(new last_last_dst_rule(last_last_dst_rule::start_rule(Sunday,Mar),
  24. last_last_dst_rule::end_rule(Sunday,Oct)));
  25. boost::shared_ptr<dst_calc_rule> rule4; // no daylight savings
  26. time_zone_names pst("Pacific Standard Time",
  27. "PST",
  28. "Pacific Daylight Time" ,
  29. "PDT");
  30. time_zone_names mst("Mountain Standard Time",
  31. "MST",
  32. "" ,
  33. "");
  34. dst_adjustment_offsets of(hours(1), hours(2), hours(2));
  35. dst_adjustment_offsets of2(hours(0), hours(0), hours(0)); // no daylight savings
  36. time_zone_ptr tz1(new custom_time_zone(pst, hours(-8), of, rule1));
  37. time_zone_ptr tz2(new custom_time_zone(pst, hours(-8), of, rule2));
  38. time_zone_ptr tz3(new custom_time_zone(pst, hours(-8), of, rule3));
  39. time_zone_ptr tz4(new custom_time_zone(mst, hours(-7), of2, rule4));
  40. check("out string",
  41. tz1->dst_zone_abbrev() == std::string("PDT"));
  42. check("out string",
  43. tz1->std_zone_abbrev() == std::string("PST"));
  44. check("out string",
  45. tz1->std_zone_name() == std::string("Pacific Standard Time"));
  46. check("out string",
  47. tz1->dst_zone_name() == std::string("Pacific Daylight Time"));
  48. check("dst offset", tz1->dst_offset() == hours(1));
  49. check("base offset", tz1->base_utc_offset() == hours(-8));
  50. check("has dst", tz1->has_dst());
  51. check("dst start time",
  52. tz1->dst_local_start_time(2003) == ptime(date(2003,Apr,30),hours(2)));
  53. check("dst end time",
  54. tz1->dst_local_end_time(2003) == ptime(date(2003,Oct,30),hours(2)));
  55. check("tz1 to posix string",
  56. tz1->to_posix_string() == std::string("PST-08PDT+01,120/02:00,303/02:00"));
  57. check("tz2 to posix string",
  58. tz2->to_posix_string() == std::string("PST-08PDT+01,M4.1.0/02:00,M10.5.0/02:00"));
  59. check("tz3 to posix string",
  60. tz3->to_posix_string() == std::string("PST-08PDT+01,M3.5.0/02:00,M10.5.0/02:00"));
  61. check("tz4 to posix string",
  62. tz4->to_posix_string() == std::string("MST-07"));
  63. // test start/end for non-dst zone
  64. check("has dst in non-dst zone", !tz4->has_dst());
  65. check("dst start in non-dst zone",
  66. tz4->dst_local_start_time(2005) == ptime(not_a_date_time));
  67. check("dst end in non-dst zone",
  68. tz4->dst_local_end_time(2005) == ptime(not_a_date_time));
  69. return printTestStats();
  70. }