Skip to content

Commit 8097eca

Browse files
authored
Fix JewishCalendar.getMoladAsInstant()
1 parent 61ae480 commit 8097eca

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishCalendar.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import java.time.Duration;
2121
import java.time.Instant;
2222
import java.time.LocalDate;
23-
import java.time.ZoneOffset;
23+
import java.time.ZoneId;
2424
import java.time.ZonedDateTime;
25-
import java.time.temporal.ChronoUnit;
2625
import java.util.Calendar;
2726

2827
/**
@@ -1234,38 +1233,34 @@ public boolean isTishaBav() {
12341233
public Instant getMoladAsInstant() {
12351234
JewishDate molad = getMolad();
12361235

1237-
// Har Habayis coordinates
1238-
//double latitude = 31.778;
1239-
double longitude = 35.2354;
1240-
12411236
// Standard time offset for Jerusalem: GMT+2
1242-
ZoneOffset jerusalemStandardOffset = ZoneOffset.ofHours(2);
1237+
// The raw molad Date (point in time) must be generated using standard time. Using "Asia/Jerusalem" timezone will
1238+
// result in the time being incorrectly off by an hour in the summer due to DST. Proper adjustment for the actual
1239+
// time in DST will be done by the date formatter class used to display the Date.
1240+
ZoneId jerusalemStandardOffset = ZoneId.of("GMT+2");
12431241

1244-
// Compute molad seconds from chalakim
1245-
double moladSeconds = molad.getMoladChalakim() * 10.0 / 3.0;
1242+
double moladSeconds = molad.getMoladChalakim() * 10.0 / 3.0; // Compute molad seconds from chalakim
12461243
int seconds = (int) moladSeconds;
1247-
int millis = (int) ((moladSeconds - seconds) * 1000);
1244+
int nanos = (int) ((moladSeconds - seconds) * 1_000_000_000); // convert remainder to nanos
12481245

1249-
// Construct ZonedDateTime in standard time (GMT+2)
12501246
ZonedDateTime moladZdt = ZonedDateTime.of(
12511247
molad.getGregorianYear(),
12521248
molad.getGregorianMonth() +1, // 1-based FIXME
12531249
molad.getGregorianDayOfMonth(),
12541250
molad.getMoladHours(),
12551251
molad.getMoladMinutes(),
12561252
seconds,
1257-
millis * 1_000_000, // nanos
1253+
nanos,
12581254
jerusalemStandardOffset
12591255
);
12601256

1261-
// Compute local mean time offset in milliseconds (example: 20.94 minutes = 1256400 ms)
1262-
long localMeanOffsetMillis = (long) ((longitude - 35.0) * 4 * 60 * 1000);
1263-
1264-
// Apply offset using ChronoUnit.MILLIS
1265-
moladZdt = moladZdt.minus(localMeanOffsetMillis, ChronoUnit.MILLIS);
1257+
// Har Habayis at a longitude of 35.2354 offset vs longitude 35 in standard time, so we subtract the time difference
1258+
// of 20.94 minutes (20 minutes and 56 seconds and 496 millis) to get to Standard time from local mean time
1259+
Duration jerusalemStandardTimeOffset = Duration.ofMinutes(20)
1260+
.plusSeconds(56)
1261+
.plusMillis(496);
12661262

1267-
// Return precise Instant
1268-
return moladZdt.toInstant();
1263+
return moladZdt.toInstant().minus(jerusalemStandardTimeOffset);
12691264
}
12701265

12711266
/**

0 commit comments

Comments
 (0)