From 1e2e66ecf776c0ec9a1b215d2bdb90f1b3d87e65 Mon Sep 17 00:00:00 2001 From: Oliver Mitchell Date: Tue, 29 Oct 2024 00:00:37 +1030 Subject: [PATCH] fix: non-whole-hour timezones now correctly shown on Clock Timezones that used non-whole-hour definitions (ie. Australia/ACDT) not correctly shown on the Clock widget. This fix changes how the difference is calculated to account for both minutes, and hours. Due to the change, the text shown for relative timezone differences also had to be changed. --- internal/assets/static/js/main.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/assets/static/js/main.js b/internal/assets/static/js/main.js index ffa7eb7..13c9e40 100644 --- a/internal/assets/static/js/main.js +++ b/internal/assets/static/js/main.js @@ -502,9 +502,24 @@ function timeInZone(now, zone) { timeInZone = now } - const diffInHours = Math.round((timeInZone.getTime() - now.getTime()) / 1000 / 60 / 60); + const diffInMinutes = Math.round((timeInZone.getTime() - now.getTime()) / 1000 / 60); - return { time: timeInZone, diffInHours: diffInHours }; + return { time: timeInZone, diffInMinutes: diffInMinutes }; +} + +function zoneDiffText(diffInMinutes) { + if (diffInMinutes == 0) { + return ""; + } + + const sign = diffInMinutes < 0 ? "-" : "+"; + + diffInMinutes = Math.abs(diffInMinutes); + + const hours = `${Math.floor(diffInMinutes / 60)}`.padStart(2, '0'); + const minutes = `${diffInMinutes % 60}`.padStart(2, '0'); + + return `${sign}${hours}:${minutes}`; } function setupClocks() { @@ -547,9 +562,9 @@ function setupClocks() { ); updateCallbacks.push((now) => { - const { time, diffInHours } = timeInZone(now, timeZoneContainer.dataset.timeInZone); + const { time, diffInMinutes } = timeInZone(now, timeZoneContainer.dataset.timeInZone); setZoneTime(time); - diffElement.textContent = (diffInHours <= 0 ? diffInHours : '+' + diffInHours) + 'h'; + diffElement.textContent = zoneDiffText(diffInMinutes); }); } }