seppemans / businesstimedelta

Timedelta for business time. Calculate the exact working time between two datetimes (hours, seconds). Supports custom schedules, holidays, and time zones.
MIT License
35 stars 7 forks source link

Infinite loop when subtracting business hours from timezone-aware date with US holidays and workday rules #9

Open garthompson opened 2 years ago

garthompson commented 2 years ago

A simple test-case to reproduce this issue:

from datetime import datetime, time
from pytz import timezone
from businesstimedelta import BusinessTimeDelta, HolidayRule, Rules, WorkDayRule
from holidays import UnitedStates

la_tz = timezone("America/Los_Angeles")
workday_rule = WorkDayRule(
    start_time=time(8), end_time=time(17), working_days=[0, 1, 2, 3, 4], tz=la_tz,
)
us_holidays_rule = HolidayRule(UnitedStates())
business_hours = Rules([workday_rule, us_holidays_rule])
test_datetime = datetime(2021, 11, 10, 17, 0, 0, 0, la_tz)
output = test_datetime - BusinessTimeDelta(rule=business_hours, hours=4)

Notes:

julifgo commented 10 months ago

We experienced a similar issue and turns out that it was an inconsistency of tz. Each set of rules takes a tz info as argument and if not provided will do calculations in UTC. So, in your case you set your workdays in la_tz but not your holidays. Have you tried this?

us_holidays_rule = HolidayRule(UnitedStates(), tz= la_tz)

That solved the issue for us.