src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

sunrise.go (1082B)


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