XSLT 1.0 日期持续时间(年)

XSLT 1.0 Date Duration (Years)

我正在使用 XSLT 1.0 输出两个日期字段之间的持续时间。以下代码(来自 )按预期输出以天、小时、分钟和秒为单位的持续时间,但我需要更新它以输出年。有没有人对我如何更新此代码以输出 Year 持续时间有任何建议?也欢迎使用替代代码,但它必须使用 XSLT 1.0

非常感谢任何帮助。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="event">
<xsl:variable name="start">
    <xsl:call-template name="dateTime-to-seconds">
        <xsl:with-param name="dateTime" select="start/@time" />
    </xsl:call-template>
</xsl:variable> 

<xsl:variable name="end">
    <xsl:call-template name="dateTime-to-seconds">
        <xsl:with-param name="dateTime" select="end/@time" />
    </xsl:call-template>
</xsl:variable>

<xsl:variable name="duration" select="$end - $start" />
<xsl:variable name="d" select="floor($duration div 86400)"/>
<xsl:variable name="t" select="$duration mod 86400"/>
<xsl:variable name="h" select="floor($t div 3600)"/>
<xsl:variable name="r" select="$t mod 3600"/>
<xsl:variable name="m" select="floor($r div 60)"/>
<xsl:variable name="s" select="$r mod 60"/>
<xsl:copy>
    <xsl:copy-of select="name"/>
    <duration>
        <xsl:value-of select="$d"/>
        <xsl:text> days, </xsl:text>
        <xsl:value-of select="$h"/>
        <xsl:text> hours, </xsl:text>
        <xsl:value-of select="$m"/>
        <xsl:text> minutes and </xsl:text>
        <xsl:value-of select="$s"/>
        <xsl:text> seconds</xsl:text>
    </duration>
    </xsl:copy>
   </xsl:template>

   <xsl:template name="dateTime-to-seconds">
  <xsl:param name="dateTime"/>

<xsl:variable name="date" select="substring-before($dateTime, 'T')" />
<xsl:variable name="time" select="substring-after($dateTime, 'T')" />

<xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 6)" />
<xsl:variable name="offset" select="substring-after($time, $local-time)" />

<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />

<xsl:variable name="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />

<xsl:variable name="offset-sign" select="1 - 2 * starts-with($offset, '-')" />
<xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign" />
<xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign" />

<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:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />

 <xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600*$offset-hour - 60*$offset-minute" />


 </xsl:template> 

 </xsl:stylesheet>

样本XML:

<events>
<event>
    <name>Alpha</name>
    <start time="2015-02-21T00:59:06+02:00"/>
    <end time="2018-02-22T02:24:38+02:00"/>
</event>
<event>
    <name>Bravo</name>
    <start time="2016-02-21T00:59:06+02:00"/>
    <end time="2020-03-05T02:24:38+02:00"/>
</event>
<event>
    <name>Charlie</name>
    <start time="2016-02-21T00:59:06+02:00"/>
    <end time="2020-02-21T00:59:06+01:00"/>
</event>
在此处输入代码

如评论所述,如果你愿意假设每365天为一年,那么你只需要改变这部分:

<duration>
    <xsl:value-of select="$d"/>
    <xsl:text> days, </xsl:text>
    <xsl:value-of select="$h"/>
    <xsl:text> hours, </xsl:text>
    <xsl:value-of select="$m"/>
    <xsl:text> minutes and </xsl:text>
    <xsl:value-of select="$s"/>
    <xsl:text> seconds</xsl:text>
</duration>

至:

<duration>
    <xsl:value-of select="floor($d div 365)"/>
    <xsl:text> years, </xsl:text>
    <xsl:value-of select="$d mod 365"/>
    <xsl:text> days, </xsl:text>
    <xsl:value-of select="$h"/>
    <xsl:text> hours, </xsl:text>
    <xsl:value-of select="$m"/>
    <xsl:text> minutes and </xsl:text>
    <xsl:value-of select="$s"/>
    <xsl:text> seconds</xsl:text>
</duration>

正如评论中所述,在计算精度为秒的计算中,您愿意接受每 4 年大约 1 天的错误是相当奇怪的。如果您只需要天的精度,那么您应该通过删除计算小时、分钟和秒的部分来节省处理器时间和电力。