From c513a206914bb9878c8a84bbe83e96e168202b73 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Thu, 30 Nov 2017 10:28:14 -0500 Subject: [PATCH] Fix off-by-one bug in iso_week::year_lastweek_weekday * The conversion from year_lastweek_weekday to sys_days and local_days had an off by one error. * Added test for this case. --- include/date/iso_week.h | 4 ++-- test/iso_week/year_lastweek_weekday.pass.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/date/iso_week.h b/include/date/iso_week.h index bbb2c8d..5bbeb0c 100644 --- a/include/date/iso_week.h +++ b/include/date/iso_week.h @@ -1314,7 +1314,7 @@ inline year_lastweek_weekday::operator sys_days() const NOEXCEPT { return sys_days(date::year{static_cast(y_)}/date::dec/date::thu[date::last]) - + (mon - thu) - (mon - wd_); + + (sun - thu) - (sun - wd_); } CONSTCD14 @@ -1322,7 +1322,7 @@ inline year_lastweek_weekday::operator local_days() const NOEXCEPT { return local_days(date::year{static_cast(y_)}/date::dec/date::thu[date::last]) - + (mon - thu) - (mon - wd_); + + (sun - thu) - (sun - wd_); } CONSTCD11 diff --git a/test/iso_week/year_lastweek_weekday.pass.cpp b/test/iso_week/year_lastweek_weekday.pass.cpp index 8827523..5f65f6f 100644 --- a/test/iso_week/year_lastweek_weekday.pass.cpp +++ b/test/iso_week/year_lastweek_weekday.pass.cpp @@ -115,4 +115,15 @@ main() std::ostringstream os; os << x0; assert(os.str() == "2015-W last-Tue"); + + for (auto y = 1950_y; y <= 2050_y; ++y) + { + auto wd = mon; + do + { + auto x = y/last/wd; + assert(date::year_month_day{x} == date::year_month_day{year_weeknum_weekday{x}}); + ++wd; + } while (wd != mon); + } }