mirror of https://github.com/home-assistant/core
29 lines
938 B
Python
29 lines
938 B
Python
"""Utils for trafikverket_train."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, timedelta
|
|
|
|
from homeassistant.const import WEEKDAYS
|
|
|
|
|
|
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 = date.today()
|
|
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]))
|