Working with the Islamic Calendar

Problem

You need to work with dates in the Islamic system.

Warning

It is difficult if not impossible to devise universally accepted algorithms for the Islamic calendar. This is because each month starts when the lunar crescent is first seen (by an actual human being) after a new moon. New moons can be calculated quite precisely, but the actual visibility of the crescent depends on factors such as weather and the location of the observer. It is therefore very difficult to compute accurately in advance when a new month will start. Furthermore, some Muslims depend on a local sighting of the moon, whereas others depend on a sighting by authorities somewhere in the Muslim world. Saudi Arabia is an exception, since they use astronomical calculation rather than visual sightings. The algorithms provided here can be off by a few days when computing Islamic dates far in advance.

Solution

The last day of an Islamic month can be estimated quite accurately by assigning 30 days to odd months and 29 days to even months, except during a leap year.

<xsl:template name="date:last-day-of-islamic-month">
     <xsl:param name="month"/>
     <xsl:param name="year"/>
     
     <xsl:variable name="islamic-leap-year" 
          select="(11 * $year + 14) mod 30 &lt; 11"/>
     
     <xsl:choose>
       <xsl:when test="$month mod 2 or ($month = 12 and $islamic-leap-year)">
         <xsl:value-of select="30"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="29"/>
       </xsl:otherwise>
     </xsl:choose>
</xsl:template>

The Islamic calendar ...

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.