Render UK time difference server-side and simplify trip timezone JS

This commit is contained in:
Edward Betts 2026-02-26 15:26:25 +00:00
parent 701023db59
commit f2752383f2
2 changed files with 42 additions and 32 deletions

View file

@ -781,13 +781,13 @@ def _timezone_name_from_datetime(value: typing.Any) -> str | None:
return None
def _format_offset_from_bristol(offset_minutes: int) -> str:
"""Format offset from Bristol in +/-HH:MM."""
def _format_offset_from_uk(offset_minutes: int) -> str:
"""Format offset from UK in +/-HH:MM."""
if offset_minutes == 0:
return "Same time as Bristol"
return "No difference"
sign = "+" if offset_minutes > 0 else "-"
hours, mins = divmod(abs(offset_minutes), 60)
return f"{sign}{hours:02d}:{mins:02d} vs Bristol"
return f"{sign}{hours:02d}:{mins:02d} vs UK"
def _timezone_from_coordinates(latitude: float, longitude: float) -> str | None:
@ -888,7 +888,7 @@ def get_destination_timezones(trip: Trip) -> list[StrDict]:
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_bristol(offset_minutes)
offset_display = _format_offset_from_uk(offset_minutes)
current_time = dest_now.strftime("%a %H:%M:%S")
destination_times.append(
@ -902,7 +902,41 @@ def get_destination_timezones(trip: Trip) -> list[StrDict]:
}
)
return destination_times
grouped: list[StrDict] = []
grouped_index: dict[tuple[str, str, str | None], int] = {}
for item in destination_times:
key = (item["country_name"], item["country_flag"], item["timezone"])
if key in grouped_index:
existing = grouped[grouped_index[key]]
existing_locations = typing.cast(list[str], existing["locations"])
existing_locations.append(typing.cast(str, item["location"]))
existing["location_count"] = (
typing.cast(int, existing["location_count"]) + 1
)
continue
grouped_index[key] = len(grouped)
grouped.append(
{
**item,
"locations": [item["location"]],
"location_count": 1,
}
)
for item in grouped:
location_count = typing.cast(int, item["location_count"])
country_name = typing.cast(str, item["country_name"])
country_flag = typing.cast(str, item["country_flag"])
if location_count > 1:
label = f"{country_name} ({location_count} locations)"
else:
label = f"{item['location']} ({country_name})"
if trip.show_flags:
label = f"{label} {country_flag}"
item["destination_label"] = label
return grouped
@app.route("/trip/<start>")