sunrisesunset icon indicating copy to clipboard operation
sunrisesunset copied to clipboard

sunrise for lat/lon in UTC+8 erroneosuly set to 00:00:00 UTC (should be on previous day)

Open seankhl opened this issue 4 years ago • 1 comments

Couldn't think of a title to accurately sum up the issue, but the problem here is that in UTC+8, when running the sample program with a UTC offset of 0, I get

Sunrise: 00:00:00
Sunset: 11:08:43

But this is wrong, the sunrise should be on the previous day actually in UTC, as when I set the timzeone offset to 8, I get

Sunrise: 07:00:40
Sunset: 19:08:43

and this is confirmed to be correct by other sources.

I'm using a program that uses your library, which does all its calculations in UTC, and therefore, my sunrise times are always wrong.

I took a quick look at the code but it's a bit much to process, and I decided to open an issue instead of just tackling it myself in case the solution was easy for you.

seankhl avatar Sep 01 '21 10:09 seankhl

I hit the same or a similar issue. My UTC offset is +2 and the sunrise/set times calculated by the library are one day behind what other sources say for my location (Hamburg, Germany). This is my test program:

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kelvins/sunrisesunset"
)

const (
	Location  = "Europe/Berlin"
	Latitude  = 53.536268
	Longitude = 9.992086
	// StartDate = "2018-01-01"
	StartDate = "2021-09-18"
	// EndDate   = "2099-12-31"
	EndDate = "2021-09-30"
)

func main() {
	loc, err := time.LoadLocation(Location)
	if err != nil {
		log.Fatalf("Failed to load location %q: %v", Location, err)
	}

	start, err := time.ParseInLocation("2006-01-02", StartDate, loc)
	if err != nil {
		log.Fatalf("Failed to parse start date %q: %v", StartDate, err)
	}

	end, err := time.ParseInLocation("2006-01-02", EndDate, loc)
	if err != nil {
		log.Fatalf("Failed to parse end date %q: %v", EndDate, err)
	}

	for t := start; !t.After(end); t = t.AddDate(0, 0, 1) {
		_, offset := t.Zone()
		sunrise, sunset, err := sunrisesunset.GetSunriseSunset(Latitude, Longitude, float64(offset)/3600.0, t)

		// If no error has occurred, print the results
		if err == nil {
			fmt.Printf("Sunrise @ %s: %s\n", t.Format("2006-01-02"), sunrise.Format("15:04:05")) // Sunrise: 06:11:44
			fmt.Printf("Sunset @ %s : %s\n", t.Format("2006-01-02"), sunset.Format("15:04:05"))  // Sunset: 18:14:27
		} else {
			fmt.Println(err)
		}
	}
}

which outputs

Sunrise @ 2021-09-18: 06:57:11
Sunset @ 2021-09-18 : 19:30:39
Sunrise @ 2021-09-19: 06:58:56
Sunset @ 2021-09-19 : 19:28:12
Sunrise @ 2021-09-20: 07:00:40 <-- example this should be the sunrise time of 2019-09-19 (Sunday)
Sunset @ 2021-09-20 : 19:25:45
Sunrise @ 2021-09-21: 07:02:26
Sunset @ 2021-09-21 : 19:23:17
Sunrise @ 2021-09-22: 07:04:11
Sunset @ 2021-09-22 : 19:20:50
Sunrise @ 2021-09-23: 07:05:56
Sunset @ 2021-09-23 : 19:18:23
Sunrise @ 2021-09-24: 07:07:41
Sunset @ 2021-09-24 : 19:15:56
Sunrise @ 2021-09-25: 07:09:27
Sunset @ 2021-09-25 : 19:13:28
Sunrise @ 2021-09-26: 07:11:13
Sunset @ 2021-09-26 : 19:11:01
Sunrise @ 2021-09-27: 07:12:59
Sunset @ 2021-09-27 : 19:08:35
Sunrise @ 2021-09-28: 07:14:45
Sunset @ 2021-09-28 : 19:06:08
Sunrise @ 2021-09-29: 07:16:32
Sunset @ 2021-09-29 : 19:03:41
Sunrise @ 2021-09-30: 07:18:18
Sunset @ 2021-09-30 : 19:01:15

I will simply add a day in my calculations to "fix" this, as in my case I don't need to worry about the location or UTC offset changing too much.

xrstf avatar Sep 19 '21 16:09 xrstf