testtime_formatters.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2002,2003 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. */
  7. #include "boost/date_time/posix_time/posix_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. #define CHECK_ROUNDTRIP(_PT) check_equal("from_iso_string(to_iso_string()) roundtrip of \"" + to_iso_string(_PT) + "\"", from_iso_string(to_iso_string(_PT)), _PT)
  10. int
  11. main()
  12. {
  13. using namespace boost::posix_time;
  14. using namespace boost::gregorian;
  15. date d1(2002,Jan,1);
  16. std::string d1_string("2002-Jan-01");
  17. std::string t1_string("01:02:03");
  18. std::string t1_result = d1_string + " " + t1_string;
  19. ptime t1(d1,time_duration(1,2,3)); //2002-Jan-1 01:02:03
  20. std::cout << to_simple_string(t1) << std::endl;
  21. check("simple: " + t1_result, t1_result == to_simple_string(t1));
  22. std::string iso_result = "20020101T010203";
  23. check("iso: " + iso_result, iso_result == to_iso_string(t1));
  24. std::string iso_ext_result = "2002-01-01T01:02:03";
  25. check("iso ext: " + iso_ext_result, iso_ext_result == to_iso_extended_string(t1));
  26. CHECK_ROUNDTRIP(t1);
  27. #ifdef BOOST_DATE_TIME_HAS_MILLISECONDS
  28. if (time_duration::resolution() == boost::date_time::milli) {
  29. ptime t4(d1,hours(1)+minutes(2)+seconds(3)+millisec(4));
  30. std::string r3 = to_simple_string(t4);
  31. check("simple subsecond: "+r3 ,
  32. std::string("2002-Jan-01 01:02:03.004000") == r3);
  33. CHECK_ROUNDTRIP(t4);
  34. }
  35. #endif
  36. #ifdef BOOST_DATE_TIME_HAS_MICROSECONDS
  37. if (time_duration::resolution() == boost::date_time::micro) {
  38. ptime t3(d1,hours(1)+minutes(2)+seconds(3)+microsec(4));
  39. std::string result = to_simple_string(t3);
  40. check("microsecond: "+result ,
  41. std::string("2002-Jan-01 01:02:03.000004") == to_simple_string(t3));
  42. time_duration td2 = hours(-12)+minutes(4)+seconds(2)+microsec(1);
  43. // time_duration td2 = hours(-12)+minutes(4)+seconds(2)+millisec(1);
  44. std::string r2 = to_simple_string(td2);
  45. check("microseond neg subsecond duration: "+r2 ,
  46. std::string("-11:55:57.999999") == r2);
  47. std::string s1("-01:25:00"), s2("-00:40:00"), is1("-012500"), is2("-004000");
  48. time_duration td1(-1,25,0);
  49. time_duration tds2(0,-40,0);
  50. check("to string: " + to_simple_string(td1), to_simple_string(td1) == s1);
  51. check("to string: " + to_simple_string(tds2), to_simple_string(tds2) == s2);
  52. check("to string: " + to_iso_string(td1), to_iso_string(td1) == is1);
  53. check("to string: " + to_iso_string(tds2), to_iso_string(tds2) == is2);
  54. CHECK_ROUNDTRIP(t3);
  55. }
  56. #endif
  57. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  58. if (time_duration::resolution() == boost::date_time::nano) {
  59. ptime t2(d1,hours(12) + minutes(5) + seconds(1));
  60. time_period p1(t1,t2); //last value in period is 12:05:00 1/10000 sec less than t2
  61. std::string period_result("["+t1_result + "/" + d1_string + " " + "12:05:00.999999999]" );
  62. check("simple: " + period_result + "==" + to_simple_string(p1), period_result == to_simple_string(p1));
  63. ptime t3(d1,hours(1)+minutes(2)+seconds(3)+nanosec(4));
  64. std::string result = to_simple_string(t3);
  65. check("simple subsecond: "+result ,
  66. std::string("2002-Jan-01 01:02:03.000000004") == to_simple_string(t3));
  67. std::string s1("-01:25:00"), s2("-00:40:00"), is1("-012500"), is2("-004000");
  68. time_duration td1(-1,25,0);
  69. time_duration tds2(0,-40,0);
  70. check("to string: " + to_simple_string(td1), to_simple_string(td1) == s1);
  71. check("to string: " + to_simple_string(tds2), to_simple_string(tds2) == s2);
  72. check("to string: " + to_iso_string(td1), to_iso_string(td1) == is1);
  73. check("to string: " + to_iso_string(tds2), to_iso_string(tds2) == is2);
  74. time_duration td2 = hours(-12)+minutes(4)+seconds(2)+nanosec(100);
  75. std::string r2 = to_simple_string(td2);
  76. check("neg subsecond duration: "+r2 ,
  77. std::string("-11:55:57.999999900") == r2);
  78. ptime t4(d1,hours(1)+minutes(2)+seconds(3)+millisec(4));
  79. std::string r3 = to_simple_string(t4);
  80. check("simple subsecond: "+r3 ,
  81. std::string("2002-Jan-01 01:02:03.004000000") == r3);
  82. CHECK_ROUNDTRIP(t3);
  83. }
  84. #endif
  85. // Boost Trac 1078 (https://svn.boost.org/trac10/ticket/1078)
  86. // from_iso_string should be able to parse output of to_iso_string
  87. CHECK_ROUNDTRIP(ptime());
  88. CHECK_ROUNDTRIP(ptime(not_a_date_time)); // should be same as previous
  89. CHECK_ROUNDTRIP(ptime(pos_infin));
  90. CHECK_ROUNDTRIP(ptime(neg_infin));
  91. // when min_date_time is formatted out, it is done so as an actual date/time
  92. // i.e. it is not "special" in that sense
  93. check_equal("from_iso_string(\"minimum-date-time\")", from_iso_string("minimum-date-time"), ptime(min_date_time));
  94. check_equal("from_iso_string(\"maximum-date-time\")", from_iso_string("maximum-date-time"), ptime(max_date_time));
  95. return printTestStats();
  96. }