Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getSunLowerTransit algorithm to detect solar midnight #234

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading