Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[XSLT] Convert RFC 2822 format to ISO date format
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Name: RSS feed date format to ISO 8601
Version: 1.0
Author: Ændrew Rininsland <aendrew.rininsland@thetimes.co.uk>
Based on work by: Brian Zerangue <brian.zerangue@gmail.com>
URL: https://gist.github.com/aendrew/eaa220e619008cbb628c
N.b., currently the timezone handling is a bit screwy.
Description:
Convert RSS feed date format to ISO 8601 format
RFC 2822 format: Sun, 7 Jan 2007 12:00:00 GMT
ISO 8601 format: 2007-01-07T12:00:00+00:00
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="format-from-rfc-to-iso">
<xsl:param name="rfc-date"/>
<xsl:param name="day-with-zero" select="format-number(substring(substring($rfc-date,6,11),1,2),'00')"/>
<xsl:param name="month-with-zero">
<xsl:if test="contains($rfc-date,'Jan')">01</xsl:if>
<xsl:if test="contains($rfc-date,'Feb')">02</xsl:if>
<xsl:if test="contains($rfc-date,'Mar')">03</xsl:if>
<xsl:if test="contains($rfc-date,'Apr')">04</xsl:if>
<xsl:if test="contains($rfc-date,'May')">05</xsl:if>
<xsl:if test="contains($rfc-date,'Jun')">06</xsl:if>
<xsl:if test="contains($rfc-date,'Jul')">07</xsl:if>
<xsl:if test="contains($rfc-date,'Aug')">08</xsl:if>
<xsl:if test="contains($rfc-date,'Sep')">09</xsl:if>
<xsl:if test="contains($rfc-date,'Oct')">10</xsl:if>
<xsl:if test="contains($rfc-date,'Nov')">11</xsl:if>
<xsl:if test="contains($rfc-date,'Dec')">12</xsl:if>
</xsl:param>
<xsl:param name="year-full" select="format-number(substring(substring($rfc-date,6,11),7,5),'0000')"/>
<xsl:param name="hour-with-zero" select="format-number(substring(substring($rfc-date,6),13,2), '00')"/>
<xsl:param name="minute-with-zero" select="format-number(substring(substring($rfc-date,6),16,2),'00')"/>
<xsl:param name="second-with-zero" select="format-number(substring(substring($rfc-date,6),19,2),'00')"/>
<xsl:param name="timezone-identity" select="substring(substring($rfc-date,6),22,1)"/>
<xsl:param name="timezone-offset" select="format-number(substring(substring($rfc-date,6),23,4), '0000')"/>
<xsl:param name="rfc-date-to-iso" select="concat($year-full,'-',$month-with-zero,'-',$day-with-zero,'T',$hour-with-zero,':',$minute-with-zero,':',$second-with-zero,$timezone-identity,$timezone-offset)"/>
<xsl:value-of select="$rfc-date-to-iso"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.