Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utils for trafikverket_train."""
2 
3 from __future__ import annotations
4 
5 from datetime import date, timedelta
6 
7 from homeassistant.const import WEEKDAYS
8 
9 
10 def next_weekday(fromdate: date, weekday: int) -> date:
11  """Return the date of the next time a specific weekday happen."""
12  days_ahead = weekday - fromdate.weekday()
13  if days_ahead <= 0:
14  days_ahead += 7
15  return fromdate + timedelta(days_ahead)
16 
17 
18 def next_departuredate(departure: list[str]) -> date:
19  """Calculate the next departuredate from an array input of short days."""
20  today_date = date.today()
21  today_weekday = date.weekday(today_date)
22  if WEEKDAYS[today_weekday] in departure:
23  return today_date
24  for day in departure:
25  next_departure = WEEKDAYS.index(day)
26  if next_departure > today_weekday:
27  return next_weekday(today_date, next_departure)
28  return next_weekday(today_date, WEEKDAYS.index(departure[0]))
date next_weekday(date fromdate, int weekday)
Definition: util.py:10
date next_departuredate(list[str] departure)
Definition: util.py:18