views:

1182

answers:

2

How can I elegantly print the date in RFC822 format in Perl?

+15  A: 
use POSIX qw(strftime);
print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";
njsf
Oh, nice, I didn't know there was something in the core that would do this.
Adam Bellaire
Thank you ,this is exactly, what I was looking for when I asked for an elegant way :)
Tom Feiner
+10  A: 

The DateTime suite gives you a number of different ways, e.g.:

use DateTime;
print DateTime->now()->strftime("%a, %d %b %Y %H:%M:%S %z");

use DateTime::Format::Mail;
print DateTime::Format::Mail->format_datetime( DateTime->now() );

print DateTime->now( formatter => DateTime::Format::Mail->new() );

Update: to give time for some particular timezone, add a time_zone argument to now():

DateTime->now( time_zone => $ENV{'TZ'}, ... )
ysth