diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/danielsmith.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/danielsmith.xcuserdatad/UserInterfaceState.xcuserstate index 1d2e347..6054f3a 100644 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/danielsmith.xcuserdatad/UserInterfaceState.xcuserstate and b/.swiftpm/xcode/package.xcworkspace/xcuserdata/danielsmith.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/KosherSwift.podspec b/KosherSwift.podspec index 231aef7..fcdd1bf 100644 --- a/KosherSwift.podspec +++ b/KosherSwift.podspec @@ -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. diff --git a/Sources/KosherSwift/HebrewCalendar/JewishCalendar.swift b/Sources/KosherSwift/HebrewCalendar/JewishCalendar.swift index 0475990..d995d5d 100644 --- a/Sources/KosherSwift/HebrewCalendar/JewishCalendar.swift +++ b/Sources/KosherSwift/HebrewCalendar/JewishCalendar.swift @@ -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) @@ -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) } diff --git a/Sources/KosherSwift/HebrewCalendar/Molad.swift b/Sources/KosherSwift/HebrewCalendar/Molad.swift index 695eeee..7eb6ee9 100644 --- a/Sources/KosherSwift/HebrewCalendar/Molad.swift +++ b/Sources/KosherSwift/HebrewCalendar/Molad.swift @@ -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) @@ -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)! @@ -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) } } diff --git a/Tests/KosherSwiftTests/KosherSwiftTests.swift b/Tests/KosherSwiftTests/KosherSwiftTests.swift index c897a91..b74e639 100644 --- a/Tests/KosherSwiftTests/KosherSwiftTests.swift +++ b/Tests/KosherSwiftTests/KosherSwiftTests.swift @@ -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) }