1
#include
<
boost
/
date_time
/
posix_time
/
posix_time.hpp
>
2 #include < boost / date_time / gregorian / gregorian.hpp >
3 #include < iostream >
4 #include < stdint.h >
5
6 int main()
7 {
8 using namespace boost::posix_time;
9 using namespace boost::gregorian;
10
11 ptime ptNow( second_clock::local_time() );
12 std::cout << to_simple_string( ptNow ) << std::endl;
13
14 tm tm1 = to_tm( ptNow );
15 time_t tt = mktime( & tm1 );
16
17 tm * tm2 = localtime( & tt );
18
19 ptime ptt = from_time_t( tt );
20 ptime pttm = ptime_from_tm( tm1 );
21 std::cout << to_simple_string( ptt ) << " \n " << to_simple_string( pttm ) << std::endl;
22
23 return 0 ;
24 }
2 #include < boost / date_time / gregorian / gregorian.hpp >
3 #include < iostream >
4 #include < stdint.h >
5
6 int main()
7 {
8 using namespace boost::posix_time;
9 using namespace boost::gregorian;
10
11 ptime ptNow( second_clock::local_time() );
12 std::cout << to_simple_string( ptNow ) << std::endl;
13
14 tm tm1 = to_tm( ptNow );
15 time_t tt = mktime( & tm1 );
16
17 tm * tm2 = localtime( & tt );
18
19 ptime ptt = from_time_t( tt );
20 ptime pttm = ptime_from_tm( tm1 );
21 std::cout << to_simple_string( ptt ) << " \n " << to_simple_string( pttm ) << std::endl;
22
23 return 0 ;
24 }
输出结果:
2009-Sep-10 15:24:10
2009-Sep-10 07:24:10
2009-Sep-10 15:24:10
第一个为ptime获得的当前时间的输出。
第三个为ptime转为tm,再tm转成ptime的输出
第二个为那个tm转成time_t,time_t再转回ptime的输出。
从结果看,第二个输出的时间很诡异上居然差了8个小时,难道是UTC时间?但boost doc那里没有这个说明,只是说用from_time_t可以将time_t转成ptime。程序中间将time_t用localtime转回tm作验证,结果是正确的。