Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Remove 'recent', clarify time:strftime takes rather than necessarily generates RFC 3339-formatted strings

In this recipe we will be performing transmogrification between time format specification. A recent 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:

Code Block
languagejavascript
themeConfluencelanguagejavascript
Wed, 21 Sep 2011 15:02:01 -0700

and will be converted to RFC3339 format:

Code Block
language
languagejavascript
themeConfluencejavascript
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:

Code Block
languagejavascript
themeConfluencelanguagejavascript
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:

Code Block
language
languagejavascript
themeConfluencejavascript
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/re# /#);
  tzoff = tarr[5].replace(re/re#\d\d$/d$#, "");
  rfcTime = tarr[3] + "-" + mon2num{[tarr[2]]} + "-" +
            tarr[1] + "T" + tarr[4] + tzoff + ":00";
  rfcTime
};

...