fix: weather forecast starts from today (Moscow timezone)
All checks were successful
Build & Deploy Dashboard / deploy (push) Successful in 2m43s
All checks were successful
Build & Deploy Dashboard / deploy (push) Successful in 2m43s
This commit is contained in:
@@ -25,7 +25,7 @@ export async function GET() {
|
||||
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}` +
|
||||
`¤t=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,weather_code` +
|
||||
`&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_probability_max` +
|
||||
`&wind_speed_unit=kmh&timezone=Europe/Moscow&forecast_days=7`,
|
||||
`&wind_speed_unit=kmh&timezone=Europe/Moscow&forecast_days=8`,
|
||||
{ signal: AbortSignal.timeout(6000) }
|
||||
);
|
||||
|
||||
@@ -33,6 +33,22 @@ export async function GET() {
|
||||
const d = await res.json();
|
||||
const c = d.current;
|
||||
|
||||
// Определяем сегодняшнюю дату в московском времени
|
||||
const todayMsk = new Date().toLocaleDateString("en-CA", { timeZone: "Europe/Moscow" });
|
||||
|
||||
// Фильтруем прогноз начиная с сегодня
|
||||
const forecastRaw = d.daily.time.map((date: string, i: number) => ({
|
||||
date,
|
||||
maxTemp: Math.round(d.daily.temperature_2m_max[i]),
|
||||
minTemp: Math.round(d.daily.temperature_2m_min[i]),
|
||||
desc: WMO[d.daily.weather_code[i]] ?? "Неизвестно",
|
||||
icon: WMO_ICON[d.daily.weather_code[i]] ?? "🌡️",
|
||||
precipProb: d.daily.precipitation_probability_max[i],
|
||||
}));
|
||||
|
||||
const todayIdx = forecastRaw.findIndex((f: {date: string}) => f.date === todayMsk);
|
||||
const forecast = todayIdx >= 0 ? forecastRaw.slice(todayIdx, todayIdx + 7) : forecastRaw.slice(0, 7);
|
||||
|
||||
return NextResponse.json({
|
||||
current: {
|
||||
temp: Math.round(c.temperature_2m),
|
||||
@@ -42,14 +58,7 @@ export async function GET() {
|
||||
desc: WMO[c.weather_code] ?? "Неизвестно",
|
||||
icon: WMO_ICON[c.weather_code] ?? "🌡️",
|
||||
},
|
||||
forecast: d.daily.time.map((date: string, i: number) => ({
|
||||
date,
|
||||
maxTemp: Math.round(d.daily.temperature_2m_max[i]),
|
||||
minTemp: Math.round(d.daily.temperature_2m_min[i]),
|
||||
desc: WMO[d.daily.weather_code[i]] ?? "Неизвестно",
|
||||
icon: WMO_ICON[d.daily.weather_code[i]] ?? "🌡️",
|
||||
precipProb: d.daily.precipitation_probability_max[i],
|
||||
})),
|
||||
forecast,
|
||||
});
|
||||
} catch (e) {
|
||||
return NextResponse.json({ error: "Failed to fetch weather" }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user