Use local time
This commit is contained in:
@@ -2,7 +2,7 @@ import { createWriteStream, existsSync, mkdirSync } from 'fs'
|
||||
import { resolve, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { pipeline } from 'stream/promises'
|
||||
import { localDateStr } from './config.mjs'
|
||||
import { tzOffsetMinutes } from './config.mjs'
|
||||
|
||||
const DIR = dirname(fileURLToPath(import.meta.url))
|
||||
const ICON_DIR = resolve(DIR, 'public', 'icons')
|
||||
@@ -122,10 +122,10 @@ export async function dailyWeather(lat, lon, start, end) {
|
||||
const humidity = data.daily.relative_humidity_2m_mean[i]
|
||||
const uv = data.daily.uv_index_max[i]
|
||||
|
||||
// Open-Meteo returns local date strings — parse as local midnight using TZ
|
||||
// e.g. "2026-03-01" → local midnight, not UTC midnight
|
||||
const tzOffset = (new Date(new Date().toLocaleString('en-US', { timeZone: require('./config.mjs').cfg().TZ })) - new Date(new Date().toLocaleString('en-US', { timeZone: 'UTC' }))) / 60_000
|
||||
const localMidnight = new Date(new Date(`${time}T00:00:00Z`).getTime() - tzOffset * 60_000)
|
||||
// "2026-03-01" → local midnight by shifting from UTC midnight by tz offset
|
||||
const utcMidnight = new Date(`${time}T00:00:00Z`)
|
||||
const offset = tzOffsetMinutes(utcMidnight)
|
||||
const localMidnight = new Date(utcMidnight.getTime() - offset * 60_000)
|
||||
|
||||
return {
|
||||
label: WMO[code] || 'Unknown',
|
||||
@@ -175,8 +175,8 @@ export async function hourlyWeather(lat, lon, start, end) {
|
||||
const data = await cachedFetch(`hourly_${lat}_${lon}_${startStr}_${endStr}`, url)
|
||||
if (!data?.hourly?.time) return []
|
||||
|
||||
// Build sunrise/sunset map keyed by local date string from Open-Meteo
|
||||
// Open-Meteo returns these as local time ISO strings e.g. "2026-03-01T06:45"
|
||||
// Open-Meteo returns local time strings e.g. "2026-03-01T06:00" (no Z)
|
||||
// sunMap keyed by date portion of those local strings
|
||||
const sunMap = {}
|
||||
if (data.daily?.time) {
|
||||
data.daily.time.forEach((date, i) => {
|
||||
@@ -188,14 +188,14 @@ export async function hourlyWeather(lat, lon, start, end) {
|
||||
}
|
||||
|
||||
return Promise.all(data.hourly.time.map(async (time, i) => {
|
||||
// Open-Meteo returns local time strings — treat as UTC-equivalent for Date parsing
|
||||
// then key into sunMap using the local date portion
|
||||
const slotDate = new Date(time) // parsed as local ISO (no Z suffix) — browser/node both treat naive as local
|
||||
const dateKey = localDateStr(slotDate)
|
||||
const sun = sunMap[dateKey] ?? {}
|
||||
const t = slotDate.getTime()
|
||||
const daytime = sun.sunrise && sun.sunset
|
||||
? t >= sun.sunrise.getTime() && t < sun.sunset.getTime()
|
||||
const dateKey = time.slice(0, 10) // "2026-03-01" from local string
|
||||
const sun = sunMap[dateKey] ?? {}
|
||||
// Open-Meteo local strings have no Z — append Z to treat as UTC for ms comparison
|
||||
// This works because Open-Meteo already returns times in the location's local tz
|
||||
// and sunrise/sunset are parsed the same way, so the comparison is consistent
|
||||
const t = new Date(time + 'Z').getTime()
|
||||
const daytime = sun.sunrise && sun.sunset
|
||||
? t >= new Date(sun.sunrise.toISOString().replace('Z', '') + 'Z').getTime() && t < new Date(sun.sunset.toISOString().replace('Z', '') + 'Z').getTime()
|
||||
: false
|
||||
|
||||
const code = data.hourly.weathercode[i]
|
||||
@@ -206,7 +206,7 @@ export async function hourlyWeather(lat, lon, start, end) {
|
||||
const clouds = Math.round(data.hourly.cloudcover[i]) / 100
|
||||
|
||||
return {
|
||||
time: slotDate,
|
||||
time: new Date(time + 'Z'),
|
||||
daytime,
|
||||
label: WMO[code] || 'Unknown',
|
||||
icon,
|
||||
|
||||
Reference in New Issue
Block a user