Tuesday, September 30, 2008

XML RFC822 and W3CDTF Validation

I've been tasked to clean up XML pages at work. We streamline the process with a perl script (just like many of our other publishing tools) and then a nice neat XML file is produced.

So, I've been spending my time lately validating XML feeds on FeedValidator.org (very useful validator, bookmark it!). Apparently, some of our date formats were not valid. We weren't following the proper RFC822 format for the "pubDate" tag and W3CDTF format for the "dc:date" tag.

Luckily with localtime and strftime, this process is easy. So, I wrote a new subroutine for our perl module and here we have it!

use POSIX qw(strftime);



sub Now2
{
my $a = shift;

if ($a == 1) {
## returns in RFC822 format:
## e.g. Tue, 30 Sep 2008 12:57:06 -0400
my $now_string = strftime "%a, %e %b %Y %H:%M:%S %z", localtime;
return $now_string;
}
if ($a == 2) {
## returns in W3CDTF format:
## e.g. 2008-09-30T13:00:35-0400
my $now_string = strftime "%Y-%m-%dT%H:%M:%S%z", localtime;
return $now_string;
}
else {
return;
}
}


You basically call the subroutine specifying a "1" or "2" depending on which date format you want it in. So for example to get a W3CDTF date format, call the subroutine like this: Now2(2)

Cheers!

No comments: