+import re
from datetime import date, timedelta
+from typing import Optional
def day_of_time_table_change(year: int):
def default_query_date(today: date) -> date:
- """Calculates a work_day date in Dezember, before the schedule change on 12th of December"""
- query_date = date(today.year, 12, 11)
- if query_date < today:
- query_date = date(query_date.year+1, query_date.month, query_date.day)
- while query_date.isoweekday() in [6, 7]: # Saturday or Sunday
+ """Calculates a work_day after the date given, but before the schedule change on the second Sunday of December."""
+ change_date = day_of_time_table_change(today.year)
+ query_date = change_date - timedelta(days=2)
+ if query_date.day == 8:
query_date -= timedelta(days=1)
+ if query_date < today:
+ return default_query_date(today + timedelta(days=14))
return query_date
+
+
+def vao_ext_id_to_ifopt_stop_id(vao_ext_id: str) -> Optional[str]:
+ """Converts a VAO EXT ID like "476164600" to the IFOPT stop ID like "at:47:61646".
+ In case the vao_ext_id cannot be converted, None is returned."""
+ if match := re.match(r'(47)(\d{5})00', vao_ext_id):
+ g1, g2 = match.groups()
+ return f"at:{g1}:{g2}"
+ return None