我正在使用的系统仅输出中央时区 (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 < 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/