From f0698acd596f7725fc3cff9d51be81c047a01bbb Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 26 Feb 2026 15:31:13 +0000 Subject: [PATCH] Base trip timezone differences on trip dates and remove live local-time column --- templates/trip_page.html | 19 ---------------- web_view.py | 48 +++++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/templates/trip_page.html b/templates/trip_page.html index 86f4064..b32346d 100644 --- a/templates/trip_page.html +++ b/templates/trip_page.html @@ -108,7 +108,6 @@ Destination Timezone Difference from UK - Current local time @@ -117,7 +116,6 @@ {{ item.destination_label }} {{ item.timezone or "Unknown" }} {{ item.offset_display }} - {{ item.current_time or "Unknown" }} {% endfor %} @@ -490,22 +488,5 @@ var routes = {{ routes | tojson }}; build_map("map", coordinates, routes); -function updateDestinationTimes() { - for (const el of document.querySelectorAll(".destination-time[data-timezone]")) { - const tz = el.dataset.timezone; - el.textContent = new Intl.DateTimeFormat("en-GB", { - timeZone: tz, - weekday: "short", - hour: "2-digit", - minute: "2-digit", - second: "2-digit", - hour12: false - }).format(new Date()); - } -} - -updateDestinationTimes(); -setInterval(updateDestinationTimes, 1000); - {% endblock %} diff --git a/web_view.py b/web_view.py index b6afc9a..ea2d5c8 100755 --- a/web_view.py +++ b/web_view.py @@ -790,6 +790,40 @@ def _format_offset_from_uk(offset_minutes: int) -> str: return f"{sign}{hours:02d}:{mins:02d} vs UK" +def _trip_offset_minutes( + trip_start: date, trip_end: date, destination_timezone: str +) -> list[int]: + """Unique UTC offset differences vs UK across the trip date range.""" + destination_tz = ZoneInfo(destination_timezone) + uk_timezone = ZoneInfo("Europe/London") + current = trip_start + offsets: set[int] = set() + + while current <= trip_end: + instant = datetime( + current.year, current.month, current.day, 12, tzinfo=timezone.utc + ) + destination_offset = instant.astimezone(destination_tz).utcoffset() + uk_offset = instant.astimezone(uk_timezone).utcoffset() + if destination_offset is not None and uk_offset is not None: + offsets.add(int((destination_offset - uk_offset).total_seconds() // 60)) + current += timedelta(days=1) + + return sorted(offsets) + + +def _format_trip_offset_display(offsets: list[int]) -> str: + """Format trip-range offsets; include variation if DST changes during trip.""" + if not offsets: + return "Timezone unknown" + if len(offsets) == 1: + return _format_offset_from_uk(offsets[0]) + return ( + "Varies during trip: " + f"{_format_offset_from_uk(offsets[0])} to {_format_offset_from_uk(offsets[-1])}" + ) + + def _timezone_from_coordinates(latitude: float, longitude: float) -> str | None: """Resolve IANA timezone name from coordinates.""" timezone_finder = _get_timezone_finder() @@ -853,8 +887,8 @@ def get_destination_timezones(trip: Trip) -> list[StrDict]: if candidate: per_location[key].append(candidate) - home_now = datetime.now(ZoneInfo("Europe/London")) destination_times: list[StrDict] = [] + trip_end = trip.end or trip.start for location, country in trip.locations(): country_code = country.alpha_2.lower() @@ -881,15 +915,10 @@ def get_destination_timezones(trip: Trip) -> list[StrDict]: timezone_name = country_timezones[0] offset_display = "Timezone unknown" - current_time = None if timezone_name: - dest_now = datetime.now(ZoneInfo(timezone_name)) - dest_offset = dest_now.utcoffset() - home_offset = home_now.utcoffset() - if dest_offset is not None and home_offset is not None: - offset_minutes = int((dest_offset - home_offset).total_seconds() // 60) - offset_display = _format_offset_from_uk(offset_minutes) - current_time = dest_now.strftime("%a %H:%M:%S") + offset_display = _format_trip_offset_display( + _trip_offset_minutes(trip.start, trip_end, timezone_name) + ) destination_times.append( { @@ -898,7 +927,6 @@ def get_destination_timezones(trip: Trip) -> list[StrDict]: "country_flag": country.flag, "timezone": timezone_name, "offset_display": offset_display, - "current_time": current_time, } )