-
Notifications
You must be signed in to change notification settings - Fork 13
/
sunrise.go
31 lines (27 loc) · 1.06 KB
/
sunrise.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package sunrise
import (
"math"
"time"
)
// SunriseSunset calculates when the sun will rise and when it will set on the
// given day at the specified location.
// Returns time.Time{} if there sun does not rise or set
func SunriseSunset(latitude, longitude float64, year int, month time.Month, day int) (time.Time, time.Time) {
var (
d = MeanSolarNoon(longitude, year, month, day)
solarAnomaly = SolarMeanAnomaly(d)
equationOfCenter = EquationOfCenter(solarAnomaly)
eclipticLongitude = EclipticLongitude(solarAnomaly, equationOfCenter, d)
solarTransit = SolarTransit(d, solarAnomaly, eclipticLongitude)
declination = Declination(eclipticLongitude)
hourAngle = HourAngle(latitude, declination)
frac = hourAngle / 360
sunrise = solarTransit - frac
sunset = solarTransit + frac
)
// Check for no sunrise, no sunset
if hourAngle == math.MaxFloat64 || hourAngle == -1*math.MaxFloat64 {
return time.Time{}, time.Time{}
}
return JulianDayToTime(sunrise), JulianDayToTime(sunset)
}