Update: The KosherCocoa 2.0 code described in this post now lives in the koshercocoa-legacy repository. The current KosherCocoa (3.x, available via CocoaPods) was later rewritten, so the sample below is historical code from 2011 (it predates ARC) and has not been tested against the current version.
Moshe Berman completed the 2.0 release of his port of the KosherJava Zmanim API from Java to a Cocoa API using Objective-C. You can see the work in the KosherCocoa project page. The original work on the port dates back to Moshe’s iPhone Ultimate Omer 2 (App Store link) app. In that app, he ported the minimum amount of code needed to calculate sunset in order to roll the day of the Omer after sunset. With Moshe’s latest check-in at GitHub, he provided a port of a good portion of the API, sticking to the basic design of the KosherJava API. The ComplexZmanimCalendar still remains to be ported, but the majority of zmanim in common use are in the ported ZmanimCalendar. Moshe’s blog post Introducing KosherCocoa 2.0 has additional details. This port will be a boon to iOS developers who now have a simple way to include zmanim in their iPhone and iPad apps.
Here is some sample code that outputs zmanim to the console.
Zmanim.m
#import <Foundation/Foundation.h>
#import "ZmanimCalendar.h"
int main (int argc, const char * argv[]){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Set up the location
NSString *locationName = @"Lakewood, NJ";
double latitude = 40.096; //Lakewood, NJ
double longitude = -74.222; //Lakewood, NJ
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-18000];
// Initialize the Zmanim Calendar
GeoLocation *geoLocation = [[GeoLocation alloc] initWithName:locationName andLatitude:latitude andLongitude:longitude andTimeZone:timeZone];
ZmanimCalendar *zmanimCalendar = [[ZmanimCalendar alloc] initWithLocation:geoLocation];
// Create a Formatter for the date information
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:timeZone];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSDate *sunrise = [zmanimCalendar sunrise];
NSDate *sofZmanShmaMGA = [zmanimCalendar sofZmanShmaMogenAvraham];
NSDate *sofZmanShmaGRA = [zmanimCalendar sofZmanShmaGra];
NSDate *sunset = [zmanimCalendar sunset];
NSLog(@"Today's Zmanim for %@", [geoLocation locationName]);
NSLog(@"Sunrise: %@", [formatter stringFromDate:sunrise]);
NSLog(@"Sof Zman Shema MGA: %@", [formatter stringFromDate:sofZmanShmaMGA]);
NSLog(@"Sof Zman Shema GRA: %@", [formatter stringFromDate:sofZmanShmaGRA]);
NSLog(@"Sunset: %@", [formatter stringFromDate:sunset]);
[pool drain];
return 0;
}
This would output (on February 8th):
Today's Zmanim for Lakewood, NJ
Sunrise: 6:58:43 AM
Sof Zman Shema MGA: 8:59:04 AM
Sof Zman Shema GRA: 9:35:04 AM
Sunset: 5:24:09 PM
Note: The code sample in this post has been updated for the KosherJava Zmanim 3.x API, where these are instance methods of the JewishCalendar that return a Java Instant.
Calculating the earliest and latest times for קידוש לבנה Kiddush Levana has not been part of the KosherJava Zmanim API until now. This is because unlike other zmanim that solely rely on solar calculations that are tied to the Gregorian calendar, times for Kiddush Levana depend on the Jewish calendar molad (lunar conjunction) computation. With the recent addition of Jewish calendar support to the alpha releases of the KosherJava Zmanim API 1.3, molad calculation was added, allowing for calculation of Kiddush Levana times. Times include the earliest time calculated as 3 and 7 days after the molad. Sof zman Kiddush Levana includes the מהרי״ל Maharil’s opinion in שו״ת מהרי״ל ס׳ י״ט She’elos Utshuvos Maharil no. 19 that it is calculated as halfway between molad and molad
הטעם הוא משום דאמר בפרק היו בודקין עד שתתמלא פגימתו … וא״כ במילוי תליא מילתא ולאו דווקא ט״ו וי״ו אלא חצי כ״ט י״ב תשצ״ג
and the more lenient full 15 days from the molad mentioned by the Mechaber in the Shulchan Aruch. It should be noted that some opinions hold that the Rema, who brings down the Maharil’s opinion of calculating halfway between molad and molad, is of the opinion that the Mechaber agrees with him. Also see the Aruch Hashulchan. For additional details on the subject, see Rabbi Dovid Heber’s very detailed writeup in Siman Daled (chapter 4) of Shaarei Zemanim.
Calculating the Molad
Kiddush Levana times depend on the time of the molad. The time of the molad announced in shuls on Shabbos Mevarchim is the time of the Molad Emtzai (Average Molad) in Yerushalayim local mean time. This has to be converted to standard time. Standard time uses time zones to unify clock times across a large area. With 360° of longitude around the globe, the world is divided into 24 time zones (one per hour), resulting in time zones that are 15° of longitude each. Har Habayis, with a longitude of 35.2354°, is 5.2354° away from the 30° longitude line. Multiply the 5.2354° by 4 minutes per degree (15° of longitude per hour) to reach 20.9416 minutes, or 20 minutes and 56.496 seconds (5.2354 × 4 = 20.9416). This time is subtracted from the local molad time to arrive at standard time. Since the time of the molad is at the same instant globally (unlike zmanim such as sunrise that depend on a person’s location), converting this to a user’s local time involves simply calculating the time difference between the time in Yerushalayim and your location. If daylight saving time is in use, this has to be added to the calculation. The API returns the molad as a Java Instant, and Java’s DateTimeFormatter does this conversion to any time zone without forcing the developer to do any calculations.
Calculating the Start and End of Kiddush Levana Times
The JewishCalendar class contains the methods for calculating these zmanim. Calculating tchilas zman Kiddush Levana (the earliest time Kiddush Levana can be said) is done by adding 3 days or 7 days to the molad time. Sof zman Kiddush Levana (the latest time Kiddush Levana can be said) is either the time between molad and molad, calculated by adding 14 days, 18 hours, 22 minutes and 1.666 seconds to the molad (half the 29 days, 12 hours, 44 minutes and 1 chelek (3.333 seconds)), or by adding 15 days to the molad.
Using the Zmanim API to Calculate Molad Based Times
Here is sample code for calculating various Kiddush Levana times for anywhere in the world for Shevat 5729 (1969). Since formatting an Instant requires a time zone, the simple code below formats the times in your computer’s default time zone. If you want the times for a different time zone, replace ZoneId.systemDefault() with the time zone you wish to display the times for, such as ZoneId.of("America/New_York").
This will output the following in the US Eastern time zone:
Tchilas Zman Kiddush Levana 3 Days: Jan 21, 1969 at 06:06:30 EST
Tchilas Zman Kiddush Levana 7 Days: Jan 25, 1969 at 06:06:30 EST
Sof Zman Kiddush Levana Between Moldos: Feb 02, 1969 at 00:28:31 EST
Sof Zman Kiddush Levana 15 Days: Feb 02, 1969 at 06:06:30 EST
Kiddush Levana Times During Daylight Hours
As you can see, all of these times are at night (after tzais 72 and prior to alos 72 minutes in Montreal). Many times, these calculations will result in times that are during daylight hours when Kiddush Levana can’t be said. When the calculated time of tchilas zman Kiddush Levana is during daylight hours, the earliest time should be tzais the following night. When the calculated time of sof zman Kiddush Levana is during daylight hours, the time posted should be alos on that morning. The API now supports this in the ComprehensiveZmanimCalendar, whose getTchilasZmanKidushLevana3Days(Instant alos, Instant tzais) and related methods return tzais or alos when the time falls during the day, and return null on days when the zman does not occur.
Until this point the KosherJava Zmanim API has been released under the GPLV2.0open source license. This had the effect of forcing any application written using the library to release its source code under the same license. Being that the Zmanim API is a library, the LGPL is a more appropriate license. The LGPL allows developers to use the KosherJava Zmanim API, yet keep their application code closed source. Only changes to the API itself (such as ports to different languages) would have to be released as open source. I had in the past on request released the source under the LGPL (the Zmanim API .NET port has already been released under the LGPL with my permission), and with the recent March 27, 2011 SVNcheck-in I formally changed the Java Zmanim API license to the LGPL 2.1 (not the newer LGPL 3.0). The next 1.3.0 release will be the first formal release under the LGPL. In one case the change to the license required permission from a developer who wrote code used by the Zmanim API, and this was done.
The Zmanim API did not have dedicated zmanim to calculate the Erev Pesach zmanim of sof zman achilas chametz (the latest time one can eat chametz), and sof zman biur chametz (the latest time to burn chametz) till the April 14 check in to the KosherJava Zmanim Project GitHub repository. The latest time for eating chametz is at the end of the fourth hour of the day. This corresponds to sof zman tfila. The API has about 12 of those, so that does not require any special programming, but to help developers who are unaware of how they work I created three wrapper getSofZmanAchilasChametz methods (getSofZmanAchilasChametzGRA(), getSofZmanAchilasChametzMGA72Minutes() and getSofZmanAchilasChametzMGA16Point1Degrees()) calling the three most commonly used getSofZmanTfila methods (getSofZmanTfilaGRA(), getSofZmanTfilaMGA72Minutes() and getSofZmanTfilaMGA16Point1Degrees()).
Note: The code samples below have been updated for the KosherJava Zmanim 3.x API. In the current API, the Erev Pesach methods return null on any day other than Erev Pesach.
For example, here is the current code used in getSofZmanAchilasChametzGRA():
public Instant getSofZmanAchilasChametzGRA() {
return getSofZmanAchilasChametz(getSunriseBasedOnElevationSetting(), getSunsetBasedOnElevationSetting(), true);
}
The API itself is very flexible, and as long as you know the calculation of the zman, you can easily calculate it. For example, to calculate sof zman biur chametz according to the GRA, the time would be 5 shaos zmaniyos after sunrise. Using the Zmanim API this would be coded as:
Thanks to Jay Gindin, the Zmanim API source code is now on a publicly available SVN server. The Google Code project can be found at code.google.com/p/kosherjava. This will have a number of benefits to the project. It will more easily allow users to find the code, ensure that they have the latest code, track changes, and allow reverting to prior versions in case an issue arises with newly checked-in code. It will also allow me to stop the manual backups (part of the project’s Antbuild script) done as part of every development build. The repository has the new hebrewcalendar package mentioned in the recently posted FAQ: How do I Calculate the Jewish/Hebrew Date for …?. This new code is under heavy revision, and there will be changes that will likely break code using the current alpha version. The interface will likely stabilize once the first beta version is released (probably after Pesach). The next step will be to touch base with various people who have copies of the API in their own SVN to either update or link to this official project SVN server.