Alfred Alfred - 2 months ago 30
C++ Question

Delete seconds, milliseconds and microseconds from a boost ptime

I would like to construct a new

boost::posix_time::ptime
from an existing
ptime
, by only conserving the year, month, day, hour and minute.
Is there any solution?

EDIT: Thanks to Rudolfs Bundulis, I found a solution which works:

#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;
}

Answer Source

Not 100% sure, since I'm also new to boost but I think this could do it:

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);