Skip to content

Commit

Permalink
Fix possible error in alwaysUp/alwaysDown
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Jul 3, 2019
1 parent 142d092 commit e7552dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/org/shredzone/commons/suncalc/SunTimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/shredzone/commons/suncalc/Locations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
7 changes: 7 additions & 0 deletions src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e7552dc

Please sign in to comment.