mirror of
https://github.com/home-assistant/core.git
synced 2026-09-06 21:41:28 +01:00
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: joostlek <7083755+joostlek@users.noreply.github.com>
28 lines
955 B
Python
28 lines
955 B
Python
"""Utils for trafikverket_train."""
|
|
|
|
from datetime import date, timedelta
|
|
|
|
from homeassistant.const import WEEKDAYS
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
|
def next_weekday(fromdate: date, weekday: int) -> date:
|
|
"""Return the date of the next time a specific weekday happen."""
|
|
days_ahead = weekday - fromdate.weekday()
|
|
if days_ahead <= 0:
|
|
days_ahead += 7
|
|
return fromdate + timedelta(days_ahead)
|
|
|
|
|
|
def next_departuredate(departure: list[str]) -> date:
|
|
"""Calculate the next departuredate from an array input of short days."""
|
|
today_date = dt_util.now().date()
|
|
today_weekday = date.weekday(today_date)
|
|
if WEEKDAYS[today_weekday] in departure:
|
|
return today_date
|
|
for day in departure:
|
|
next_departure = WEEKDAYS.index(day)
|
|
if next_departure > today_weekday:
|
|
return next_weekday(today_date, next_departure)
|
|
return next_weekday(today_date, WEEKDAYS.index(departure[0]))
|