diff --git a/CHANGELOG.md b/CHANGELOG.md index c575152a..aa6ef31a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ _This release is scheduled to be released on 2021-04-01._ - Added default log levels to stop calendar log spamming. - Fix socket.io cors errors, see [breaking change since socket.io v3](https://socket.io/docs/v3/handling-cors/) - Fix Issue with weather forecast icons due to fixed day start and end time (#2221) +- Fix Issue with weather forecast icons unit tests with different timezones (#2221) ## [2.14.0] - 2021-01-01 diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index 7ec20929..619ea9ce 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -355,8 +355,8 @@ Module.register("weatherforecast", { var dayEnds = 17; if (data.city && data.city.sunrise && data.city.sunset) { - dayStarts = moment.unix(data.city.sunrise).toDate().getHours(); - dayEnds = moment.unix(data.city.sunset).toDate().getHours(); + dayStarts = new Date(moment.unix(data.city.sunrise).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); + dayEnds = new Date(moment.unix(data.city.sunset).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); } // Handle different structs between forecast16 and onecall endpoints @@ -378,10 +378,10 @@ Module.register("weatherforecast", { var hour; if (forecast.dt_txt) { day = moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss").format("ddd"); - hour = moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss").toDate().getHours(); + hour = new Date(moment(forecast.dt_txt).locale("en").format("YYYY-MM-DD HH:mm:ss")).getHours(); } else { day = moment(forecast.dt, "X").format("ddd"); - hour = moment(forecast.dt, "X").toDate().getHours(); + hour = new Date(moment(forecast.dt, "X")).getHours(); } if (day !== lastDay) { diff --git a/tests/unit/functions/weatherforecast_spec.js b/tests/unit/functions/weatherforecast_spec.js index f34c69e5..96ddc77f 100644 --- a/tests/unit/functions/weatherforecast_spec.js +++ b/tests/unit/functions/weatherforecast_spec.js @@ -1,5 +1,6 @@ /* eslint no-multi-spaces: 0 */ const expect = require("chai").expect; +const moment = require("moment-timezone"); var data = require("../functions/weatherforecast_data.json"); describe("Functions module weatherforecast", function () { @@ -69,6 +70,16 @@ describe("Functions module weatherforecast", function () { Log = { error: function () {} }; + + var originalLocale; + var originalTimeZone; + before(function () { + originalLocale = moment.locale(); + originalTimeZone = moment.tz.guess(); + moment.locale("hi"); + moment.tz.setDefault("Europe/Warsaw"); + }); + describe("forecastIcons sunset specified", function () { before(function () { Module.definitions.weatherforecast.Log = {}; @@ -97,5 +108,10 @@ describe("Functions module weatherforecast", function () { expect(forecastData[2].icon).to.equal("wi-rain"); }); }); + + after(function () { + moment.locale(originalLocale); + moment.tz.setDefault(originalTimeZone); + }); }); });