New answer to old question:
chrono
Related low-level date algorithms
has formulas for converting triples {year, month, day} to sequential counting of days and vice versa. You can use it to calculate the number of days between two dates, such as:
std::cout << days_from_civil(2012, 4, 2) - days_from_civil(2003, 2, 2) << '\n';
which outputs:
3347
Paper is a practical guide, not a library. It uses C ++ 14 to demonstrate formulas. Each formula contains a detailed description and conclusion, which you only need to read if you are interested in learning how this formula works.
The formulas are very effective and valid over an extremely large range. For example, using 32-bit arithmetic +/- 5 million years (more than enough).
A serial day is the number of days from (or to negative values) New Years 1970, which makes the formulas compatible with Unix Time and all known implementations of std::chrono::system_clock
.
The days_from_civil
algorithm days_from_civil
not new, and it should be very similar to other algorithms to do the same. But, going the other way, from the number of days to {year, month, day} the three are more complicated. This is a formula documented by civil_from_days
, and I have not seen other formulations that are as compact as this one.
The document provides examples of the use of typical calculations , std::chrono
interoperability and extensive, demonstrating the accuracy of more than +/- 1 million years (using the proleptographic Gregorian calendar ).
All formulas and software are in the public domain.