Make Twilight record and add helper function
This commit is contained in:
parent
57488bc625
commit
fc709aa6ba
3 changed files with 15 additions and 27 deletions
|
@ -3,25 +3,13 @@ package eu.m724.wtapi.object;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
public class Twilight {
|
|
||||||
/**
|
/**
|
||||||
* The date this object contains times for
|
* @param date The date this object contains times for
|
||||||
|
* @param sunrise Time of sunrise relative to UTC midnight of the date
|
||||||
|
* @param sunset Time of sunset relative to UTC midnight of the date
|
||||||
*/
|
*/
|
||||||
public final LocalDate date;
|
public record Twilight(LocalDate date, Duration sunrise, Duration sunset) {
|
||||||
|
public Duration getDayLength() {
|
||||||
/**
|
return sunset.minus(sunrise);
|
||||||
* Time of sunrise relative to UTC midnight of the date
|
|
||||||
*/
|
|
||||||
public final Duration sunrise;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Time of sunset relative to UTC midnight of the date
|
|
||||||
*/
|
|
||||||
public final Duration sunset;
|
|
||||||
|
|
||||||
public Twilight(LocalDate date, Duration sunrise, Duration sunset) {
|
|
||||||
this.date = date;
|
|
||||||
this.sunrise = sunrise;
|
|
||||||
this.sunset = sunset;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,12 +37,12 @@ public class ApproximateTwilightTimeTest {
|
||||||
System.out.println(date);
|
System.out.println(date);
|
||||||
System.out.println(coordinates.latitude + " " + coordinates.longitude);
|
System.out.println(coordinates.latitude + " " + coordinates.longitude);
|
||||||
|
|
||||||
System.out.println("Calculated sunrise: " + date.atStartOfDay().plus(twilight.sunrise));
|
System.out.println("Calculated sunrise: " + date.atStartOfDay().plus(twilight.sunrise()));
|
||||||
System.out.println("Actual sunrise: " + date.atStartOfDay().plusMinutes(actualSunrise));
|
System.out.println("Actual sunrise: " + date.atStartOfDay().plusMinutes(actualSunrise));
|
||||||
assert Math.abs(twilight.sunrise.toMinutes() - actualSunrise) < ACCEPTABLE_DIFFERENCE;
|
assert Math.abs(twilight.sunrise().toMinutes() - actualSunrise) < ACCEPTABLE_DIFFERENCE;
|
||||||
|
|
||||||
System.out.println("Calculated sunset: " + date.atStartOfDay().plus(twilight.sunset));
|
System.out.println("Calculated sunset: " + date.atStartOfDay().plus(twilight.sunset()));
|
||||||
System.out.println("Actual sunset: " + date.atStartOfDay().plusMinutes(actualSunset));
|
System.out.println("Actual sunset: " + date.atStartOfDay().plusMinutes(actualSunset));
|
||||||
assert Math.abs(twilight.sunset.toMinutes() - actualSunset) < ACCEPTABLE_DIFFERENCE;
|
assert Math.abs(twilight.sunset().toMinutes() - actualSunset) < ACCEPTABLE_DIFFERENCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ public class MockTwilightTimeTest {
|
||||||
Coordinates coordinates = new Coordinates(52.4796012, 62.1847245);
|
Coordinates coordinates = new Coordinates(52.4796012, 62.1847245);
|
||||||
Twilight twilight = provider.calculateTwilightTime(date, coordinates);
|
Twilight twilight = provider.calculateTwilightTime(date, coordinates);
|
||||||
|
|
||||||
assert twilight.date.equals(date);
|
assert twilight.date().equals(date);
|
||||||
assert twilight.sunrise.getSeconds() == -6840;
|
assert twilight.sunrise().getSeconds() == -6840;
|
||||||
assert twilight.sunset.getSeconds() == 6840;
|
assert twilight.sunset().getSeconds() == 6840;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue