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