I have a SQL server database and I am pulling dates from it and converting the type of timestamp_t into Int64 as such:
Int64 from_timestamp_t(dtl::timestamp_t& t)
{
// create a new posix time structure
boost::posix_time::ptime pt
(
boost::gregorian::date ( t.year, t.month, t.day),
boost::posix_time::time_duration ( t.hour, t.minute, t.second, t.fraction )
);
ptime epoch(date(1970, Jan, 1));
boost::posix_time::time_duration fromEpoch = pt - epoch;
// return it to caller
return fromEpoch.total_milliseconds();
}
I attempt to convert back to a boost ptime from an Int64 as such:
ptime from_epoch_ticks(Int64 ticksFromEpoch)
{
ptime epoch(date(1970, Jan, 1), time_duration(0,0,0));
ptime time = epoch + boost::posix_time::milliseconds(ticksFromEpoch);
return time;
}
For some reason, and I can't figure out why, my dates, hours, etc are all correct, but my minutes are ahead a few minutes from what they should be. Is it because timestamps from the database are in seconds resolution and I'm using milliseconds? How do I fix this?
Applying the following modification as Dan suggested seems to have fixed the problem:
Int64 from_timestamp_t(dtl::timestamp_t& t)
{
int count = t.fraction * (time_duration::ticks_per_second() % 1000);
boost::posix_time::ptime pt
(
boost::gregorian::date ( t.year, t.month, t.day ),
boost::posix_time::time_duration ( t.hour, t.minute, t.second, count )
);
ptime epoch(date(1970, Jan, 1), time_duration(0, 0, 0, 0));
boost::posix_time::time_duration fromEpoch = pt - epoch;
return fromEpoch.total_milliseconds();
}