How to calculate the difference between two timestamp strings in Perl

前端 未结 2 1133
傲寒
傲寒 2020-12-18 05:33

I searched through all the possible questions but couldn\'t find the answer, so can Perl experts help me on this one?

I have two timestamps like 05/25/2011 05:

2条回答
  •  甜味超标
    2020-12-18 05:48

    I recommend that you use the Time::Piece module. It has been a core module since the release of version 9.5 of Perl 5, so it shouldn't need installing.

    This code demonstrates

    use strict;
    use warnings;
    
    use Time::Piece;
    
    my $str1 = 'Execution started at 05/25/2011 05:22:03 PM';
    my $str2 = 'Execution completed at 05/25/2011 05:34:08 PM';
    
    my @times = map Time::Piece->strptime(/(\d.+M)/, '%m/%d/%Y %H:%M:%S %p'), $str1, $str2;
    
    my $delta = $times[1] - $times[0];
    print $delta->pretty;
    

    output

    12 minutes, 5 seconds
    

提交回复
热议问题