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, %e %b %Y %H:%M:%S %z", localtime($now)) . "\n";
The output is
2005-01-05T01:35:08+01:00
Wed, 5 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.
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.
Posted at 14:21, in the comp category. Comments [0]
Submit a comment
Please enter comments as plain text only; no HTML tags are allowed. Email or URL are optional.
Comments are closed for this story.