Skip to content

Commit

Permalink
Remove unnecessary Calendar <-> Date conversions
Browse files Browse the repository at this point in the history
in Daf Yomi calculations
  • Loading branch information
KosherJava committed Sep 28, 2017
1 parent b2d555d commit bdc01da
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions src/net/sourceforge/zmanim/hebrewcalendar/YomiCalculator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Zmanim Java API
* Copyright (C) 2011 Eliyahu Hershfeld
* Copyright (C) 2011-2017 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)
Expand All @@ -16,23 +16,21 @@
package net.sourceforge.zmanim.hebrewcalendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* This class calculates the Daf Yomi page (daf) for a given date. The class currently only supports Daf Yomi Bavli, but
* may cover Yerushalmi, Mishna Yomis etc in the future.
*
* @author &copy; Bob Newell (original C code)
* @author &copy; Eliyahu Hershfeld 2011 - 2015
* @version 0.0.1
* @author &copy; Eliyahu Hershfeld 2011 - 2017
*/
public class YomiCalculator {

private static Date dafYomiStartDate = new GregorianCalendar(1923, Calendar.SEPTEMBER, 11).getTime();
private static int dafYomiJulianStartDay = getJulianDay(dafYomiStartDate);
private static Date shekalimChangeDate = new GregorianCalendar(1975, Calendar.JUNE, 24).getTime();
private static int shekalimJulianChangeDay = getJulianDay(shekalimChangeDate);
private static Calendar dafYomiStartDay = new GregorianCalendar(1923, Calendar.SEPTEMBER, 11);
private static int dafYomiJulianStartDay = getJulianDay(dafYomiStartDay);
private static Calendar shekalimChangeDay = new GregorianCalendar(1975, Calendar.JUNE, 24);
private static int shekalimJulianChangeDay = getJulianDay(shekalimChangeDay);

/**
* Returns the <a href="http://en.wikipedia.org/wiki/Daf_yomi">Daf Yomi</a> <a
Expand All @@ -49,33 +47,34 @@ public class YomiCalculator {
* cycle beginning on June 24, 1975 the length of the Yerushalmi shekalim was changed from 13 to 22 daf to follow
* the Vilna Shas that is in common use today.
*
* @param calendar
* the calendar date for calculation
* @param jewishCalendar
* The JewishCalendar date for calculation. TODO: this can be changed to use a regular GregorianCalendar since
* there is nothing specific to the JewishCalendar in this class.
* @return the {@link Daf}.
*
* @throws IllegalArgumentException
* if the date is prior to the September 11, 1923 start date of the first Daf Yomi cycle
*/
public static Daf getDafYomiBavli(JewishCalendar calendar) {
public static Daf getDafYomiBavli(JewishCalendar jewishCalendar) {
/*
* The number of daf per masechta. Since the number of blatt in Shekalim changed on the 8th Daf Yomi cycle
* beginning on June 24, 1975 from 13 to 22, the actual calculation for blattPerMasechta[4] will later be
* adjusted based on the cycle.
*/
int[] blattPerMasechta = { 64, 157, 105, 121, 22, 88, 56, 40, 35, 31, 32, 29, 27, 122, 112, 91, 66, 49, 90, 82,
119, 119, 176, 113, 24, 49, 76, 14, 120, 110, 142, 61, 34, 34, 28, 22, 4, 10, 4, 73 };
Date date = calendar.getGregorianCalendar().getTime();
Calendar calendar = jewishCalendar.getGregorianCalendar();

Daf dafYomi = null;
int julianDay = getJulianDay(date);
int julianDay = getJulianDay(calendar);
int cycleNo = 0;
int dafNo = 0;
if (date.before(dafYomiStartDate)) {
if (calendar.before(dafYomiStartDay)) {
// TODO: should we return a null or throw an IllegalArgumentException?
throw new IllegalArgumentException(date + " is prior to organized Daf Yomi Bavli cycles that started on "
+ dafYomiStartDate);
throw new IllegalArgumentException(calendar + " is prior to organized Daf Yomi Bavli cycles that started on "
+ dafYomiStartDay);
}
if (date.equals(shekalimChangeDate) || date.after(shekalimChangeDate)) {
if (calendar.equals(shekalimChangeDay) || calendar.after(shekalimChangeDay)) {
cycleNo = 8 + ((julianDay - shekalimJulianChangeDay) / 2711);
dafNo = ((julianDay - shekalimJulianChangeDay) % 2711);
} else {
Expand Down Expand Up @@ -116,15 +115,13 @@ public static Daf getDafYomiBavli(JewishCalendar calendar) {
}

/**
* Return the <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> from a Java Date.
* Return the <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> from a Java Calendar.
*
* @param date
* The Java Date
* @param calendar
* The Java Calendar of the date to be calculated
* @return the Julian day number corresponding to the date
*/
private static int getJulianDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
private static int getJulianDay(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
Expand All @@ -136,4 +133,4 @@ private static int getJulianDay(Date date) {
int b = 2 - a + a / 4;
return (int) (Math.floor(365.25 * (year + 4716)) + Math.floor(30.6001 * (month + 1)) + day + b - 1524.5);
}
}
}

0 comments on commit bdc01da

Please sign in to comment.