You won't be able to use high_resolution_clock
for this because the epoch for the high_resolution_clock
varies from platform to platform. For example on my platform (OS X), the epoch of high_resolution_clock
is whenever I booted up my computer. This clock just counts nanoseconds since then.
system_clock
also has an unspecified epoch. However, all 3 implementations I'm aware of use Unix Time as their definition. So if you are willing to assume this observation as fact, you can easily transform time_point
s between the Julian epoch and the Unix Time epoch by subtracting off the difference between these two epochs.
I.e. to convert a Julian time_point
to a Unix Time time_point
subtract the time duration from noon in Greenwich on November 24, 4714 B.C. to 1970-01-01 00:00:00 UTC.
To help do this, here are some handy and efficient date algorithms:
http://howardhinnant.github.io/date_algorithms.html
For example to get the number of days between these two epochs as a double
:
double const JulianEpoch = days_from_civil(-4714+1, 11, 24) - 0.5;
Note the +1 to convert B.C. years to proleptic Gregorian years (1 B.C. is the year 0, 2 B.C. is the year -1, etc.). I've also subtracted half a day to account for one epoch being at noon, and the other being at midnight.
If estimatedGrowFinishJulian
is a time_point
representing the number of days since the Julian epoch:
double estimatedGrowFinishJulian = getPlantTimeEstimatedGrowingStopped(); // `14903312`
Then you can get the estimate in days with respect to the Unix Time epoch:
double estimatedGrowFinishCivil = estimatedGrowFinishJulian + JulianEpoch;
Note that this is a time_point
. And it is a time_point
with epoch consistent with the de-facto epoch of std::system_clock
. You can create such a time_point
. To do so, first it is handy to create a days
duration based on double
:
using days = std::chrono::duration
<
double,
std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>
>;
Now you can say:
days estimatedGrowFinish(estimatedGrowFinishJulian + JulianEpoch);
Recall that this is still a duration
, but it represents the number of days since the system_clock
epoch. Thus it is really a time_point
. You can create the type of such a time_point
with:
using DTP = std::chrono::time_point
<
std::chrono::system_clock,
days
>;
This is a time_point
with the same epoch as system_clock
, and it ticks once per day, and stores the tick count as a double. Now you can:
DTP end(days(estimatedGrowFinishJulian + JulianEpoch));
This time_point
can be compared with std::chrono::system_clock::now()
and subtracted from std::chrono::system_clock::now()
. If you subtract them, you will get a duration
with the same precision as std::chrono::system_clock::period
, but stored as a double. On my system this is a duration<double, micro>
. And so:
std::cout << (end - now).count() << '\n';
outputs the number of microseconds (as a double) between 14903312 days past the Julian epoch and now (which is quite far into the future).