Move the weekend adjust code in its own function

This commit is contained in:
jean 2020-03-22 12:34:39 -07:00
parent 268695f19c
commit 66fce05308

View File

@ -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: /* This is the only real algorithm related to recurrences. It goes:
Step 1) Go forward one period from the reference date. Step 1) Go forward one period from the reference date.
Step 2) Back up to align to the phase of the start 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. */ 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. // 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; adjusted_start = *start;
if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) adjust_for_weekend(pt,wadj,&adjusted_start);
{
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;
}
}
}
if (g_date_compare(ref, &adjusted_start) < 0) if (g_date_compare(ref, &adjusted_start) < 0)
{ {
g_date_set_julian(next, g_date_get_julian(&adjusted_start)); 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*/ g_date_set_day(next, g_date_get_day(start)); /*same day as start*/
/* Adjust for dates on the weekend. */ /* Adjust for dates on the weekend. */
if (pt == PERIOD_YEAR || pt == PERIOD_MONTH || pt == PERIOD_END_OF_MONTH) adjust_for_weekend(pt,wadj,next);
{
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;
}
}
}
} }
break; break;
case PERIOD_WEEK: case PERIOD_WEEK: