Skip to content

Commit 1223dd0

Browse files
authored
SunTimesCalculator implement getUTCMidnight
- Implement getUTCMidnight - add default constructor to avoid warnings on recent JDKs - Replace while loops with modulo - Update JavaDocs to avoid warnings on recent JDKs and other tweaks
1 parent f1904b1 commit 1223dd0

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/main/java/com/kosherjava/zmanim/util/SunTimesCalculator.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Zmanim Java API
3-
* Copyright (C) 2004-2023 Eliyahu Hershfeld
3+
* Copyright (C) 2004-2025 Eliyahu Hershfeld
44
*
55
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
66
* Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
@@ -26,10 +26,17 @@
2626
* account for leap years. It is not as accurate as the Jean Meeus based {@link NOAACalculator} that is the default calculator
2727
* use by the KosherJava <em>zmanim</em> library.
2828
*
29-
* @author &copy; Eliyahu Hershfeld 2004 - 2023
29+
* @author &copy; Eliyahu Hershfeld 2004 - 2025
3030
* @author &copy; Kevin Boone 2000
3131
*/
3232
public class SunTimesCalculator extends AstronomicalCalculator {
33+
34+
/**
35+
* Default constructor of the SunTimesCalculator.
36+
*/
37+
public SunTimesCalculator() {
38+
super();
39+
}
3340

3441
/**
3542
* @see com.kosherjava.zmanim.util.AstronomicalCalculator#getCalculatorName()
@@ -57,11 +64,12 @@ public double getUTCSunset(Calendar calendar, GeoLocation geoLocation, double ze
5764
}
5865

5966
/**
60-
* The number of degrees of longitude that corresponds to one-hour time difference.
67+
* The number of degrees of longitude that corresponds to one hour of time difference.
6168
*/
6269
private static final double DEG_PER_HOUR = 360.0 / 24.0;
6370

6471
/**
72+
* The sine in degrees.
6573
* @param deg the degrees
6674
* @return sin of the angle in degrees
6775
*/
@@ -70,6 +78,7 @@ private static double sinDeg(double deg) {
7078
}
7179

7280
/**
81+
* Return the arc cosine in degrees.
7382
* @param x angle
7483
* @return acos of the angle in degrees
7584
*/
@@ -78,6 +87,7 @@ private static double acosDeg(double x) {
7887
}
7988

8089
/**
90+
* Return the arc sine in degrees.
8191
* @param x angle
8292
* @return asin of the angle in degrees
8393
*/
@@ -86,6 +96,7 @@ private static double asinDeg(double x) {
8696
}
8797

8898
/**
99+
* Return the tangent in degrees.
89100
* @param deg degrees
90101
* @return tan of the angle in degrees
91102
*/
@@ -145,6 +156,7 @@ private static double getMeanAnomaly(int dayOfYear, double longitude, boolean is
145156
}
146157

147158
/**
159+
* Returns the Sun's true longitude in degrees.
148160
* @param sunMeanAnomaly the Sun's mean anomaly in degrees
149161
* @return the Sun's true longitude in degrees. The result is an angle &gt;= 0 and &lt;= 360.
150162
*/
@@ -239,14 +251,8 @@ private static double getTimeUTC(Calendar calendar, GeoLocation geoLocation, dou
239251

240252
double localMeanTime = getLocalMeanTime(localHour, sunRightAscensionHours,
241253
getApproxTimeDays(dayOfYear, getHoursFromMeridian(geoLocation.getLongitude()), isSunrise));
242-
double processedTime = localMeanTime - getHoursFromMeridian(geoLocation.getLongitude());
243-
while (processedTime < 0.0) {
244-
processedTime += 24.0;
245-
}
246-
while (processedTime >= 24.0) {
247-
processedTime -= 24.0;
248-
}
249-
return processedTime;
254+
double pocessedTime = localMeanTime - getHoursFromMeridian(geoLocation.getLongitude());
255+
return pocessedTime > 0 ? pocessedTime % 24 : pocessedTime % 24 + 24; // ensure that the time is >= 0 and < 24
250256
}
251257

252258
/**
@@ -278,4 +284,25 @@ public double getUTCNoon(Calendar calendar, GeoLocation geoLocation) {
278284
}
279285
return noon;
280286
}
287+
288+
/**
289+
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
290+
* of midnight for the given day at the given location on earth. This implementation returns solar midnight as 12 hours
291+
* after utc noon that is halfway between sunrise and sunset.
292+
* {@link NOAACalculator}, the default calculator, returns true solar noon. See <a href=
293+
* "https://kosherjava.com/2020/07/02/definition-of-chatzos/">The Definition of Chatzos</a> for details on solar
294+
* noon calculations.
295+
* @see com.kosherjava.zmanim.util.AstronomicalCalculator#getUTCNoon(Calendar, GeoLocation)
296+
* @see NOAACalculator
297+
*
298+
* @param calendar
299+
* The Calendar representing the date to calculate solar noon for
300+
* @param geoLocation
301+
* The location information used for astronomical calculating sun times.
302+
* @return the time in minutes from zero UTC. If an error was encountered in the calculation (expected behavior for
303+
* some locations such as near the poles, {@link Double#NaN} will be returned.
304+
*/
305+
public double getUTCMidnight(Calendar calendar, GeoLocation geoLocation) {
306+
return (getUTCNoon(calendar, geoLocation) + 12);
307+
}
281308
}

0 commit comments

Comments
 (0)