How to convert text date to timestamp?

后端 未结 3 1888
轻奢々
轻奢々 2020-12-09 17:09

How to convert date in text format:

23.10.2011  11:35:00

to timestamp ?

相关标签:
3条回答
  • 2020-12-09 17:37

    You can use Time::Piece, a core module. The formats can be found on the man page for strptime.

    use Time::Piece;
    my $tp = Time::Piece->strptime("23.10.2011  11:35:00", 
                                   "%d.%m.%Y  %H:%M:%S");
    print $tp->epoch; # 1319369700
    print $tp;        # "Sun Oct 23 11:35:00 2011", same as $tp->cdate
    
    0 讨论(0)
  • 2020-12-09 17:41

    Using Time::Local , you can do:

    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
    
    0 讨论(0)
  • 2020-12-09 17:48

    I'd look into DateTime and the parsing 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.
    

    Same as above, but not a 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;
    
    0 讨论(0)
提交回复
热议问题