使用 XSLT 1.0 将时间戳转换为日期
Convert timestamp to date with XSLT 1.0
如何在没有 EXSLT 扩展或日期函数的情况下转换 xslt 1.0 中的时间戳。
<timestamp>1569509813</timestamp>
我以为这会是微不足道的,但事实并非如此。
要在 XSLT 1.0 中将 Unix time 转换为 ISO 8601 日期时间:
<xsl:template name="UnixTime-to-dateTime">
<xsl:param name="unixTime"/>
<xsl:variable name="JDN" select="floor($unixTime div 86400) + 2440588" />
<xsl:variable name="secs" select="$unixTime mod 86400" />
<xsl:variable name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:variable name="e" select="4*$f + 3"/>
<xsl:variable name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:variable name="h" select="5*$g + 2"/>
<xsl:variable name="d" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:variable name="m" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:variable name="y" select="floor($e div 1461) - 4716 + floor((14 - $m) div 12)"/>
<xsl:variable name="H" select="floor($secs div 3600)"/>
<xsl:variable name="M" select="floor($secs mod 3600 div 60)"/>
<xsl:variable name="S" select="$secs mod 60"/>
<xsl:value-of select="$y"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="format-number($m, '00')"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="format-number($d, '00')"/>
<xsl:text>T</xsl:text>
<xsl:value-of select="format-number($H, '00')"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($M, '00')"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($S, '00')"/>
</xsl:template>
演示:https://xsltfiddle.liberty-development.net/ncntCRZ/2
对于 XSLT 2.0,请参阅:
如何在没有 EXSLT 扩展或日期函数的情况下转换 xslt 1.0 中的时间戳。
<timestamp>1569509813</timestamp>
我以为这会是微不足道的,但事实并非如此。
要在 XSLT 1.0 中将 Unix time 转换为 ISO 8601 日期时间:
<xsl:template name="UnixTime-to-dateTime">
<xsl:param name="unixTime"/>
<xsl:variable name="JDN" select="floor($unixTime div 86400) + 2440588" />
<xsl:variable name="secs" select="$unixTime mod 86400" />
<xsl:variable name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:variable name="e" select="4*$f + 3"/>
<xsl:variable name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:variable name="h" select="5*$g + 2"/>
<xsl:variable name="d" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:variable name="m" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:variable name="y" select="floor($e div 1461) - 4716 + floor((14 - $m) div 12)"/>
<xsl:variable name="H" select="floor($secs div 3600)"/>
<xsl:variable name="M" select="floor($secs mod 3600 div 60)"/>
<xsl:variable name="S" select="$secs mod 60"/>
<xsl:value-of select="$y"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="format-number($m, '00')"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="format-number($d, '00')"/>
<xsl:text>T</xsl:text>
<xsl:value-of select="format-number($H, '00')"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($M, '00')"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($S, '00')"/>
</xsl:template>
演示:https://xsltfiddle.liberty-development.net/ncntCRZ/2
对于 XSLT 2.0,请参阅: