I have an XML that has timestamp in format like 2016-06-07T07:46:04
I would like to convert timestamp in UTC format like 1465278364 using xslt
How can it be done?
Thanks in advance.
I have an XML that has timestamp in format like 2016-06-07T07:46:04
I would like to convert timestamp in UTC format like 1465278364 using xslt
How can it be done?
Thanks in advance.
I actually wrote a named template specifically for this a while back. Here it is:
<xsl:template name="converttime">
<xsl:param name="date" select="."/>
<xsl:choose>
<xsl:when test="translate($date,'123456789','000000000') = '0000-00-00T00:00:00'">
<xsl:value-of select="
(
((substring($date,1,4) - 1970) * 365) +
floor((substring($date,1,4) - 1970) div 4) +
substring(
'000,031,059,090,120,151,181,212,243,273,304,334,365',
substring($date,6,2)*4-3, 3
) +
(substring($date,9,2)-1) +
(1-floor(((substring($date,1,4) mod 4) + 2) div 3))*floor((substring($date,6,2)+17) div 20)
) * 86400 +
(substring($date,12,2)*3600) +
(substring($date,15,2)*60) +
substring($date,18,2)
"/>
</xsl:when>
<!-- Unknown format -->
<xsl:otherwise>-1</xsl:otherwise>
</xsl:choose>
</xsl:template>
This adds up:
All of the above is then multiplied by the number of seconds in a day Then it's just a simple matter of adding hours * (seconds in an hour), minutes * (seconds in a minute) and seconds.
You can support different date formats just by putting a different <xsl:when>
element with a different date pattern, and obviously adjusting the substring calls to match.
Honestly though, only use this if you can't use XSLT 2.0 (or later) or an extension function. Also, I noticed the number you gave is 7200 seconds out from what this produces, but if you're in a time zone 2 hours different from UTC, that's probably why.