XML technologies / XSLT / XSLT Cookbook / Dates and times / Working with the Islamic calendar

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need dates in the Islamic system.

Caution!
Generally accepted Islamic calendar algorithms are difficult, perhaps impossible, because a month begins when a person first sees the crescent after the new moon. The new moon is predictable, but visibility depends on weather and location, making month starts hard to predict exactly. Some Muslims use local sightings, others those of authorities elsewhere in the Muslim world. Saudi Arabia is an exception, using astronomical calculations rather than sightings. These algorithms may differ by several days when predicting dates far ahead.

Solution

Except in leap years, approximate month ends by assigning 30 days to odd months and 29 to even months:

<xsl:template name="ckbk: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 calendar began with the Hijra, Muhammad’s flight to Medina, for most Muslims at sunset on July 15, 622 CE in the Julian calendar. This is absolute day 227,015, hence offset 227,014 in the otherwise simple conversion:

<xsl:template name="ckbk:islamic-date-to-absolute-day">
  <xsl:param name="year"/>
  <xsl:param name="month"/>
  <xsl:param name="day"/>
  <xsl:value-of select="$day + 29 * ($month - 1) + floor($month div 2) + 354 * ($year - 1) + floor((11 * $year + 3) div 30) + 227014"/>
</xsl:template>

Conversion from absolute days differs from its original Lisp implementation. I use floating-point calculations to avoid Lisp’s search technique, designed to retain utmost accuracy within 24-bit integer limits. Those techniques translate poorly to XSLT; avoiding floating point at all costs makes little sense when XSLT 1.0 supports it. The numbers derive from a mean lunar month of 29.530555... days. Estimate the month and adjust if the day would be below 1. Once year and day are found, calculate the day as an offset from the year’s first day:

<xsl:template name="ckbk:absolute-day-to-islamic-date">
  <xsl:param name="abs-day"/>
  <xsl:variable name="year" select="floor(($abs-day - 227014) div 354.36667) + 1"/>
  <xsl:variable name="month">
    <xsl:variable name="a" select="$abs-day - 227014 - floor((11 * $year + 3) div 30) - 354 * ($year - 1)"/>
    <xsl:variable name="approx" select="floor($a div 29.53056)+1"/>
    <xsl:choose>
      <xsl:when test="(29 * ($approx - 1) + floor($approx div 2)) - $a &lt; 1">
        <xsl:value-of select="$approx - 1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$approx"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="day">
    <xsl:variable name="a">
      <xsl:call-template name="ckbk:islamic-date-to-absolute-day">
        <xsl:with-param name="year" select="$year"/>
        <xsl:with-param name="month" select="$month"/>
        <xsl:with-param name="day" select="1"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$abs-day - $a + 1"/>
  </xsl:variable>
  <xsl:value-of select="concat($year,'/',$month,'/',$day)"/>
</xsl:template>

Discussion

The Islamic calendar is purely lunar, so months do not stay within seasons. Its year has about 354.36 days. Year 1 marks the Hijra from Mecca to Medina. The calendar has great religious significance and nearly always relies on visual lunar observations. Algorithmic calendars can be calculated ahead, but should generally serve only rough planning.

See also

Differences between countries’ conventions are described in The Islamic Calendar.

  

 

 

 

back to top