|
| 1 | +package com.kosherjava.zmanim.hebrewcalendar; |
| 2 | + |
| 3 | +import static com.kosherjava.zmanim.AstronomicalCalendar.GEOMETRIC_ZENITH; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | + |
| 8 | +import com.kosherjava.zmanim.ComplexZmanimCalendar; |
| 9 | +import com.kosherjava.zmanim.util.AstronomicalCalculator; |
| 10 | +import com.kosherjava.zmanim.util.GeoLocation; |
| 11 | +import com.kosherjava.zmanim.util.NOAACalculator; |
| 12 | +import com.kosherjava.zmanim.util.SunTimesCalculator; |
| 13 | + |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import java.io.BufferedReader; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.util.Calendar; |
| 20 | +import java.util.Date; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.TimeZone; |
| 24 | + |
| 25 | +public class ZmanimUnitTests { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void sofZmanKidushLevana_Jerusalem() { |
| 29 | + // Jerusalem, Israel, 2019-May |
| 30 | + TimeZone tz = TimeZone.getTimeZone("Asia/Jerusalem"); |
| 31 | + Calendar cal = Calendar.getInstance(tz); |
| 32 | + cal.set(2019, Calendar.MAY, 1); |
| 33 | + GeoLocation location = new GeoLocation("test", 31.76904, 35.21633, tz); |
| 34 | + ComplexZmanimCalendar zcal = new ComplexZmanimCalendar(location); |
| 35 | + zcal.setUseElevation(true); |
| 36 | + zcal.setCalendar(cal); |
| 37 | + Date date = zcal.getSofZmanKidushLevanaBetweenMoldos(); |
| 38 | + assertNull(date); |
| 39 | + |
| 40 | + cal.set(2019, Calendar.MAY, 19); |
| 41 | + zcal.setCalendar(cal); |
| 42 | + date = zcal.getSofZmanKidushLevanaBetweenMoldos(); |
| 43 | + assertNotNull(date); |
| 44 | + assertEquals(1558246265170L, date.getTime()); |
| 45 | + cal.setTime(date); |
| 46 | + assertEquals(cal.get(Calendar.YEAR), 2019); |
| 47 | + assertEquals(cal.get(Calendar.MONTH), Calendar.MAY); |
| 48 | + assertEquals(cal.get(Calendar.DAY_OF_MONTH), 19); |
| 49 | + assertEquals(cal.get(Calendar.HOUR_OF_DAY), 9); |
| 50 | + assertEquals(cal.get(Calendar.MINUTE), 11); |
| 51 | + assertEquals(cal.get(Calendar.SECOND), 5); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void sofZmanKidushLevana_NewJersey() { |
| 56 | + // Teaneck, NJ, USA, 2019-May |
| 57 | + TimeZone tz = TimeZone.getTimeZone("EDT"); |
| 58 | + Calendar cal = Calendar.getInstance(tz); |
| 59 | + cal.set(2019, Calendar.MAY, 1); |
| 60 | + GeoLocation location = new GeoLocation("test", 40.896027, -74.0128627, tz); |
| 61 | + ComplexZmanimCalendar zcal = new ComplexZmanimCalendar(location); |
| 62 | + zcal.setUseElevation(true); |
| 63 | + zcal.setCalendar(cal); |
| 64 | + Date date = zcal.getSofZmanKidushLevanaBetweenMoldos(); |
| 65 | + assertNull(date); |
| 66 | + |
| 67 | + cal.set(2019, Calendar.MAY, 19); |
| 68 | + zcal.setCalendar(cal); |
| 69 | + date = zcal.getSofZmanKidushLevanaBetweenMoldos(); |
| 70 | + assertNotNull(date); |
| 71 | + assertEquals(1558246265170L, date.getTime()); |
| 72 | + cal.setTime(date); |
| 73 | + assertEquals(cal.get(Calendar.YEAR), 2019); |
| 74 | + assertEquals(cal.get(Calendar.MONTH), Calendar.MAY); |
| 75 | + assertEquals(cal.get(Calendar.DAY_OF_MONTH), 19); |
| 76 | + assertEquals(cal.get(Calendar.HOUR_OF_DAY), 6); |
| 77 | + assertEquals(cal.get(Calendar.MINUTE), 11); |
| 78 | + assertEquals(cal.get(Calendar.SECOND), 5); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void dawn_NewYork() throws Exception { |
| 83 | + dawn_NewYork(new SunTimesCalculator(), "dawn_new_york_sun.csv"); |
| 84 | + dawn_NewYork(new NOAACalculator(), "dawn_new_york_noa.csv"); |
| 85 | + } |
| 86 | + |
| 87 | + private void dawn_NewYork(AstronomicalCalculator calculator, String name) throws Exception { |
| 88 | + Class<?> clazz = calculator.getClass(); |
| 89 | + InputStream res = clazz.getResourceAsStream("/" + name); |
| 90 | + BufferedReader reader = new BufferedReader(new InputStreamReader(res)); |
| 91 | + String line = reader.readLine(); |
| 92 | + Map<Integer, Map<Integer, Long>> dates = new HashMap<>(); |
| 93 | + Map<Integer, Long> m; |
| 94 | + |
| 95 | + while (line != null) { |
| 96 | + if (line.isEmpty()) continue; |
| 97 | + String[] tokens = line.split(","); |
| 98 | + int o = Integer.parseInt(tokens[0]); |
| 99 | + int d = Integer.parseInt(tokens[1]); |
| 100 | + long t = Long.parseLong(tokens[2]); |
| 101 | + m = dates.get(o); |
| 102 | + if (m == null) { |
| 103 | + m = new HashMap<>(); |
| 104 | + dates.put(o, m); |
| 105 | + } |
| 106 | + m.put(d, t); |
| 107 | + line = reader.readLine(); |
| 108 | + } |
| 109 | + |
| 110 | + // New York, USA, 2021-June |
| 111 | + TimeZone tz = TimeZone.getTimeZone("America/New_York"); |
| 112 | + Calendar cal = Calendar.getInstance(tz); |
| 113 | + GeoLocation location = new GeoLocation("test", 40.7, -74.0, 10.0, tz); |
| 114 | + ComplexZmanimCalendar zcal = new ComplexZmanimCalendar(location); |
| 115 | + zcal.setAstronomicalCalculator(calculator); |
| 116 | + zcal.setUseElevation(true); |
| 117 | + |
| 118 | + Date date; |
| 119 | + Long dateExpected; |
| 120 | + double offsetZenith; |
| 121 | + cal.set(2021, Calendar.JUNE, 1); |
| 122 | + |
| 123 | + for (int o = 0; o <= 30; o++) { |
| 124 | + offsetZenith = GEOMETRIC_ZENITH + o; |
| 125 | + m = dates.get(o); |
| 126 | + for (int d = 1; d <= 366; d++) { |
| 127 | + cal.set(Calendar.DAY_OF_YEAR, d); |
| 128 | + zcal.setCalendar(cal); |
| 129 | + date = zcal.getSunriseOffsetByDegrees(offsetZenith); |
| 130 | + dateExpected = m.get(d); |
| 131 | + if (dateExpected != null) { |
| 132 | + assertNotNull("day=" + d + " zenith=" + offsetZenith, date); |
| 133 | + assertEquals(dateExpected.longValue(), date.getTime()); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments