42. Day and week of the year

The solution to this two-part problem should be straightforward from the previous two:

  • To compute the day of the year, you subtract two date::sys_days objects, one representing the given day and the other January 0 of the same year. Alternatively, you could start from January 1 and add 1 to the result.
  • To determine the week number of the year, construct a year_weeknum_weekday object, like in the previous problem, and retrieve the weeknum() value:
int day_of_year(int const y, unsigned int const m,                 unsigned int const d){   using namespace date;   if(m < 1 || m > 12 || d < 1 || d > 31) return 0;   return (sys_days{ year{ y } / month{ m } / day{ d } } -           sys_days{ year{ y } / jan / 0 }).count();}unsigned int calendar_week(int ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.