Render UK time difference server-side and simplify trip timezone JS
This commit is contained in:
parent
701023db59
commit
f2752383f2
2 changed files with 42 additions and 32 deletions
|
|
@ -114,9 +114,9 @@
|
|||
<tbody>
|
||||
{% for item in destination_times %}
|
||||
<tr>
|
||||
<td>{{ item.location }} ({{ item.country_name }}) {{ item.country_flag if trip.show_flags }}</td>
|
||||
<td>{{ item.destination_label }}</td>
|
||||
<td>{{ item.timezone or "Unknown" }}</td>
|
||||
<td class="destination-offset" {% if item.timezone %}data-timezone="{{ item.timezone }}"{% endif %}>{{ item.offset_display }}</td>
|
||||
<td class="destination-offset">{{ item.offset_display }}</td>
|
||||
<td class="destination-time" {% if item.timezone %}data-timezone="{{ item.timezone }}"{% endif %}>{{ item.current_time or "Unknown" }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
@ -490,24 +490,6 @@ var routes = {{ routes | tojson }};
|
|||
|
||||
build_map("map", coordinates, routes);
|
||||
|
||||
function timezoneOffsetLabel(offsetMinutes) {
|
||||
if (offsetMinutes === 0) {
|
||||
return "No difference";
|
||||
}
|
||||
const sign = offsetMinutes > 0 ? "+" : "-";
|
||||
const abs = Math.abs(offsetMinutes);
|
||||
const hours = String(Math.floor(abs / 60)).padStart(2, "0");
|
||||
const minutes = String(abs % 60).padStart(2, "0");
|
||||
return `${sign}${hours}:${minutes} vs Bristol`;
|
||||
}
|
||||
|
||||
function getOffsetMinutes(timeZone) {
|
||||
const now = new Date();
|
||||
const localInZone = new Date(now.toLocaleString("en-US", { timeZone }));
|
||||
const localInBristol = new Date(now.toLocaleString("en-US", { timeZone: "Europe/London" }));
|
||||
return Math.round((localInZone - localInBristol) / 60000);
|
||||
}
|
||||
|
||||
function updateDestinationTimes() {
|
||||
for (const el of document.querySelectorAll(".destination-time[data-timezone]")) {
|
||||
const tz = el.dataset.timezone;
|
||||
|
|
@ -520,12 +502,6 @@ function updateDestinationTimes() {
|
|||
hour12: false
|
||||
}).format(new Date());
|
||||
}
|
||||
|
||||
for (const el of document.querySelectorAll(".destination-offset[data-timezone]")) {
|
||||
const tz = el.dataset.timezone;
|
||||
const offset = getOffsetMinutes(tz);
|
||||
el.textContent = timezoneOffsetLabel(offset);
|
||||
}
|
||||
}
|
||||
|
||||
updateDestinationTimes();
|
||||
|
|
|
|||
46
web_view.py
46
web_view.py
|
|
@ -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>")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue