XML technologies / XSLT / XSLT Cookbook / Dates and times / Calculating Julian and absolute day numbers from a date
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
Given a date, you want its Julian and/or absolute day number.
Julian day versus Julian date
Do not confuse the Julian day with the Julian calendar. Joseph J. Scaliger invented the Julian period, associating each year with a positive number without needing BC/BCE or AD/CE. It begins on January 1, 4713 BCE in the Julian calendar and lasts 7980 years. Astronomers used it to assign a unique number to each day since January 1, 4713 BCE: the Julian day. This chapter was written on Julian day 2,452,376. If curious, use its code to find the actual date. Another absolute numbering scheme, used by N. Dershowitz and E. Reingold in their calendar algorithms, starts on January 1, 1 CE. I call a number in their system the absolute day number. Absolute day 1 corresponds to Julian day 1,721,426.
Solution
XSLT 1.0
This template gives the Julian day from a year, month and day:
<xsl:template name="ckbk:calculate-julian-day">
<xsl:param name="year"/>
<xsl:param name="month"/>
<xsl:param name="day"/>
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12 * $a - 3"/>
<xsl:value-of select="$day + floor((153 * $m + 2) div 5) + $y * 365 + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045"/>
</xsl:template>Once you can calculate the Julian day number, finding the number of days between any two dates is easy:
<xsl:template name="ckbk:date-difference">
<xsl:param name="from-year"/>
<xsl:param name="from-month"/>
<xsl:param name="from-day"/>
<xsl:param name="to-year"/>
<xsl:param name="to-month"/>
<xsl:param name="to-day"/>
<xsl:variable name="jd1">
<xsl:call-template name="ckbk:calculate-julian-day">
<xsl:with-param name="year" select="$from-year"/>
<xsl:with-param name="month" select="$from-month"/>
<xsl:with-param name="day" select="$from-day"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="jd2">
<xsl:call-template name="ckbk:calculate-julian-day">
<xsl:with-param name="year" select="$to-year"/>
<xsl:with-param name="month" select="$to-month"/>
<xsl:with-param name="day" select="$to-day"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$jd1 - $jd2"/>
</xsl:template>The following templates convert a Julian day to a Gregorian date in JJJJ/MM/TT form. Use substring-before, substring-after and translate to parse or convert the conventions of a particular locale:
<xsl:template name="ckbk:julian-day-to-julian-date">
<xsl:param name="j-day"/>
<xsl:call-template name="ckbk:julian-or-gregorian-date-elem">
<xsl:with param name="b" select="0"/>
<xsl:with param name="c" select="$j-day + 32082"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:julian-day-to-gregorian-date">
<xsl:param name="j-day"/>
<xsl:variable name="a" select="$j-day + 32044"/>
<xsl:variable name="b" select="floor((4 * $a + 3) div 146097)"/>
<xsl:variable name="c" select="$a - 146097 * floor($b div 4)"/>
<xsl:call-template name="ckbk:julian-or-gregorian-date-elem">
<xsl:with param name="b" select="$b"/>
<xsl:with param name="c" select="$c"/>
</xsl:call-template>
</xsl:template>
<!-- Ein Hilfsmittel, das sowohl für gregorianische als auch für julianische Kalender verwendet wird. -->
<xsl:template name="ckbk:julian-or-gregorian-date-elem">
<xsl:param name="b"/>
<xsl:param name="c"/>
<xsl:variable name="d" select="floor((4 * $c + 3) div 1461)"/>
<xsl:variable name="e" select="$c - floor((1461 * $d) div 4)"/>
<xsl:variable name="m" select="floor((5 * $e + 2) div 153)"/>
<xsl:variable name="day" select="$e - floor((153 * $m + 2) div 5) + 1"/>
<xsl:variable name="month" select="$m + 3 - (12 * floor($m div 10))"/>
<xsl:variable name="year" select="100 * $b + $d - 4800 + floor($m div 10)"/>
<xsl:value-of select="concat($year,'/',$month,'/',$day)"/>
</xsl:template>These templates convert easily between Julian and absolute days:
<xsl:template name="ckbk:julian-day-to-absolute-day">
<xsl:param name="j-day"/>
<xsl:value-of select="$j-day - 1721425"/>
</xsl:template>
<xsl:template name="ckbk:absolute-day-to-julian-day">
<xsl:param name="abs-day"/>
<xsl:value-of select="$abs-day + 1721425"/>
</xsl:template>You can then express absolute-day-to-Gregorian conversions in terms of the existing Julian-day-to-Gregorian conversions:
<xsl:template name="ckbk:date-to-absolute-day">
<xsl:param name="year"/>
<xsl:param name="month"/>
<xsl:param name="day"/>
<xsl:call-template name="ckbk:julian-day-to-absolute-day">
<xsl:with-param name="j-day">
<xsl:call-template name="ckbk:date-to-julian-day">
<xsl:with-param name="year" select="$year"/>
<xsl:with-param name="month" select="$month"/>
<xsl:with-param name="day" select="$day"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:absolute-day-to-date">
<xsl:param name="abs-day"/>
<xsl:call-template name="ckbk:julian-day-to-date">
<xsl:with-param name="j-day">
<xsl:call-template name="ckbk:absolute-day-to-julian-day">
<xsl:with-param name="abs-day" select="$abs-day"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>XSLT 2.0
In XSLT 2.0 Julian and absolute day functions are no longer needed because date calculations are supported directly.
<dateDiff>
<xsl:value-of select="xs:date('2005-02-21') - xs:date('2005-01-01')"/>
</dateDiff>for example, yields:
<dateDiff>P51D</dateDiff>Some applications may need these values for other purposes. Here, we implement the 1.0 templates as functions accepting an xs:date instead of separate components:
<xsl:function name="ckbk:calculate-julian-day">
<xsl:param name="date" as="xs:date"/>
<xsl:variable name="year" select="year-from-date($date)" as="xs:integer"/>
<xsl:variable name="month" select="month-from-date($date)" as="xs:integer"/>
<xsl:variable name="day" select="month-from-date($date)" as="xs:integer"/>
<xsl:variable name="a" select="(14 - $month) idiv 12" as="xs:integer"/>
<xsl:variable name="y" select="$year + 4800 - $a" as="xs:integer"/>
<xsl:variable name="m" select="$month + 12 * $a - 3" as="xs:integer"/>
<xsl:sequence select="$day + ((153 * $m + 2) idiv 5) + $y * 365 + floor($y div 4) - ($y idiv 100) + ($y idiv 400) - 32045"/>
</xsl:function>Discussion
Julian and absolute days greatly simplify other date algorithms. Other examples in this chapter repeatedly use these conversions. The numbering schemes provide a common currency for all calendar systems discussed here. If you ever need to convert a Hebrew date to a Muslim date, you can conveniently take the route “Muslim to absolute to Hebrew date”.
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