Skip to content

Instantly share code, notes, and snippets.

@fawx
Forked from bzerangue/rfc-date-to-iso-date.xsl
Created March 10, 2011 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fawx/865128 to your computer and use it in GitHub Desktop.
Save fawx/865128 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Name: RSS feed date format to Symphony date format
Version: 1.0
Author: Brian Zerangue <brian.zerangue@gmail.com>
URL: http://symphony21.com/downloads/xslt/file/20457/
Description:
Convert RSS feed date format to Symphony date format
Convert RFC 2822 timestamp format to ISO date format (Symphony CMS date format)
RFC 2822 format: Sun, 7 Jan 2007 12:00:00 -0600
ISO 8601 format: 2007-01-07T18:00:00
Usage:
The named template "format-from-rfc-to-iso" takes 2 parameters, 1 required and 1 optional:
1. rfc-date - [required] a valid rfc 2822/rss date
2. with-time - [optional] 'yes' (default) or 'no'. in addition to the date, output the time in UTC
Notes:
This has not been tested with RFC dates which append a timezone identifier rather than a 4-digit offset.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:template name="format-from-rfc-to-iso">
<xsl:param name="rfc-date"/>
<xsl:param name="with-time" select="'yes'" />
<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),'####')"/>
<xsl:param name="time" select="substring-before(substring-after($rfc-date, concat($year-full,' ')), ' ')" />
<xsl:param name="offset" select="substring-after($rfc-date, concat($time, ' '))" /> <!-- grab the timezone offset -->
<xsl:param name="duration" select="concat(translate(translate(substring($offset,1,1),'-',''),'+','-'),'PT',substring($offset,2,2),'H',substring($offset,4,2),'M')" /> <!-- convert the timezone offset to a usable xml duration for date:add, http://www.w3.org/TR/xmlschema-2/#duration -->
<xsl:param name="iso-date" select="concat($year-full,'-',$month-with-zero,'-',$day-with-zero)" />
<xsl:param name="rfc-date-to-iso">
<xsl:if test="$with-time = 'yes'">
<xsl:value-of select="substring-before(date:add(concat($iso-date,'T',$time), $duration), 'Z')" /> <!-- add the timezone difference to the date+time, drop the 'Z' -->
</xsl:if>
<xsl:if test="not($with-time = 'yes')">
<xsl:value-of select="$iso-date" />
</xsl:if>
</xsl:param>
<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