Dates returned could contain NaN time
Description
With some longitude and latitude, the SunCalc dates (dawn, dusk, nauticalDawn, night, …) are created with a NaN time. This can lead to unexpected behavior when using this library. This issue can be reproduced if the latitude/longitude is set for example to 89.0/179.0 or 84.0/111.0.
The root problem is caused by the use of acos() with invalid input:
class func getHourAngle(h:Double, phi:Double, d:Double) -> Double {
return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d)))
}
Note that the original Javascript library also has the same issue, as well as most of the implementations based on this original library. See for example the related issues:
- https://github.com/mourner/suncalc/issues/134
- https://github.com/shanus/flutter_suncalc/issues/1
- https://github.com/mourner/suncalc/issues/65
Workaround
One solution would be to implement the fix suggested in the Flutter library: https://github.com/shanus/flutter_suncalc/issues/1 I implemented this workaround in a Swift package port of this code and you can find the commit here: https://github.com/Timac/SunCalc/commit/cad82c30be5052aa0ffd4d4855f47946403200a1
I am not sure if this code is still under development. Applying this workaround would break the compatibility with old software using this source code.