testwposix_time_zone.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2003-2004 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/gregorian/gregorian.hpp"
  8. //#include "boost/date_time/local_time/time_zone.hpp"
  9. #include "../testfrmwk.hpp"
  10. #include "boost/date_time/local_time/posix_time_zone.hpp"
  11. #include <string>
  12. #include <iostream>
  13. int main(){
  14. using namespace boost::local_time;
  15. using namespace boost::posix_time;
  16. using namespace boost::gregorian;
  17. typedef posix_time_zone_base<wchar_t> w_posix_time_zone;
  18. std::wstring specs[] = {L"MST-07", L"MST-07:00:00",L"EST-05EDT,M4.1.0,M10.5.0", L"EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00",L"PST-08PDT,J46/1:30,J310",L"PST-08PDT,45,310/0:30:00"};
  19. w_posix_time_zone nyc1(specs[2]);
  20. w_posix_time_zone nyc2(specs[3]);
  21. time_duration td = hours(-5);
  22. check("Has DST", nyc1.has_dst() && nyc2.has_dst());
  23. check("UTC offset", nyc1.base_utc_offset() == td);
  24. check("UTC offsets match", nyc1.base_utc_offset() == nyc2.base_utc_offset());
  25. check("Abbrevs", nyc1.std_zone_abbrev() == std::wstring(L"EST"));
  26. check("Abbrevs", nyc2.std_zone_abbrev() == std::wstring(L"EST"));
  27. check("Abbrevs", nyc1.dst_zone_abbrev() == std::wstring(L"EDT"));
  28. check("Abbrevs", nyc2.dst_zone_abbrev() == std::wstring(L"EDT"));
  29. // names not available for w_posix_time_zone, abbrevs used in their place
  30. check("Names", nyc1.std_zone_name() == std::wstring(L"EST"));
  31. check("Names", nyc2.std_zone_name() == std::wstring(L"EST"));
  32. check("Names", nyc1.dst_zone_name() == std::wstring(L"EDT"));
  33. check("Names", nyc2.dst_zone_name() == std::wstring(L"EDT"));
  34. td = hours(1);
  35. check("dst offset", nyc1.dst_offset() == td);
  36. check("dst offsets match", nyc1.dst_offset() == nyc2.dst_offset());
  37. check("dst start", nyc1.dst_local_start_time(2003) ==
  38. ptime(date(2003,Apr,6),time_duration(2,0,0)));
  39. check("dst starts match", nyc1.dst_local_start_time(2003) ==
  40. nyc2.dst_local_start_time(2003));
  41. check("dst end", nyc1.dst_local_end_time(2003) ==
  42. ptime(date(2003,Oct,26),time_duration(2,0,0)));
  43. check("dst ends match", nyc1.dst_local_end_time(2003) ==
  44. nyc2.dst_local_end_time(2003));
  45. check("to posix string",
  46. nyc1.to_posix_string() == std::wstring(L"EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00"));
  47. check("to posix string",
  48. nyc2.to_posix_string() == std::wstring(L"EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00"));
  49. w_posix_time_zone az1(specs[0]);
  50. w_posix_time_zone az2(specs[1]);
  51. td = hours(-7);
  52. check("Has DST", !az1.has_dst() && !az2.has_dst());
  53. check("UTC offset", az1.base_utc_offset() == td);
  54. check("UTC offsets match", az1.base_utc_offset() == az2.base_utc_offset());
  55. check("dst start in non-dst zone",
  56. az1.dst_local_start_time(2005) == ptime(not_a_date_time));
  57. check("dst end in non-dst zone",
  58. az2.dst_local_end_time(2005) == ptime(not_a_date_time));
  59. check("Abbrevs", az1.std_zone_abbrev() == std::wstring(L"MST"));
  60. check("Abbrevs", az2.std_zone_abbrev() == std::wstring(L"MST"));
  61. // non-dst zones default to empty strings for dst names & abbrevs
  62. check("Abbrevs", az1.dst_zone_abbrev() == std::wstring(L""));
  63. check("Abbrevs", az2.dst_zone_abbrev() == std::wstring(L""));
  64. check("Names", az1.std_zone_name() == std::wstring(L"MST"));
  65. check("Names", az2.std_zone_name() == std::wstring(L"MST"));
  66. check("Names", az1.dst_zone_name() == std::wstring(L""));
  67. check("Names", az2.dst_zone_name() == std::wstring(L""));
  68. check("to posix string",
  69. az1.to_posix_string() == std::wstring(L"MST-07"));
  70. check("to posix string",
  71. az2.to_posix_string() == std::wstring(L"MST-07"));
  72. // bizzar time zone spec to fully test parsing
  73. std::cout << "\nFictitious time zone" << std::endl;
  74. w_posix_time_zone bz(L"BST+11:21:15BDT-00:28,M2.2.4/03:15:42,M11.5.2/01:08:53");
  75. check("hast dst", bz.has_dst());
  76. check("UTC offset", bz.base_utc_offset() == time_duration(11,21,15));
  77. check("Abbrev", bz.std_zone_abbrev() == std::wstring(L"BST"));
  78. check("Abbrev", bz.dst_zone_abbrev() == std::wstring(L"BDT"));
  79. check("dst offset", bz.dst_offset() == time_duration(0,-28,0));
  80. check("dst start", bz.dst_local_start_time(1962) ==
  81. ptime(date(1962,Feb,8),time_duration(3,15,42)));
  82. check("dst end", bz.dst_local_end_time(1962) ==
  83. ptime(date(1962,Nov,27),time_duration(1,8,53)));
  84. // only checking start & end rules w/ 'J' notation
  85. std::cout << "\n'J' notation Start/End rule tests..." << std::endl;
  86. w_posix_time_zone la1(specs[4]); // "PST-08PDT,J124,J310"
  87. //w_posix_time_zone la1("PST-08PDT,J1,J365");// Jan1/Dec31
  88. check("dst start", la1.dst_local_start_time(2003) ==
  89. ptime(date(2003,Feb,15),time_duration(1,30,0)));
  90. check("dst end", la1.dst_local_end_time(2003) ==
  91. ptime(date(2003,Nov,6),time_duration(2,0,0)));
  92. /* NOTE: la1 was created from a 'J' notation string but to_posix_string
  93. * returns an 'n' notation string. The difference between the two
  94. * is Feb-29 is always counted in an 'n' notation string and numbering
  95. * starts at zero ('J' notation starts at one).
  96. * Every possible date spec that can be written in 'J' notation can also
  97. * be written in 'n' notation. The reverse is not true so 'n' notation
  98. * is used as the output for to_posix_string(). */
  99. check("to posix string",
  100. la1.to_posix_string() == std::wstring(L"PST-08PDT+01,45/01:30,310/02:00"));
  101. // only checking start & end rules w/ 'n' notation
  102. std::cout << "\n'n' notation Start/End rule tests..." << std::endl;
  103. w_posix_time_zone la2(specs[5]); // "PST-08PDT,124,310"
  104. //w_posix_time_zone la2("PST-08PDT,0,365");// Jan1/Dec31
  105. check("dst start", la2.dst_local_start_time(2003) ==
  106. ptime(date(2003,Feb,15),time_duration(2,0,0)));
  107. check("dst end", la2.dst_local_end_time(2003) ==
  108. ptime(date(2003,Nov,6),time_duration(0,30,0)));
  109. check("to posix string",
  110. la2.to_posix_string() == std::wstring(L"PST-08PDT+01,45/02:00,310/00:30"));
  111. // bad posix time zone strings tests
  112. std::cout << "\nInvalid time zone string tests..." << std::endl;
  113. try {
  114. w_posix_time_zone badz(L"EST-13");
  115. check("Exception not thrown: bad UTC offset", false);
  116. }catch(bad_offset& boff){
  117. std::string msg(boff.what());
  118. check("Exception caught: "+msg , true);
  119. }
  120. try {
  121. w_posix_time_zone badz(L"EST-5EDT24:00:01,J124/1:30,J310");
  122. check("Exception not thrown: bad DST adjust", false);
  123. }catch(bad_adjustment& badj){
  124. std::string msg(badj.what());
  125. check("Exception caught: "+msg , true);
  126. }
  127. try {
  128. w_posix_time_zone badz(L"EST-5EDT01:00:00,J124/-1:30,J310");
  129. check("Exception not thrown: bad DST start/end offset", false);
  130. }catch(bad_offset& boff){
  131. std::string msg(boff.what());
  132. check("Exception caught: "+msg , true);
  133. }
  134. try {
  135. w_posix_time_zone badz(L"EST-5EDT01:00:00,J124/1:30,J370");
  136. check("Exception not thrown: invalid date spec", false);
  137. }catch(boost::gregorian::bad_day_of_month& boff){
  138. std::string msg(boff.what());
  139. check("Exception caught: "+msg , true);
  140. }catch(boost::gregorian::bad_month& boff){
  141. std::string msg(boff.what());
  142. check("Exception caught: "+msg , true);
  143. }catch(...){
  144. check("Unexpected exception caught: ", false);
  145. }
  146. std::cout << "\nTest some Central Europe specs" << std::endl;
  147. //Test a timezone spec on the positive side of the UTC line.
  148. //This is the time for central europe which is one hour in front of UTC
  149. //Note these Summer time transition rules aren't actually correct.
  150. w_posix_time_zone cet_tz(L"CET+01:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00");
  151. check("Has DST", cet_tz.has_dst());
  152. check("UTC offset", cet_tz.base_utc_offset() == hours(1));
  153. check("Abbrevs", cet_tz.std_zone_abbrev() == std::wstring(L"CET"));
  154. // check("Abbrevs", nyc2.std_zone_abbrev() == std::wstring("EST"));
  155. std::cout << "\nTest some Central Austrialia UTC+8:30" << std::endl;
  156. //Test a timezone spec on the positive side of the UTC line.
  157. //This is the time for central europe which is one hour in front of UTC
  158. //Note these Summer time transition rules aren't actually correct.
  159. w_posix_time_zone caus_tz(L"CAS+08:30:00CDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00");
  160. check("Has DST", caus_tz.has_dst());
  161. check("UTC offset", caus_tz.base_utc_offset() == hours(8)+minutes(30));
  162. check("Abbrevs", caus_tz.std_zone_abbrev() == std::wstring(L"CAS"));
  163. // check("Abbrevs", nyc2.std_zone_abbrev() == std::wstring("EST"));
  164. {
  165. /**** first/last of month Julian & non-Julian tests ****/
  166. // Mar-01 & Oct-31, count begins at 1
  167. std::wstring spec(L"FST+3FDT,J60,J304");
  168. w_posix_time_zone fl_1(spec);
  169. check("Julian First/last of month", fl_1.dst_local_start_time(2003) ==
  170. ptime(date(2003,Mar,1),hours(2)));
  171. check("Julian First/last of month", fl_1.dst_local_end_time(2003) ==
  172. ptime(date(2003,Oct,31),hours(2)));
  173. check("Julian First/last of month", fl_1.dst_local_start_time(2004) ==
  174. ptime(date(2004,Mar,1),hours(2)));
  175. check("Julian First/last of month", fl_1.dst_local_end_time(2004) ==
  176. ptime(date(2004,Oct,31),hours(2)));
  177. // Mar-01 & Oct-31 Non-leap year, count begins at 0
  178. spec = L"FST+3FDT,59,304"; // "304" is not a mistake here, see posix_time_zone docs
  179. w_posix_time_zone fl_2(spec);
  180. try{
  181. check("Non-Julian First/last of month", fl_2.dst_local_start_time(2003) ==
  182. ptime(date(2003,Mar,1),hours(2)));
  183. }catch(std::exception&){
  184. check("Expected exception caught for Non-Julian day of 59, in non-leap year (Feb-29)", true);
  185. }
  186. check("Non-Julian First/last of month", fl_2.dst_local_end_time(2003) ==
  187. ptime(date(2003,Oct,31),hours(2)));
  188. // Mar-01 & Oct-31 leap year, count begins at 0
  189. spec = L"FST+3FDT,60,304";
  190. w_posix_time_zone fl_3(spec);
  191. check("Non-Julian First/last of month", fl_3.dst_local_start_time(2004) ==
  192. ptime(date(2004,Mar,1),hours(2)));
  193. check("Non-Julian First/last of month", fl_3.dst_local_end_time(2004) ==
  194. ptime(date(2004,Oct,31),hours(2)));
  195. }
  196. printTestStats();
  197. return 0;
  198. }