testlocal_time_facet.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (c) 2004 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland, Bart Garst
  6. * $Date$
  7. */
  8. #include <iostream>
  9. #include <sstream>
  10. #include <boost/shared_ptr.hpp>
  11. #include "boost/date_time/local_time/local_time.hpp"
  12. #include "../testfrmwk.hpp"
  13. template<class temporal_type, typename charT>
  14. inline
  15. void
  16. teststreaming(std::string const& testname,
  17. temporal_type value,
  18. std::basic_string<charT> const& expected_result,
  19. const std::locale& locale)
  20. {
  21. std::basic_stringstream<charT> ss;
  22. ss.imbue(locale);
  23. ss << value;
  24. if (!check(testname, ss.str() == expected_result))
  25. #if !defined(BOOST_NO_STD_WSTRING)
  26. std::wcout << L"Expected: \"" << expected_result.c_str() << L"\"\nGot: \"" << ss.str().c_str() << L"\"" << std::endl;
  27. #else
  28. std::cout << "Expected: \"" << expected_result.c_str() << "\"\nGot: \"" << ss.str().c_str() << L"\"" << std::endl;
  29. #endif
  30. }
  31. int main(int /* argc */, char const* argv[]){
  32. /* use the tz_database for the time zones.
  33. * Chicago, Denver, Los_Angeles, New_Tork, and Phoenix
  34. * have all had full names added */
  35. using namespace boost;
  36. using namespace boost::local_time;
  37. using namespace boost::posix_time;
  38. using namespace boost::gregorian;
  39. tz_database time_zones;
  40. try {
  41. // first try to find the data file from the test dir
  42. time_zones.load_from_file(argv[1]);
  43. }
  44. catch(const data_not_accessible&) {
  45. check("Cannot locate data file - aborting.", false);
  46. return printTestStats();
  47. }
  48. time_zone_ptr utc;
  49. time_zone_ptr chicago = time_zones.time_zone_from_region("America/Chicago");
  50. time_zone_ptr denver = time_zones.time_zone_from_region("America/Denver");
  51. time_zone_ptr la = time_zones.time_zone_from_region("America/Los_Angeles");
  52. time_zone_ptr nyc = time_zones.time_zone_from_region("America/New_York");
  53. time_zone_ptr phx = time_zones.time_zone_from_region("America/Phoenix");
  54. // sydney does not have full time zone names in the tz_database
  55. time_zone_ptr sydney = time_zones.time_zone_from_region("Australia/Sydney");
  56. ptime a_time(date(2004,Dec,15), hours(12));
  57. ptime b_time(date(2004,Aug,15), hours(12));
  58. local_date_time ldt1(a_time, utc);
  59. local_date_time ldt2(b_time, chicago);
  60. local_date_time ldt3(a_time, denver);
  61. local_date_time ldt4(b_time, la);
  62. local_date_time ldt5(a_time, nyc);
  63. local_date_time ldt6(b_time, phx);
  64. local_time_period ltp1(ldt1, hours(10) + minutes(24) + seconds(5));
  65. local_time_period ltp2(ldt4, hours(15) + minutes(20) + seconds(41));
  66. typedef boost::date_time::time_facet<local_date_time, char> ldt_facet;
  67. //ldt_facet* timefacet = new ldt_facet("%c %Z"); // full name
  68. ldt_facet* timefacet = new ldt_facet("%a %b %d %H:%M:%S %Y %Z"); // full name
  69. std::locale loc1(std::locale::classic(), timefacet);
  70. typedef boost::date_time::time_facet<ptime, char> pt_facet;
  71. //pt_facet* ptimefacet1 = new pt_facet("%c %Z"); // show that zone is ignored
  72. pt_facet* ptimefacet1 = new pt_facet("%a %b %d %H:%M:%S %Y %Z"); // show that zone is ignored
  73. std::locale loc2(std::locale::classic(), ptimefacet1);
  74. std::cout << "\nFull time zone names tests" << std::endl;
  75. teststreaming("ptime with %Z flag\n", a_time, std::string("Wed Dec 15 12:00:00 2004") , loc2);
  76. teststreaming("UTC local_date_time", ldt1, std::string("Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc1);
  77. teststreaming("Chicago in summer", ldt2, std::string("Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc1);
  78. teststreaming("Denver in winter", ldt3, std::string("Wed Dec 15 05:00:00 2004 Mountain Standard Time"), loc1);
  79. teststreaming("Los Angeles in summer", ldt4, std::string("Sun Aug 15 05:00:00 2004 Pacific Daylight Time"), loc1);
  80. teststreaming("New York in winter", ldt5, std::string("Wed Dec 15 07:00:00 2004 Eastern Standard Time"), loc1);
  81. teststreaming("Phoenix in Summer", ldt6, std::string("Sun Aug 15 05:00:00 2004 Mountain Standard Time"), loc1);
  82. teststreaming("UTC local_time_period", ltp1, std::string("[Wed Dec 15 12:00:00 2004 Coordinated Universal Time/Wed Dec 15 22:24:04 2004 Coordinated Universal Time]"), loc1);
  83. teststreaming("LA local_time_period", ltp2, std::string("[Sun Aug 15 05:00:00 2004 Pacific Daylight Time/Sun Aug 15 20:20:40 2004 Pacific Daylight Time]"), loc1);
  84. //ptimefacet1->format("%c %z"); // show that zone abbrev is ignored
  85. ptimefacet1->format("%a %b %d %H:%M:%S %Y %z"); // show that zone abbrev is ignored
  86. std::cout << "\nTime zone abbreviation tests" << std::endl;
  87. teststreaming("ptime with %z flag\n", a_time, std::string("Wed Dec 15 12:00:00 2004") , loc2);
  88. // using standard format
  89. //timefacet->format("%c %z"); // abbreviated zone
  90. timefacet->format("%a %b %d %H:%M:%S %Y %z"); // abbreviated zone
  91. teststreaming("UTC local_date_time", ldt1, std::string("Wed Dec 15 12:00:00 2004 UTC"), loc1);
  92. teststreaming("Chicago in summer", ldt2, std::string("Sun Aug 15 07:00:00 2004 CDT") , loc1);
  93. teststreaming("Denver in winter", ldt3, std::string("Wed Dec 15 05:00:00 2004 MST"), loc1);
  94. teststreaming("Los Angeles in summer", ldt4, std::string("Sun Aug 15 05:00:00 2004 PDT"), loc1);
  95. teststreaming("New York in winter", ldt5, std::string("Wed Dec 15 07:00:00 2004 EST"), loc1);
  96. teststreaming("Phoenix in Summer", ldt6, std::string("Sun Aug 15 05:00:00 2004 MST"), loc1);
  97. // iso format
  98. // show that zone offset is ignored
  99. ptimefacet1->format(pt_facet::iso_time_format_specifier);
  100. std::cout << "\nLocal time iso format tests" << std::endl;
  101. teststreaming("ptime with iso format\n", a_time,
  102. std::string("20041215T120000") , loc2);
  103. timefacet->format(ldt_facet::iso_time_format_specifier);
  104. teststreaming("UTC local_date_time", ldt1,
  105. std::string("20041215T120000Z"), loc1);
  106. teststreaming("Chicago in summer", ldt2,
  107. std::string("20040815T070000-0500") , loc1);
  108. teststreaming("Denver in winter", ldt3,
  109. std::string("20041215T050000-0700"), loc1);
  110. teststreaming("Los Angeles in summer", ldt4,
  111. std::string("20040815T050000-0700"), loc1);
  112. teststreaming("New York in winter", ldt5,
  113. std::string("20041215T070000-0500"), loc1);
  114. teststreaming("Phoenix in Summer", ldt6,
  115. std::string("20040815T050000-0700"), loc1);
  116. teststreaming("Sydney in December", ldt1.local_time_in(sydney),
  117. std::string("20041215T230000+1100"), loc1);
  118. // iso extended format
  119. // show that zone offset is ignored
  120. ptimefacet1->format(pt_facet::iso_time_format_extended_specifier);
  121. std::cout << "\nLocal time iso_extended tests" << std::endl;
  122. teststreaming("ptime with iso extended format\n", a_time,
  123. std::string("2004-12-15 12:00:00") , loc2);
  124. timefacet->format(ldt_facet::iso_time_format_extended_specifier);
  125. teststreaming("UTC local_date_time", ldt1,
  126. std::string("2004-12-15 12:00:00Z"), loc1);
  127. teststreaming("Chicago in summer", ldt2,
  128. std::string("2004-08-15 07:00:00-05:00") , loc1);
  129. teststreaming("Denver in winter", ldt3,
  130. std::string("2004-12-15 05:00:00-07:00"), loc1);
  131. teststreaming("Los Angeles in summer", ldt4,
  132. std::string("2004-08-15 05:00:00-07:00"), loc1);
  133. teststreaming("New York in winter", ldt5,
  134. std::string("2004-12-15 07:00:00-05:00"), loc1);
  135. teststreaming("Phoenix in Summer", ldt6,
  136. std::string("2004-08-15 05:00:00-07:00"), loc1);
  137. teststreaming("Sydney in December", ldt1.local_time_in(sydney),
  138. std::string("2004-12-15 23:00:00+11:00"), loc1);
  139. #if !defined(BOOST_NO_STD_WSTRING)
  140. typedef boost::date_time::time_facet<local_date_time, wchar_t> wldt_facet;
  141. //wldt_facet* wtimefacet = new wldt_facet(L"%c %Z"); // full name
  142. wldt_facet* wtimefacet = new wldt_facet(L"%a %b %d %H:%M:%S %Y %Z"); // full name
  143. std::locale loc3(std::locale::classic(), wtimefacet);
  144. /* Again, wide stream tests are more thoroughly done in the
  145. * time_facet tests. Here we just need to show that they work */
  146. std::cout << "\nFull time zone names tests - wide stream" << std::endl;
  147. teststreaming("UTC local_date_time", ldt1, std::wstring(L"Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc3);
  148. teststreaming("Chicago in summer", ldt2, std::wstring(L"Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc3);
  149. teststreaming("UTC local_time_period", ltp1, std::wstring(L"[Wed Dec 15 12:00:00 2004 Coordinated Universal Time/Wed Dec 15 22:24:04 2004 Coordinated Universal Time]"), loc3);
  150. teststreaming("LA local_time_period", ltp2, std::wstring(L"[Sun Aug 15 05:00:00 2004 Pacific Daylight Time/Sun Aug 15 20:20:40 2004 Pacific Daylight Time]"), loc3);
  151. //wtimefacet->format(L"%c %z"); // abbrev
  152. wtimefacet->format(L"%a %b %d %H:%M:%S %Y %z"); // abbrev
  153. std::cout << "\nAbbreviated time zone names tests - wide stream" << std::endl;
  154. teststreaming("UTC local_date_time", ldt1, std::wstring(L"Wed Dec 15 12:00:00 2004 UTC"), loc3);
  155. teststreaming("Phoenix in Summer", ldt6, std::wstring(L"Sun Aug 15 05:00:00 2004 MST"), loc3);
  156. #endif // BOOST_NO_STD_WSTRING
  157. return printTestStats();
  158. }