From 7183e7c43ab47d0d334d6433325b38eff9c1fa9f Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 10 Nov 2019 15:34:21 -0800 Subject: [PATCH] Fix begin/end of quarter calculations. Set the period to the calendar year if it's not set in prefs. Handle correctly the FY start month being after the current month. --- libgnucash/app-utils/gnc-option.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libgnucash/app-utils/gnc-option.cpp b/libgnucash/app-utils/gnc-option.cpp index 278ca112c7..87c4eb6838 100644 --- a/libgnucash/app-utils/gnc-option.cpp +++ b/libgnucash/app-utils/gnc-option.cpp @@ -81,6 +81,13 @@ GncOptionDateValue::get_value() const struct tm now{static_cast(GncDateTime())}; struct tm period{static_cast(GncDateTime(gnc_accounting_period_fiscal_start()))}; + if (period.tm_mon == now.tm_mon && period.tm_mday == now.tm_mday) + { + //No set accounting period, use the calendar year + period.tm_mon = 0; + period.tm_mday = 0; + } + if (m_period == RelativeDatePeriod::CAL_YEAR || m_period == RelativeDatePeriod::PREV_YEAR) { @@ -91,7 +98,9 @@ GncOptionDateValue::get_value() const else if (m_period == RelativeDatePeriod::PREV_QUARTER || m_period == RelativeDatePeriod::CURRENT_QUARTER) { - now.tm_mon = now.tm_mon - (now.tm_mon - period.tm_mon) % 3; + auto offset = (now.tm_mon > period.tm_mon ? now.tm_mon - period.tm_mon : + period.tm_mon - now.tm_mon) % 3; + now.tm_mon = now.tm_mon - offset; if (m_period == RelativeDatePeriod::PREV_QUARTER) now.tm_mon -= 3; if (m_type == DateType::ENDING)