Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 732
Tom Feiner Avatar asked Oct 05 '08 15:10

Tom Feiner


People also ask

How do I format a date in Perl?

say $datetime->month; say $datetime->day; say $datetime->strftime( '%Y-%m-%d-%H-%M-%S' );

What is Strftime in Perl?

You can use the POSIX function strftime() in Perl to format the date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent. Specifier. Replaced by.

How do I get current month in Perl?

To get the day, month, and year, you should use localtime : my($day, $month, $year)=(localtime)[3,4,5]; To then get the last day of the month, use your code from above: $av_tmp_TODAY = DateTime->last_day_of_month({ year => $year + 1900, month => $month +1 })->ymd('-');

How do I get yesterday's date in Perl?

use DateTime qw( ); my $yday_date = DateTime ->now( time_zone => 'local' ) ->set_time_zone('floating') ->truncate( to => 'day' ) ->subtract( days => 1 ) ->strftime('%Y-%m-%d');


2 Answers

use POSIX qw(strftime);
print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";
like image 197
njsf Avatar answered Oct 11 '22 16:10

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'}, ... )
like image 26
ysth Avatar answered Oct 11 '22 17:10

ysth