testwcustom_time_zone.cpp 3.3 KB

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