From 1fddf70e214a62c3eccece76a02fe5b3d3a0c1d9 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 31 Mar 2022 12:27:58 -0700 Subject: [PATCH] [C++options] Fix previous month and previous quarter at the end of March . Because March has more days than February the previous month offset was getting normalized back to the current month--th 29 February this year is really 1 March, so normalizing before setting the day caused begin/end previous month to return the begin/end of the current month. That probably happened on the 31st of May, July, October, and December as well, I just hadn't managed to test on those days. Switching the normalization to after calculating the day of the month broke the previous quarter calculation because now the month was out of range, so normalize month & year first. --- libgnucash/app-utils/gnc-option-date.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libgnucash/app-utils/gnc-option-date.cpp b/libgnucash/app-utils/gnc-option-date.cpp index 3d2a3e141f..2d43f732c9 100644 --- a/libgnucash/app-utils/gnc-option-date.cpp +++ b/libgnucash/app-utils/gnc-option-date.cpp @@ -481,7 +481,11 @@ reldate_set_day_and_time(struct tm& now, RelativeDateType type) } else if (type == RelativeDateType::END) { - now.tm_mday = gnc_date_get_last_mday(now.tm_mon, now.tm_year + 1900); + /* Ensure that the month is between 0 and 12*/ + auto year_delta = now.tm_mon / 12 + now.tm_mon < 0 ? -1 : 0; + auto month = now.tm_mon - 12 * year_delta; + auto year = now.tm_year + year_delta + 1900; + now.tm_mday = gnc_date_get_last_mday(month, year); gnc_tm_set_day_end(&now); } // Do nothing for LAST and NEXT. @@ -559,8 +563,8 @@ gnc_relative_date_to_time64(RelativeDatePeriod period) else if (reldate_is_next(period)) now.tm_mday += 7; } - normalize_reldate_tm(now); reldate_set_day_and_time(now, checked_reldate(period).m_type); + normalize_reldate_tm(now); return static_cast(GncDateTime(now)); }