Zmanim API Ported to .NET (C#)

Yitzchok ported the Zmanim API from Java to a .NET API using C#. The Zmanim .NET project was released under the LGPL 2.1. This is a change from the GPL used by the Java API, something that may change shortly. Also part of the project was the creation of a C# version of the Zmanim CLI, matching Moshe Wagner’s Java Zmanim CLI. When developing the project, Yitzchok created the ZmanimTest JUnit test case class to confirm that the C# port output matched the Java API. I will likely add this to the core Zmanim API in the near future. The port currently relies on IKVM assemblies as can be seen in the Java references in the code sample below, mostly because of the lack of a native .NET equivalent of the Java TimeZone class. Yitzchok also created some examples of the use of the Zmanim .NET API that will be of help to developers. Below are two of the simpler examples in C# and VB.NET demonstrating a very simple use of the API to output zmanim from the console.

C#

using Zmanim.TimeZone;
using Zmanim.TzDatebase; //in Zmanim.TzDatebase.dll assembly
using Zmanim.Utilities;

namespace Zmanim.Samples.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            string locationName = "Lakewood, NJ";
            double latitude = 40.09596; //Lakewood, NJ
            double longitude = -74.22213; //Lakewood, NJ
            double elevation = 0; //optional elevation
            ITimeZone timeZone = new OlsonTimeZone("America/New_York");
            GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
            ComplexZmanimCalendar zc = new ComplexZmanimCalendar(location);
            //optionally set it to a specific date with a year, month and day
            //ComplexZmanimCalendar zc = new ComplexZmanimCalendar(new DateTime(1969, 2, 8), location);

            System.Console.WriteLine("Today's Zmanim for " + locationName);
            System.Console.WriteLine("Sunrise: " + zc.GetSunrise()); //output sunrise
            System.Console.WriteLine("Sof Zman Shema MGA: " + zc.GetSofZmanShmaMGA()); //output Sof Zman Shema MGA
            System.Console.WriteLine("Sof Zman Shema GRA: " + zc.GetSofZmanShmaGRA()); //output Sof Zman Shema GRA
            System.Console.WriteLine("Sunset: " + zc.GetSunset()); //output sunset

            System.Console.WriteLine("Press enter to exit.");
            System.Console.ReadLine();
        }
    }
}

VB.NET

mports Zmanim.TzDatebase 'in Zmanim.TzDatebase.dll assembly
Imports Zmanim.Utilities
Imports Zmanim.TimeZone

Module Module1

    Sub Main()
        Dim locationName As String = "Lakewood, NJ"
        Dim latitude As Double = 40.09596 'Lakewood, NJ
        Dim longitude As Double = -74.22213 'Lakewood, NJ
        Dim elevation As Double = 0 'optional elevation
        Dim timeZone As ITimeZone = New OlsonTimeZone("America/New_York")
        Dim location As New GeoLocation(locationName, latitude, longitude, elevation, timeZone)
        Dim zc As New ComplexZmanimCalendar(location)
        'optionally set it to a specific date with a year, month and day
        'Dim zc As New ComplexZmanimCalendar(New DateTime(1969, 2, 8), location)
        System.Console.WriteLine("Today's Zmanim for " & locationName)
        System.Console.WriteLine("Sunrise: " & zc.GetSunrise().ToString)
        'output sunrise
        System.Console.WriteLine("Sof Zman Shema MGA: " & zc.GetSofZmanShmaMGA().ToString)
        'output Sof Zman Shema MGA
        System.Console.WriteLine("Sof Zman Shema GRA: " & zc.GetSofZmanShmaGRA().ToString)
        'output Sof Zman Shema GRA
        System.Console.WriteLine("Sunset: " & zc.GetSunset().ToString)
        'output sunset
        System.Console.WriteLine("Press enter to exit.")
        System.Console.ReadLine()

    End Sub

End Module

The current Zmanim .NET TODO list for the project includes:

  • Remove dependency to Java (IKVM assemblies)
  • The API should follow the .NET guidelines
  • Make it Linq friendly
  • Add examples how to use this project in a ASP.NET MVC site and WPF Application
  • Try to get it to work on Silverlight

Android Zmanim Using the KosherJava Zmanim API

Android Logo
Android ZmanimThere are various software projects using the KosherJava Zmanim API. One of the active ones is Jay Gindin’s open source Android Zmanim app for the Android platform. Activity in the project is constant. The upcoming version allows the selection of a specific calculation for zmanim you want such as the zman Talis/Tefilin pictured here. There are plans to add direction to Yerushalayim functionality using the Zmanim API. (For more information on calculating bearing using the API, see Calculating the Bearing/Direction to Har Habayis Using the Zmanim API article.) A large part of Jay’s motivations for developing the code was lezecher nishmas his nephew Shemuel Reuven ben Yehudit Rachel who, lost his battle with cancer on October 21, 2009. The one very minor issue Jay had with the API (and documentation) was the Ashkenazi spelling of the zmanim names, something that as an Ashekenazi I do not plan to change :), but as you can see his Android Zmanim front end used Sephardi labeling. Asked how he found the project, he answered with the typical answer to this question

I Googled around, and found your project.

One of my goals with the API was to make it easy for developers to use and port. This was confirmed by Jay

I found it to be very easy to pull into my app, even on Android…no changes necessary, not even a recompile

Zmanim API 1.2 Released

