Skip to content

Commit

Permalink
Convert moment(..., "X") to moment.unix(...) (#2950)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rejas and veeck authored Oct 16, 2022
1 parent 835c893 commit fc59ed2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions modules/default/weather/providers/darksky.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions modules/default/weather/providers/envcanada.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
29 changes: 14 additions & 15 deletions modules/default/weather/providers/openweathermap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/default/weather/providers/weatherbit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions modules/default/weather/providers/weatherflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions modules/default/weather/weatherobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(times.sunrise);
this.sunset = moment(times.sunset);
}

/**
Expand Down

0 comments on commit fc59ed2

Please sign in to comment.