From e7552dcac88ff2a29adea20e9d7352582a0b497b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Thu, 4 Jul 2019 00:51:02 +0200 Subject: [PATCH] Fix possible error in alwaysUp/alwaysDown --- .../org/shredzone/commons/suncalc/SunTimes.java | 15 +++++++++------ .../org/shredzone/commons/suncalc/Locations.java | 6 ++++++ .../shredzone/commons/suncalc/SunTimesTest.java | 7 +++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/shredzone/commons/suncalc/SunTimes.java b/src/main/java/org/shredzone/commons/suncalc/SunTimes.java index cc47bbb..437a2da 100644 --- a/src/main/java/org/shredzone/commons/suncalc/SunTimes.java +++ b/src/main/java/org/shredzone/commons/suncalc/SunTimes.java @@ -274,9 +274,9 @@ public SunTimes execute() { Double set = null; Double noon = null; Double nadir = null; + boolean alwaysUp = true; + boolean alwaysDown = true; double ye; - double noonYe = 0.0; - double nadirYe = 0.0; double y_minus = correctedSunHeight(jd); @@ -308,15 +308,18 @@ public SunTimes execute() { } if (hour < 24) { + if (y_0 > 0.0) { + alwaysDown = false; + } else { + alwaysUp = false; + } double xeAbs = Math.abs(qi.getXe()); if (xeAbs <= 1.0) { double xeHour = qi.getXe() + hour; if (qi.isMaximum()) { noon = xeHour; - noonYe = ye; } else { nadir = xeHour; - nadirYe = ye; } } } @@ -333,8 +336,8 @@ public SunTimes execute() { set != null ? jd.atHour(set).getDateTruncated(getTruncatedTo()) : null, noon != null ? jd.atHour(noon).getDateTruncated(getTruncatedTo()) : null, nadir != null ? jd.atHour(nadir).getDateTruncated(getTruncatedTo()) : null, - nadir == null || nadirYe > 0.0, - noon == null || noonYe < 0.0 + alwaysUp, + alwaysDown ); } diff --git a/src/test/java/org/shredzone/commons/suncalc/Locations.java b/src/test/java/org/shredzone/commons/suncalc/Locations.java index 6c6dcc5..02b73c9 100644 --- a/src/test/java/org/shredzone/commons/suncalc/Locations.java +++ b/src/test/java/org/shredzone/commons/suncalc/Locations.java @@ -60,4 +60,10 @@ public final class Locations { public static final double[] MARTINIQUE = new double[] { 14.640725, -61.0112 }; public static final TimeZone MARTINIQUE_TZ = TimeZone.getTimeZone("America/Martinique"); + /** + * Sydney. To test a fix for issue #14. + */ + public static final double[] SYDNEY = new double[] { -33.744272, 151.231291 }; + public static final TimeZone SYDNEY_TZ = TimeZone.getTimeZone("Australia/Sydney"); + } diff --git a/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java b/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java index 5288ac6..bf75b09 100644 --- a/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java @@ -138,6 +138,13 @@ public void testMartinique() { assertTimes(t1, "2019-07-01T09:38:35Z", "2019-07-01T22:37:23Z", "2019-07-01T16:06:08Z"); } + @Test + public void testSydney() { + SunTimes t1 = SunTimes.compute().at(SYDNEY).on(2019, 7, 3).timezone(SYDNEY_TZ) + .truncatedTo(Unit.SECONDS).fullCycle().execute(); + assertTimes(t1, "2019-07-02T21:00:35Z", "2019-07-03T06:58:02Z", "2019-07-03T01:59:18Z"); + } + @Test public void testSequence() { long acceptableError = 62 * 1000L;