regex - XSLT 时区转换

标签 regex datetime xslt

我正在使用的系统仅输出中央时区 (CT) 中的服务器时间。我需要将 XSLT 格式的时间转换为美国东部时间。

有内置方法来翻译它还是我需要使用正则表达式?

<node time="02:14 pm CT" />

当前输出:02:14 pm CT

所需输出:03:14 pm ET

最佳答案

至少有两条主要路径可供选择,将其转换为时间并使用基于时间的库,或者将其作为字符串并进行直接字符串操作。以下是字符串操作:

    <xsl:variable name="time" select="'11:14 pm CT'"/> <!-- the input value -->
    <xsl:variable name="hours" select="number(substring-before($time,':'))"/>  <!-- numeric hours -->
    <xsl:variable name="mer" select="substring($time,7,2)"/>  <!-- the am or pm part -->

    <xsl:choose>
        <xsl:when test="$hours &lt; 12">  <!-- if we are 01-11 -->
            <xsl:value-of select="substring(concat('0', $hours + 1), string-length(concat('0', $hours + 1)) - 1, 2)"/> <!-- add an hour and repad the string with leading zero, messy -->
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>01</xsl:text> <!-- we were 12, so just use 01 -->
        </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="substring($time, 3,4)"/> <!-- pull the minutes forward -->
    <xsl:choose>
        <xsl:when test="not($hours = 11)">  <!-- if we were not 11 for hours we keep the same am/pm -->
            <xsl:value-of select="$mer"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="$mer = 'pm'"> <!-- otherwise we flip it -->
                    <xsl:text>am</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>pm</xsl:text>                       
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text> ET</xsl:text>

关于regex - XSLT 时区转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23373550/

相关文章:

c# - 将 XSLT 中的模板附加到另一个 XSLT

mysql - 匹配连字符的单词

c# - 如何显示哪个子正则表达式匹配失败?

string - 可以直接比较存储为字符串/文本的日期吗?

mysql ORDER BY datetime 类型字段未按预期排序

XSLT:将 <element></element> 转换为 <element/>

xml - 在现有文档的特定位置插入 XML 节点

php - preg_replace 文本的一部分以 PHP 开始和结束

regex - 使用 awk 提取所有电子邮件

java - DateTime 的参数类型不正确