Skip to content

Commit 42650bd

Browse files
committed
Java: added true solar time demo
1 parent 1dcdb77 commit 42650bd

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

Diff for: demo/java/correct/solar_time.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
True solar time = 11.6286 hours (11:37:42.979)

Diff for: demo/java/demotest

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if ! ./gradlew jar test; then
1919
exit 1
2020
fi
2121

22+
TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z
2223
TestDemo constellation 2021-06-01T00:00:00Z
2324
TestDemo jupiter_moons 2021-04-16T00:26:18Z
2425
TestDemo lunar_eclipse 1988-01-01T00:00:00Z

Diff for: demo/java/demotest.bat

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ call gradlew.bat jar test || exit /b 1
1111

1212
REM ----------------------------------------------------------------------------------
1313

14+
call :TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z || exit /b 1
1415
call :TestDemo constellation 2021-06-01T00:00:00Z || exit /b 1
1516
call :TestDemo jupiter_moons 2021-04-16T00:26:18Z || exit /b 1
1617
call :TestDemo lunar_eclipse 1988-01-01T00:00:00Z || exit /b 1

Diff for: demo/java/src/main/java/io/github/cosinekitty/astronomy/demo/Main.java

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public class Main {
6060
" seasons year",
6161
" Given an integer year number, displays the solstices and equinoxes for that year.",
6262
" The year must be in the range 0000..9999.",
63+
"",
64+
" solar_time latitude longitude [yyyy-mm-ddThh:mm:ssZ]",
65+
" Displays the true solar time for the observer at the",
66+
" given geographic coordinates.",
6367
""
6468
);
6569

@@ -198,6 +202,12 @@ public Demo(String name, int minArgs, int maxArgs, DemoRunner runner) {
198202
Seasons.run(
199203
parseYear(args[1])
200204
)
205+
),
206+
new Demo("solar_time", 3, 4, args ->
207+
SolarTime.run(
208+
parseObserver(args, 1),
209+
parseTime(args, 3)
210+
)
201211
)
202212
);
203213
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.cosinekitty.astronomy.demo;
2+
import io.github.cosinekitty.astronomy.*;
3+
4+
public class SolarTime {
5+
/**
6+
* Display true solar time for a given geographic location at a given time.
7+
*
8+
* @param observer
9+
* The geographic location for which to calculate true solar time.
10+
*
11+
* @param time
12+
* The date and time for which to calculate true solar time.
13+
*/
14+
public static int run(Observer observer, Time time) {
15+
double ha = Astronomy.hourAngle(Body.Sun, time, observer);
16+
double solarTimeHours = (ha + 12.0) % 24.0;
17+
int milli = (int) Math.round(solarTimeHours * 3.6e+6);
18+
int second = milli / 1000;
19+
milli %= 1000;
20+
int minute = second / 60;
21+
second %= 60;
22+
int hour = minute / 60;
23+
minute %= 60;
24+
hour %= 24;
25+
26+
System.out.printf("True solar time = %7.4f hours (%02d:%02d:%02d.%03d)%n", solarTimeHours, hour, minute, second, milli);
27+
return 0;
28+
}
29+
}

0 commit comments

Comments
 (0)