From c2f975897d8a754fd1580b399ca85b4d9decfc8b Mon Sep 17 00:00:00 2001 From: Kimmo Brunfeldt <1232405+kimmobrunfeldt@users.noreply.github.com> Date: Sun, 29 Oct 2023 08:44:33 +0200 Subject: [PATCH] Allow meteo hourly data matches from +-1h --- render/src/weather/weather.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/render/src/weather/weather.ts b/render/src/weather/weather.ts index 7bef83a..0491a56 100644 --- a/render/src/weather/weather.ts +++ b/render/src/weather/weather.ts @@ -167,7 +167,7 @@ export function calculateShortTermForecast( ) } const foundMeteoHourData = attrsByTime(meteoForecastData.hourly).find((d) => - dateFns.isEqual(d.time, time) + isApproximatelyMatchingDate(d.time, time) ) const nextIndex = index + 1 @@ -445,8 +445,7 @@ function findWeatherSymbolForDay( // Take DST into account when finding matching date const found = dates.find( - (d) => - dateFns.differenceInSeconds(d.time, time) <= dateFns.hoursToSeconds(1) + (d) => isApproximatelyMatchingDate(d.time, time) ) if (!found) { logger.error('Unable to find matching date', { @@ -465,3 +464,7 @@ function isBetweenInclusive(time: Date, start: Date, end: Date): boolean { (dateFns.isBefore(time, end) || dateFns.isEqual(time, end)) ) } + +function isApproximatelyMatchingDate(a: Date, b: Date): boolean { + return Math.abs(dateFns.differenceInSeconds(a, b)) <= dateFns.hoursToSeconds(1) +}