(archive 'newLISPer)

December 18, 2005

ISO dates

Filed under: newLISP — newlisper @ 21:35
Tags:

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”.

Advertisements

2 Comments »

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

    Comment by Sammo — December 22, 2005 @ 17:32 | Reply

  2. >Thanks!

    Comment by newlisper — December 29, 2005 @ 21:42 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: