Fix upcoming forecast hour filtering
This commit is contained in:
@@ -50,9 +50,40 @@ function getWarsawForecastHour(date = new Date()) {
|
|||||||
return `${getPart("year")}-${getPart("month")}-${getPart("day")}T${getPart("hour")}:00`;
|
return `${getPart("year")}-${getPart("month")}-${getPart("day")}T${getPart("hour")}:00`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseForecastHour(time: string) {
|
||||||
|
const match = time.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/);
|
||||||
|
if (!match) return null;
|
||||||
|
const [, year, month, day, hour, minute] = match;
|
||||||
|
const timestamp = Date.UTC(Number(year), Number(month) - 1, Number(day), Number(hour), Number(minute));
|
||||||
|
return Number.isFinite(timestamp) ? timestamp : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getForecastHourOfDay(time: string) {
|
||||||
|
const match = time.match(/T(\d{2}):/);
|
||||||
|
return match ? Number(match[1]) : null;
|
||||||
|
}
|
||||||
|
|
||||||
export function getUpcomingHourlyForecast(hours: HourlyForecast[], limit = 24) {
|
export function getUpcomingHourlyForecast(hours: HourlyForecast[], limit = 24) {
|
||||||
const currentHour = getWarsawForecastHour();
|
const currentHour = getWarsawForecastHour();
|
||||||
return hours.filter((hour) => hour.time >= currentHour).slice(0, limit);
|
const currentTimestamp = parseForecastHour(currentHour);
|
||||||
|
const upcomingHours = currentTimestamp === null
|
||||||
|
? hours.filter((hour) => hour.time >= currentHour)
|
||||||
|
: hours.filter((hour) => {
|
||||||
|
const hourTimestamp = parseForecastHour(hour.time);
|
||||||
|
return hourTimestamp === null ? hour.time >= currentHour : hourTimestamp >= currentTimestamp;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (upcomingHours.length) return upcomingHours.slice(0, limit);
|
||||||
|
|
||||||
|
const currentHourOfDay = getForecastHourOfDay(currentHour);
|
||||||
|
const fallbackHours = currentHourOfDay === null
|
||||||
|
? hours
|
||||||
|
: hours.filter((hour) => {
|
||||||
|
const forecastHourOfDay = getForecastHourOfDay(hour.time);
|
||||||
|
return forecastHourOfDay === null || forecastHourOfDay >= currentHourOfDay;
|
||||||
|
});
|
||||||
|
|
||||||
|
return (fallbackHours.length ? fallbackHours : hours).slice(0, limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getHourlyForecastForDay(hours: HourlyForecast[], date: string) {
|
export function getHourlyForecastForDay(hours: HourlyForecast[], date: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user