From abb9206af2de941204ecdda5bceb6d0578db7fd3 Mon Sep 17 00:00:00 2001 From: Taylor Raack Date: Sun, 28 Oct 2018 19:41:21 -1000 Subject: [PATCH] Forecasts from OpenWeatherMap do not show "Yesterday" during any portion of a day --- AUTHORS | 1 + README.md | 3 +++ src/openweathermap_org.js | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index fd72553..76dfefc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,3 +7,4 @@ Simon Legner , Mattia Meneguzzo , Christian Metzler , Jens Lody +Taylor Raack diff --git a/README.md b/README.md index e3ee344..37e5450 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,9 @@ Copyright © 2011 - 2013 Copyright © 2013 - 2017 * Jens Lody . +Copyright © 2018 +* Taylor Raack . + This file is part of *gnome-shell-extension-openweather*. diff --git a/src/openweathermap_org.js b/src/openweathermap_org.js index d36414c..53ceede 100644 --- a/src/openweathermap_org.js +++ b/src/openweathermap_org.js @@ -17,6 +17,8 @@ * Jens Lody * Copyright (C) 2014 -2015 * Jens Lody , + * Copyright (C) 2018 + * Taylor Raack * * * This file is part of gnome-shell-extension-openweather. @@ -428,10 +430,29 @@ function parseWeatherForecast() { let forecast = this.forecastWeatherCache; let beginOfDay = new Date(new Date().setHours(0, 0, 0, 0)); + // OpenWeatherMap sometimes returns the previous day's forecast, especially in the early morning hours + // of the lat / lng being queried. To prevent the first forecast element in the UI from being the previous + // day's forecast, check for the returned forecast elements being for a previous day and maintain a + // forecast index advance counter to skip previous day forecasts. + let dateAdvanceIndex = 0; + for (let i = 0; i < this._days_forecast; i++) { + let forecastData = forecast[i]; + if (forecastData === undefined) + continue; + let forecastDate = new Date(forecastData.dt * 1000).setHours(0,0,0,0); + if (forecastDate >= beginOfDay) { + // forecast is at least at the beginning of the current day; no need to look any further + break; + } + // forecast is behind the current day, so advance the increment index + dateAdvanceIndex++; + } + // Refresh forecast for (let i = 0; i < this._days_forecast; i++) { let forecastUi = this._forecast[i]; - let forecastData = forecast[i]; + // make sure to use the dateAdvanceIndex to skip any previous day forecasts + let forecastData = forecast[i + dateAdvanceIndex]; if (forecastData === undefined) continue;