有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java XSL使用偏移量比较将UTC时间转换为本地日期时间

我们接收与UTC时间相关的datetime元素,如2004-04-12T13:20:00Z

我们希望在本地datetime中输出datetime,即用相对于UTC时间的偏移量表示,如2004-04-12T12:20:00-01:00

有人能在XSLT中帮助实现这一点吗
或者是否存在实现此目的的功能模板


共 (3) 个答案

  1. # 1 楼答案

    假设您正在使用Oracle SOA套件进行转换

    xp20:subtract-dayTimeDuration-from-dateTime (/ns0:Your/@DateTime, concat ("PT", substring-after (substring-before (xp20:timezone-from-dateTime (xp20:current-dateTime() ), ":" ), "-0" ), "H" ) )
    

    这会解决你的问题

  2. # 2 楼答案

    假设有这样一个XML测试文件

    <?xml version="1.0"?>
    <root>
        <val>2004-04-12T13:20:00Z</val>
        <val>2004-05-12T23:20:00Z</val>
        <val>2004-06-12T00:20:00Z</val>
    </root>
    

    类似这样的XSLT-2.0文件将时区设置为-1

    <?xml version="1.0"?>
    <xsl:stylesheet version="2.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xsl:output indent="yes" omit-xml-declaration="yes" />
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="//val">
      <xsl:variable name="t_offset" select="xs:dayTimeDuration('-PT1H')" />     <!  set timezone to -1 hours  >
      <xsl:variable name="time"     select="xs:dateTime(normalize-space(text()))"  />
      <xsl:value-of select="$time" />
      <xsl:value-of select="'&#10;'" />
      <xsl:value-of select="adjust-dateTime-to-timezone($time, $t_offset)" />   <!  adjust the time to the new timezone  >
      <xsl:value-of select="'&#10;'" />
      <xsl:value-of select="'         &#10;'" />
     </xsl:template>
    
    </xsl:stylesheet>
    

    其产出是:

    2004-04-12T13:20:00Z
    2004-04-12T12:20:00-01:00
             
    2004-05-12T23:20:00Z
    2004-05-12T22:20:00-01:00
             
    2004-06-12T00:20:00Z
    2004-06-11T23:20:00-01:00
             
    
  3. # 3 楼答案

    要将给定的dateTime值转换为当前本地时区,请使用adjust-dateTime-to-timezone()函数,而不指定timezone参数

    例如:

    <xsl:variable name="datetime">2004-04-12T13:20:00Z</xsl:variable>
    <xsl:value-of select="adjust-dateTime-to-timezone($datetime)"/>
    

    将返回:

    2004-04-12T12:20:00-01:00
    

    如果在转换时,系统的本地时间与UTC的偏移量为-1小时


    重要:

    如果您的本地时间与UTC的偏移不是恒定的,而是由于夏令时而发生变化,则这可能不会产生预期的结果。要正确地将2004年4月的日期转换为当时的当地时间,您需要知道该特定时间点的UTC偏移量。XSLT没有这种功能,您必须在另一个可以访问Olson database的应用程序中进行转换


    增加:

    所有这些都需要XSLT2.0。既然您现在已经澄清了,那么您实际上是在使用XSLT 1.0:

    1. XSLT1.0无法知道当前与UTC的本地偏移量是多少,更不用说给定时间点的偏移量了
    2. 有一种方法可以将给定的dateTime值调整为另一个值 时区-提供所需的偏移量作为参数 调用XSL转换时(或偏移量为 常数)

    以下是将UTC转换为UTC-1:00(作为常量)的模板示例:

    <xsl:template name="UTC-minus-one">
        <xsl:param name="dateTime"/>
    
        <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
        <xsl:variable name="time" select="substring-before(substring-after($dateTime, 'T'), 'Z')" />
    
        <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:variable name="total-seconds" select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600" />
    
        <xsl:variable name="new-jd" select="floor($total-seconds div 86400)"/>  
        <xsl:variable name="new-hour" select="floor($total-seconds mod 86400 div 3600)"/>
        <xsl:variable name="new-minute" select="floor($total-seconds mod 3600 div 60)"/>
        <xsl:variable name="new-second" select="$total-seconds mod 60"/>
    
        <xsl:variable name="f" select="$new-jd + 1401 + floor((floor((4 * $new-jd + 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="concat($Y, format-number($M, '-00'), format-number($D, '-00'))"/>
        <xsl:text>T</xsl:text>
        <xsl:value-of select="concat(format-number($new-hour, '00'), format-number($new-minute, ':00'), format-number($new-second, ':00.###'))"/>
        <xsl:text>-01:00</xsl:text>
    </xsl:template>
    

    演示:http://xsltransform.net/bFWR5F8