I have a fractional epoch timestamp, represented as double
, that I would like to convert to an appropriate std::chrono::time_point
. The epoch is the usual UNIX epoch since 1/1/1970. I know that there exists std::chrono::system_clock::from_time_t
, but a time_t
does not have a fractional part. What would be the best way to do this with C++11 means?
This question is related to unix timestamp to boost::posix_time::ptime, except that it's asking for the C++11 rather than Boost version of it.
Assuming the epoch is the same as a known
clock
type you can use a duration with adouble
representation and convert to the duration used by that clock.This should be ok for any calculation involving that clock, since you're using the same precision as the clock, but it may lose some of the precision in the conversion. If you don't want to lose that you'll have to use a
time_point
specialization that uses thatdouble
-based duration type. And then use that in your calculations (of course, with all the caveats of floating-point math).However, this will complicate any calculations involving that same clock, as it will require conversions. To make this easier, you can build your own clock wrapper that does the conversions and use that: