Converting epoch to date & time with time zone in

2019-07-06 03:07发布

问题:

How do I convert epoch time to date & time based on time zone in xslt 2.0 ?

For example, epoch time 1212497304 converts to

GMT: Tue, 03 Jun 2008 12:48:24 GMT time zone: martes, 03 de junio de 2008 14:48:24 GMT+2

This is because of the Daylight Saving Time (DST): in many countries there is one hour more in summer, during some dates that varies each year.

For example, it is supposed that this instruction:

<xsl:value-of select=" format-dateTime(xs:dateTime('2013-07-07T23:08:00+00:00'), '[D] [MNn] [Y] [h]:[m01][PN,*-2] [Z] ([C])', 'en', 'AD', 'IST') "/>

would calculate the given GMT date event into Indian Standard Time (IST) with Gregorian Calendar (AD) but it just prints:

7 July 2013 11:08PM +00:00 (Gregorian)

So it does not shift the time zone. To shift the time zone we must use:

adjust-dateTime-to-timezone

But this function accepts only a duration in number of hours/minutes, not a TimeZone so that the processor determines if there is DST or not.

Any advise, please

回答1:

Edit: This is not really the answer to the actual question asked, which was how to get the correct local time, respecting its timezone, but I'll still leave it here since it might be usefull to someone.

Because epoch is just seconds past since unix time you can just add it to unix time like this:

Unix time is the number of seconds elapsed since the epoch of midnight 1970-01-01, you can do:

<xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration('PT1212497304S')"
    />

This will give you the correct xs:dateTime of 2008-06-03T12:48:24

Put into a function:

<xsl:function name="fn:epochToDate">
    <xsl:param name="epoch"/>
    <xsl:variable name="dayTimeDuration" select="concat('PT',$epoch,'S')"/>
    <xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration($dayTimeDuration)"/>
</xsl:function>