Skip to content

Commit

Permalink
add getSunLowerTransit algorithm to detect solar midnight (#234)
Browse files Browse the repository at this point in the history
* add sunLowerTransit algorithm to detect solar midnight

* skip modifications to latitude
  • Loading branch information
pinnymz authored Apr 26, 2024
1 parent 328eee8 commit a76a3b6
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/main/java/com/kosherjava/zmanim/AstronomicalCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Date getSunrise() {
if (Double.isNaN(sunrise)) {
return null;
} else {
return getDateFromTime(sunrise, true);
return getDateFromTime(sunrise, SolarEvent.SUNRISE);
}
}

Expand All @@ -149,7 +149,7 @@ public Date getSeaLevelSunrise() {
if (Double.isNaN(sunrise)) {
return null;
} else {
return getDateFromTime(sunrise, true);
return getDateFromTime(sunrise, SolarEvent.SUNRISE);
}
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public Date getSunset() {
if (Double.isNaN(sunset)) {
return null;
} else {
return getDateFromTime(sunset, false);
return getDateFromTime(sunset, SolarEvent.SUNSET);
}
}

Expand All @@ -236,7 +236,7 @@ public Date getSeaLevelSunset() {
if (Double.isNaN(sunset)) {
return null;
} else {
return getDateFromTime(sunset, false);
return getDateFromTime(sunset, SolarEvent.SUNSET);
}
}

Expand Down Expand Up @@ -329,7 +329,7 @@ public Date getSunriseOffsetByDegrees(double offsetZenith) {
if (Double.isNaN(dawn)) {
return null;
} else {
return getDateFromTime(dawn, true);
return getDateFromTime(dawn, SolarEvent.SUNRISE);
}
}

Expand All @@ -352,7 +352,7 @@ public Date getSunsetOffsetByDegrees(double offsetZenith) {
if (Double.isNaN(sunset)) {
return null;
} else {
return getDateFromTime(sunset, false);
return getDateFromTime(sunset, SolarEvent.SUNSET);
}
}

Expand Down Expand Up @@ -511,7 +511,21 @@ public long getTemporalHour(Date startOfDay, Date endOfDay) {
*/
public Date getSunTransit() {
double noon = getAstronomicalCalculator().getUTCNoon(getAdjustedCalendar(), getGeoLocation());
return getDateFromTime(noon, false);
return getDateFromTime(noon, SolarEvent.NOON);
}

public Date getSunLowerTransit() {
Calendar cal = getAdjustedCalendar();
GeoLocation lowerGeoLocation = (GeoLocation) getGeoLocation().clone();
double meridian = lowerGeoLocation.getLongitude();
double lowerMeridian = meridian + 180;
if (lowerMeridian > 180){
lowerMeridian = lowerMeridian - 360;
cal.add(Calendar.DAY_OF_MONTH, -1);
}
lowerGeoLocation.setLongitude(lowerMeridian);
double noon = getAstronomicalCalculator().getUTCNoon(cal, lowerGeoLocation);
return getDateFromTime(noon, SolarEvent.MIDNIGHT);
}

/**
Expand Down Expand Up @@ -556,6 +570,9 @@ public Date getSunTransit(Date startOfDay, Date endOfDay) {
return getTimeOffset(startOfDay, temporalHour * 6);
}

protected enum SolarEvent {
SUNRISE, SUNSET, NOON, MIDNIGHT
}
/**
* A method that returns a <code>Date</code> from the time passed in as a parameter.
*
Expand All @@ -565,7 +582,7 @@ public Date getSunTransit(Date startOfDay, Date endOfDay) {
* @param isSunrise true if this time is for sunrise
* @return The Date object representation of the time double
*/
protected Date getDateFromTime(double time, boolean isSunrise) {
protected Date getDateFromTime(double time, SolarEvent solarEvent) {
if (Double.isNaN(time)) {
return null;
}
Expand All @@ -588,10 +605,12 @@ protected Date getDateFromTime(double time, boolean isSunrise) {
// Check if a date transition has occurred, or is about to occur - this indicates the date of the event is
// actually not the target date, but the day prior or after
int localTimeHours = (int)getGeoLocation().getLongitude() / 15;
if (isSunrise && localTimeHours + hours > 18) {
if (solarEvent == SolarEvent.SUNRISE && localTimeHours + hours > 18) {
cal.add(Calendar.DAY_OF_MONTH, -1);
} else if (!isSunrise && localTimeHours + hours < 6) {
} else if (solarEvent == SolarEvent.SUNSET && localTimeHours + hours < 6) {
cal.add(Calendar.DAY_OF_MONTH, 1);
} else if (solarEvent == SolarEvent.MIDNIGHT && localTimeHours + hours > 12) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}

cal.set(Calendar.HOUR_OF_DAY, hours);
Expand Down

0 comments on commit a76a3b6

Please sign in to comment.