From fb9b665b79fccd8ef0a13a514dce11f0369e89df Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 1 Feb 2026 22:26:40 +0200 Subject: Update --- converter.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'converter.go') diff --git a/converter.go b/converter.go index fa49a61..e843f35 100644 --- a/converter.go +++ b/converter.go @@ -1,4 +1,4 @@ -// converter.go - Update to use exported FmiToOwm map +// converter.go package main import ( @@ -15,8 +15,8 @@ func (fd *ForecastData) ToOWMResponse(mapper *WeatherMapper) (*OWMResponse, erro } // Calculate timezone offset - firstTime := time.Unix(fd.Timestamps[0], 0).UTC() - _, offset := firstTime.In(loc).Zone() + now := time.Now().In(loc) + _, offset := now.Zone() // Create response response := &OWMResponse{ @@ -26,13 +26,13 @@ func (fd *ForecastData) ToOWMResponse(mapper *WeatherMapper) (*OWMResponse, erro TimezoneOffset: offset, } - // Calculate sunrise/sunset for first day + // Calculate sunrise/sunset for TODAY (not the forecast date) sunCalc := NewSunCalculator(fd.Latitude, fd.Longitude, float64(offset)/3600) - sunrise, sunset, err := sunCalc.Calculate(firstTime) + sunrise, sunset, err := sunCalc.Calculate(now) if err != nil { - // Use defaults if calculation fails - sunrise = firstTime.Add(8 * time.Hour) - sunset = firstTime.Add(16 * time.Hour) + // Fallback to approximate values based on location and time of year + sunrise = time.Date(now.Year(), now.Month(), now.Day(), 8, 0, 0, 0, loc) + sunset = time.Date(now.Year(), now.Month(), now.Day(), 16, 0, 0, 0, loc) } // Convert each forecast point @@ -60,7 +60,8 @@ func (fd *ForecastData) convertToCurrent(index int, timestamp int64, loc *time.L } // Add ISO 8601 local time - current.LocalTime = time.Unix(timestamp, 0).In(loc).Format(time.RFC3339) + forecastTime := time.Unix(timestamp, 0).In(loc) + current.LocalTime = forecastTime.Format(time.RFC3339) // Extract temperature if val, err := fd.getFloatValue(index, "Temperature"); err == nil && !math.IsNaN(val) { @@ -82,9 +83,9 @@ func (fd *ForecastData) convertToCurrent(index int, timestamp int64, loc *time.L current.DewPoint = val } - // Extract wind speed + // Extract wind speed (convert m/s to km/h) if val, err := fd.getFloatValue(index, "WindSpeedMS"); err == nil && !math.IsNaN(val) { - current.WindSpeed = val + current.WindSpeed = val * 3.6 } // Extract wind direction @@ -92,9 +93,9 @@ func (fd *ForecastData) convertToCurrent(index int, timestamp int64, loc *time.L current.WindDeg = int(math.Round(val)) } - // Extract wind gust + // Extract wind gust (convert m/s to km/h) if val, err := fd.getFloatValue(index, "WindGust"); err == nil && !math.IsNaN(val) { - current.WindGust = val + current.WindGust = val * 3.6 } // Extract cloud cover @@ -110,7 +111,7 @@ func (fd *ForecastData) convertToCurrent(index int, timestamp int64, loc *time.L // Calculate feels-like temperature current.FeelsLike = CalculateFeelsLike(current.Temp, float64(current.Humidity), current.WindSpeed) - // Handle precipitation + // Handle precipitation (convert from mm/h) if val, err := fd.getFloatValue(index, "PrecipitationRate"); err == nil && !math.IsNaN(val) && val > 0 { current.Rain = &Rain{OneH: val} } @@ -118,7 +119,6 @@ func (fd *ForecastData) convertToCurrent(index int, timestamp int64, loc *time.L // Map weather symbol if val, err := fd.getFloatValue(index, "WeatherSymbol3"); err == nil && !math.IsNaN(val) { symbol := int(math.Round(val)) - forecastTime := time.Unix(timestamp, 0) // Use the mapper or fallback to the global map var weather Weather -- cgit v1.3-1-g0d28