SunCalc returning times for wrong day

Is my use of SunCalc below correct? It’s reporting sunrise/sunset for the day before I specified. Is it a timezone issue?

Many thanks.

julia> s=Date(2025,1,5)
2025-01-05

julia> lat,lon = (52.9335, -9.3441)
(52.9335, -9.3441)

julia> times = SunCalc.getTimes(s, lat, lon);

julia> sr,ss = times.sunrise,times.sunset
(DateTime("2025-01-04T08:50:53"), DateTime("2025-01-04T16:36:03"))

I get 3pm yesterday for sunrise and 1am today for sunset, which is obviously ridiculous. This isn’t an issue with displays based on host system time zones, the internal UTC epoch millisecond count for the reported sunset time is actually ~15 hours before now(), which is almost my local sunset at time of writing. The original Javascript implementation gives me the correct times, though a brief search shows an open Github issue #107 there about conditional off-by-one problems, so I encourage you to check that for your location.

To answer your first question, your usage is technically incorrect because you’re using an internal function instead of the documented API getSunlightTimes (ALWAYS check documentation for the specific library, no matter what it claims to be based on), but it was weird for the API to diverge like that from the reference Javascript implementation’s API to begin with and it gives the same wrong numbers for me anyway. You can open an issue on the SunCalc.jl Github repo about the wrong times, but it’s been over 2 years since the last release with no community interaction so I wouldn’t expect anything.

1 Like

Maybe you can give SolarPosition.jl v0.4.0 a try?

julia> using SolarPosition, Dates

julia> dt = Date(2025,1,5)
2025-01-05

julia> obs = Observer(52.9335, -9.3441)
Observer(latitude=52.9335°, longitude=-9.3441°, altitude=0.0m, horizon=0.0°)

julia> next_sunrise(obs, dt)
2025-01-05T08:49:14

julia> next_sunset(obs, dt)
2025-01-05T16:36:51

Thank you! I definitely will :slightly_smiling_face:

I had been following the discussion in a parallel thread so I’m delighted to read you’ve implemented this feature.

Thanks again.