feat: add week boundary computation with DST handling
Made-with: Cursor
This commit is contained in:
38
src/ntr_fetcher/week.py
Normal file
38
src/ntr_fetcher/week.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
EASTERN = ZoneInfo("America/New_York")
|
||||
SHOW_DAY_DEFAULT = 2 # Wednesday (Monday=0)
|
||||
SHOW_HOUR_DEFAULT = 22
|
||||
|
||||
|
||||
def get_show_week(
|
||||
now_utc: datetime,
|
||||
show_day: int = SHOW_DAY_DEFAULT,
|
||||
show_hour: int = SHOW_HOUR_DEFAULT,
|
||||
) -> tuple[datetime, datetime]:
|
||||
"""Return (week_start_utc, week_end_utc) for the show week containing now_utc.
|
||||
|
||||
The week starts at show_day at show_hour Eastern Time and runs for 7 days.
|
||||
"""
|
||||
now_et = now_utc.astimezone(EASTERN)
|
||||
|
||||
days_since_show_day = (now_et.weekday() - show_day) % 7
|
||||
candidate_date = now_et.date() - timedelta(days=days_since_show_day)
|
||||
candidate = datetime(
|
||||
candidate_date.year,
|
||||
candidate_date.month,
|
||||
candidate_date.day,
|
||||
show_hour,
|
||||
0,
|
||||
0,
|
||||
tzinfo=EASTERN,
|
||||
)
|
||||
|
||||
if candidate > now_et:
|
||||
candidate -= timedelta(days=7)
|
||||
|
||||
week_start_utc = candidate.astimezone(timezone.utc).replace(tzinfo=timezone.utc)
|
||||
week_end_utc = (candidate + timedelta(days=7)).astimezone(timezone.utc).replace(tzinfo=timezone.utc)
|
||||
|
||||
return week_start_utc, week_end_utc
|
||||
Reference in New Issue
Block a user