开发者

Parsedate question in Perl

开发者 https://www.devze.com 2023-01-10 02:02 出处:网络
In Perl, why I get different results from parsedate(201开发者_运维问答0-7-2 13:0:0) and parsedate(2010-7-2 13:00:0) ?The 2010-7-2 13:0:0 string is not in a valid format, and is actually not being pars

In Perl, why I get different results from parsedate(201开发者_运维问答0-7-2 13:0:0) and parsedate(2010-7-2 13:00:0) ?


The 2010-7-2 13:0:0 string is not in a valid format, and is actually not being parsed at all (it appears) as evidenced by the fact that parsedate("2010-7-2") returns the same value as parsedate("2010-7-2 13:0:0") for me.

Based on the docs, it's simply parsing the YYYY-MM-DD, but not parsing the 13:0:0 at all because it is expecting it to be in HH:MM format and not HH:M format. Basically, you have to use two digits for the minutes in order for it to be valid input.


To handle your date format with more flexibility, try using DateTime::Format::Strptime

my $strp = DateTime::Format::Strptime->new(
    pattern   => '%Y-%m-%d %T',
    locale    => 'en_AU',
    time_zone => 'Australia/Melbourne',
);

my $dt1 = $strp->parse_datetime('2010-7-2 13:0:0');
my $date_1 = $strp->format_datetime($dt1);

$date_1 is now converted into a well-formatted date format "2010-07-02 13:00:00". Then you can call parsedate($date_1) & get epoch.

0

精彩评论

暂无评论...
验证码 换一张
取 消