Skip to content

Commit

Permalink
Get more accurate sunrise/set times
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Sep 12, 2020
1 parent bea3bbf commit 766b57d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,12 @@ internal object Astro {
coordinate: Coordinate,
standardAltitude: Double = -0.8333
): RiseSetTransitTimes {
val ut = ut(date).toLocalDate().atStartOfDay()
val sr = meanSiderealTime(julianDay(ut))
val sun = solarCoordinates(julianDay(ut))
val ut = ut0hOnDate(date)
val jd = julianDay(ut)
val longitudeNutation = nutationInLongitude(jd)
val eclipticObliquity = trueObliquityOfEcliptic(jd)
val sr = apparentSiderealTime(jd, longitudeNutation, eclipticObliquity)
val sun = solarCoordinates(jd)
val times = riseSetTransitTimes(
coordinate.latitude,
coordinate.longitude,
Expand Down Expand Up @@ -528,6 +531,22 @@ internal object Astro {
private fun square(a: Double): Double {
return a * a
}


private fun ut0hOnDate(date: ZonedDateTime): LocalDateTime {
val localDate = date.toLocalDate()

for (i in -1..1){
val ut0h = ut(date.plusDays(i.toLong())).toLocalDate().atStartOfDay()
val local0h = utToLocal(ut0h, date.zone)
if (localDate == local0h.toLocalDate()){
return ut0h
}
}

return ut(date).toLocalDate().atStartOfDay()
}

}

data class AstroCoordinates(val declination: Double, val rightAscension: Double)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fun Instant.toZonedDateTime(): ZonedDateTime {

fun LocalDateTime.plusHours(hours: Double): LocalDateTime {
val h = hours.toLong()
val m = ((hours % 1) * 60).toLong()
val s = (hours * 60) % 1
val m = (hours % 1) * 60
val s = (m % 1) * 60
val ns = (1e9 * s).toLong()
return this.plusHours(h).plusMinutes(m).plusNanos(ns)
return this.plusHours(h).plusMinutes(m.toLong()).plusNanos(ns)
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,5 @@ class AstroTest {
assertEquals(-7.78507, coords.declination, 0.0001)
assertEquals(-161.61917, coords.rightAscension, 0.0001)
}

}

0 comments on commit 766b57d

Please sign in to comment.