Skip to content

Commit

Permalink
Java: added 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 1dcdb77 commit 42650bd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions demo/java/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/java/demotest
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ if ! ./gradlew jar test; then
exit 1
fi

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/java/demotest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ call gradlew.bat jar test || 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class Main {
" seasons year",
" 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.",
""
);

Expand Down Expand Up @@ -198,6 +202,12 @@ public Demo(String name, int minArgs, int maxArgs, DemoRunner runner) {
Seasons.run(
parseYear(args[1])
)
),
new Demo("solar_time", 3, 4, args ->
SolarTime.run(
parseObserver(args, 1),
parseTime(args, 3)
)
)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.cosinekitty.astronomy.demo;
import io.github.cosinekitty.astronomy.*;

public class SolarTime {
/**
* Display true solar time for a given geographic location at a given time.
*
* @param observer
* The geographic location for which to calculate true solar time.
*
* @param time
* The date and time for which to calculate true solar time.
*/
public static int run(Observer observer, Time time) {
double ha = Astronomy.hourAngle(Body.Sun, time, observer);
double solarTimeHours = (ha + 12.0) % 24.0;
int milli = (int) Math.round(solarTimeHours * 3.6e+6);
int second = milli / 1000;
milli %= 1000;
int minute = second / 60;
second %= 60;
int hour = minute / 60;
minute %= 60;
hour %= 24;

System.out.printf("True solar time = %7.4f hours (%02d:%02d:%02d.%03d)%n", solarTimeHours, hour, minute, second, milli);
return 0;
}
}

0 comments on commit 42650bd

Please sign in to comment.