2011-10-11 30 views

Trả lời

16

Sử dụng Time::Local, bạn có thể làm:

use Time::Local; 

my $date = '23.10.2011 11:35:00'; 
my ($mday,$mon,$year,$hour,$min,$sec) = split(/[\s.:]+/, $date); 
my $time = timelocal($sec,$min,$hour,$mday,$mon-1,$year); 
print $time,"\n",scalar localtime $time; 

Output:

1319362500 
Sun Oct 23 11:35:00 2011 
16

Tôi muốn xem xét DateTimeparsing modules.

perl -MDateTime::Format::Strptime -le'$strp = DateTime::Format::Strptime->new(pattern => "%d.%m.%Y %T", time_zone => "local"); $dt = $strp->parse_datetime("23.10.2011 11:35:00"); print $dt->epoch' 
1319384100 at -e line 1. 

Tương tự như trên, nhưng không phải là một one-liner:

use DateTime::Format::Strptime; 
my $strp = DateTime::Format::Strptime->new(
    pattern => '%d.%m.%Y %T', 
    time_zone => 'local', 
); 
my $dt = $strp->parse_datetime('23.10.2011 11:35:00'); 
print $dt->epoch; 
Các vấn đề liên quan