janrg / MeeusSunMoon

JavaScript module to calculate times for sunrise, solar noon, and sunset, as well as moon phases based on "Astronomical Algorithms" by Jean Meeus
MIT License
31 stars 9 forks source link

Possible wrong Timezone handling #15

Closed S-Man42 closed 4 years ago

S-Man42 commented 4 years ago

https://github.com/janrg/MeeusSunMoon/blob/master/src/sunTimes.js#L15-L16

Here you are creating a new dateTime object for UTC by using the date fields of the input datetime. I am not quite sure about it, but for me it seems not to be entirely correctly. E.g. Imagine a timezone == UTC -8h. If you had a time like 20h (== 8pm) at this tz, it would be already the next date at UTC (at 4am because of the shifted 8 hours). So, using the same date for the input datetime and the UTC datetime will be incorrect, IMHO... I believe you have to subtract the tz offset from the date part first to get the real UTC date.

However, I am not sure about it, maybe I miss something?

janrg commented 4 years ago

It's been a while since I implemented this so I'm not 100% sure anymore about the exact details in the algorithm, but I think the correction occurs in normalizeM() where I modify to the fractional date of the solar event while taking into account the UTC offset so that it can actually be < 0 or > 1 and hence push the resulting time to a different UTC date which will then be on the correct date once the timezone is applied.