You should be automatically redirected . If not, visit
and update your bookmarks.


ISO dates

For another web site (not this one), I occasionally have to generate an RSS feed 'manually', by running a short script that extracts headlines and paragraphs and puts them into an XML file. Thanks to newLISP's regular expressions, this is not a difficult (or interesting) task.

However, one small problem I had was that, for some strange reason, the RSS XML standard doesn't seem to allow the use of dates written using the ISO 8601 standard. So I had to convert these into the RFC822 standard. Again, newLISP offered a convenient solution.

The parse function is great because it allows you to specify more than one delimiter, using a regular expression. So, this ISO format date:

2005-10-16 12:12:12

is easily split into its constituent parts if you use three delimiters, " ", "-", and ":". (The 0 says to use regular expressions.)

(parse "2005-10-16 12:12:12" { |-|:} 0)

which returns ("2005" "10" "16" "12" "12" "12"). These strings are then converted to integers by mapping the int function over the list:

(map int (parse "2005-10-16 12:12:12" { |-|:} 0))

which returns (2005 10 16 12 12 12). These numbers happen to be in the right order for use with date-value, which gives them temporal significance. Since date-value wants six integers, we can use apply to apply the date-value function directly to the six list elements.

(apply date-value (map int (parse "2005-10-16 12:12:12" { |-|:} 0)))

The integer result, 1129464732, is the time in seconds of our ISO date since 1970-1-1 00:00:00. Finally, date is now able to output a suitable string given this number. date uses the standard strftime formatting techniques, so it's easy to find the right mystic runes for RFC822:

(date (apply date-value (map int (parse "2005-10-16 12:12:12" { |-|:} 0)))  0 "%a, %d %b %Y %H:%M %Z" )

giving us "Sun, 16 Oct 2005 13:12 BST".


At 17:32, Anonymous Sammo said...

Great explanation! Your step-by-step approach is clear and easy to read.

At 21:42, Blogger newlisper said...



Post a Comment

Links to this post:

Create a Link

<< Home