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

Hi I am trying to find out difference between two dates with Time:Piece. I am able to get the days difference, but I want the Days as wells as hours, mins and sec difference. Below is my code for both, but the later is now working. Can anyone help me out. With only days
use Time::Piece; use Time::Seconds; $before = Time::Piece->strptime("2001/01/01", "%Y/%m/%d"); $now = localtime; $diff = $now - $before; print int($diff->days), " days since $before\n";
I am changing the days with hms, but it is giving me the error. Please some check this and let me know where I am doing the mistake. error: Can't call method "hms" on an undefined value at timediff line 7. Note that I dont have Date::Calc installed in my production systems.

Replies are listed 'Best First'.
Re: Time:Piece help
by marto (Cardinal) on Apr 24, 2014 at 15:47 UTC

    I added strict/warnings to your code:

    use strict; use warnings; use Time::Piece; use Time::Seconds; my $before = Time::Piece->strptime("2001/01/01", "%Y/%m/%d"); my $now = localtime; my $diff = $now - $before; print int($diff->days), " days since $before\n";

    Which produced:

    4861 days since Mon Jan 1 00:00:00 2001

    Which is correct. Did you copy and paste your actual code?

Re: Time:Piece help
by poj (Abbot) on Apr 24, 2014 at 16:25 UTC
    Try pretty method ;
    #!perl use strict; use Time::Piece; use Time::Seconds; my $before = Time::Piece->strptime("2001/01/01", "%Y/%m/%d"); my $now = localtime; my $diff = $now - $before; print $diff->pretty . " since $before\n";
    poj
Re: Time:Piece help
by runrig (Abbot) on Apr 24, 2014 at 16:10 UTC
    I am changing the days with hms, but it is giving me the error.

    What do you mean by this? Show code.

    error: Can't call method "hms" on an undefined value at timediff line 7.

    I don't see a line 7 in your code. What are you calling "hms" on? Show code.

Re: Time:Piece help
by Anonymous Monk on Apr 24, 2014 at 16:07 UTC

    Post the actual code that produces the error. As it is & as already noted, your code (currently) does not produce the error of "Can't call method "hms" on an undefined value".

    hms is a method of Time::Piece to get the time parts as string.

    Look up Time::Seconds API to get seconds, hours, minutes from difference (object) as noted in Time::Piece.

      (On a side note, Time::Piece API is bad for it returns T::P object when adding a plain number; returns Time::Seconds object when adding two T::P objects.)

        On a side note, Time::Piece API is bad for it returns T::P object when adding a plain number; returns Time::Seconds object when adding two T::P objects.

        Adding a number to a T::P object and getting back a T::P object makes sense (the 'number' is assumed to be seconds). What do you expect? The last part of your statement is wrong...you cannot add two T::P objects...that would make no sense, and it is rightfully an error to do so. You can subtract two T::P objects and get back a T::S object, as that makes sense. So what is bad about the API?