project Utilities / Gitlab Recurring Issue avatar

utilities/gitlab_recurring_issue#10: day is out of range for month



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.1
Created: 08-Sep-24 16:39



Description

When running a config with an nth day config:

        schedule:
            nth:
                n: 3
                weekday: Sat

I received

Error creating ticket: day is out of range for month 


Toggle State Changes

Activity


assigned to @btasker

The issue occurs within first_dow() (that's what I get for lifting code from Stackoverflow...)

def first_dow(year, month, dow):
    ''' Derived from a Stackoverflow answer: https://stackoverflow.com/a/71688384
    Credit@ Molomy
    '''
    day = ((8 + dow) - date(year, month, 1).weekday()) % 7
    return date(year, month, day)

We're currently in September and asking for the first Saturday. This month started on Sunday.

So, if we break the day calculation logic out a bit:

dow = 5 # saturday

a = (8 + dow) # 13
b = date(2024, 9, 1).weekday() # 6

c = (a - b) # 7
day = 7 % 7 # 0

The next call tries to create a day with the date 2024-09-0 which is (obviously) invalid.

We need to adjust the logic so that it calculates correctly.

The correct value in this case would be 7 - the first saturday of Sep 2024 was the 7th.

But, we need to figure out how to get there.

I'm not sure that it's actually possible with that function - with modulo 7 we're never, ever, going to get the value 7 back.

Unfortunately, I think the entire function probably needs to be ripped and replaced

verified

mentioned in commit c26600746be14dd2e3f8d7e7e7cddbb900d684a5

Commit: c26600746be14dd2e3f8d7e7e7cddbb900d684a5 
Author: B Tasker                            
                            
Date: 2024-09-08T23:42:17.000+01:00 

Message

fix: rewrite function to calculate first occurrence of a weekday (utilities/gitlab_recurring_issue#10)

+27 -5 (32 lines changed)

OK, replacement is done.

The new function is also able to accept a weekday name rather than an number (though it still accepts those too)