Skip to content

Commit

Permalink
bug fix for Yomi Calculators showing the wrong dafs, bug fix for sett…
Browse files Browse the repository at this point in the history
…ing Jewish Year throwing an error on leap years
  • Loading branch information
Elyahu41 committed Feb 14, 2024
1 parent 5cd4ed8 commit 06104ea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Sources/KosherSwift/hebrewcalendar/JewishCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ public class JewishCalendar {
public func setJewishYear(year:Int) {
var hebrewCalendar = Calendar(identifier: .hebrew)
hebrewCalendar.timeZone = timeZone
workingDate = hebrewCalendar.date(bySetting: .year, value: year, of: workingDate)!
workingDate = hebrewCalendar.date(byAdding: .year, value: (year - getJewishYear()), to: workingDate)!
}

/**
Expand Down
27 changes: 8 additions & 19 deletions Sources/KosherSwift/hebrewcalendar/YerushalmiYomiCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public class YerushalmiYomiCalculator {
nextCycle.year = 1980
nextCycle.month = 2
nextCycle.day = 2

// let n = dateCreator.date(from: nextCycle)
// let p = dateCreator.date(from: prevCycle)

// Go cycle by cycle, until we get the next cycle
while jewishCalendar.workingDate.compare(dateCreator.date(from: nextCycle)!) == .orderedDescending {
Expand All @@ -76,7 +73,7 @@ public class YerushalmiYomiCalculator {
}

// Get the number of days from cycle start until request.
let dafNo = getDiffBetweenDays(start: dateCreator.date(from: prevCycle)!, end: jewishCalendar.workingDate.addingTimeInterval(-86400))// this should be a temporary solution. Not sure why the dates are one day off
let dafNo = getDiffBetweenDays(start: dateCreator.date(from: prevCycle)!, end: jewishCalendar.workingDate)

// Get the number of special day to subtract
let specialDays = getNumOfSpecialDays(startDate: dateCreator.date(from: prevCycle)!, endDate: jewishCalendar.workingDate)
Expand Down Expand Up @@ -114,28 +111,20 @@ public class YerushalmiYomiCalculator {

var specialDays = 0

let dateCreator = Calendar(identifier: .hebrew)

//create a hebrew calendar set to the date 7/10/5770
var yomKippurComponents = DateComponents()
yomKippurComponents.year = 5770
yomKippurComponents.month = 1
yomKippurComponents.day = 10
//create a jewish calendar set to the date Tishrei/10/5770
let yom_kippur = JewishCalendar(jewishYear: 5770, jewishMonth: JewishCalendar.TISHREI, jewishDayOfMonth: 10)

var tishaBeavComponents = DateComponents()
tishaBeavComponents.year = 5770
tishaBeavComponents.month = 5
tishaBeavComponents.day = 9
let tisha_beav = JewishCalendar(jewishYear: 5770, jewishMonth: JewishCalendar.AV, jewishDayOfMonth: 9)

while startYear <= endYear {
yomKippurComponents.year = startYear
tishaBeavComponents.year = startYear
yom_kippur.setJewishYear(year: startYear)
tisha_beav.setJewishYear(year: startYear)

if isBetween(start: startDate, date: dateCreator.date(from: yomKippurComponents)!, end: endDate) {
if isBetween(start: startDate, date: yom_kippur.workingDate, end: endDate) {
specialDays += 1
}

if isBetween(start: startDate, date: dateCreator.date(from: tishaBeavComponents)!, end: endDate) {
if isBetween(start: startDate, date: tisha_beav.workingDate, end: endDate) {
specialDays += 1
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/KosherSwift/hebrewcalendar/YomiCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class YomiCalculator {
*/
var 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, 9, 5, 73 ];
let calendar = jewishCalendar.workingDate.addingTimeInterval(-86400)//temp fix
let calendar = jewishCalendar.workingDate

var dafYomi: Daf? = nil;
let julianDay = getJulianDay(date: calendar);
Expand Down Expand Up @@ -134,9 +134,9 @@ public class YomiCalculator {
let a = year / 100;
let b = 2 - a + a / 4;
let c = floor(365.25 * (Double(year) + 4716))
let d = floor(30.6001 * Double(month))
let d = floor(30.6001 * Double(month + 1))
let e = Double(day) + Double(b) - 1524.5
return Int(c + d + e);
return Int(Double(c + d + e))
}

private static func gregorianDate(forYear year: Int, month: Int, andDay day: Int) -> Date? {
Expand Down
20 changes: 18 additions & 2 deletions Tests/KosherSwiftTests/KosherSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,28 @@ class KosherSwiftTests: XCTestCase {

func testDafYomis() {
jewishCalendar.setGregorianDate(year: 2023, month: 12, dayOfMonth: 21)
let dafYomi = jewishCalendar.getDafYomiBavli()
let dafYomiYeru = jewishCalendar.getDafYomiYerushalmi()
var dafYomi = jewishCalendar.getDafYomiBavli()
var dafYomiYeru = jewishCalendar.getDafYomiYerushalmi()
XCTAssertEqual(dafYomi?.getMasechta(), "בבא קמא")
XCTAssertEqual(dafYomi?.getDaf(), 49)
XCTAssertEqual(dafYomiYeru?.getYerushalmiMasechta(), "שבת")
XCTAssertEqual(dafYomiYeru?.getDaf(), 8)

jewishCalendar.setGregorianDate(year: 2024, month: 2, dayOfMonth: 4)
dafYomi = jewishCalendar.getDafYomiBavli()
dafYomiYeru = jewishCalendar.getDafYomiYerushalmi()
XCTAssertEqual(dafYomi?.getMasechta(), "בבא קמא")
XCTAssertEqual(dafYomi?.getDaf(), 94)
XCTAssertEqual(dafYomiYeru?.getYerushalmiMasechta(), "שבת")
XCTAssertEqual(dafYomiYeru?.getDaf(), 53)

jewishCalendar.setGregorianDate(year: 2020, month: 6, dayOfMonth: 18)
dafYomi = jewishCalendar.getDafYomiBavli()
dafYomiYeru = jewishCalendar.getDafYomiYerushalmi()
XCTAssertEqual(dafYomi?.getMasechta(), "שבת")
XCTAssertEqual(dafYomi?.getDaf(), 104)
XCTAssertEqual(dafYomiYeru?.getYerushalmiMasechta(), "יומא")
XCTAssertEqual(dafYomiYeru?.getDaf(), 17)
}

func testCalculatorSunrise() throws {
Expand Down

0 comments on commit 06104ea

Please sign in to comment.