1
votes

I am working with xslt and using the fn:current-dateTime( ) to get the current date time (https://www.w3.org/TR/xpath-functions/#func-current-dateTime). The time returned is in UTC. I need to convert this to PST. I see that the fn:adjust-dateTime-to-timezone (http://www.xqueryfunctions.com/xq/fn_adjust-datetime-to-timezone.html) can be used to perform the timezone conversion. But i see that this function accepts the timeZone argument in the form of offset, i.e -PT8H for Pacific timezone while it is -PT5H for US Eastern Standard Time. When timezone is passed in terms of offset, its subject to modifications when Daylight Savings Time (DST) starts and ends right? Is there a better way for performing timezone conversion in xslt, a method that requires no modification periodically? or is there a recommendation when using fn:adjust-dateTime-to-timezone to handle this better?

2
Did you try a few date/times to see what it does?Jim Garrison

2 Answers

4
votes

The XPath functions for manipulating timezone offsets work entirely in terms of numeric (duration) offsets, e.g UTC -5 hours or UTC +10 hours. They have no knowledge of civil timezone names or of daylight savings adjustments.

The one exception is the format-date[Time] function, where the fifth argument can be used to force conversion to the local timezone of a particular place, for example "America/New_York" - but it's somewhat implementation-defined whether such names are recognized. From the spec:

If the $place argument is supplied in the form of an IANA timezone name that is recognized by the implementation, then the date or time being formatted is adjusted to the timezone offset applicable in that timezone. For example, if the xs:dateTime value 2010-02-15T12:00:00Z is formatted with the $place argument set to America/New_York, then the output will be as if the value 2010-02-15T07:00:00-05:00 had been supplied. This adjustment takes daylight savings time into account where possible; if the date in question falls during daylight savings time in New York, then it is adjusted to timezone offset -PT4H rather than -PT5H. Adjustment using daylight savings time is only possible where the value includes a date, and where the date is within the range covered by the timezone database.

0
votes

Use this if the code is itself running in a US timezone:

<xsl:variable name="currentTimeZone" select="timezone-from-dateTime(current-dateTime())" />

<xsl:variable name="date_PDT_PST" select="adjust-dateTime-to-timezone($custom_date, xs:dayTimeDuration($currentTimeZone))" />