Search

I was pulling in a Twitter feed and noticed their published date (<created_at>) wasn't in ISO format. So I took the prepackaged date-time.xsl utility and modified it enough to do the exact same thing, but with the Twitter date type. Same instructions aside from the input type.

https://gist.github.com/87331

Sweet! A buddy of mine was having this same issue and I suggested modifying the same file. He didn't really feel like messing with it, so I'm glad someone else did.

I'll be sure to point him to this thread.

Cheers!

  • W

You could always just grab the Atom feed, no?

@willhelm, i thought it was about time i contributed something! @czheng, you are partially right. I didn't think about pointing to just the atom feed (even though I do have a link pointing to it on my page, go figure), but the date-time.xsl file would still need to be modified b/c this is what the atom feed spits out:

<published>2009-03-12T19:44:02+00:00</published>
<updated>2009-03-12T19:44:02+00:00</updated>

it is in ISO format, but it is lacking the actual weekday value. also the way the symphony file is written, the weekday and time values are attributes, which the info above does not display them as (well, time, at least).

i'm actually working on a Pandora date-time.xsl utility as well, but I keep on running into a weird bug that I haven't been able to pinpoint where/when it's happening yet.

Good work all! Date formatting has always been a pain. I wonder whether there could be a neater approach, particularly since there are many different date formats. Rather than duplicating the whole date-time.xsl each time, I wonder whether it'd be better to have a middle-man intermediary template to convert a date into the ISO format that the date-time.xsl accepts.

This way one can keep the original date-time.xsl but call a "convert Twitter to ISO" template, or "convert Pandora to ISO" template first. This would boil down any date into a single understood format.

I'm not sure how much added complexity this create, but thought it worth a mention.

Yes, I was going to suggest the same...

@nick, I was thinking the same thing while I started to work on my Pandora date/time utility. It would be nice to be able to have some sort of "middle man" that can convert the date into an ISO formatted one. I feel like this may be best handled as an extension since PHP will have a lot more features/functionality to do this string conversion vs. using XSLT for this. though I think it could be possible, it would just be very verbose and then you may as well have written a new date-time utility at that point.

I'm getting errors when I try to use this:

Line 80
    XSLTProcessor::transformToXml(): runtime error: file /path/to/workspace/utilities/twitter-date-formatter.xsl line 80 element call-template
Line 140
    XSLTProcessor::transformToXml(): runtime error: file /path/to/workspace/utilities/twitter-date-formatter.xsl line 140 element param

XSLTProcessor::transformToXml(): Variable 'uppercase' has not been declared.
XSLTProcessor::transformToXml(): xmlXPathCompOpEval: parameter error
XSLTProcessor::transformToXml(): Failed to evaluate the expression of variable 'month'.

Any ideas why?

I'm calling it like so:

<xsl:call-template name="twitter-format-date">
<xsl:with-param name="date" select="pubDate"/>
<xsl:with-param name="format" select="'d-n-Y T'"/>
</xsl:call-template>

@eoghanobrien, i forgot to include two variables in the initial posting: uppercase and lowercase. these are used to translate any text into lowercase letters. you may have grabbed the older version. if you go to git now, you'll see the two variables in the utility.

@wtdan that fixed the XSLT errors alright, thanks.

Although now I'm getting output like: NaN-NaN-+0000 2009 08 ?

@eoghan, what's the xml source you're pointing to?

@wtdan my raw twitter rss xml

can you post a snippet of the trouble xml source you're using? i don't see why that would be happening in the first place though...

@wtdan, ya sorry, XML as follows:

<twitter status="fresh" creation="2009-04-03T23:30:34+01:00">
<rss version="2.0">
    <channel>
        <title>Twitter / eoghanobrien</title>
        <link>http://twitter.com/eoghanobrien</link>
        <description>Twitter updates from Eoghan O'Brien / eoghanobrien.</description>
        <language>en-us</language>
        <ttl>40</ttl>
        <item>
            <title>eoghanobrien: blah blah blah</title>
            <description>eoghanobrien: blah blah blah</description>
            <pubDate>Thu, 02 Apr 2009 08:57:25 +0000</pubDate>
            <guid>http://twitter.com/eoghanobrien/statuses/1437307682</guid>
            <link>http://twitter.com/eoghanobrien/statuses/1437307682</link>
        </item>
     </channel>
</rss>

Basic Twitter RSS Feed

hmm...seems like my template isn't set up to handle that particular structure (d'oh!). the structure i was getting was from a different type of feed with the <created_at> node. i'll have to reexamine this to see if i can compensate for that type of date structure or you can check this out by bzerangue. I haven't used it personally, but it may be a little more universal.

Is there also a utility to format a Twitter date that is like this?

<published>2010-11-12T21:27:54Z</published>

Is EXSL’s format-date XSLT implementation any help here?

Pure XSLT

EXSLT function

Although it might be a bit too much, 500+ lines of markup

Or @kanduvisla, you could use the Format Date/Time utility by Allen.

Call the format-date and format time templates like so…

<xsl:call-template name="format-date">
  <xsl:with-param name="date" select="substring(published,1,10)"/>
  <xsl:with-param name="format" select="'M x, Y'"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="format-time">
  <xsl:with-param name="time" select="substring(published,12,5)"/>
  <xsl:with-param name="format" select="'t'"/>
</xsl:call-template>

Which would output November 12, 2010 9:27pm. Or you have other formatting options in the Format Date/Time Utility (see the commented code in the utility).

how do you guys deal with the timezone difference? the xml feed provides a utc_offset in seconds, but i'd prefer to use the atom or rss feeds (they include retweets).

still, even given the seconds, i'm not sure what i would do to evaluate the date to a local time without using php functions.

@fawx - you'll want to use the EXSLT date:add() function

FIRST: you'll want to add the EXSLT date namespace to your xsl:stylesheet declaration.

<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">

THEN: you'll want to convert date from RFC format to ISO format (you can use this utility to do that for you). Just import that utility at the top of your stylesheet.

THEN:

Here's a basic template using the EXSLT date:add() function...

<xsl:template match="item">
          <!-- This will get your date in ISO format 2011-03-07 -->
      <xsl:param name="item-date">
       <xsl:call-template name="format-from-rfc-to-iso">
         <xsl:with-param name="rfc-date" select="pubDate"/>
       </xsl:call-template>
      </xsl:param>

          <!-- This will get your time -->
      <xsl:param name="item-time" select="substring(pubDate,18,8)"/>

      <li>
        Original Time<br/>
        <xsl:value-of select="concat($item-date,'T',$item-time)"/>
      </li>
      <li>
        Adjusted time<br/>
        <xsl:value-of select="date:add(concat($item-date,'T',$item-time),'-PT6H')"/>
      </li>
    </xsl:template>

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details