Converting Time Formats

In this recipe we will be performing transmogrification between time format specification. A project involved reading an RSS feed and formatting it for display within a web page. Unfortunately the pubDate time format in the RSS feed was not compatible with the KRL time library. So we developed a KRL function to convert the time format. Perhaps you will find this function useful when you embark on your own time travels.

 The RSS pubDate time string accepted by the KRL function is of the format:

Wed, 21 Sep 2011 15:02:01 -0700

and will be converted to RFC3339 format:

2011-09-21T15:02:01-07:00

We use the function to convert the RSS pubDate to RFC 3339 format, then call the time:strftime() function to generate the needed display format:

krlDate = rss2krlTime("Wed, 21 Sep 2011 15:02:01 -0700");
strDate = time:strftime(krlDate, "%b %d %Y");

Here is the KRL time travel function is all it's glory:

rss2krlTime = function(pubDate) {
  mon2num = { "Jan": "01", "Feb": "02", "Mar": "03",
              "Apr": "04", "May": "05", "Jun": "06",
              "Jul": "07", "Aug": "08", "Sep": "09",
              "Oct": "10", "Nov": "11", "Dec": "12" };
  tarr = pubDate.split(re# #);
  tzoff = tarr[5].replace(re#\d\d$#, "");
  rfcTime = tarr[3] + "-" + mon2num{[tarr[2]]} + "-" +
            tarr[1] + "T" + tarr[4] + tzoff + ":00";
  rfcTime
};

This recipe is from Ed Orcutt

Copyright Picolabs | Licensed under Creative Commons.