Skip to content

Commit

Permalink
calendar constants
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 authored and Moshe Waisberg committed Jun 20, 2022
1 parent 4a15861 commit 0fc105b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,15 @@ public String formatMonth(JewishDate jewishDate) {
final int month = jewishDate.getJewishMonth();
if (isHebrewFormat()) {
if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
return hebrewMonths[13] + (useGershGershayim ? GERESH : ""); // return Adar I, not Adar in a leap year
return hebrewMonths[JewishDate.ADAR_II] + (useGershGershayim ? GERESH : ""); // return Adar I, not Adar in a leap year
} else if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR_II) {
return hebrewMonths[12] + (useGershGershayim ? GERESH : "");
return hebrewMonths[JewishDate.ADAR] + (useGershGershayim ? GERESH : "");
} else {
return hebrewMonths[month - 1];
}
} else {
if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
return transliteratedMonths[13]; // return Adar I, not Adar in a leap year
return transliteratedMonths[JewishDate.ADAR_II]; // return Adar I, not Adar in a leap year
} else {
return transliteratedMonths[month - 1];
}
Expand Down
71 changes: 35 additions & 36 deletions src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class JewishDate implements Comparable<JewishDate>, Cloneable {
/** The number of <em>chalakim</em> (1080) in an hour.*/
private static final int CHALAKIM_PER_HOUR = 1080;
/** The number of <em>chalakim</em> (25,920) in a 24 hour day .*/
private static final int CHALAKIM_PER_DAY = 25920; // 24 * 1080
private static final long CHALAKIM_PER_DAY = 25920; // 24 * 1080
/** The number of <em>chalakim</em> in an average Jewish month. A month has 29 days, 12 hours and 793
* <em>chalakim</em> (44 minutes and 3.3 seconds) for a total of 765,433 <em>chalakim</em>*/
private static final long CHALAKIM_PER_MONTH = 765433; // (29 * 24 + 12) * 1080 + 793
Expand Down Expand Up @@ -336,23 +336,23 @@ boolean isGregorianLeapYear(int year) {
* Returns the number of days in a given month in a given month and year.
*
* @param month
* the month. As with other cases in this class, this is 1-based, not zero-based.
* the month.
* @param year
* the year (only impacts February)
* @return the number of days in the month in the given year
*/
private static int getLastDayOfGregorianMonth(int month, int year) {
switch (month) {
case 2:
case Calendar.FEBRUARY:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
} else {
return 28;
}
case 4:
case 6:
case 9:
case 11:
case Calendar.APRIL:
case Calendar.JUNE:
case Calendar.SEPTEMBER:
case Calendar.NOVEMBER:
return 30;
default:
return 31;
Expand All @@ -365,11 +365,11 @@ private static int getLastDayOfGregorianMonth(int month, int year) {
*/
private void absDateToDate(int absDate) {
int year = absDate / 366; // Search forward year by year from approximate year
while (absDate >= gregorianDateToAbsDate(year + 1, 1, 1)) {
while (absDate >= gregorianDateToAbsDate(year + 1, Calendar.JANUARY, 1)) {
year++;
}

int month = 1; // Search forward month by month from January
int month = Calendar.JANUARY; // Search forward month by month from January
while (absDate > gregorianDateToAbsDate(year, month, getLastDayOfGregorianMonth(month, year))) {
month++;
}
Expand Down Expand Up @@ -401,14 +401,15 @@ public int getAbsDate() {
*/
private static int gregorianDateToAbsDate(int year, int month, int dayOfMonth) {
int absDate = dayOfMonth;
for (int m = month - 1; m > 0; m--) {
for (int m = Calendar.JANUARY; m < month; m++) {
absDate += getLastDayOfGregorianMonth(m, year); // days in prior months of the year
}
int yearPrevious = year - 1;
return (absDate // days this year
+ 365 * (year - 1) // days in previous years ignoring leap days
+ (year - 1) / 4 // Julian leap days before this year
- (year - 1) / 100 // minus prior century years
+ (year - 1) / 400); // plus prior years divisible by 400
+ 365 * yearPrevious // days in previous years ignoring leap days
+ yearPrevious / 4 // Julian leap days before this year
- yearPrevious / 100 // minus prior century years
+ yearPrevious / 400); // plus prior years divisible by 400
}

/**
Expand Down Expand Up @@ -460,8 +461,8 @@ private static int getLastMonthOfJewishYear(int year) {
*/
public static int getJewishCalendarElapsedDays(int year) {
long chalakimSince = getChalakimSinceMoladTohu(year, TISHREI);
int moladDay = (int) (chalakimSince / (long) CHALAKIM_PER_DAY);
int moladParts = (int) (chalakimSince - moladDay * (long) CHALAKIM_PER_DAY);
int moladDay = (int) (chalakimSince / CHALAKIM_PER_DAY);
int moladParts = (int) (chalakimSince - moladDay * CHALAKIM_PER_DAY);
// delay Rosh Hashana for the 4 dechiyos
return addDechiyos(year, moladDay, moladParts);
}
Expand Down Expand Up @@ -670,7 +671,7 @@ private static void validateGregorianDate(int year, int month, int dayOfMonth) {
* {@link GregorianCalendar}, where {@link Calendar#JANUARY} has a value of 0.
*/
private static void validateGregorianMonth(int month) {
if (month > 11 || month < 0) {
if (month > Calendar.DECEMBER || month < Calendar.JANUARY) {
throw new IllegalArgumentException("The Gregorian month has to be between 0 - 11. " + month
+ " is invalid.");
}
Expand Down Expand Up @@ -908,8 +909,8 @@ private static int moladToAbsDate(long chalakim) {
public JewishDate(long molad) {
absDateToDate(moladToAbsDate(molad));
// long chalakimSince = getChalakimSinceMoladTohu(year, TISHREI);// tishrei
int conjunctionDay = (int) (molad / (long) CHALAKIM_PER_DAY);
int conjunctionParts = (int) (molad - conjunctionDay * (long) CHALAKIM_PER_DAY);
long conjunctionDay = molad / CHALAKIM_PER_DAY;
int conjunctionParts = (int) (molad - conjunctionDay * CHALAKIM_PER_DAY);
setMoladTime(conjunctionParts);
}

Expand Down Expand Up @@ -1042,13 +1043,10 @@ public void setDate(Calendar calendar) {
throw new IllegalArgumentException("Calendars with a BC era are not supported. The year "
+ calendar.get(Calendar.YEAR) + " BC is invalid.");
}
gregorianMonth = calendar.get(Calendar.MONTH) + 1;
gregorianDayOfMonth = calendar.get(Calendar.DATE);
gregorianYear = calendar.get(Calendar.YEAR);
gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init the date
absDateToJewishDate();

dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DATE);
int year = calendar.get(Calendar.YEAR);
setInternalGregorianDate(year, month, dayOfMonth);
}

/**
Expand Down Expand Up @@ -1095,7 +1093,7 @@ public void setDate(LocalDate localDate) {
*/
public void setGregorianDate(int year, int month, int dayOfMonth) {
validateGregorianDate(year, month, dayOfMonth);
setInternalGregorianDate(year, month + 1, dayOfMonth);
setInternalGregorianDate(year, month, dayOfMonth);
}

/**
Expand All @@ -1110,15 +1108,16 @@ public void setGregorianDate(int year, int month, int dayOfMonth) {
*/
private void setInternalGregorianDate(int year, int month, int dayOfMonth) {
// make sure date is a valid date for the given month, if not, set to last day of month
if (dayOfMonth > getLastDayOfGregorianMonth(month, year)) {
dayOfMonth = getLastDayOfGregorianMonth(month, year);
int lastDayOfMonth = getLastDayOfGregorianMonth(month, year);
if (dayOfMonth > lastDayOfMonth) {
dayOfMonth = lastDayOfMonth;
}
// init month, date, year
gregorianMonth = month;
gregorianDayOfMonth = dayOfMonth;
gregorianYear = year;

gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init date
gregorianAbsDate = gregorianDateToAbsDate(year, month, dayOfMonth); // init date
absDateToJewishDate();

dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week
Expand Down Expand Up @@ -1267,9 +1266,9 @@ public void forward(int field, int amount) {
if (gregorianDayOfMonth == getLastDayOfGregorianMonth(gregorianMonth, gregorianYear)) {
gregorianDayOfMonth = 1;
// if last day of year
if (gregorianMonth == 12) {
if (gregorianMonth == Calendar.DECEMBER) {
gregorianYear++;
gregorianMonth = 1;
gregorianMonth = Calendar.JANUARY;
} else {
gregorianMonth++;
}
Expand Down Expand Up @@ -1357,8 +1356,8 @@ private void forwardJewishMonth(int amount) {
public void back() {
// Change Gregorian date
if (gregorianDayOfMonth == 1) { // if first day of month
if (gregorianMonth == 1) { // if first day of year
gregorianMonth = 12;
if (gregorianMonth == Calendar.JANUARY) { // if first day of year
gregorianMonth = Calendar.DECEMBER;
gregorianYear--;
} else {
gregorianMonth--;
Expand Down Expand Up @@ -1421,7 +1420,7 @@ public int compareTo(JewishDate jewishDate) {
* @return the Gregorian month (between 0-11). Like the java.util.Calendar, months are 0 based.
*/
public int getGregorianMonth() {
return gregorianMonth - 1;
return gregorianMonth;
}

/**
Expand Down Expand Up @@ -1491,7 +1490,7 @@ public int getDayOfWeek() {
*/
public void setGregorianMonth(int month) {
validateGregorianMonth(month);
setInternalGregorianDate(gregorianYear, month + 1, gregorianDayOfMonth);
setInternalGregorianDate(gregorianYear, month, gregorianDayOfMonth);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package com.kosherjava.zmanim.hebrewcalendar;

import static org.junit.Assert.assertEquals;

import org.junit.*;

import java.util.Calendar;
Expand All @@ -18,7 +20,6 @@ public class UT_DaysInGregorianMonth {

@Test
public void testDaysInMonth() {

JewishDate hebrewDate = new JewishDate();

Calendar cal = Calendar.getInstance();
Expand All @@ -33,7 +34,6 @@ public void testDaysInMonth() {

@Test
public void testDaysInMonthLeapYear() {

JewishDate hebrewDate = new JewishDate();

Calendar cal = Calendar.getInstance();
Expand All @@ -47,7 +47,6 @@ public void testDaysInMonthLeapYear() {

@Test
public void testDaysInMonth100Year() {

JewishDate hebrewDate = new JewishDate();

Calendar cal = Calendar.getInstance();
Expand All @@ -61,7 +60,6 @@ public void testDaysInMonth100Year() {

@Test
public void testDaysInMonth400Year() {

JewishDate hebrewDate = new JewishDate();

Calendar cal = Calendar.getInstance();
Expand All @@ -77,19 +75,18 @@ private void assertDaysInMonth(
boolean febIsLeap,
JewishDate hebrewDate
) {

Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(1));
Assert.assertEquals(febIsLeap ? 29 : 28, hebrewDate.getLastDayOfGregorianMonth(2));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(3));
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(4));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(5));
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(6));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(7));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(8));
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(9));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(10));
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(11));
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(12));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.JANUARY));
assertEquals(febIsLeap ? 29 : 28, hebrewDate.getLastDayOfGregorianMonth(Calendar.FEBRUARY));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.MARCH));
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.APRIL));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.MAY));
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.JUNE));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.JULY));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.AUGUST));
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.SEPTEMBER));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.OCTOBER));
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.NOVEMBER));
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.DECEMBER));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class UT_DaysInJewishMonth {

@Test
public void daysInMonthsInHaserYear() {

assertHaser(5773);
assertHaser(5777);
assertHaser(5781);
Expand All @@ -26,7 +25,6 @@ public void daysInMonthsInHaserYear() {

@Test
public void daysInMonthsInQesidrahYear() {

assertQesidrah(5769);
assertQesidrah(5772);
assertQesidrah(5778);
Expand All @@ -40,7 +38,6 @@ public void daysInMonthsInQesidrahYear() {

@Test
public void daysInMonthsInShalemYear() {

assertShalem(5770);
assertShalem(5780);
assertShalem(5783);
Expand Down Expand Up @@ -80,8 +77,8 @@ private void assertQesidrah(int year) {
JewishDate jewishDate = new JewishDate();
jewishDate.setJewishYear(year);

Assert.assertFalse(jewishDate.isCheshvanLong( ));
Assert.assertFalse(jewishDate.isKislevShort( ));
Assert.assertFalse(jewishDate.isCheshvanLong());
Assert.assertFalse(jewishDate.isKislevShort());
}


Expand All @@ -90,16 +87,16 @@ private void assertQesidrahLeap(int year) {
jewishDate.setJewishYear(year);

assertQesidrah(year);
Assert.assertTrue(jewishDate.isJewishLeapYear( ));
Assert.assertTrue(jewishDate.isJewishLeapYear());
}


private void assertShalem(int year) {
JewishDate jewishDate = new JewishDate();
jewishDate.setJewishYear(year);

Assert.assertTrue(jewishDate.isCheshvanLong( ));
Assert.assertFalse(jewishDate.isKislevShort( ));
Assert.assertTrue(jewishDate.isCheshvanLong());
Assert.assertFalse(jewishDate.isKislevShort());
}


Expand All @@ -108,7 +105,7 @@ private void assertShalemLeap(int year) {
jewishDate.setJewishYear(year);

assertShalem(year);
Assert.assertTrue(jewishDate.isJewishLeapYear( ));
Assert.assertTrue(jewishDate.isJewishLeapYear());
}

} // End of UT_DaysInJewishMonth class
Loading

0 comments on commit 0fc105b

Please sign in to comment.