Skip to content

Commit

Permalink
Added method to get the length of day
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed Jan 1, 2017
1 parent c39d43d commit 21b84ec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static void main(String[] args) throws Throwable {
printCalendar(tz, now);

System.out.println("Current day period is " + SunriseSunset.getDayPeriod(now, latitude, longitude));
long dayLength = SunriseSunset.getDayLength(now, latitude, longitude);
System.out.println("Day is " + dayLength + " seconds long");

System.out.println("Lookup for date:");
printCalendar(tz, day);
Expand Down
29 changes: 29 additions & 0 deletions library/src/main/java/ca/rmen/sunrisesunset/SunriseSunset.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum DayPeriod {
private static final int JULIAN_DATE_2000_01_01 = 2451545;
private static final double CONST_0009 = 0.0009;
private static final double CONST_360 = 360;
private static final long SECONDS_IN_DAY = 60 * 60 * 24;

/**
* Intermediate variables used in the sunrise equation
Expand Down Expand Up @@ -633,4 +634,32 @@ public static DayPeriod getDayPeriod(Calendar calendar, double latitude, double
return DayPeriod.NIGHT;
}

/**
*
* @param calendar the datetime for which to determine the day length
* @param latitude the latitude of the location in degrees.
* @param longitude the longitude of the location in degrees (West is negative)
* @return the number of milliseconds between sunrise and sunset.
*/
public static long getDayLength(Calendar calendar, double latitude, double longitude) {
Calendar[] sunriseSunset = getSunriseSunset(calendar, latitude, longitude);
if (sunriseSunset == null) {
int month = calendar.get(Calendar.MONTH); // Reminder: January = 0
if (latitude > 0) {
if (month >= 3 && month <= 10) {
return SECONDS_IN_DAY; // Always day at the north pole in June
} else {
return 0; // Always night at the north pole in December
}
} else {
if (month >= 3 && month <= 10) {
return 0; // Always night at the south pole in June
} else {
return SECONDS_IN_DAY; // Always day at the south pole in December
}
}
}
return (sunriseSunset[1].getTimeInMillis() - sunriseSunset[0].getTimeInMillis()) / 1000;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package ca.rmen.sunrisesunset.test;

import ca.rmen.sunrisesunset.SunriseSunset;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -59,6 +60,7 @@ public void testAntarctica() {
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20150621 11:35", -77.8456, 166.6693, SunriseSunset.DayPeriod.NIGHT);
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20150621 13:35", -77.8456, 166.6693, SunriseSunset.DayPeriod.NIGHT);
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20150621 15:35", -77.8456, 166.6693, SunriseSunset.DayPeriod.ASTRONOMICAL_TWILIGHT);
SunriseSunsetTestUtils.testDayLength("Antarctica/McMurdo", "20150621", -77.8456, 166.6693, 0);

SunriseSunsetTestUtils.testSunriseSunset("Antarctica/McMurdo", "20150921", -77.8456, 166.6693, "06:48", "18:46", accuracyMinutes);
SunriseSunsetTwilightTestUtils.testCivilTwilight("Antarctica/McMurdo", "20150921", -77.8456, 166.6693, "5:07", "20:27", accuracyMinutes);
Expand Down Expand Up @@ -92,6 +94,7 @@ public void testAntarctica() {
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20151221 12:00", -77.8456, 166.6693, SunriseSunset.DayPeriod.DAY);
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20151221 18:00", -77.8456, 166.6693, SunriseSunset.DayPeriod.DAY);
SunriseSunsetTestUtils.testGetDayPeriod("Antarctica/McMurdo", "20151221 23:00", -77.8456, 166.6693, SunriseSunset.DayPeriod.DAY);
SunriseSunsetTestUtils.testDayLength("Antarctica/McMurdo", "20151221", -77.8456, 166.6693, 86400);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ static void testSolarNoon(String timeZoneString,
validateSolarNoon(actualSolarNoon, timeZoneString, inputDayString, expectedNoonString);
}

static void testDayLength(String timeZoneString,
String inputDayString, double inputLatitude, double inputLongitude,
long expectedDayLength) {

Calendar inputDay = parseDate(timeZoneString, inputDayString);
Assert.assertEquals(expectedDayLength, SunriseSunset.getDayLength(inputDay, inputLatitude, inputLongitude));
}

static Calendar parseDate(String timeZoneString, String inputDayString) {
TimeZone tz = TimeZone.getTimeZone(timeZoneString);

Expand Down

0 comments on commit 21b84ec

Please sign in to comment.