Skip to content

Commit

Permalink
made molad safer
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Smith committed Apr 4, 2024
1 parent 9dce266 commit 49f84c1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion KosherSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KosherSwift'
s.version = '2.1.13'
s.version = '2.1.15'
s.summary = 'A Swift library for getting zmanim.'

# This description is used to generate tags and improve search results.
Expand Down
10 changes: 5 additions & 5 deletions Sources/KosherSwift/HebrewCalendar/JewishCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public class JewishCalendar: JewishDate {
public let isInIsrael: Bool
public let moladDate: MoladDate
public let moladDate: MoladDate?

public convenience init(withJewishYear year: Int, andMonth month: JewishMonth, andDay day: Int, isInIsrael: Bool = false) {
var hebCal = Calendar(identifier: .hebrew)
Expand Down Expand Up @@ -319,10 +319,10 @@ public class JewishCalendar: JewishDate {
return nil
}

public var earliestKiddushLevana3Days: Date? { moladDate.gregDate.withAdded(days: 3) }
public var earliestKiddushLevana7Days: Date? { moladDate.gregDate.withAdded(days: 7) }
public var latestZmanKidushLevanaBetweenMoldos: Date? { moladDate.gregDate.withAdded(days: 14, hours: 18, minutes: 22, seconds: 1, milliseconds: 666) }
public var latestKiddushLevana15Days: Date? { moladDate.gregDate.withAdded(days: 15) }
public var earliestKiddushLevana3Days: Date? { moladDate?.gregDate.withAdded(days: 3) }
public var earliestKiddushLevana7Days: Date? { moladDate?.gregDate.withAdded(days: 7) }
public var latestZmanKidushLevanaBetweenMoldos: Date? { moladDate?.gregDate.withAdded(days: 14, hours: 18, minutes: 22, seconds: 1, milliseconds: 666) }
public var latestKiddushLevana15Days: Date? { moladDate?.gregDate.withAdded(days: 15) }

public var dafYomiBavli: Daf? { DafYomiCalculator.getDafYomiBavli(cal: self) }
public var dafYomiYerushalmi: Daf? { DafYomiCalculator.getDafYomiYerushalmi(cal: self) }
Expand Down
36 changes: 12 additions & 24 deletions Sources/KosherSwift/HebrewCalendar/Molad.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MoladDate: JewishDate {
MoladDate(date: gregDate, hour: hours, minute: molad.minutes, chalakim: molad.chalakim)
}

static func calculate(forJewishDate jewishDate: JewishDate) -> MoladDate {
static func calculate(forJewishDate jewishDate: JewishDate) -> MoladDate? {
let m = getChalakimSinceMoladTohu(year: jewishDate.year, month: jewishDate.month)

let absM = moladToAbsDate(chalakim: m)
Expand All @@ -46,7 +46,7 @@ public class MoladDate: JewishDate {
let hours = (temp.hours + 18) % 24
let retMolad = Molad(hours: hours, minutes: temp.minutes, chalakim: temp.chalakim)

var dt = getMoladAsDate(retMolad, gdate)
guard var dt = getMoladAsDate(retMolad, gdate) else { return nil }

if temp.hours >= 6 {
dt = dt.withAdded(days: 1)!
Expand All @@ -68,28 +68,16 @@ public class MoladDate: JewishDate {
}


static func getMoladAsDate(_ molad: Molad, _ mdate: Date) -> Date {
let locationName = "Jerusalem, Israel"
let latitude = 31.778 // Har Habayis latitude
let longitude = 35.2354 // Har Habayis longitude
// The raw molad Date (point in time) must be generated using standard time. Using "Asia/Jerusalem" timezone will result in the time
// being incorrectly off by an hour in the summer due to DST. Proper adjustment for the actual time in DST will be done by the date
// formatter class used to display the Date.
let year = String(Calendar.current.component(.year, from: mdate))
let month = String(format: "%02d", Calendar.current.component(.month, from: mdate))
let day = String(format: "%02d", Calendar.current.component(.day, from: mdate))
let hour = String(format: "%02d", Calendar.current.component(.hour, from: mdate))
let minute = String(format: "%02d", Calendar.current.component(.minute, from: mdate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let dateTime = dateFormatter.date(from: "\(year)-\(month)-\(day) \(hour):\(minute)")!
let geo = GeoLocation(lat: latitude, lng: longitude, name: locationName)
let moladSeconds = Double(molad.chalakim) * 10 / 3
var cal = Calendar.current.date(bySettingHour: molad.hours, minute: molad.minutes, second: Int(moladSeconds), of: dateTime)!

// subtract local time difference of 20.94 minutes (20 minutes and 56.496 seconds) to get to Standard time
cal.addTimeInterval(-1 * geo.localMeanTimeOffsetWithMillis * 0.001)
return cal
static func getMoladAsDate(_ molad: Molad, _ mdate: Date) -> Date? {
let moladHours = (molad.hours + 18) % 24
let moladMinutes = molad.minutes - 20
let moladSeconds = (Double(molad.chalakim) * 10 / 3 ) - 56.496

var calendar = Calendar.init(identifier: .gregorian)
calendar.timeZone = Calendar.current.timeZone
let moladDay = DateComponents(calendar: calendar, year: mdate.year, month: mdate.month, day: mdate.day, hour: moladHours, minute: moladMinutes, second: Int(moladSeconds) - 1)

return calendar.date(from: moladDay)
}
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/KosherSwiftTests/KosherSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ final class KosherSwiftTests: XCTestCase {

func testMolad() {
let jewishCalendar = JewishCalendar(date: Date(year: 2023, month: 12, day: 20))
XCTAssertEqual(jewishCalendar.moladDate.molad.hours, 20)
XCTAssertEqual(jewishCalendar.moladDate.molad.minutes, 1)
XCTAssertEqual(jewishCalendar.moladDate.molad.chalakim, 3)
XCTAssertEqual(jewishCalendar.moladDate?.molad.hours, 20)
XCTAssertEqual(jewishCalendar.moladDate?.molad.minutes, 1)
XCTAssertEqual(jewishCalendar.moladDate?.molad.chalakim, 3)

let moladFromKosherJava = Date(timeIntervalSince1970: 1702402813.0) //had to go up or down a few intervals to make it work
let g = jewishCalendar.moladDate.gregDate
XCTAssertEqual(g.withAdded(hours: !Calendar.current.timeZone.isDaylightSavingTime(for: g) ? -1 : 0)!, moladFromKosherJava)
let g = jewishCalendar.moladDate?.gregDate
XCTAssertEqual(g!.withAdded(hours: !Calendar.current.timeZone.isDaylightSavingTime(for: g!) ? -1 : 0)!, moladFromKosherJava)
}


Expand Down

0 comments on commit 49f84c1

Please sign in to comment.