Convert moment(..., "X") to moment.unix(...) (#2950)

because I thought it was more readable and I found a little bug when
calculatin suntimes on the way....

Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
Veeck 2022-10-16 23:37:50 +02:00 committed by GitHub
parent 835c893205
commit fc59ed20e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 26 deletions

View File

@ -28,6 +28,7 @@ Special thanks to: @rejas, @sdetweil
- Updated da translation - Updated da translation
- Rework weather module - Rework weather module
- Use fetch instead of XMLHttpRequest in weatherprovider - Use fetch instead of XMLHttpRequest in weatherprovider
- Use unix() method for parsing times, fix suntimes on the way
### Fixed ### Fixed

View File

@ -81,8 +81,8 @@ WeatherProvider.register("darksky", {
currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed); currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
currentWeather.windDirection = currentWeatherData.currently.windBearing; currentWeather.windDirection = currentWeatherData.currently.windBearing;
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon); currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon);
currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X"); currentWeather.sunrise = moment.unix(currentWeatherData.daily.data[0].sunriseTime);
currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X"); currentWeather.sunset = moment.unix(currentWeatherData.daily.data[0].sunsetTime);
return currentWeather; return currentWeather;
}, },
@ -93,7 +93,7 @@ WeatherProvider.register("darksky", {
for (const forecast of forecasts) { for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); 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.minTemperature = forecast.temperatureMin;
weather.maxTemperature = forecast.temperatureMax; weather.maxTemperature = forecast.temperatureMax;
weather.weatherType = this.convertWeatherType(forecast.icon); weather.weatherType = this.convertWeatherType(forecast.icon);

View File

@ -340,7 +340,7 @@ WeatherProvider.register("envcanada", {
// Add 1 to the date to reflect the current forecast day we are building // Add 1 to the date to reflect the current forecast day we are building
lastDate = lastDate.add(1, "day"); 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 // Capture the temperatures for the current Element and the next Element in order to set
// the Min and Max temperatures for the forecast // the Min and Max temperatures for the forecast
@ -395,7 +395,7 @@ WeatherProvider.register("envcanada", {
const foreTime = moment(hourGroup[stepHour].getAttribute("dateTimeUTC"), "YYYYMMDDhhmmss"); const foreTime = moment(hourGroup[stepHour].getAttribute("dateTimeUTC"), "YYYYMMDDhhmmss");
const currTime = foreTime.add(hourOffset, "hours"); const currTime = foreTime.add(hourOffset, "hours");
weather.date = moment(currTime, "X"); weather.date = moment.unix(currTime);
// Capture the temperature // Capture the temperature

View File

@ -131,8 +131,8 @@ WeatherProvider.register("openweathermap", {
currentWeather.windSpeed = currentWeatherData.wind.speed; currentWeather.windSpeed = currentWeatherData.wind.speed;
currentWeather.windDirection = currentWeatherData.wind.deg; currentWeather.windDirection = currentWeatherData.wind.deg;
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon); currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon);
currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X"); currentWeather.sunrise = moment.unix(currentWeatherData.sys.sunrise);
currentWeather.sunset = moment(currentWeatherData.sys.sunset, "X"); currentWeather.sunset = moment.unix(currentWeatherData.sys.sunset);
return currentWeather; 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); let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
for (const forecast of forecasts) { 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 // calculate minimum/maximum temperature, specify rain amount
weather.minTemperature = Math.min.apply(null, minTemp); weather.minTemperature = Math.min.apply(null, minTemp);
weather.maxTemperature = Math.max.apply(null, maxTemp); weather.maxTemperature = Math.max.apply(null, maxTemp);
@ -195,16 +195,16 @@ WeatherProvider.register("openweathermap", {
snow = 0; snow = 0;
// set new date // set new date
date = moment(forecast.dt, "X").format("YYYY-MM-DD"); date = moment.unix(forecast.dt).format("YYYY-MM-DD");
// specify date // 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! // 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); 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); weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
} }
@ -252,7 +252,7 @@ WeatherProvider.register("openweathermap", {
for (const forecast of forecasts) { for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); 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.minTemperature = forecast.temp.min;
weather.maxTemperature = forecast.temp.max; weather.maxTemperature = forecast.temp.max;
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
@ -298,11 +298,11 @@ WeatherProvider.register("openweathermap", {
// get current weather, if requested // get current weather, if requested
const current = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); const current = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
if (data.hasOwnProperty("current")) { 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.windSpeed = data.current.wind_speed;
current.windDirection = data.current.wind_deg; current.windDirection = data.current.wind_deg;
current.sunrise = moment(data.current.sunrise, "X").utcOffset(data.timezone_offset / 60); current.sunrise = moment.unix(data.current.sunrise).utcOffset(data.timezone_offset / 60);
current.sunset = moment(data.current.sunset, "X").utcOffset(data.timezone_offset / 60); current.sunset = moment.unix(data.current.sunset).utcOffset(data.timezone_offset / 60);
current.temperature = data.current.temp; current.temperature = data.current.temp;
current.weatherType = this.convertWeatherType(data.current.weather[0].icon); current.weatherType = this.convertWeatherType(data.current.weather[0].icon);
current.humidity = data.current.humidity; current.humidity = data.current.humidity;
@ -334,8 +334,7 @@ WeatherProvider.register("openweathermap", {
const hours = []; const hours = [];
if (data.hasOwnProperty("hourly")) { if (data.hasOwnProperty("hourly")) {
for (const hour of data.hourly) { for (const hour of data.hourly) {
weather.date = moment(hour.dt, "X").utcOffset(data.timezone_offset / 60); weather.date = moment.unix(hour.dt).utcOffset(data.timezone_offset / 60);
// weather.date = moment(hour.dt, "X").utcOffset(data.timezone_offset/60).format(onecallDailyFormat+","+onecallHourlyFormat);
weather.temperature = hour.temp; weather.temperature = hour.temp;
weather.feelsLikeTemp = hour.feels_like; weather.feelsLikeTemp = hour.feels_like;
weather.humidity = hour.humidity; weather.humidity = hour.humidity;
@ -372,9 +371,9 @@ WeatherProvider.register("openweathermap", {
const days = []; const days = [];
if (data.hasOwnProperty("daily")) { if (data.hasOwnProperty("daily")) {
for (const day of data.daily) { for (const day of data.daily) {
weather.date = moment(day.dt, "X").utcOffset(data.timezone_offset / 60); weather.date = moment.unix(day.dt).utcOffset(data.timezone_offset / 60);
weather.sunrise = moment(day.sunrise, "X").utcOffset(data.timezone_offset / 60); weather.sunrise = moment.unix(day.sunrise).utcOffset(data.timezone_offset / 60);
weather.sunset = moment(day.sunset, "X").utcOffset(data.timezone_offset / 60); weather.sunset = moment.unix(day.sunset).utcOffset(data.timezone_offset / 60);
weather.minTemperature = day.temp.min; weather.minTemperature = day.temp.min;
weather.maxTemperature = day.temp.max; weather.maxTemperature = day.temp.max;
weather.humidity = day.humidity; weather.humidity = day.humidity;

View File

@ -108,7 +108,7 @@ WeatherProvider.register("weatherbit", {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); 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.humidity = parseFloat(currentWeatherData.data[0].rh);
currentWeather.temperature = parseFloat(currentWeatherData.data[0].temp); currentWeather.temperature = parseFloat(currentWeatherData.data[0].temp);
currentWeather.windSpeed = parseFloat(currentWeatherData.data[0].wind_spd); currentWeather.windSpeed = parseFloat(currentWeatherData.data[0].wind_spd);

View File

@ -51,8 +51,8 @@ WeatherProvider.register("weatherflow", {
currentWeather.windSpeed = data.current_conditions.wind_avg; currentWeather.windSpeed = data.current_conditions.wind_avg;
currentWeather.windDirection = data.current_conditions.wind_direction; currentWeather.windDirection = data.current_conditions.wind_direction;
currentWeather.weatherType = data.forecast.daily[0].icon; currentWeather.weatherType = data.forecast.daily[0].icon;
currentWeather.sunrise = moment(data.forecast.daily[0].sunrise, "X"); currentWeather.sunrise = moment.unix(data.forecast.daily[0].sunrise);
currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); currentWeather.sunset = moment.unix(data.forecast.daily[0].sunset);
this.setCurrentWeather(currentWeather); this.setCurrentWeather(currentWeather);
}) })
.catch(function (request) { .catch(function (request) {
@ -69,7 +69,7 @@ WeatherProvider.register("weatherflow", {
for (const forecast of data.forecast.daily) { for (const forecast of data.forecast.daily) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); 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.minTemperature = forecast.air_temp_low;
weather.maxTemperature = forecast.air_temp_high; weather.maxTemperature = forecast.air_temp_high;
weather.weatherType = forecast.icon; weather.weatherType = forecast.icon;

View File

@ -143,8 +143,8 @@ class WeatherObject {
updateSunTime(lat, lon) { updateSunTime(lat, lon) {
const now = !this.date ? new Date() : this.date.toDate(); const now = !this.date ? new Date() : this.date.toDate();
const times = SunCalc.getTimes(now, lat, lon); const times = SunCalc.getTimes(now, lat, lon);
this.sunrise = moment(times.sunrise, "X"); this.sunrise = moment(times.sunrise);
this.sunset = moment(times.sunset, "X"); this.sunset = moment(times.sunset);
} }
/** /**