如何使用XSLT 1.0将ISO 8601 2013-08-13T17:57:55Z
日期/时间文本转换为"自epoch以来的毫秒"?更具体地说,是Google Chrome的XSLT版本。
我的老经理(嗨,George,对于时区Z中的给定ISO 8601时间戳,以下模板计算自儒略日0开始以来的毫秒数,不包括闰秒。儒略历第0天开始于公元前4713年1月1日中午;那是公历公元前4714年11月24日中午。
检查输入的健全性、使用不同纪元的调整、处理Z以外时区的扩展以及处理闰秒和公共纪元之前日期的扩展,都留给读者练习。(或者你可以调用Javascript,但的乐趣在哪里?)
<xsl:template name="ts2i">
<!--* timestamp to integer: convert an ISO 8601 time stamp
* to the number of milliseconds since an epoch.
* Our epoch is 1 January 4713 BCE (Julian!),
* which is Julian day 0.
* To use 1970-01-01T00:00:00Z as the epoch,
* subtract 210866760000000 ms.
*-->
<xsl:param name="ts"/>
<!--* checking the timestamp for bad data is left as an
* exercise of the reader. Our contract is simpler:
* give me a correct timestamp in time zone Z, for a
* date on or after 0001-01-01, and I'll
* give you a correct answer (modulo overflow).
*-->
<!--* yyyy-mm-ddThh:mm:ss.sss...Z
* ....|....|....|....|... |
* 1 5 10 15 20 n
*-->
<!--* Parse out c, y, m, d, hh, mm, ss (for century,
* years in current century, months since February,
* days in current month, hours, minutes, seconds).
* the y and m values are adjusted to make the
* year begin 1 March (so leap day is always the last
* day of the year).
*-->
<xsl:variable name="y0" select="substring($ts,1,4)"/>
<xsl:variable name="m0" select="substring($ts,6,2)"/>
<xsl:variable name="d" select="substring($ts,9,2)"/>
<xsl:variable name="y1">
<xsl:choose>
<xsl:when test="$m0 < 3"><xsl:value-of select="$y0 - 1"/></xsl:when>
<xsl:otherwise><xsl:value-of select="$y0"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="m" select="($m0 + 9) mod 12"/>
<xsl:variable name="c" select="floor($y1 div 100)"/>
<xsl:variable name="y" select="($y1 mod 100)"/>
<xsl:variable name="hh" select="substring($ts,12,2)"/>
<xsl:variable name="mm" select="substring($ts,15,2)"/>
<xsl:variable name="s0" select="substring($ts,18)"/>
<xsl:variable name="ss">
<xsl:choose>
<xsl:when test="contains($s0,'Z')">
<xsl:value-of select="substring-before($s0,'Z')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--* H holds the offset in days between Julian day 0
* and the beginning of the common era.
* J holds the offset in ms between midnight and
* noon, when Julian day 0 actually began.
*-->
<xsl:variable name="H" select="1721119"/>
<xsl:variable name="J" select="43200000"/>
<!--* Calculate the Julian day that begins on the
* given date. There are 146097 days in each
* 400-year period (including 25 leap days),
* 1461 in each 4-year period, and there are
* (($m * 153) + 2) div 5 days in $m months
* elapsed since 1 March.
* This is straight from the Collected Algorithms.
*-->
<xsl:variable name="j" select="floor(($c * 146097) div 4)
+ floor(($y * 1461) div 4)
+ floor((($m * 153) + 2) div 5)
+ $d + $H"/>
<!--* Calculate the milliseconds since the beginning
* of Julian day 0. This is my extension, and
* it could have an off-by-one error.
*-->
<xsl:value-of select="$j * 86400000
+ $hh * 3600000
+ $mm * 60000
+ $ss * 1000
- $J"/>
</xsl:template>
当给定输入值"2013-08-13T17:57:55Z"时,给定的模板返回数值2.12243176675e+14,即212243176675000。
正如在其中一条评论中所指出的,当我写这篇文章时,我担心溢出,但XSLT1.0数字是IEEE的双精度,因此(我相信)尾数是52位。因此,在未来几千年内,溢流不太可能成为问题;在2001年4月15日左右之前,您的毫秒数不会出现舍入错误。
后记:ACM数字图书馆的一项小研究发现了我认为我的经理George Yanos学习该算法的来源:Robert G.Tantzen,"算法199:日历日期和儒略日数字之间的转换",ACM通讯6.8(1963年8月):444。我注意到坦岑没有解释任何神奇的常数,这让他简洁的阿尔戈密码充满了神秘感。
唯一的方法是调用Javascript代码。