本文介绍了在 XSLT 1.0 中将 EDT 转换为 GMT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入值为Tue, 12 Sep 2017 15:03:22 EDT"或2017-09-12T15:03:22.0000000".我需要这样的东西:2017-09-12T19:03:22Z"

My input values are "Tue, 12 Sep 2017 15:03:22 EDT" or "2017-09-12T15:03:22.0000000". I need something like: ""2017-09-12T19:03:22Z""

是否可以在 XSLT 1.0 中将 EDT 日期时间转换为 GMT 格式?

Is it possible to convert EDT date-time to GMT format in XSLT 1.0?

推荐答案

要在纯 XSLT 1.0 中具有已知偏移量的两个时区之间进行转换,可以使用以下示例:

To convert between two timezones with a known offset between them in pure XSLT 1.0, you can use the following example:

XML

<input>2017-09-12T15:03:22.0000000</input>

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:template match="input">
    <output>
        <xsl:call-template name="add-hours-to-dateTime">
            <xsl:with-param name="dateTime" select="."/>
        </xsl:call-template>
    </output>
</xsl:template>

<xsl:template name="add-hours-to-dateTime">
    <xsl:param name="dateTime"/>
    <xsl:param name="hours" select="4"/>

    <xsl:variable name="dateTime-in-seconds">
        <xsl:call-template name="dateTime-to-seconds">
            <xsl:with-param name="dateTime" select="$dateTime"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="total-seconds" select="$dateTime-in-seconds + 3600 * $hours" />
    <!-- new date -->
    <xsl:variable name="new-date">
        <xsl:call-template name="JDN-to-Gregorian">
            <xsl:with-param name="JDN" select="floor($total-seconds div 86400)"/>
        </xsl:call-template>
    </xsl:variable>
    <!-- new time -->
    <xsl:variable name="t" select="$total-seconds 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"/>
    <!-- output -->
    <xsl:value-of select="$new-date" />
    <xsl:text>T</xsl:text>
    <xsl:value-of select="format-number($h, '00')"/>
    <xsl:value-of select="format-number($m, ':00')"/>
    <xsl:value-of select="format-number($s, ':00.###')"/>
</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="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($time, 1, 2)" />
    <xsl:variable name="minute" select="substring($time, 4, 2)" />
    <xsl:variable name="second" select="substring($time, 7)" />

    <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" />
</xsl:template>

<xsl:template name="JDN-to-Gregorian">
    <xsl:param name="JDN"/>
    <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:value-of select="$Y" />
    <xsl:value-of select="format-number($M, '-00')"/>
    <xsl:value-of select="format-number($D, '-00')"/>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>2017-09-12T19:03:22</output>

这篇关于在 XSLT 1.0 中将 EDT 转换为 GMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:13