Skip to content

Commit a76a3b6

Browse files
authored
add getSunLowerTransit algorithm to detect solar midnight (#234)
* add sunLowerTransit algorithm to detect solar midnight * skip modifications to latitude
1 parent 328eee8 commit a76a3b6

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/main/java/com/kosherjava/zmanim/AstronomicalCalendar.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Date getSunrise() {
127127
if (Double.isNaN(sunrise)) {
128128
return null;
129129
} else {
130-
return getDateFromTime(sunrise, true);
130+
return getDateFromTime(sunrise, SolarEvent.SUNRISE);
131131
}
132132
}
133133

@@ -149,7 +149,7 @@ public Date getSeaLevelSunrise() {
149149
if (Double.isNaN(sunrise)) {
150150
return null;
151151
} else {
152-
return getDateFromTime(sunrise, true);
152+
return getDateFromTime(sunrise, SolarEvent.SUNRISE);
153153
}
154154
}
155155

@@ -215,7 +215,7 @@ public Date getSunset() {
215215
if (Double.isNaN(sunset)) {
216216
return null;
217217
} else {
218-
return getDateFromTime(sunset, false);
218+
return getDateFromTime(sunset, SolarEvent.SUNSET);
219219
}
220220
}
221221

@@ -236,7 +236,7 @@ public Date getSeaLevelSunset() {
236236
if (Double.isNaN(sunset)) {
237237
return null;
238238
} else {
239-
return getDateFromTime(sunset, false);
239+
return getDateFromTime(sunset, SolarEvent.SUNSET);
240240
}
241241
}
242242

@@ -329,7 +329,7 @@ public Date getSunriseOffsetByDegrees(double offsetZenith) {
329329
if (Double.isNaN(dawn)) {
330330
return null;
331331
} else {
332-
return getDateFromTime(dawn, true);
332+
return getDateFromTime(dawn, SolarEvent.SUNRISE);
333333
}
334334
}
335335

@@ -352,7 +352,7 @@ public Date getSunsetOffsetByDegrees(double offsetZenith) {
352352
if (Double.isNaN(sunset)) {
353353
return null;
354354
} else {
355-
return getDateFromTime(sunset, false);
355+
return getDateFromTime(sunset, SolarEvent.SUNSET);
356356
}
357357
}
358358

@@ -511,7 +511,21 @@ public long getTemporalHour(Date startOfDay, Date endOfDay) {
511511
*/
512512
public Date getSunTransit() {
513513
double noon = getAstronomicalCalculator().getUTCNoon(getAdjustedCalendar(), getGeoLocation());
514-
return getDateFromTime(noon, false);
514+
return getDateFromTime(noon, SolarEvent.NOON);
515+
}
516+
517+
public Date getSunLowerTransit() {
518+
Calendar cal = getAdjustedCalendar();
519+
GeoLocation lowerGeoLocation = (GeoLocation) getGeoLocation().clone();
520+
double meridian = lowerGeoLocation.getLongitude();
521+
double lowerMeridian = meridian + 180;
522+
if (lowerMeridian > 180){
523+
lowerMeridian = lowerMeridian - 360;
524+
cal.add(Calendar.DAY_OF_MONTH, -1);
525+
}
526+
lowerGeoLocation.setLongitude(lowerMeridian);
527+
double noon = getAstronomicalCalculator().getUTCNoon(cal, lowerGeoLocation);
528+
return getDateFromTime(noon, SolarEvent.MIDNIGHT);
515529
}
516530

517531
/**
@@ -556,6 +570,9 @@ public Date getSunTransit(Date startOfDay, Date endOfDay) {
556570
return getTimeOffset(startOfDay, temporalHour * 6);
557571
}
558572

573+
protected enum SolarEvent {
574+
SUNRISE, SUNSET, NOON, MIDNIGHT
575+
}
559576
/**
560577
* A method that returns a <code>Date</code> from the time passed in as a parameter.
561578
*
@@ -565,7 +582,7 @@ public Date getSunTransit(Date startOfDay, Date endOfDay) {
565582
* @param isSunrise true if this time is for sunrise
566583
* @return The Date object representation of the time double
567584
*/
568-
protected Date getDateFromTime(double time, boolean isSunrise) {
585+
protected Date getDateFromTime(double time, SolarEvent solarEvent) {
569586
if (Double.isNaN(time)) {
570587
return null;
571588
}
@@ -588,10 +605,12 @@ protected Date getDateFromTime(double time, boolean isSunrise) {
588605
// Check if a date transition has occurred, or is about to occur - this indicates the date of the event is
589606
// actually not the target date, but the day prior or after
590607
int localTimeHours = (int)getGeoLocation().getLongitude() / 15;
591-
if (isSunrise && localTimeHours + hours > 18) {
608+
if (solarEvent == SolarEvent.SUNRISE && localTimeHours + hours > 18) {
592609
cal.add(Calendar.DAY_OF_MONTH, -1);
593-
} else if (!isSunrise && localTimeHours + hours < 6) {
610+
} else if (solarEvent == SolarEvent.SUNSET && localTimeHours + hours < 6) {
594611
cal.add(Calendar.DAY_OF_MONTH, 1);
612+
} else if (solarEvent == SolarEvent.MIDNIGHT && localTimeHours + hours > 12) {
613+
cal.add(Calendar.DAY_OF_MONTH, -1);
595614
}
596615

597616
cal.set(Calendar.HOUR_OF_DAY, hours);

0 commit comments

Comments
 (0)