From 1223dd0b6ad2b492818aacc5eb478747989e0ace Mon Sep 17 00:00:00 2001 From: KosherJava Date: Wed, 1 Jan 2025 11:37:54 -0500 Subject: [PATCH] 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 --- .../zmanim/util/SunTimesCalculator.java | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/kosherjava/zmanim/util/SunTimesCalculator.java b/src/main/java/com/kosherjava/zmanim/util/SunTimesCalculator.java index cc6110bc..5a097d91 100644 --- a/src/main/java/com/kosherjava/zmanim/util/SunTimesCalculator.java +++ b/src/main/java/com/kosherjava/zmanim/util/SunTimesCalculator.java @@ -1,6 +1,6 @@ /* * Zmanim Java API - * Copyright (C) 2004-2023 Eliyahu Hershfeld + * Copyright (C) 2004-2025 Eliyahu Hershfeld * * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) @@ -26,10 +26,17 @@ * account for leap years. It is not as accurate as the Jean Meeus based {@link NOAACalculator} that is the default calculator * use by the KosherJava zmanim library. * - * @author © Eliyahu Hershfeld 2004 - 2023 + * @author © Eliyahu Hershfeld 2004 - 2025 * @author © Kevin Boone 2000 */ public class SunTimesCalculator extends AstronomicalCalculator { + + /** + * Default constructor of the SunTimesCalculator. + */ + public SunTimesCalculator() { + super(); + } /** * @see com.kosherjava.zmanim.util.AstronomicalCalculator#getCalculatorName() @@ -57,11 +64,12 @@ public double getUTCSunset(Calendar calendar, GeoLocation geoLocation, double ze } /** - * The number of degrees of longitude that corresponds to one-hour time difference. + * The number of degrees of longitude that corresponds to one hour of time difference. */ private static final double DEG_PER_HOUR = 360.0 / 24.0; /** + * The sine in degrees. * @param deg the degrees * @return sin of the angle in degrees */ @@ -70,6 +78,7 @@ private static double sinDeg(double deg) { } /** + * Return the arc cosine in degrees. * @param x angle * @return acos of the angle in degrees */ @@ -78,6 +87,7 @@ private static double acosDeg(double x) { } /** + * Return the arc sine in degrees. * @param x angle * @return asin of the angle in degrees */ @@ -86,6 +96,7 @@ private static double asinDeg(double x) { } /** + * Return the tangent in degrees. * @param deg degrees * @return tan of the angle in degrees */ @@ -145,6 +156,7 @@ private static double getMeanAnomaly(int dayOfYear, double longitude, boolean is } /** + * Returns the Sun's true longitude in degrees. * @param sunMeanAnomaly the Sun's mean anomaly in degrees * @return the Sun's true longitude in degrees. The result is an angle >= 0 and <= 360. */ @@ -239,14 +251,8 @@ private static double getTimeUTC(Calendar calendar, GeoLocation geoLocation, dou double localMeanTime = getLocalMeanTime(localHour, sunRightAscensionHours, getApproxTimeDays(dayOfYear, getHoursFromMeridian(geoLocation.getLongitude()), isSunrise)); - double processedTime = localMeanTime - getHoursFromMeridian(geoLocation.getLongitude()); - while (processedTime < 0.0) { - processedTime += 24.0; - } - while (processedTime >= 24.0) { - processedTime -= 24.0; - } - return processedTime; + double pocessedTime = localMeanTime - getHoursFromMeridian(geoLocation.getLongitude()); + return pocessedTime > 0 ? pocessedTime % 24 : pocessedTime % 24 + 24; // ensure that the time is >= 0 and < 24 } /** @@ -278,4 +284,25 @@ public double getUTCNoon(Calendar calendar, GeoLocation geoLocation) { } return noon; } + + /** + * Return the Universal Coordinated Time (UTC) + * of midnight for the given day at the given location on earth. This implementation returns solar midnight as 12 hours + * after utc noon that is halfway between sunrise and sunset. + * {@link NOAACalculator}, the default calculator, returns true solar noon. See The Definition of Chatzos for details on solar + * noon calculations. + * @see com.kosherjava.zmanim.util.AstronomicalCalculator#getUTCNoon(Calendar, GeoLocation) + * @see NOAACalculator + * + * @param calendar + * The Calendar representing the date to calculate solar noon for + * @param geoLocation + * The location information used for astronomical calculating sun times. + * @return the time in minutes from zero UTC. If an error was encountered in the calculation (expected behavior for + * some locations such as near the poles, {@link Double#NaN} will be returned. + */ + public double getUTCMidnight(Calendar calendar, GeoLocation geoLocation) { + return (getUTCNoon(calendar, geoLocation) + 12); + } }