- Notifications
You must be signed in to change notification settings - Fork2
KosherJava Zmanim API / Library ported to Swift. KosherJava is a library used to calculate astronomical and religious dates and times based on your location.
License
Elyahu41/KosherSwift
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This Zmanim library is an API for a specialized calendar that can calculate different astronomicaltimes including sunrise and sunset and Jewishzmanim or religious times for prayers and otherJewish religious duties.
For non religious astronomical / solarcalculations use theAstronomicalCalendar.
The ZmanimCalendar contains the most common zmanim or religious time calculations. For a much moreextensive list ofzmanim use the ComplexZmanimCalendar.This class contains the main functionality of the Zmanim library.
For a basic set of instructions on the use of the API, seeHow to Use the Zmanim API,zmanim code samples and theKosherJava FAQ. See theKosherJava Zmanim site for additional information.
Add KosherSwift as a dependency to your project:
Copy this line:
https://github.com/Elyahu41/KosherSwift.git
Go toPackage Dependencies in Xcode and search for this repository and add it!
If you want to use CocoaPods add this line to your podfile:
pod"KosherSwiftNew"
KosherSwift was already taken on CocoaPods, hence the "New"
Once you have added this project, you can go to any file/viewcontroller and import the framework:
import KosherSwift
If you used CocoaPods:
import KosherSwiftNew
Then, it is as simple as instantiating a class and calling it's methods:
letjewishCalendar=JewishCalendar() // by default it is set to today's dateprint(jewishCalendar.getJewishMonth()) // This will print the jewish month as a number, so Nissan will be 8. See the JewishCalendar class for more details// To check for holidays/yom tovs, you can use the built in methods of the jewish calendar:if jewishCalendar.isPesach(){print("Today is Passover!")}// or you can check it yourself with the getYomTovIndex() method just like in KosherJava:if jewishCalendar.getYomTovIndex()==JewishCalendar.PESACH{print("Today is Passover!")}
For sunrise and sunset times, you will need a GeoLocation object. Then, you will need to pass that into the AstronomicalCalendar class like so:
letgeoLocation=GeoLocation(locationName:"Lakewood, NJ", latitude:40.08213, longitude:-74.20970, timeZone:TimeZone(identifier:"America/New_York")!)// The locationName string can be left empty if you'd like. There is also a constructor for elevation to be added.// There are two Sunrise/Sunset calculators in this API, the NOAA Calculator and the SunTimes Calculator. By default, the NOAA calculator is used as it is more accurate than the SunTimes calculator as it takes into account leap years and other things.letcal=AstronomicalCalendar(location: geoLocation)// To use the SunTimesCalculator:// cal.astronomicalCalculator = SunTimesCalculator()letsunrise= cal.getSunrise() // This will return a nullable Date? object
For zmanim, you will do the same thing, just use the ComplexZmanimCalendar or ZmanimCalendar classes:
letgeoLocation=GeoLocation(locationName:"Lakewood, NJ", latitude:40.08213, longitude:-74.20970, timeZone:TimeZone(identifier:"America/New_York")!)letcal=ComplexZmanimCalendar(location: geoLocation)letsofZmanShmaGra= cal.getSofZmanShmaGRA() // This will return a nullable Date? object
If you want to calculate a zman for a certain date:
vargregorianCalendar=Calendar(identifier:.gregorian)gregorianCalendar.timeZone=TimeZone(identifier:"America/New_York")! // It is important to set the Timezone as the date can change in SwiftletgeoLocation=GeoLocation(locationName:"", latitude:40.08213, longitude:-74.20970, timeZone:TimeZone(identifier:"America/New_York")!)letcalendar=ComplexZmanimCalendar(geoLocation: geoLocation) // Timezone is kept track of inside these classes with the geolocationvarjanuaryFirst=DateComponents()januaryFirst.year=2023januaryFirst.month=1januaryFirst.day=1 calendar.workingDate= gregorianCalendar.date(from: januaryFirst)!varsunrise= calendar.getSunrise()
For converting any of the data in this API to hebrew, look at the HebrewDateFormatter class:
letjewishCalendar=JewishCalendar() // by default it is set to today's datelethebrewDateFormatter=HebrewDateFormatter()print(hebrewDateFormatter.formatMonth(jewishCalendar: jewishCalendar)) // prints "Nissan"hebrewDateFormatter.hebrewFormat=trueprint(hebrewDateFormatter.formatMonth(jewishCalendar: jewishCalendar)) // prints "ניסן"
Lastly, look at the TefilaRules class for methods that use the JewishCalendar to tell you which prayers to say:
letjewishCalendar=JewishCalendar()lettefilaRules=TefilaRules()tefilaRules.isVeseinTalUmatarRecited(jewishCalendar: jewishCalendar) // returns true or false depending on the DAY of the year
If you want to add onto what is already available in KosherSwift, Swift has a powerful feature called extensions that lets you do just that!For example, if you wanted to make a method that gives you a jewishCalendar object set to the first day of the jewish month, you could do this:
publicextensionJewishCalendar{func getFirstDayOfJewishMonth(jewishCalendar:JewishCalendar)->JewishCalendar{letfirstDayOfMonth= jewishCalendar jewishCalendar.setJewishDayOfMonth(dayOfMonth:1)return firstDayOfMonth}}
The same goes for zmanim! If you want to create your own zmanim, it would look like this:
publicextensionComplexZmanimCalendar{ // for regular minutes (note the need to convert to milliseconds)func getTzais45()->Date?{returnComplexZmanimCalendar.getTimeOffset(time:getElevationAdjustedSunset(), offset:45* ComplexZmanimCalendar.MINUTE_MILLIS);} // for degreesfunc getTzais8PointSevenDegrees()->Date?{returngetSunsetOffsetByDegrees(offsetZenith:ComplexZmanimCalendar.GEOMETRIC_ZENITH+8.7);} // for seasonal timesfunc getTwoSeasonalHoursIntoTheDay()->Date?{letseasonalHour=getTemporalHour() // it is preferred to use getTimeOffset because it will handle nil valuesreturnAstronomicalCalendar.getTimeOffset(time:getElevationAdjustedSunrise(), offset: seasonalHour*2)}}
KosherSwift is very similar to KosherJava as it has brought over almost all of it's methods to Swift. I have tried to keep the code as similar as possible to the original repository, because I personally wanted to bring over my Java code from my android project to iOS without having to refactor almost all of the code. However, as much as I tried to keep them similar, there are still big differences in the inner workings of the classes. For starters, the Calendar class in Swift does not contain a time in milliseconds to keep track of time, it only contains functions that can create a date. So everywhere there would be a need to use a Calendar has been replaced with a Date. If you look inside the code of the classes, there is a workingDate variable that can be changed instead of a calendar class.
Timezone issues are a big deal in Swift since Date objects are set to UTC by default and only afterwards are timezones taken into account, this leads to the date changing if the timezone for the Calendar class in Swift is not set to the same as timezone as the system. All objects that have a GeoLocation object take this into account, however, if you are using the JewishCalendar class in another timezone, use the JewishCalendar(workingDate: , timezone: ) constructor.
Otherwise, most methods have been tried and tested, however, if you come accross a method that is not working correctly, please create a issue or (more preferably) a pull request.
The library is released under theLGPL 2.1 license.
The KosherJava Zmanim API has been ported to:
- Java (original repo) -https://github.com/KosherJava/zmanim/
- Swift -https://github.com/Elyahu41/KosherSwift (You are here)
- Objective-C -https://github.com/MosheBerman/KosherCocoa
- .NET -https://github.com/Yitzchok/Zmanim
- TypeScript -https://github.com/BehindTheMath/KosherZmanim
- JavaScript -https://github.com/yparitcher/zmanJS
- Kotlin -https://github.com/Sternbach-Software/KosherKotlin
- Ruby -https://github.com/pinnymz/ruby-zmanim
- Scala -https://github.com/nafg/jewish-date
- C -https://github.com/yparitcher/libzmanim
- Python -https://github.com/pinnymz/python-zmanim &https://pypi.org/project/zmanim/
- PHP -https://github.com/zachweix/PhpZmanim/
- Dart / Flutter -https://github.com/yakir8/kosher_dart
- Go -https://github.com/vlipovetskii/go-zmanim
The .NET port was used to create a desktop app that is available athttps://github.com/NykUser/MyZman.
While I did my best to get accurate results, please double check before relying on these zmanim forhalacha lemaaseh.
About
KosherJava Zmanim API / Library ported to Swift. KosherJava is a library used to calculate astronomical and religious dates and times based on your location.