boost ptime 与 time_t等的转换

时间:2021-09-27 21:27:04
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
#include <stdint.h>
int main()
{
 using namespace boost::posix_time;
 using namespace boost::gregorian;
 ptime ptNow( second_clock::local_time() );
 std::cout << to_simple_string( ptNow ) << std::endl;
 // 转换成time_t
 tm tm1 = to_tm( ptNow );
 time_t tt = mktime( &tm1 );
 
 tm* tm2 = localtime( &tt );
 ptime ptt = from_time_t( tt );
 ptime pttm = ptime_from_tm( *tm2 );
 std::cout << to_simple_string( ptt ) << "/n" << to_simple_string( pttm ) << std::endl;
 return 0;
}