diff --git a/libgnucash/engine/Recurrence.c b/libgnucash/engine/Recurrence.c index 073e2649ca..b69b2d23ce 100644 --- a/libgnucash/engine/Recurrence.c +++ b/libgnucash/engine/Recurrence.c @@ -174,6 +174,28 @@ nth_weekday_compare(const GDate *start, const GDate *next, PeriodType pt) } +static void adjust_for_weekend(PeriodType pt, WeekendAdjust wadj, GDate *date) +{ + if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) + { + if (g_date_get_weekday(date) == G_DATE_SATURDAY || g_date_get_weekday(date) == G_DATE_SUNDAY) + { + switch (wadj) + { + case WEEKEND_ADJ_BACK: + g_date_subtract_days(date, g_date_get_weekday(date) == G_DATE_SATURDAY ? 1 : 2); + break; + case WEEKEND_ADJ_FORWARD: + g_date_add_days(date, g_date_get_weekday(date) == G_DATE_SATURDAY ? 2 : 1); + break; + case WEEKEND_ADJ_NONE: + default: + break; + } + } + } +} + /* This is the only real algorithm related to recurrences. It goes: Step 1) Go forward one period from the reference date. Step 2) Back up to align to the phase of the start date. @@ -200,24 +222,7 @@ recurrenceNextInstance(const Recurrence *r, const GDate *ref, GDate *next) occurrence is always the start date, and we're done. */ // However, it's possible for the start date to fall on an exception (a weekend), in that case, it needs to be corrected. adjusted_start = *start; - if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) - { - if (g_date_get_weekday(&adjusted_start) == G_DATE_SATURDAY || g_date_get_weekday(&adjusted_start) == G_DATE_SUNDAY) - { - switch (wadj) - { - case WEEKEND_ADJ_BACK: - g_date_subtract_days(&adjusted_start, g_date_get_weekday(&adjusted_start) == G_DATE_SATURDAY ? 1 : 2); - break; - case WEEKEND_ADJ_FORWARD: - g_date_add_days(&adjusted_start, g_date_get_weekday(&adjusted_start) == G_DATE_SATURDAY ? 2 : 1); - break; - case WEEKEND_ADJ_NONE: - default: - break; - } - } - } + adjust_for_weekend(pt,wadj,&adjusted_start); if (g_date_compare(ref, &adjusted_start) < 0) { g_date_set_julian(next, g_date_get_julian(&adjusted_start)); @@ -363,25 +368,7 @@ recurrenceNextInstance(const Recurrence *r, const GDate *ref, GDate *next) g_date_set_day(next, g_date_get_day(start)); /*same day as start*/ /* Adjust for dates on the weekend. */ - if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) - { - if (g_date_get_weekday(next) == G_DATE_SATURDAY || g_date_get_weekday(next) == G_DATE_SUNDAY) - { - switch (wadj) - { - case WEEKEND_ADJ_BACK: - g_date_subtract_days(next, g_date_get_weekday(next) == G_DATE_SATURDAY ? 1 : 2); - break; - case WEEKEND_ADJ_FORWARD: - g_date_add_days(next, g_date_get_weekday(next) == G_DATE_SATURDAY ? 2 : 1); - break; - case WEEKEND_ADJ_NONE: - default: - break; - } - } - } - + adjust_for_weekend(pt,wadj,next); } break; case PERIOD_WEEK: