From d255506a6d711110b7113479c010e13dbed1f672 Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 16 Oct 2022 21:36:34 +0200 Subject: [PATCH 1/3] Use unix timestamp method from moment.js instead of "X" parameter, see https://momentjs.com/docs/#/parsing/unix-timestamp/ --- modules/default/weather/providers/darksky.js | 6 ++-- .../default/weather/providers/envcanada.js | 4 +-- .../weather/providers/openweathermap.js | 29 +++++++++---------- .../default/weather/providers/weatherbit.js | 2 +- .../default/weather/providers/weatherflow.js | 6 ++-- modules/default/weather/weatherobject.js | 4 +-- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/modules/default/weather/providers/darksky.js b/modules/default/weather/providers/darksky.js index a248bc54ae..b5bf20e3b2 100644 --- a/modules/default/weather/providers/darksky.js +++ b/modules/default/weather/providers/darksky.js @@ -81,8 +81,8 @@ WeatherProvider.register("darksky", { currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed); currentWeather.windDirection = currentWeatherData.currently.windBearing; currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon); - currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X"); - currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X"); + currentWeather.sunrise = moment.unix(currentWeatherData.daily.data[0].sunriseTime); + currentWeather.sunset = moment.unix(currentWeatherData.daily.data[0].sunsetTime); return currentWeather; }, @@ -93,7 +93,7 @@ WeatherProvider.register("darksky", { for (const forecast of forecasts) { const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); - weather.date = moment(forecast.time, "X"); + weather.date = moment.unix(forecast.time); weather.minTemperature = forecast.temperatureMin; weather.maxTemperature = forecast.temperatureMax; weather.weatherType = this.convertWeatherType(forecast.icon); diff --git a/modules/default/weather/providers/envcanada.js b/modules/default/weather/providers/envcanada.js index a903fd04ba..0d3e34ae95 100644 --- a/modules/default/weather/providers/envcanada.js +++ b/modules/default/weather/providers/envcanada.js @@ -340,7 +340,7 @@ WeatherProvider.register("envcanada", { // Add 1 to the date to reflect the current forecast day we are building lastDate = lastDate.add(1, "day"); - weather.date = moment(lastDate, "X"); + weather.date = moment.unix(lastDate); // Capture the temperatures for the current Element and the next Element in order to set // the Min and Max temperatures for the forecast @@ -395,7 +395,7 @@ WeatherProvider.register("envcanada", { const foreTime = moment(hourGroup[stepHour].getAttribute("dateTimeUTC"), "YYYYMMDDhhmmss"); const currTime = foreTime.add(hourOffset, "hours"); - weather.date = moment(currTime, "X"); + weather.date = moment.unix(currTime); // Capture the temperature diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index 6ba73c2f4b..5f4cfa8385 100644 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -131,8 +131,8 @@ WeatherProvider.register("openweathermap", { currentWeather.windSpeed = currentWeatherData.wind.speed; currentWeather.windDirection = currentWeatherData.wind.deg; currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon); - currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X"); - currentWeather.sunset = moment(currentWeatherData.sys.sunset, "X"); + currentWeather.sunrise = moment.unix(currentWeatherData.sys.sunrise); + currentWeather.sunset = moment.unix(currentWeatherData.sys.sunset); return currentWeather; }, @@ -177,7 +177,7 @@ WeatherProvider.register("openweathermap", { let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); for (const forecast of forecasts) { - if (date !== moment(forecast.dt, "X").format("YYYY-MM-DD")) { + if (date !== moment.unix(forecast.dt).format("YYYY-MM-DD")) { // calculate minimum/maximum temperature, specify rain amount weather.minTemperature = Math.min.apply(null, minTemp); weather.maxTemperature = Math.max.apply(null, maxTemp); @@ -195,16 +195,16 @@ WeatherProvider.register("openweathermap", { snow = 0; // set new date - date = moment(forecast.dt, "X").format("YYYY-MM-DD"); + date = moment.unix(forecast.dt).format("YYYY-MM-DD"); // specify date - weather.date = moment(forecast.dt, "X"); + weather.date = moment.unix(forecast.dt); // If the first value of today is later than 17:00, we have an icon at least! weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); } - if (moment(forecast.dt, "X").format("H") >= 8 && moment(forecast.dt, "X").format("H") <= 17) { + if (moment.unix(forecast.dt).format("H") >= 8 && moment.unix(forecast.dt).format("H") <= 17) { weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); } @@ -252,7 +252,7 @@ WeatherProvider.register("openweathermap", { for (const forecast of forecasts) { const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); - weather.date = moment(forecast.dt, "X"); + weather.date = moment.unix(forecast.dt); weather.minTemperature = forecast.temp.min; weather.maxTemperature = forecast.temp.max; weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); @@ -298,11 +298,11 @@ WeatherProvider.register("openweathermap", { // get current weather, if requested const current = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); if (data.hasOwnProperty("current")) { - current.date = moment(data.current.dt, "X").utcOffset(data.timezone_offset / 60); + current.date = moment.unix(data.current.dt).utcOffset(data.timezone_offset / 60); current.windSpeed = data.current.wind_speed; current.windDirection = data.current.wind_deg; - current.sunrise = moment(data.current.sunrise, "X").utcOffset(data.timezone_offset / 60); - current.sunset = moment(data.current.sunset, "X").utcOffset(data.timezone_offset / 60); + current.sunrise = moment.unix(data.current.sunrise).utcOffset(data.timezone_offset / 60); + current.sunset = moment.unix(data.current.sunset).utcOffset(data.timezone_offset / 60); current.temperature = data.current.temp; current.weatherType = this.convertWeatherType(data.current.weather[0].icon); current.humidity = data.current.humidity; @@ -334,8 +334,7 @@ WeatherProvider.register("openweathermap", { const hours = []; if (data.hasOwnProperty("hourly")) { for (const hour of data.hourly) { - weather.date = moment(hour.dt, "X").utcOffset(data.timezone_offset / 60); - // weather.date = moment(hour.dt, "X").utcOffset(data.timezone_offset/60).format(onecallDailyFormat+","+onecallHourlyFormat); + weather.date = moment.unix(hour.dt).utcOffset(data.timezone_offset / 60); weather.temperature = hour.temp; weather.feelsLikeTemp = hour.feels_like; weather.humidity = hour.humidity; @@ -372,9 +371,9 @@ WeatherProvider.register("openweathermap", { const days = []; if (data.hasOwnProperty("daily")) { for (const day of data.daily) { - weather.date = moment(day.dt, "X").utcOffset(data.timezone_offset / 60); - weather.sunrise = moment(day.sunrise, "X").utcOffset(data.timezone_offset / 60); - weather.sunset = moment(day.sunset, "X").utcOffset(data.timezone_offset / 60); + weather.date = moment.unix(day.dt).utcOffset(data.timezone_offset / 60); + weather.sunrise = moment.unix(day.sunrise).utcOffset(data.timezone_offset / 60); + weather.sunset = moment.unix(day.sunset).utcOffset(data.timezone_offset / 60); weather.minTemperature = day.temp.min; weather.maxTemperature = day.temp.max; weather.humidity = day.humidity; diff --git a/modules/default/weather/providers/weatherbit.js b/modules/default/weather/providers/weatherbit.js index 66d496a336..7c8010a837 100644 --- a/modules/default/weather/providers/weatherbit.js +++ b/modules/default/weather/providers/weatherbit.js @@ -108,7 +108,7 @@ WeatherProvider.register("weatherbit", { const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); - currentWeather.date = moment(currentWeatherData.data[0].ts, "X"); + currentWeather.date = moment.unix(currentWeatherData.data[0].ts); currentWeather.humidity = parseFloat(currentWeatherData.data[0].rh); currentWeather.temperature = parseFloat(currentWeatherData.data[0].temp); currentWeather.windSpeed = parseFloat(currentWeatherData.data[0].wind_spd); diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js index dc3dc19c66..a847fabd06 100644 --- a/modules/default/weather/providers/weatherflow.js +++ b/modules/default/weather/providers/weatherflow.js @@ -51,8 +51,8 @@ WeatherProvider.register("weatherflow", { currentWeather.windSpeed = data.current_conditions.wind_avg; currentWeather.windDirection = data.current_conditions.wind_direction; currentWeather.weatherType = data.forecast.daily[0].icon; - currentWeather.sunrise = moment(data.forecast.daily[0].sunrise, "X"); - currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); + currentWeather.sunrise = moment.unix(data.forecast.daily[0].sunrise); + currentWeather.sunset = moment.unix(data.forecast.daily[0].sunset); this.setCurrentWeather(currentWeather); }) .catch(function (request) { @@ -69,7 +69,7 @@ WeatherProvider.register("weatherflow", { for (const forecast of data.forecast.daily) { const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); - weather.date = moment(forecast.day_start_local, "X"); + weather.date = moment.unix(forecast.day_start_local); weather.minTemperature = forecast.air_temp_low; weather.maxTemperature = forecast.air_temp_high; weather.weatherType = forecast.icon; diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 14eca49fd8..6fb6156b85 100644 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -143,8 +143,8 @@ class WeatherObject { updateSunTime(lat, lon) { const now = !this.date ? new Date() : this.date.toDate(); const times = SunCalc.getTimes(now, lat, lon); - this.sunrise = moment(times.sunrise, "X"); - this.sunset = moment(times.sunset, "X"); + this.sunrise = moment.unix(times.sunrise); + this.sunset = moment.unix(times.sunset); } /** From f98fdfaca27c413cbfe5db123281608e83875690 Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 16 Oct 2022 21:44:04 +0200 Subject: [PATCH 2/3] SunCalc provides a date object, not just a unix timestamp therefore we dont need this explicit formatting --- modules/default/weather/weatherobject.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 6fb6156b85..8df71721b4 100644 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -143,8 +143,8 @@ class WeatherObject { updateSunTime(lat, lon) { const now = !this.date ? new Date() : this.date.toDate(); const times = SunCalc.getTimes(now, lat, lon); - this.sunrise = moment.unix(times.sunrise); - this.sunset = moment.unix(times.sunset); + this.sunrise = moment(times.sunrise); + this.sunset = moment(times.sunset); } /** From ebd9a64ceef612509e53734a639aaec79dc3a6cb Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 16 Oct 2022 21:55:49 +0200 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7628820d8d..049925625b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ Special thanks to: @rejas, @sdetweil - Updated da translation - Rework weather module - Use fetch instead of XMLHttpRequest in weatherprovider + - Use unix() method for parsing times, fix suntimes on the way ### Fixed