Outputting dates in RFC822 and ISO8601 formats in Perl
Correctly formatted date strings are needed for RSS generation, among other things RSS. Diego shows how to do this in Java. This is my version in Perl
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
my $now = time();
# We need to munge the timezone indicator to add a colon between the hour and minute part
my $tz = strftime("%z", localtime($now));
$tz =~ s/(\d{2})(\d{2})/$1:$2/;
# ISO8601
print strftime("%Y-%m-%dT%H:%M:%S", localtime($now)) . $tz . "\n";
# RFC822 (actually RFC2822, as the year has 4 digits)
print strftime("%a, %d %b %Y %H:%M:%S %z", localtime($now)) . "\n";
The output is
2005-01-05T01:35:08+01:00
Wed, 05 Jan 2005 01:35:08 +0100
There are some small issues I haven’t found clarification for at this
late hour of writing. The first is whether the day should be
zero-padded in the RFC822 case. As it is now it’s
space-padded. Accordng to
RFC822, the day is
zero-padded.
The second is how to handle locale settings — RFC822 specifies that the weekdays and months be in the US locale. I’m pretty sure that you need extra magic in Perl for this not to work, but I’ll take a look at that tomorrow.
Yet another reason to standardize on ISO8601, I guess.
Update 2006-03-10: Eli Billauer was kind enough to mention this error to me, kudos to him.
Updated on Friday, 2006-03-10.
Posted at 14:21,
in the comp category. Comments [0]
Submit this story to: » del.icio.us
» digg
» reddit. Search for it on
technorati.
Submit a comment
Please enter comments as plain text only; HTML is not supported. Submitting an URL is optional.
Comments are moderated and may not appear immediately.
Comments are closed for this story.