Base trip timezone differences on trip dates and remove live local-time column

This commit is contained in:
Edward Betts 2026-02-26 15:31:13 +00:00
parent f2752383f2
commit f0698acd59
2 changed files with 38 additions and 29 deletions

View file

@ -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,
}
)