Here is an example. I suggested that fractional seconds should be ignored. I also suggested that if the initial time was already exactly one hour, then it does not need to be increased until the next hour.
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
namespace bpt = boost::posix_time;
bpt::ptime roundedToNextHour(const bpt::ptime& time)
{
bpt::time_duration tod = bpt::seconds(time.time_of_day().total_seconds());
bpt::time_duration roundedDownTod = bpt::hours(tod.hours());
bpt::ptime result(time.date(), roundedDownTod);
if (tod != roundedDownTod)
result += bpt::hours(1);
return result;
}
int main()
{
bpt::ptime aTime( bpt::time_from_string("2012-01-01 11:15:00"));
bpt::ptime boundaryTime( bpt::time_from_string("2012-01-01 23:45:00"));
bpt::ptime onTheHour( bpt::time_from_string("2012-01-01 23:00:00"));
std::cout << roundedToNextHour(aTime) << "\n";
std::cout << roundedToNextHour(boundaryTime) << "\n";
std::cout << roundedToNextHour(onTheHour) << "\n";
}
Conclusion:
2012-Jan-01 12:00:00
2012-Jan-02 00:00:00
2012-Jan-01 23:00:00
I hope this example helped you learn how Boost Posix Time works.
source
share