Bläddra i källkod

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.
Oliver Mitchell 9 månader sedan
förälder
incheckning
1e2e66ecf7
1 ändrade filer med 19 tillägg och 4 borttagningar
  1. 19 4
      internal/assets/static/js/main.js

+ 19 - 4
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);
             });
         }
     }