You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSL v1.1将HL7格式山地时间转换为东部时间

用XSLT 1.1将HL7格式山地时间转换为指定格式东部时间

要实现这个需求,我们需要手动拆解HL7时间字符串、处理时区时差(山地时间MST比东部时间EST晚2小时,需加2小时),再格式化输出中文日期时间格式。以下是完整实现代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="/">
        <!-- 输入的HL7格式山地时间 -->
        <xsl:variable name="hl7-time" select="'20221201080600'"/>

        <!-- 拆分HL7时间的各个部分 -->
        <xsl:variable name="year" select="substring($hl7-time, 1, 4)"/>
        <xsl:variable name="month" select="substring($hl7-time, 5, 2)"/>
        <xsl:variable name="day" select="substring($hl7-time, 7, 2)"/>
        <xsl:variable name="hour" select="substring($hl7-time, 9, 2)"/>
        <xsl:variable name="minute" select="substring($hl7-time, 11, 2)"/>

        <!-- 计算时区转换后的小时 -->
        <xsl:variable name="new-hour" select="number($hour) + 2"/>
        <xsl:variable name="is-next-day" select="$new-hour >= 24"/>

        <!-- 调整小时和日期 -->
        <xsl:variable name="adjusted-hour">
            <xsl:choose>
                <xsl:when test="$is-next-day">
                    <xsl:value-of select="$new-hour - 24"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$new-hour"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="adjusted-day">
            <xsl:choose>
                <xsl:when test="$is-next-day">
                    <xsl:value-of select="number($day) + 1"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$day"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <!-- 处理日期跨月/跨年的情况(示例包含常见月份天数判断,可按需扩展) -->
        <xsl:variable name="max-day-of-month">
            <xsl:choose>
                <xsl:when test="$month = '02'">
                    <!-- 简单处理闰年,如需精确可扩展判断逻辑 -->
                    <xsl:choose>
                        <xsl:when test="(number($year) mod 4 = 0 and number($year) mod 100 != 0) or number($year) mod 400 = 0">29</xsl:when>
                        <xsl:otherwise>28</xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:when test="$month = '04' or $month = '06' or $month = '09' or $month = '11'">30</xsl:when>
                <xsl:otherwise>31</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="final-year" select="$year"/>
        <xsl:variable name="final-month" select="$month"/>
        <xsl:variable name="final-day">
            <xsl:choose>
                <xsl:when test="number($adjusted-day) > number($max-day-of-month)">
                    <xsl:value-of select="number($adjusted-day) - number($max-day-of-month)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$adjusted-day"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <!-- 格式化输出为中文日期时间 -->
        <xsl:value-of select="concat(
            $final-year, '年',
            number($final-month), '月',
            number($final-day), '日 ',
            format-number($adjusted-hour, '00'), ':', $minute
        )"/>
    </xsl:template>
</xsl:stylesheet>

代码说明:

  1. 拆分HL7时间:通过substring函数提取年、月、日、时、分各部分。
  2. 时区转换:山地时间转东部时间需加2小时,判断小时是否超过24,若超过则日期加1天并调整小时。
  3. 跨月处理:加入了月份最大天数的判断,处理日期跨月的情况(如4月30日加1天转为5月1日),闰年2月的判断做了基础实现,可按需扩展更精确的逻辑。
  4. 格式化输出:使用concat拼接字符串,通过number()去掉日期的前导零,format-number保证小时为两位数字格式。

内容的提问来源于stack exchange,提问作者user17170756

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:25:32