Wednesday, February 9, 2011

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

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

  • use POSIX qw(strftime);
    print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";
    
    Adam Bellaire : Oh, nice, I didn't know there was something in the core that would do this.
    Tom Feiner : Thank you ,this is exactly, what I was looking for when I asked for an elegant way :)
    From njsf
  • 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'}, ... )
    
    From ysth

0 comments:

Post a Comment