XML technologies / XSLT / XSLT Cookbook / Dates and times / Working with the Hebrew calendar
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You need to work with dates in the Hebrew system.
Solution
Build some basic utilities to work effectively with the Hebrew calendar. Hebrew years have 12 months in an ordinary year and 13 in a leap year. Leap years occur in years 3, 6, 8, 11, 14, 17 and 19 of the Metonic cycle (see Discussion). A concise test is given by the equation 7y + 1 mod 19 < 7. From this, a function determining a Hebrew year’s last month is easily derived:
<xsl:template name="ckbk:last-month-of-hebrew-year">
<xsl:param name="year"/>
<xsl:choose>
<xsl:when test="(7 * $year + 1) mod 19 < 7">
<xsl:value-of select="13"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="12"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>To determine the number of days in a month or year, first incorporate the complex rules governing the start of the Hebrew new year. Dershowitz and Reingold provide a detailed explanation:
<!-- Anzahl der Tage, die vom Sonntag vor dem Start des hebräischen Kalenders bis zum durchschnittlichen Zusammentreffen von Tischri des hebräischen Jahres verstrichen sind. -->
<xsl:template name="ckbk:hebrew-calendar-elapsed-days">
<xsl:param name="year"/>
<xsl:variable name="hebrew-leap-year" select="(7 * $year + 1) mod 19 < 7"/>
<xsl:variable name="hebrew-leap-year-last-year" select="(7 * ($year - 1) + 1) mod 19 < 7"/>
<xsl:variable name="months-elapsed" select="235 * floor(($year −1) div 19) + 12 * (($year −1) mod 19) + floor((7 * (($year - 1) mod 19) + 1) div 19)"/>
<xsl:variable name="parts-elapsed" select="13753 * $months-elapsed + 5604"/>
<xsl:variable name="day" select="1 + 29 * $months-elapsed + floor($parts-elapsed div 25920)"/>
<xsl:variable name="parts" select="$parts-elapsed mod 25920"/>
<xsl:variable name="alternative-day">
<xsl:choose>
<xsl:when test="$parts >= 19440">
<xsl:value-of select="$day + 1"/>
</xsl:when>
<xsl:when test="$day mod 7 = 2 and $parts >= 9924 and not($hebrew-leap-year)">
<xsl:value-of select="$day + 1"/>
</xsl:when>
<xsl:when test="$day mod 7 = 1 and $parts >= 16789 and $hebrew-leap-year-last-year">
<xsl:value-of select="$day + 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$day"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$alternative-day mod 7 = 0">
<xsl:value-of select="$alternative-day + 1"/>
</xsl:when>
<xsl:when test="$alternative-day mod 7 =3">
<xsl:value-of select="$alternative-day + 1"/>
</xsl:when>
<xsl:when test="$alternative-day mod 7 = 5">
<xsl:value-of select="$alternative-day + 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$alternative-day"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>A Hebrew year’s length is the difference between elapsed-day counts in consecutive years:
<xsl:template name="ckbk:days-in-hebrew-year">
<xsl:param name="year"/>
<xsl:variable name="e1">
<xsl:call-template name="ckbk:hebrew-calendar-elapsed-days">
<xsl:with-param name="year" select="$year + 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="e2">
<xsl:call-template name="ckbk:hebrew-calendar-elapsed-days">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$e1 - $e2"/>
</xsl:template>Heshvan and Kislev are the eighth and ninth months of the Hebrew year. Their lengths vary. To determine when Heshvan is long and Kislev is short, create two predicates:
<xsl:template name="ckbk:long-heshvan">
<xsl:param name="year"/>
<xsl:variable name="days">
<xsl:call-template name="ckbk:days-in-hebrew-year">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:if select="$days mod 10 = 5">
<xsl:value-of select="true( )"/>
</xsl:if>
</xsl:template>
<xsl:template name="ckbk:short-kislev">
<xsl:param name="year"/>
<xsl:variable name="days">
<xsl:call-template name="ckbk:days-in-hebrew-year">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:if select="$days mod 10 = 3">
<xsl:value-of select="true( )"/>
</xsl:if>
</xsl:template>Caution!
When writing predicate templates in XSLT 1.0, make them return true( ) (or alternatively 'true') for true, but '' (null string) for false. Templates return trees, and every tree, even one whose only node contains false( ) or '', evaluates to true. A tree containing '' has the advantage that it can actually be evaluated as a Boolean expression using the string( ) conversion. This is one of XSLT’s stranger facts. In XSLT 2.0 you can instead use functions, since they can return Boolean values.
Most mechanisms are now in place, so you can start the standard date/time functions supplied in other recipes. The first returns the last day of a given Hebrew month in a given Hebrew year:
<xsl:template name="ckbk:last-day-of-hebrew-month">
<xsl:param name="month"/>
<xsl:param name="year"/>
<xsl:variable name="hebrew-leap-year" select="(7 * $year + 1) mod 19 < 7"/>
<xsl:variable name="long-heshvan">
<xsl:call-template name="ckbk:long-heshvan">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="short-kislev">
<xsl:call-template name="ckbk:short-kislev">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$month=12 and $hebrew-leap-year">
<xsl:value-of select="30"/>
</xsl:when>
<xsl:when test="$month=8 and string($long-heshvan)">
<xsl:value-of select="30"/>
</xsl:when>
<xsl:when test="$month=9 and string($short-kislev)">
<xsl:value-of select="29"/>
</xsl:when>
<xsl:when test="$month=13">
<xsl:value-of select="29"/>
</xsl:when>
<xsl:when test="$month mod 2 = 0">
<xsl:value-of select="29"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="30"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>This recursive utility sums the last days in a range of Hebrew months for a given year. It is used when converting a Hebrew date to an absolute year:
<xsl:template name="ckbk:sum-last-day-in-hebrew-months">
<xsl:param name="year"/>
<xsl:param name="from-month"/>
<xsl:param name="to-month"/>
<xsl:param name="accum" select="0"/>
<xsl:choose>
<xsl:when test="$from-month <= $to-month">
<xsl:call-template name="ckbk:sum-last-day-in-hebrew-months">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="from-month" select="$from-month+1"/>
<xsl:with-param name="to-month" select="$to-month"/>
<xsl:with-param name="accum">
<xsl:variable name="temp">
<xsl:call-template name="ckbk:last-day-of-hebrew-month">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="month" select="$from-month"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$temp + $accum"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$accum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:hebrew-date-to-absolute-day">
<xsl:param name="year"/>
<xsl:param name="month"/>
<xsl:param name="day"/>
<xsl:variable name="prior-months-days">
<xsl:choose>
<xsl:when test="7 > $month"> <!-- before Tishri -->
<xsl:variable name="last-month-of-year">
<xsl:call-template name="ckbk:last-month-of-hebrew-year">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<!-- Addiere Tage vor und nach Nissan -->
<xsl:variable name="days-before-nisan">
<xsl:call-template name="ckbk:sum-last-day-in-hebrew-months">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="from-month" select="7"/>
<xsl:with-param name="to-month" select="$last-month-of-year"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:sum-last-day-in-hebrew-months">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="from-month" select="1"/>
<xsl:with-param name="to-month" select="$month - 1"/>
<xsl:with-param name="accum" select="$days-before-nisan"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- Tage in früheren Monaten dieses Jahres-->
<xsl:call-template name="ckbk:sum-last-day-in-hebrew-months">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="from-month" select="7"/>
<xsl:with-param name="to-month" select="$month - 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="days-in-prior-years">
<xsl:call-template name="ckbk:hebrew-calendar-elapsed-days">
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<!-- 1373429 Tage vor dem absoluten Tag 1 -->
<xsl:value-of select="$day + $prior-months-days + $days-in-prior-years - 1373429"/>
</xsl:template>Before implementing absolute-day-to-hebrew-date, you need two more recursive summation utilities. They search, from estimates of the year and month, for the actual year and month corresponding to an absolute day:
<xsl:template name="ckbk:fixup-hebrew-year">
<xsl:param name="start-year"/>
<xsl:param name="abs-day"/>
<xsl:param name="accum" select="0"/>
<xsl:variable name="next">
<xsl:call-template name="ckbk:hebrew-date-to-absolute-day">
<xsl:with-param name="month" select="7"/>
<xsl:with-param name="day" select="1"/>
<xsl:with-param name="year" select="$start-year + 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$abs-day >= $next">
<xsl:call-template name="ckbk:fixup-hebrew-year">
<xsl:with-param name="start-year" select="$start-year+1"/>
<xsl:with-param name="abs-day" select="$abs-day"/>
<xsl:with-param name="accum" select="$accum + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$accum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:fixup-hebrew-month">
<xsl:param name="year"/>
<xsl:param name="start-month"/>
<xsl:param name="abs-day"/>
<xsl:param name="accum" select="0"/>
<xsl:variable name="next">
<xsl:call-template name="ckbk:hebrew-date-to-absolute-day">
<xsl:with-param name="month" select="$start-month"/>
<xsl:with-param name="day">
<xsl:call-template name="ckbk:last-day-of-hebrew-month">
<xsl:with-param name="month" select="$start-month"/>
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$abs-day > $next">
<xsl:call-template name="ckbk:fixup-hebrew-month">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="start-month" select="$start-month + 1"/>
<xsl:with-param name="abs-day" select="$abs-day"/>
<xsl:with-param name="accum" select="$accum + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$accum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:absolute-day-to-hebrew-date">
<xsl:param name="abs-day"/>
<xsl:variable name="year">
<xsl:variable name="approx" select="floor(($abs-day + 1373429) div 366)"/>
<xsl:variable name="fixup">
<xsl:call-template name="ckbk:fixup-hebrew-year">
<xsl:with-param name="start-year" select="$approx"/>
<xsl:with-param name="abs-day" select="$abs-day"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$approx + $fixup"/>
</xsl:variable>
<xsl:variable name="month">
<xsl:variable name="first-day-of-year">
<xsl:call-template name="ckbk:hebrew-date-to-absolute-day">
<xsl:with-param name="month" select="1"/>
<xsl:with-param name="day" select="1"/>
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="approx">
<xsl:choose>
<xsl:when test="$abs-day < $first-day-of-year">
<xsl:value-of select="7"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="fixup">
<xsl:call-template name="ckbk:fixup-hebrew-month">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="start-month" select="$approx"/>
<xsl:with-param name="abs-day" select="$abs-day"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$approx + $fixup"/>
</xsl:variable>
<xsl:variable name="day">
<xsl:variable name="days-to-first-of-month">
<xsl:call-template name="ckbk:hebrew-date-to-absolute-day">
<xsl:with-param name="month" select="$month"/>
<xsl:with-param name="day" select="1"/>
<xsl:with-param name="year" select="$year"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$abs-day - ($days-to-first-of-month - 1)"/>
</xsl:variable>
<xsl:value-of select="concat($year,'-',$month,'-',$day)"/>
</xsl:template>Note:
The previous example creates three distinct but similar recursive templates summing a function. A generic utility able to sum any function would be desirable. Readers familiar with Lisp may suspect that the original Lisp code from which this XSLT was derived had a macro for exactly this purpose. Recipes for vertical XSLT applications demonstrates generic programming in XSLT, which can considerably simplify this recipe.
Discussion
The Hebrew calendar is the most complex calendar in this chapter. The Hebrew date-time code is therefore correspondingly complicated. The calendar is cumbersome because its months are strictly lunar, yet Passover must always occur in spring. Other calendars have a fixed number of months, but the Hebrew calendar has 12 in an ordinary year and 13 in a leap year.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de