test_7868.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2013 Vicente J. Botet Escriba
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // See http://www.boost.org/LICENSE_1_0.txt
  4. // See http://www.boost.org/libs/chrono for documentation.
  5. #define BOOST_CHRONO_VERSION 2
  6. //#define BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT 1
  7. #include <sstream>
  8. #include <iostream>
  9. #include <boost/chrono/chrono_io.hpp>
  10. #include <boost/chrono/floor.hpp>
  11. #include <boost/chrono/round.hpp>
  12. #include <boost/chrono/ceil.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <cstdio>
  15. int main()
  16. {
  17. using namespace boost;
  18. using namespace boost::chrono;
  19. boost::chrono::system_clock::time_point atnow= boost::chrono::system_clock::now();
  20. {
  21. std::stringstream strm;
  22. std::stringstream strm2;
  23. // does not change anything: strm<<time_fmt(boost::chrono::timezone::utc);
  24. // does not change anything: strm2<<time_fmt(boost::chrono::timezone::utc);
  25. boost::chrono::system_clock::time_point atnow2;
  26. strm<<atnow<<std::endl;
  27. time_t t = boost::chrono::system_clock::to_time_t(atnow);
  28. std::cout << "A:" << std::endl;
  29. std::puts(ctime(&t));
  30. std::cout << "A:" << std::endl;
  31. std::cout << "A:" << strm.str()<< std::endl;
  32. std::cout << "A:" << atnow.time_since_epoch().count() << std::endl;
  33. strm>>atnow2;
  34. strm2<<atnow2<<std::endl;
  35. std::cout << "B:" << strm2.str()<< std::endl;
  36. std::cout << "B:" << atnow2.time_since_epoch().count()<< std::endl;
  37. BOOST_TEST_EQ(atnow.time_since_epoch().count(), atnow2.time_since_epoch().count());
  38. // 1 sec wrong:
  39. std::cout << "diff:" << boost::chrono::duration_cast<microseconds>(atnow2 - atnow).count() <<std::endl;
  40. BOOST_TEST_EQ(boost::chrono::duration_cast<microseconds>(atnow2 - atnow).count(), 0);
  41. std::stringstream formatted;
  42. formatted << time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S");
  43. formatted << "actual:"<< atnow <<std::endl;
  44. formatted << "parsed:"<< atnow2 <<std::endl;
  45. std::cout << formatted.str();
  46. std::stringstream formatted1;
  47. formatted1 << time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S");
  48. formatted1 << atnow ;
  49. std::stringstream formatted2;
  50. formatted2 << time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S");
  51. formatted2 << atnow2 ;
  52. BOOST_TEST_EQ(formatted1.str(), formatted2.str());
  53. }
  54. {
  55. std::cout << "FORMATTED" << std::endl;
  56. std::stringstream strm;
  57. std::stringstream strm2;
  58. boost::chrono::system_clock::time_point atnow2;
  59. // the final second mark is always parsed as 01
  60. strm<<time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S");
  61. strm2<<time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S");
  62. strm<<atnow<<std::endl;
  63. std::cout << "actual:" << strm.str()<< std::endl;
  64. strm>>atnow2;
  65. strm2<<atnow2<<std::endl;
  66. // the final second mark is always parsed as 01
  67. std::cout << "parsed:" << strm2.str()<< std::endl;
  68. //BOOST_TEST_EQ(atnow, atnow2); // fails because the pattern doesn't contains nanoseconds
  69. //BOOST_TEST_EQ(atnow.time_since_epoch().count(), atnow2.time_since_epoch().count()); // fails because the pattern doesn't contains nanoseconds
  70. BOOST_TEST_EQ(boost::chrono::duration_cast<seconds>(atnow2 - atnow).count(), 0);
  71. }
  72. return boost::report_errors();
  73. }