在不同时区的日期时间的Xslt转换,节省日光

问题描述 投票:0回答:1

寻找有关不同时区的dateTime xslt转换的一些帮助,节省日光。如何在xslt中进行dateTime转换时处理EST与EDT?

在此处输入代码

Below is one Sample order xml which will be converted into another order format <salesOrders> using xslt. 
All dates in the "salesOrders" are supposed to be stored in GMT timezone. 

<!-- Input orders xml -->
<xml>
    <orders>
        <order>
            <order_id>1</order_id>
            <!--lots of other details -->
            <order_dateTime>2019/03/09 14:00</order_dateTime> <!-- yyyy/MM/dd hh24:mm-->
            <order_timezone>America/New_York</order_timezone>
            <!-- This time does not fall under day light saving ( UTC -0500) -->
        </order>
        <order>
            <order_id>2</order_id>
            <!--lots of other details -->
            <order_dateTime>2019/03/10 14:00</order_dateTime>
            <order_timezone>America/New_York</order_timezone>
            <!-- This time falls under day light saving timing ( UTC -0400) -->
        </order>
        <order>
            <order_id>3</order_id>
            <!--lots of other details -->
            <order_dateTime>2019/03/10 14:00</order_dateTime>
            <!-- format : yyyy/MM/dd hh24:mm-->
            <order_timezone>Asia/Kolkata</order_timezone>
            <!-- Indian Standard Time ( UTC +0530) -->
        </order>
    </orders>
</xml>

<!-- after xslt conversion, the date should be converted as given below.  -->

<xml>
    <salesOrders>
        <salesOrder>
            <salesOrderId>1</salesOrderId>
            <!-- required details -->
            <salesOrderDttm>2019-03-09T19:00:00</salesOrderDttm>    <!-- (14 + 5:00) -->
            <!-- converted the time into GMT (-04:30)-->
        </salesOrder>
        <salesOrder>
            <salesOrderId>2</salesOrderId>
            <!-- required details -->
            <salesOrderDttm>2019-03-09T18:00:00</salesOrderDttm>  <!-- (14 + 4:00) -->
            <!-- converted the time into GMT (-05:30)-->    
        </salesOrder>
        <salesOrder>
            <salesOrderId>2</salesOrderId>
            <salesOrderDttm>2019-03-09T08:30:00</salesOrderDttm>  <!-- (14 - 5:30) -->
        </salesOrder>
    </salesOrders>
</xml>

Need help for pupulating content of "salesOrderDttm"
    The content of <orderDateTime> and <orderTimeZone> will be used to populate <salesOrderDttm>.
    As mentioned above, it will be maintained in GMT (offset +0000) timezone.
    The main problem is, same Time zone being treated differently. At one time(first order), offset is +5:00 and at another time(2nd order) the offset is +04:30 

    How to do it in xslt?
    Came across adjust-dateTime-to-timezone() function, but that also expects us to provide the offset. 
    Is there any way to handle it?
    I hope this is a very common problem and must be solved 

看到这篇文章,但它说它不可能是我想要的。 Similar query on stack overflow

xslt xslt-2.0 exslt
1个回答
3
投票

在XSLT 3.0中,您可以使用$place函数的format-dateTime()参数来实现此目的。引用函数和运算符规范§9.8.4

如果$ place参数以实现所识别的IANA时区名称的形式提供,则格式化的日期或时间将调整为该时区中适用的时区偏移量。例如,如果将$ place参数设置为America / New_York格式化xs:dateTime值2010-02-15T12:00:00Z,那么输出将如同值2010-02-15T07:00:00- 05:00已经供应。此调整在可能的情况下将夏令时考虑在内;如果有问题的日期在纽约的夏令时期间下降,则将其调整为时区偏移-PT4H而不是-PT5H。只有在值包含日期且日期在时区数据库涵盖的范围内时,才可以使用夏令时进行调整。

请注意,这种措辞的方式为实现提供了“不识别”时区名称的选项;但我们假设他们这样做了。

首先,你需要将2019/03/09 14:00转换为xs:dateTime2019-03-09T14:00:00Z,这是一个简单的字符串操作。

现在,您可以通过执行America/New_York来确定format-time(xs:dateTime('2019-03-09T00:00:00'), '[Z]', (), (), 'America/New_York')的时区偏移量,这将为您提供诸如“-05:00”(作为字符串)之类的值。您可以使用更简单的字符串操作将其转换为dayTimeDuration -PT05H00M,然后使用adjustDateTimeToTimezone()将此位移应用于原始日期/时间值。

format-dateTime()的XSLT 2.0版本没有定义此功能。

© www.soinside.com 2019 - 2024. All rights reserved.