[gnome-weather/RocketMan/gnome-weather-forecast-patch] Fixed hourly forecast to use target weather station's timezone



commit 2637c63e90e4178e14b951da2e8f14624a57b54b
Author: Jim Mason <jmason ibinx com>
Date:   Fri Nov 30 10:52:19 2018 +0000

    Fixed hourly forecast to use target weather station's timezone
    
    Weather data are always relative to the weather station, not the user.
    This fix changes the hourly forecast time to that of the weather station.
    
    Fixes GNOME/gnome-weather!23.

 src/app/city.js     |  3 ++-
 src/app/forecast.js | 35 ++++++++++++++++++++---------------
 2 files changed, 22 insertions(+), 16 deletions(-)
---
diff --git a/src/app/city.js b/src/app/city.js
index f152ea8..e0f51b6 100644
--- a/src/app/city.js
+++ b/src/app/city.js
@@ -184,8 +184,9 @@ var WeatherWidget = GObject.registerClass({
         context.add_class(this._currentStyle);
 
         let forecasts = info.get_forecast_list();
+        let tz = GLib.TimeZone.new(info.location.get_timezone().get_tzid());
         for (let t of ['today', 'tomorrow'])
-            this._forecasts[t].update(forecasts, t);
+            this._forecasts[t].update(forecasts, tz, t);
 
         if (!this._forecasts['today'].hasForecastInfo() && this._forecasts['tomorrow'].hasForecastInfo())
             this._forecastStack.set_visible_child_name('tomorrow');
diff --git a/src/app/forecast.js b/src/app/forecast.js
index e14bbe0..94e16bf 100644
--- a/src/app/forecast.js
+++ b/src/app/forecast.js
@@ -49,23 +49,26 @@ var ForecastBox = GObject.registerClass(class ForecastBox extends Gtk.Frame {
 
     // Ensure that infos are sufficiently spaced, and
     // remove infos for the wrong day
-    _preprocess(now, infos) {
+    _preprocess(now, tz, infos) {
         let ret = [];
         let i;
         let current;
 
-        // First ignore all infos that are on a different
-        // day than now.
+        // First ignore all infos that are on a different day from now.
         // infos are ordered by time, and it's assumed at some point
         // there is an info for the current day (otherwise, nothing
-        // is shown)
+        // is shown).
+        //
+        // We must compare using the target timezone to ensure the 24-hour
+        // day period falls on the correct boundary.
         for (i = 0; i < infos.length; i++) {
             let info = infos[i];
 
             let [ok, date] = info.get_value_update();
-            let datetime = GLib.DateTime.new_from_unix_local(date);
+            let datetime = GLib.DateTime.new_from_unix_utc(date).to_timezone(tz);
 
-            if (Util.arrayEqual(now.get_ymd(), datetime.get_ymd())) {
+            if (Util.arrayEqual(now.get_ymd(), datetime.get_ymd()) &&
+                    now.get_hour() <= datetime.get_hour()) {
                 ret.push(info);
                 current = datetime;
                 break;
@@ -76,7 +79,7 @@ var ForecastBox = GObject.registerClass(class ForecastBox extends Gtk.Frame {
             let info = infos[i];
 
             let [ok, date] = info.get_value_update();
-            let datetime = GLib.DateTime.new_from_unix_local(date);
+            let datetime = GLib.DateTime.new_from_unix_utc(date).to_timezone(tz);
             if (datetime.difference(current) < ONE_HOUR)
                 continue;
 
@@ -91,21 +94,23 @@ var ForecastBox = GObject.registerClass(class ForecastBox extends Gtk.Frame {
         return ret;
     }
 
-    update(infos, day) {
-        let now = GLib.DateTime.new_now_local();
+    update(infos, tz, day) {
+        let now = GLib.DateTime.new_now(tz);
         if (day == 'tomorrow')
-            now = now.add_days(1);
-        let dayInfo = this._preprocess(now, infos);
+            now = now.add_hours(24 - now.get_hour());
+        // 'now' contains a TimeZone, but there is no accessor;
+        // thus, we must pass both the target time and zone
+        let dayInfo = this._preprocess(now, tz, infos);
 
         if (dayInfo.length == 0) {
             now = now.add_hours(-2);
-            dayInfo = this._preprocess(now, infos);
+            dayInfo = this._preprocess(now, tz, infos);
         }
 
         if (dayInfo.length > 0) {
             for (let i = 0; i < dayInfo.length; i++) {
                 let info = dayInfo[i];
-                this._addOneInfo(info, i);
+                this._addOneInfo(info, tz, i);
             }
         } else {
             let label = new Gtk.Label({ label: _("Forecast not available"),
@@ -115,9 +120,9 @@ var ForecastBox = GObject.registerClass(class ForecastBox extends Gtk.Frame {
         }
     }
 
-    _addOneInfo(info, col) {
+    _addOneInfo(info, tz, col) {
         let [ok, date] = info.get_value_update();
-        let datetime = GLib.DateTime.new_from_unix_local(date);
+        let datetime = GLib.DateTime.new_from_unix_utc(date).to_timezone(tz);
 
         let timeSetting = this._settings.get_string('clock-format');
         let timeFormat = null;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]