I want to add or subtract weeks (or days or month or years) to localized datetime objects. The problem is, that the naive approach will result in 1 hour shifts due to daylight s
I was trying to find the start and end of a week from a given date and I ran into this issue. I was able to achieve a DST-aware timedelta using the arrow package. Here's how I did it:
from dataclasses import dataclass
from datetime import datetime
import arrow
@dataclass(frozen=True)
class Week:
start: datetime
end: datetime
def get_week_range(dt: datetime, tz: str) -> Week:
"""Returns Week instance with localized, time-aware start and end datetimes"""
dt = arrow.get(dt, tz)
start = dt.shift(days=-dt.weekday())
end = start.shift(days=7).shift(seconds=-1)
return Week(start=start, end=end)
>>> get_week_range(datetime(2020, 10, 27), "America/Chicago")
Week(start=, end=)
Notice how the returned end date in this example is correctly adjusted to -06:00
offset since daylight savings ends on 11/1 at 2am for America/Chicago
time zone.