diff --git a/Examples-and-Recipes.md b/Examples-and-Recipes.md index 706bbfa..31da670 100644 --- a/Examples-and-Recipes.md +++ b/Examples-and-Recipes.md @@ -8,6 +8,7 @@ This page contains examples and recipes contributed by community members. Feel f - [Difference in months between two dates](#deltamonths) - [Parsing ISO strings](http://stackoverflow.com/a/33438989/576911) - [2Gs Birthday](#birthday2gs) +- [Calculating Ordinal Dates](#year_day) *** @@ -279,6 +280,30 @@ Note that without handling the timezone correctly, this result would be an hour Also note that this library correctly handles the changes in timezone rules throughout the decades. Several Apr 24ths have fallen within daylight saving over the decades, all of them since 1987. + +### Calculating Ordinal Dates +(by [Roland Bock](https://github.com/rbock)) + +An [ordinal date](https://en.wikipedia.org/wiki/Ordinal_date) consists of a year and a day of year (1st of January being day 1, 31st of December being day 365 or day 366). The year can be obtained directly from year_month_day. And calculating the day is wonderfully easy. In the code below we make us of the fact that year_month_day can deal with invalid dates like the 0th of January: + + int main() + { + using namespace date; + + const auto time = std::chrono::system_clock::now(); + const auto daypoint = floor(time); + const auto ymd = year_month_day{daypoint}; + + // calculating the year and the day of the year + const auto year = ymd.year(); + const auto year_day = daypoint - day_point{year/jan/0}; + + std::cout << year << '-' << std::setfill('0') << std::setw(3) << year_day.count() << std::endl; + + // inverse calculation and check + assert(ymd == year_month_day{day_point{year/jan/0} + year_day}); + } + *** ![CC BY Logo](http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by.svg) _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._ \ No newline at end of file