【问题标题】:How to convert ISO 8601 date/time into milliseconds in XSLT 1.0?如何在 XSLT 1.0 中将 ISO 8601 日期/时间转换为毫秒?
【发布时间】:2013-08-12 18:04:46
【问题描述】:

如何使用 XSLT 1.0 将 ISO 8601 2013-08-13T17:57:55Z 日期/时间文本转换为“自纪元以来的毫秒数”?更具体地说,是 Google Chrome 的 XSLT 版本。

【问题讨论】:

  • 当您声明您需要 XSLT 1.0 解决方案时,为什么要使用标签 xslt-2.0?

标签: google-chrome xslt xslt-1.0 iso8601


【解决方案1】:

扩展朱利安日期的算法是我的老经理教我的(嗨,乔治!),他又从ACM 的收集算法中得到它(尽管它似乎不包括在内)在online version of CALGO 中,我无法找到算法编号、发布日期或作者 [但请参阅下面的后记]),以下模板计算,对于时区 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 &lt; 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。

正如其中一个 cmets 所述,我在写这篇文章时担心溢出,但 XSLT 1.0 数字是 IEEE 双精度数,因此(我相信)尾数为 52 位。所以在接下来的几千年里,溢出不太可能成为问题;在 138001 年 4 月 15 日左右之前,您的毫秒数不会出现舍入误差。

后记: 对ACM Digital Library 的一点研究发现,我认为我的经理 George Yanos 一定是从那里学到了这个算法:Robert G. Tantzen,“算法 199:转换在日历日期和儒略日数之间”,Communications of the ACM 6.8(1963 年 8 月):444。我注意到 Tantzen 没有解释任何魔法常数,这使他简洁的 Algol 代码具有神秘。

【讨论】:

    【解决方案2】:

    唯一的方法是调用 Javascript 代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2012-03-03
      相关资源
      最近更新 更多