Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

date and time difference

by invisiblehand (Initiate)
on Jan 08, 2020 at 13:50 UTC ( [id://11111180]=perlquestion: print w/replies, xml ) Need Help??

invisiblehand has asked for the wisdom of the Perl Monks concerning the following question:

(perl version 5.8.7)

I have two date strings and and try to calculate date and time difference between two dates and times.

But the difference looks incorrect, could you help me to get the correct value?

notBefore=Nov 23 22:31:12 2019 GMT

notAfter=Mar 19 06:52:23 2020 GMT

#!/usr/bin/perl use Time::Local; # FILE READ IN $log_file = './raw.txt'; open(FH, '<', $log_file) or die $!; while (<FH>) { if(m/START/) {chomp; $sdname = $_;} if(m/notBefore/) { chomp; ($notB1, $notB2) = split('=', $_); $notB2 =~ tr/:/ /; my($mon,$mday,$hour,$minute,$sec,$year)=split(/ /,$notB2); #print $mon, $mday, $hour, $minute, $sec, $year; $time_1 = timelocal($sec,$minute,$hour,$mday,$mon,$year); } if(m/notAfter/) { chomp; ($notA1, $notA2) = split('=', $_); $notA2 =~ tr/:/ /; my($mon,$mday,$hour,$minute,$sec,$year)=split(/ /,$notA2); $time_2 = timelocal($sec,$minute,$hour,$mday,$mon,$year); } if(m/END/) {printf("%s %s %s\n", $sdname, $notB2, $notA2); print $time_2,"\n",$time_1, "\n"; $diff_time = $time_2 - $time_1; $diff_time1 = ($diff_time/86400); print "diff_time :$diff_time:$diff_time1:\n"; } }

===output=====

SD0101START Nov 23 22 31 12 2019 GMT Mar 19 06 52 23 2020 GMT

1579384343

1548250272

diff_time :31134071:360.348043981481:

========rax.txt===========

SD0101START

notBefore=Nov 23 22:31:12 2019 GMT

notAfter=Mar 19 06:52:23 2020 GMT

SD0101END

SD0201START

notBefore=Nov 24 11:11:11 2019 GMT

notAfter=Mar 19 06:52:23 2020 GMT

SD0201END

Replies are listed 'Best First'.
Re: date and time difference
by haukex (Archbishop) on Jan 08, 2020 at 14:10 UTC

    timelocal requires the month as a number, you're giving it a string, which is being interpreted as 0 (January). You can see this with a print gmtime($time_1)."\n";. You'd need to convert it from string to number first. Also, since your times are in GMT, you should use timegm instead.

    Note that the Time::Local docs strongly recommend to use timegm_modern instead. And personally I'd strongly recommend you switch to a better module, such as Time::Piece, which has a strptime method to parse strings directly, or DateTime with DateTime::Format::Strptime. Also, your version of Perl if about 15 years old, I would strongly recommend looking into upgrading.

      Oh, thanks a lot, it worked well after change the Nov to 11 as you recommended.

      And thanks for your concern for using old version, but I can't not upgrade. It is not my authority.

      Please I have another question more.

      1. My time zone is KST(South Korea republic), but when I executed following command in my system(openbsd),I got GMT time.

      In this case, Can I change the output GMT to KST?

        My time zone is KST(South Korea republic), but when I executed following command in my system(openbsd),I got GMT time.

        Which command do you mean? If you mean Perl's output, gmtime will give you GMT, and localtime will give you whatever the current time zone setting on that machine is. If you want more advanced functionality than that, then you need to switch to DateTime.

        And thanks for your concern for using old version, but I can't not upgrade. It is not my authority.

        Similar advice as in Yes, even you can use CPAN applies.

        Nov is 10, actually. The month number is 0-based (for consistency with localtime).

Re: date and time difference
by 1nickt (Canon) on Jan 08, 2020 at 15:10 UTC

    Hi, your technique is ancient, unreliable and unnecessarily complicated.

    DateTime::Format::Strptime is the right tool as haukex has noted. Read the documentation about date math carefully.

    Here's an example using subtract_datetime():

    $ perl -Mstrict -MData::Dumper -MDateTime::Format::Strptime -wE 'my $p + = DateTime::Format::Strptime->new(pattern => "%b %d %T %Y %Z", on_er +ror => "croak"); my ($dt1, $dt2) = map { $p->parse_datetime($_) } ("N +ov 23 22:31:12 2019 GMT", "Mar 19 06:52:23 2020 GMT"); say for ($dt2, + $dt1); say sprintf("%s : %s", $_, $dt2->subtract_datetime($dt1)->{$_ +}) for qw/months days minutes seconds/'
    2020-03-19T06:52:23 2019-11-23T22:31:12 months : 3 days : 25 minutes : 501 seconds : 11
    You can of course express the delta however you choose, using the correct method calls.

    Hope this helps!


    The way forward always starts with a minimal test.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111180]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found