Skip to content

Commit

Permalink
Kotlin: true solar time demo
Browse files Browse the repository at this point in the history
  • Loading branch information
cosinekitty committed Feb 13, 2023
1 parent 632d059 commit 1dcdb77
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions demo/kotlin/correct/solar_time.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
True solar time = 11.6286 hours (11:37:42.979)
1 change: 1 addition & 0 deletions demo/kotlin/demotest
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rm -rf build test
mkdir -p test
./gradlew jar || Fail "Error building Kotlin demo application."

TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z
TestDemo constellation 2021-06-01T00:00:00Z
TestDemo jupiter_moons 2021-04-16T00:26:18Z
TestDemo lunar_eclipse 1988-01-01T00:00:00Z
Expand Down
1 change: 1 addition & 0 deletions demo/kotlin/demotest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ call gradlew.bat jar || exit /b 1

REM ----------------------------------------------------------------------------------

call :TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z || exit /b 1
call :TestDemo constellation 2021-06-01T00:00:00Z || exit /b 1
call :TestDemo jupiter_moons 2021-04-16T00:26:18Z || exit /b 1
call :TestDemo lunar_eclipse 1988-01-01T00:00:00Z || exit /b 1
Expand Down
12 changes: 11 additions & 1 deletion demo/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Command line arguments:
Given an integer year number, displays the solstices and
equinoxes for that year. The year must be in the range 0000..9999.
solar_time latitude longitude [yyyy-mm-ddThh:mm:ssZ]
Displays the true solar time for the observer at the
given geographic coordinates.
"""

private fun printUsage(): Int {
Expand Down Expand Up @@ -172,5 +176,11 @@ internal val demoList = listOf(
`Seasons demo`(
parseYear(args[1])
)
}
},
Demo("solar_time", 3, 4) { args ->
`Solar true time`(
parseObserver(args, 1),
parseTime(args, 3)
)
},
)
29 changes: 29 additions & 0 deletions demo/kotlin/src/main/kotlin/SolarTime.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import io.github.cosinekitty.astronomy.*
import kotlin.math.roundToInt

/**
* Calculate true solar time for a given observer and UTC time.
*
* @param observer
* The geographic location for which to calculate apparent celestial body positions.
*
* @param time
* The date and time for which to calculate positions.
*/
internal fun `Solar true time`(observer: Observer, time: Time): Int {
val ha = hourAngle(Body.Sun, time, observer)
val solarTimeHours = (ha + 12.0) % 24.0
var milli = (solarTimeHours * 3.6e+6).roundToInt()
var second = milli / 1000
milli %= 1000
var minute = second / 60
second %= 60
var hour = minute / 60
minute %= 60
hour %= 24

println("True solar time = %7.4f hours (%02d:%02d:%02d.%03d)".format(
solarTimeHours, hour, minute, second, milli))

return 0
}

0 comments on commit 1dcdb77

Please sign in to comment.