RDF/RSS to ESF

Jump to menu

18 June 2003

I’ve spent the last couple of hours implementing ESF .

‘Hours?’ you say, ‘how can you take hours over implementing something so simple?’ Well, there are three reasons:

  1. I’m slow. Things take time to settle in my brain.
  2. I was making a general-purpose solution.
  3. I was doing it in XSL.

So now I have an XSL stylesheet that converts an RDF/RSS file to painlessly. I’m running it server-side, in fact: choose a feed and append ?alternate=esf to the URL. You’ll be presented with ESF, sent as text/plain. (I think that’s right, but the spec doesn’t say.)

Most of this was easy. Thanks to Chriztian Steinmeier and Eric Meyer I found out about the xsl:space attribute. That didn’t make things easier, as such, just less ugly.

The real head-scratcher was generating Epoch time from the W3C DTF . It’s not as distastrous as I first thought it was going to be, actually. I made a template called epoch_time that you call with params of year (note: subtract 1970 from it before you call the template; it’s easier that way), month, day, hours, minutes, and seconds. Parsing these out using the substring functions in XPath was easy enough, so all that remained was to convert them.

I don’t know of a simple formula for calculating this sort of thing, and if I’ve done it in maths lessons then I’ve forgotten it. What this template does is:

  1. Figure how many days have been gained due to leap years. Do this by subtracting one from the number of years if the month is February or January, then floor($years * 0.25). We know that 1970 wasn’t a leap year, so all OK there.
  2. Figure how many days have passed this year before the current month started. This is just a lookup table based on some calculations I did in my head. It could well be wrong.
  3. Total up the number of days and put it into a variable. Done by multiplying the number of years by 365, adding on the days gained from leap years, then the days in months past, then the days gone in this month.
  4. Multiply the days by 24, then days + hours by 60, then hours + minutes by 60, then add the number of seconds.

I could have shoehorned the last two into the same expression but it would have been even less easy to read than it is now. No, it doesn’t do timezones — does it have to?

Improvements, comments, etc. welcome.