Skip to content

Commit

Permalink
Issue #2: fix NPE in isDay()/isNight() for extreme latitudes.
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed May 16, 2015
1 parent d1db928 commit 13abb05
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<parent>
<groupId>ca.rmen</groupId>
<artifactId>sunrise-sunset</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>sunrise-sunset-cli</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
<parent>
<groupId>ca.rmen</groupId>
<artifactId>sunrise-sunset</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>lib-sunrise-sunset</artifactId>
<packaging>jar</packaging>
<version>1.0.1</version>
<version>1.0.2</version>
<name>lib-sunrise-sunset</name>
<url>http://rmen.ca</url>

Expand Down
18 changes: 18 additions & 0 deletions library/src/main/java/ca/rmen/sunrisesunset/SunriseSunset.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,24 @@ public static boolean isDay(double latitude, double longitude) {
public static boolean isNight(double latitude, double longitude) {
Calendar today = Calendar.getInstance();
Calendar[] sunriseSunset = getSunriseSunset(today, latitude, longitude);
// In extreme latitudes, there may be no sunrise/sunset time in summer or
// winter, because it will be day or night 24 hours
if (sunriseSunset == null) {
int month = today.get(Calendar.MONTH); // Reminder: January = 0
if (latitude > 0) {
if (month >= 3 && month <= 10) {
return false; // Always day at the north pole in June
} else {
return true; // Always night at the north pole in December
}
} else {
if (month >= 3 && month <= 10) {
return true; // Always night at the south pole in June
} else {
return false; // Always day at the south pole in December
}
}
}
Calendar sunrise = sunriseSunset[0];
Calendar sunset = sunriseSunset[1];
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void testAntarctica() {
testCivilTwilight("Antarctica/McMurdo", "20151221", -77.8456, 166.6693, null, null);
testNauticalTwilight("Antarctica/McMurdo", "20151221", -77.8456, 166.6693, null, null);
testAstronomicalTwilight("Antarctica/McMurdo", "20151221", -77.8456, 166.6693, null, null);
testDayOrNight("Antarctica", "Antarctica/McMurdo", -77.8456, 166.6693, null, null, null, null);
}

/**
Expand Down Expand Up @@ -712,11 +713,18 @@ private void testDayOrNight(String name, String timeZoneString,

Calendar now = Calendar.getInstance(TimeZone
.getTimeZone(timeZoneString));
String nowString = format(DATE_FORMAT_MINUTES, now);

if (earliestSunriseString == null || latestSunriseString == null
|| earliestSunsetString == null || latestSunsetString == null) {
// For extreme latitudes around June/December, we'll just return here. The previous lines at least make
// sure the library doesn't crash.
return;
}
Calendar earliestSunrise = getCalendarAtTime(now, earliestSunriseString);
Calendar latestSunrise = getCalendarAtTime(now, latestSunriseString);
Calendar earliestSunset = getCalendarAtTime(now, earliestSunsetString);
Calendar latestSunset = getCalendarAtTime(now, latestSunsetString);
String nowString = format(DATE_FORMAT_MINUTES, now);
if (isDay) {
Assert.assertTrue("In " + name + ", " + nowString
+ " is before sunrise at " + earliestSunriseString
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<groupId>ca.rmen</groupId>
<artifactId>sunrise-sunset</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<version>1.0.2</version>
<name>sunrise-sunset</name>
<url>http://rmen.ca</url>

Expand Down

0 comments on commit 13abb05

Please sign in to comment.