The Zmanim API 1.2 was released today. The only change in this release is the removal of elevation as a factor in the calculation of all zmanim besides sunrise and sunset. Updated JavaDocs now reflect these changes. Please see the Elevation Now Only Impacts Sunrise and Sunset Calculations in the Zmanim API post for additional information.

I would like to thank the various people who contacted me on the subject.

The main download is the Zmanim 1.2 release zip file that includes source files and JavaDoc documentation. Also available for download (included in the above zip file) is the main zmanim-1.2.jar. The zmanimAstronomical-1.1.jar that only includes the AstronomicalCalendar was not impacted by this change, but the version has been updated to zmanimAstronomical-1.2.jar for consistency. Additional detail on the downloads can be seen on the Zmanim Download page

Calculating the Bearing/Direction to Har Habayis Using the Zmanim API

Vintage Map with CompassAn earlier “Bearing to Yerushalayim and Zmanim Map” post demonstrated the use of JavaScript to render the bearing to Har Habayis on a Google Map. A more detailed follow-up post “Technical Information about the Bearing to Yerushalayim Map” dealt with detailed technical information on these calculations. The main Bearing to Yerushalayim and Zmanim Map page usually has the most up to date information on the subject. What was not detailed in previously published posts and pages was that most of the calculations available via JavaScript are now in the core Zmanim API. Available since the July, 2008 beta 2 release of version 1.1 is the ability to bearings/directions using both the great circle and rhumb line methods in Java. The GeoLocation Object was modified to calculate the great circle bearings (both initial and final), and rhumb line bearing from any GeoLocation Object to another. In addition, distance calculation between the two points using both of these line types is supported. What was not ported from the JavaScript version was the less accurate Haversine formula, or the simpler spherical law of cosines algorithms that yield identical results. Instead, the Zmanim API uses the far more accurate Vincenty formulae using the WGS84 geoid model of the earth. Published by the geodesist/mathematician Thaddeus Vincenty, it is said to be accurate to about one-half millimeter, more than adequate for our calculation. The code in the API is a Java port of the previously published, slightly modified version of Chris Veness’s JavaScript implementation . Below is a simple Java example of generating bearing and distances.

/**
 * This program demonstrates how to calculate bearing to Yerushalayim
 * using the kosherjava.com Zmanim API. Both the great circle and
 * rhumb line method are shown
 * To compile, ensure that the Zmanim Jar is in your classpath.
 */
import com.kosherjava.zmanim.util.GeoLocation;
import java.util.TimeZone;

public class BearingToYerushalayim{
	public static void main(String [] args) {
		GeoLocation lakewood = new GeoLocation("Lakewood, NJ", 40.09596, -74.22213, 0, TimeZone.getTimeZone("America/New_York"));
		GeoLocation harHabayis = new GeoLocation("Har Habayis", 31.77805, 35.235149, 0, TimeZone.getTimeZone("Asia/Jerusalem"));

		double greatCircleInitialBearing = lakewood.getGeodesicInitialBearing(harHabayis);
		double greatCircleDistance = lakewood.getGeodesicDistance(harHabayis);

		double rhumbLineBearing = lakewood.getRhumbLineBearing(harHabayis);
		double rhumbLineDistance = lakewood.getRhumbLineDistance(harHabayis);

		System.out.println("Great circle initial bearing: " + greatCircleInitialBearing + " degrees ");
		System.out.println("Great circle distance: " + greatCircleDistance / 1000 + " km");

		System.out.println("Rhumb line bearing: " + rhumbLineBearing + " degrees");
		System.out.println("Rhumb line distance: " + rhumbLineDistance / 1000 + " km");

	}
}

ZmanimCLI (Command Line Interface)

Java Logo SepiaMoshe Wagner who wrote the Zmanim GUI notified me in August that that he created a command line interface for zmanim using my Zmanim API. The technical approach of using reflection was similar to the way I used reflection in the Zmanim Clock Applet, but he took it to new heights. Sample use of accessing zmanim using his CLI interface is:

moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar sunrise
6:10:28
moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar --date 2010/08/12 tzais72
20:38:15
moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar
Usage: ZmanimCLI [options] [Time]

Options:
       -d      --date <yyyy/mm/dd>             Set date. (Year first!)
       -lat    --latitude <latitude>           Set location's latitude
       -lon    --longitude <longitude>         Set location's longitude
       -e      --elevation <elevation>         Set location's
elevation; Positive only
       -tz     --timezone <timezone>           Set location's TimeZone

Help:
       -h      --help                          Show this help
       -stl    --time-list                     Show common available
times to display
       -ftl    --full-time-list                Show all available
times to display
       -tzl    --timezone-list                 Show available timezones

Example:
       ZmanimCLI --latitude 31.7780 --longitude 35.235149 --elevation
600 --timezone Israel Sunrise
       Will show the sunrise time today in Jerusalem

While your first reaction may be that it is interesting in a theoretical geeky way, but has no practical value, I will quote Moshe’s explanation as to why it is useful:

Why is this useful? Well, first of all it was a nice experiment. But mainly, you can now use Zmanim (although externally), via any language you want, no longer being tied to Java.

Months later, Moshe actually put this to practical use in his C++ based Luach project. This Luach (similar to the known Kaluach) uses the Qt framework. utilizing libhdate for the date stuff (something not offered by the Zmanim API, and the topic of a future Zmanim API FAQ), displaying zmanim using the Zmanim API via CLI for the zmanim calculations. While you would expect such an approach to be slow, using the Luach seemed almost instantaneous. I will post more about his Luach program (recently reviewed at KosherDev.com) at some point in the future.