From 123803d61927723580abccb3d370bdbff76f2bc6 Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Thu, 12 Jun 2025 21:15:10 +0200 Subject: [PATCH] Properly use date and timezones --- buba/animations/dbf.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/buba/animations/dbf.py b/buba/animations/dbf.py index 8db52d2..2ad4a0a 100644 --- a/buba/animations/dbf.py +++ b/buba/animations/dbf.py @@ -47,32 +47,20 @@ class DBFAnimation(BubaAnimation): def countdown(dt: datetime): now = datetime.datetime.now().astimezone() try: - dep_time = datetime.datetime.strptime(dt, "%H:%M").replace(tzinfo=now.tzinfo).time() - except TypeError as e: + day = now.strftime("%y-%m-%d") + departure = datetime.datetime.strptime(f"{day} {dt}", "%y-%m-%d %H:%M").astimezone() + except ValueError as e: return "--" - # First, assume all departure times are on the current day - dep_date = now.date() - - # Calculate timedelta under the above assumption - dep_td = datetime.datetime.combine(dep_date, dep_time,tzinfo=now.tzinfo) - now - - LOG.info(f"Processing time {datetime.datetime.combine(dep_date, dep_time, tzinfo=now.tzinfo)}, delta {dep_td}") - - # If the calculated timedelta is more than one hour in the past, - # assume that the day should actually be the next day - # (This will be the case e.g. when a train departs at 00:15 and it's currently 23:50) + dep_td = departure - now if dep_td.total_seconds() <= -3600: - dep_date += datetime.timedelta(days=1) - - # If the calculated timedelta is more than 23 hours in the future, - # assume that the day should actually be the previous day. - # (This will be the case e.g. when a train should have departed at 23:50 but it's already 00:15) + # dep_date += datetime.timedelta(days=1) + departure += datetime.timedelta(days=1) if dep_td.total_seconds() >= 3600 * 23: - dep_date -= datetime.timedelta(days=1) + # dep_date -= datetime.timedelta(days=1) + departure -= datetime.timedelta(days=1) - # Recalculate the timedelta - return BubaAnimation.countdown(datetime.datetime.combine(dep_date, dep_time)) + return BubaAnimation.countdown(departure) @staticmethod def short_station(station: str) -> str: