Computing day of year in C++

I have been recently asked on my post on the date library if the library has a function for computing the day of the year. It actually does not, although it is fairly simple to compute it.

UPDATE: Howard Hinnant has shown in a comment below how to write a day_of_year() function using the date libray.

Let’s look at the days of the year.

Day Day of year
January 1 1
January 2 2
January 31 31
February 1 32
February 28 59

Here is where things complicate a bit, because during leap years February has 29 days. So we actually need to have two counts of days.

Day Day of non-leap year Day of leap year
January 1 1 1
January 2 2 2
January 31 31 31
February 1 32 32
February 28 59 59
February 29 N/A 60
March 1 60 61
December 31 365 366

It is fairly simple to compute the day of the year based on the day of the month if we knew the day of the year of each first day of the month. That can be also put in a table.

Day of month Day of non-leap year Day of leap year
January 1 1 1
February 1 32 32
March 1 60 61
April 1 91 92
May 1 121 122
June 1 152 153
July 1 182 183
August 1 213 214
September 1 244 245
October 1 274 275
November 1 305 306
December 1 335 336

So we can compute the day of the year as:

We can simplify that a bit by subtracking 1 from the day of the year values in the table above, such that January 1st is the day 0, February 1st is day 31, etc.

The following code sample shows how this can be written in C++:

And how it can be used:

This day_of_year() function can be used with the date library too. I’ll just add one more utility function that takes a date::year_month_day value and returns the day of the year.

And we want to know what day of the year today is then we can do that too:

The day_of_year() function is very simple and does not do any argument checks. That makes it possible to compute dates such as 2017.08.55 or 2017.55.100. Obviously, these not only that do not make sense, but indexing the days_to_month array beyond its bounds is undefined behavior. That means that in practice you should write a function that validates the arguments and throws an exception upon error. However, in this case, the day_of_year() can not be constexpr anymore.

This would throw an exception on dates like 2017.13.1 or 2017.1.50, but would not do so for 2017.2.30 or 2017.11.31 that are also invalid dates. That can be further corrected by verifying that the day of the month does not exceed the number of days that month can have in the given year.

, , , Hits for this post: 789 .

2 comments untill now

  1. Here’s another way to write day_of_year:

    day_of_year(date::sys_days sd)
    using namespace date;
    auto y = year_month_day{sd}.year();
    return sd - sys_days{y/jan/0};

    It can be exercised like this:

    using namespace date;
    std::cout << day_of_year(2017_y/aug/3) << '\n';

    This translates to 2017-08-03 is the 215th day of the year (starting with Jan 1 as day 1).

  2. Thank you for the example. I updated my post with a reference to your comment.

Add your comment now