Skip to content

Commit

Permalink
Fix how business days are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
mikahanninen committed Dec 6, 2024
1 parent 6a25c2a commit 71ef86f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
44 changes: 33 additions & 11 deletions packages/main/src/RPA/Calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ class Calendar:

def __init__(self) -> None:
self.logger = logging.getLogger(__name__)
self.BUSINESS_DAYS = [1, 2, 3, 4, 5] # Monday - Friday
self.DEFAULT_BUSINESS_DAYS = [0, 1, 2, 3, 4] # Monday - Friday as a constant
self.BUSINESS_DAYS = self.DEFAULT_BUSINESS_DAYS.copy() # Working copy
self.custom_holidays = holidays.HolidayBase()

@keyword
def reset_business_days(self) -> None:
"""Reset business days back to default Monday-Friday schedule."""
self.BUSINESS_DAYS = self.DEFAULT_BUSINESS_DAYS.copy()

@keyword
def set_locale(self, locale_name: str) -> str:
"""Set locale globally for the library
Expand Down Expand Up @@ -609,24 +615,40 @@ def _return_business_day(
given_dt = pdl.parse(given_date, strict=False)
else:
given_dt = given_date
previous_dt = given_dt

current_dt = given_dt
years = [given_dt.year - 1, given_dt.year, given_dt.year + 1]
holiday_list = self.return_holidays(years, country)

# Skip the current date for previous/next business day
current_dt = current_dt.add(days=direction)

while True:
is_business_day = False
previous_dt = previous_dt.add(days=direction)
prev_day = pdl.date(previous_dt.year, previous_dt.month, previous_dt.day)
if previous_dt.day_of_week in self.BUSINESS_DAYS:
is_business_day = True
current_date = pdl.date(current_dt.year, current_dt.month, current_dt.day)
weekday = current_dt.day_of_week

self.logger.debug(f"Checking date: {current_date}, weekday: {weekday}")
self.logger.debug(f"Business days: {self.BUSINESS_DAYS}")

# Check if it's a business day
is_business_day = weekday in self.BUSINESS_DAYS

self.logger.debug(f"Is business day? {is_business_day}")

# Check if it's not a holiday (if country is specified)
if country and is_business_day:
is_business_day = prev_day not in holiday_list
is_holiday = current_date in holiday_list
is_business_day = not is_holiday
self.logger.debug(f"Is holiday? {is_holiday}")

if is_business_day:
break

current_dt = current_dt.add(days=direction)

if return_format:
return previous_dt.format(fmt=return_format, locale=locale)
else:
return previous_dt
return current_dt.format(fmt=return_format, locale=locale)
return current_dt

@keyword
def return_holidays(
Expand Down
4 changes: 2 additions & 2 deletions packages/main/tests/robot/test_calendar.robot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*** Settings ***
Library RPA.Calendar
Library Collections

Test Teardown Reset Business Days

*** Variables ***
${RESOURCES} ${CURDIR}${/}..${/}resources
Expand All @@ -15,7 +15,7 @@ Setting Locale
Should Be Equal As Strings de ${previous}

Setting Business Days
@{default_business_days}= Evaluate [1,2,3,4,5]
@{default_business_days}= Evaluate [0,1,2,3,4]
@{new_business_days}= Evaluate [1,2]
@{previous}= Set Business Days ${new_business_days}
Lists Should Be Equal ${previous} ${default_business_days}
Expand Down

0 comments on commit 71ef86f

Please sign in to comment.