如何在 XSLT 上正确使用 GMT?

克莱因

这两个时间戳似乎在 XSLT 上使用了不同的 GMT。

Timestamp #1
-> 1559780505287
-> 5/6/2019, 9:21:45 PM GMT-3 (dd/mm/yyyy)

Timestamp #2
-> 1562681762005
-> 9/7/2019, 11:16:02 AM GMT-3 (dd/mm/yyyy)

使用在线 xslt 测试工具:https : //xslttest.appspot.com/

转换后,时间戳 #1 是 6/6/2019,时间戳 #2 仍然是 9/7/2019。 时间戳 #1 应该是 5/6/2019。

转化结果

如何在 XSLT 上正确使用 GMT-3?

XML:

<java version="1.6.0_45" class="java.beans.XMLDecoder">
    <object class="com.MyApp">

        <void property="date1">
            <object class="java.util.Date">
                <long>1559780505287</long>
            </object>
        </void>

        <void property="date2">
            <object class="java.util.Date">
                <long>1562681762005</long>
            </object>
        </void>

    </object>
</java>

XSLT:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:decimal-format decimal-separator="," grouping-separator="."/>

<xsl:template match="/">
    <html>
        <head>
            <meta charset="utf-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
            <meta name="viewport" content="width=device-width, initial-scale=1"/>
            <meta name="format-detection" content="telephone=no"/>

        </head>
        <body>
            <div class="container">

                <h1>Date 1</h1>
                <xsl:variable name="date1">
                    <xsl:value-of select="xs:date('1970-01-01') + java/object/void[@property='date1'] * xs:dayTimeDuration('PT0.001S')"/>
                </xsl:variable>

                <h2>
                    <xsl:value-of select="format-date($date1, '[D01]/[M01]/[Y0001]')"/>
                </h2>

                <h1>Date 2</h1>
                <xsl:variable name="date2">
                    <xsl:value-of select="xs:date('1970-01-01') + java/object/void[@property='date2'] * xs:dayTimeDuration('PT0.001S')"/>
                </xsl:variable>

                <h2>
                    <xsl:value-of select="format-date($date2, '[D01]/[M01]/[Y0001]')"/>
                </h2>

            </div>
        </body>
    </html>
</xsl:template>

michael.hor257k

我建议您将时间戳转换为日期时间,将其调整为您的时区,然后对其进行格式化:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">

<xsl:template match="/java">
    <html>
        <body>
            <xsl:for-each select="object/void">
                <xsl:variable name="dateTime-GMT" select="xs:dateTime('1970-01-01T00:00:00Z') + object/long * xs:dayTimeDuration('PT0.001S')"/> 
                <xsl:variable name="dateTime-adjusted" select="adjust-dateTime-to-timezone($dateTime-GMT, xs:dayTimeDuration('-PT3H'))"/>
                <h1>
                    <xsl:value-of select="@property"/>
                </h1>
                <h2>
                    <xsl:value-of select="format-dateTime($dateTime-adjusted, '[D01]/[M01]/[Y0001] [h]:[m01]:[s01] [P] [z]')"/>
                </h2>
            </xsl:for-each>
         </body>
    </html>
</xsl:template>

</xsl:stylesheet>

结果

<html>
   <body>
      <h1>date1</h1>
      <h2>05/06/2019 9:21:45 p.m. GMT-03:00</h2>
      <h1>date2</h1>
      <h2>09/07/2019 11:16:02 a.m. GMT-03:00</h2>
   </body>
</html>

渲染

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章