2013-10-03 4 views
1

년에서 새 boost::posix_time::ptime을 작성하고 싶습니다. 년, 월, 일, 시간 및 분만 보존하면됩니다. 해결책이 있습니까?부스트 ptime에서 초, 밀리 초 및 마이크로 초를 삭제합니다.

는 편집 : Rudolfs Bundulis 덕분에, 나는 작동하는 솔루션을 발견

#include <iostream> 
#include <boost/date_time/posix_time/posix_time.hpp> 

using namespace boost::posix_time; 

int main() 
{ 
    const ptime time(microsec_clock::local_time()); 
    const time_duration time_of_day = time.time_of_day(); 
    const ptime time2(time - microseconds(time_of_day.total_microseconds()) + hours(time_of_day.hours()) + minutes(time_of_day.minutes())); 

    std::cout << "time 1: " << time << std::endl; 
    std::cout << "time 2: " << time2 << std::endl; 
    return 0; 
} 

답변

2

100 % 확인, 나는 또한 높일 새로 온 이후를하지만 난이 그것을 할 수 있다고 생각 :

boost::posix_time::ptime time(boost::posix_time::second_clock::local_time()); 
boost::posix_time::time_duration time_of_day = time.time_of_day(); 
time_of_day -= boost::posix_time::seconds(time_of_day.seconds()); 
boost::posix_time::ptime time2(time.date(), time_of_day); 
초 및 밀리 초에 라운드
+0

감사합니다! 솔루션은 milli/microseconds가 아니라 초를 올바르게 삭제합니다. 나는 당신의 해결책을 채택했다. #INCLUDE 사용법 #include <높일 \ DATE_TIME \ posix_time \ posix_time.hpp> 네임 스페이스 부스트를 사용하여 : posix_time을; int main() { ptime time (microsec_clock :: local_time()); \t time_duration time_of_day = time.time_of_day(); \t ptime time2 (시간 - 마이크로 초 (time_of_day.total_microseconds()) + 시간 (time_of_day.hours()) + 분 (time_of_day.minutes())); } return 0; } – Alfred

+0

젠장, 내가 옳다는 것에 너무 가까웠다 - 나는 초 부분이 우물 아래의 모든 파티션을 가지고 있다고 생각했다. 어쩌면 적어도 투표 할 자격이있다. D –

+0

물론 하하하 : p – Alfred

0

이 (우리에게 MS의 오프셋 (offset)를 제거) :

namespace b_pt = boost::posix_time; 
// existing ptime which has to be rounded 
b_pt::ptime existing_pt; 

// rounding to seconds 
b_pt::ptime rounded2sec(existing_pt.date(), b_pt::seconds(existing_pt.time_of_day().total_seconds())); 

// rounding to milliseconds 
b_pt::ptime rounded2msec(existing_pt.date(), b_pt::milliseconds(existing_pt.time_of_day().total_milliseconds));