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 const y, unsigned int const m,
unsigned int const d)
{
using namespace date;
if(m < 1 || m > 12 || d < 1 || d > 31) return 0;
auto const dt = date::year_month_day{year{ y }, month{ m }, day{ d }};
auto const tiso = iso_week::year_weeknum_weekday{ dt };
return (unsigned int)tiso.weeknum();
}
These functions can be used as follows:
int main()
{
int y = 0;
unsigned int m = 0, d = 0;
std::cout << "Year:"; std::cin >> y;
std::cout << "Month:"; std::cin >> m;
std::cout << "Day:"; std::cin >> d;
std::cout << "Calendar week:" << calendar_week(y, m, d) << std::endl;
std::cout << "Day of year:" << day_of_year(y, m, d) << std::endl;
}