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

Hello All,
I sorry to be asking this but i have wasted lots of time
Trying to do this alone. I have gone over and over the examples on the Date & Time questions but I think I’m
Missing the point some ware

When using the below code its fine until i try and find the time difference between the minutes.

I’m running this on activestate perl. which gives the error of

Can't locate object method "min" via package "Time::Seconds" (perhaps to load "Time::Seconds"?) at
C:\Inetpub\GMN542\timetest.pl line 19.

Normally if i had this error on Unix i guess the perl module
was not installed but i have looked on my windows box and I found that I do have

C:\Perl\site\lib\Time\Piece.pm
and
C:\Perl\site\lib\Time\Seconds.pm
So is this just an error with the way im trying to query the
minute strings?

I guess the question is have I got a problem with my installation of the perl modules?
Or
Is it due to the wrong variable name used for minute? I think I have tried “minute” and “min”.

Again as ever any help that anybody can offer is more then appreciated

regards
Gareth

#!/usr/bin/perl use Time::Piece; use Time::Seconds; $before = Time::Piece->strptime("2003/04/30 10:00:00", "%Y/%m/%d %H:%M +:%S"); $now = localtime(time); $diff = $now - $before; $years = int($diff->years); $diff -= $years * ONE_YEAR; $months = int($diff->months); $diff -= $months * ONE_MONTH; $days = int($diff->days); $diff -= $hours * ONE_HOUR; $hours = int($diff->hours); $diff -= $min * ONE_MIN; $min = int($diff->min); print "$years years, $months months, $days days since , $hours hours s +ince, $min min since $before\n";

Replies are listed 'Best First'.
Re: Time::Piece errors?
by sauoq (Abbot) on May 01, 2003 at 09:49 UTC

    The error says it all. There is no method called min() in Time::Seconds. You probably want minutes() instead.

    -sauoq
    "My two cents aren't worth a dime.";
    
      hi there!
      yes sauoq is right, replace $min = int($diff->min); with $min = int($diff->minutes);
      Antonis
Re: Time::Piece errors?
by spacey (Scribe) on May 01, 2003 at 10:11 UTC
    Hello All,
    Thanks for that i know it was something simple.
    It just one of those days when you cant see the woods of the trees

    I have corrected the code below. But now im getting strange minute times
    Time now is:Thu May 1 11:02:12 2003
    0 years, 0 months, 0 days since , 0 hours since, 2 min since Thu May 1 10:00:00 2003

    anymore ideas you can send my way?

    regards
    Gareth

    #!/usr/bin/perl use Time::Piece; use Time::Seconds; $before = Time::Piece->strptime("2003/05/01 10:00:00", "%Y/%m/%d %H:%M +:%S"); $now = localtime(time); print "Time now is:$now\n"; $diff = $now - $before; $years = int($diff->years); $diff -= $years * ONE_YEAR; $months = int($diff->months); $diff -= $months * ONE_MONTH; $days = int($diff->days); $diff -= $hours * ONE_HOUR; $hours = int($diff->hours); $diff -= $minutes * ONE_MINUTES; $minutes = int($diff->minutes); $diff -= $minutes * ONE_SECONDS; $secounds = int($diff->seconds); print "$years years, $months months, $days days since , $hours hours s +ince, $minutes min since $before\n";
      This is a problem with daylight-saving. Time::Piece->strptime creates a GMT date, which you're then comparing to localtime()'s representation of time(), so it appears to be an hour out. You'll probably need to use the isdst() function to add ONE_HOUR if necessary. BTW, there's another slight cut'n'paste error - you need to subtract $days * ONE_DAY as well, and then swap the rest of your lines to the bottom of that block.

      Hope this helps
      Ben.
      Opps spotted a typo $diff -= $minutes * ONE_SECONDS;
      should have been $diff -= $seconds * ONE_SECONDS;
      but this has made no difference with my minute problem?

      Regards,
      Gareth

More Help please ? Time::Piece errors?
by spacey (Scribe) on May 01, 2003 at 10:42 UTC
    Only me again
    i have notice that the line
    $now = localtime(time);
    actually gives the time in the format of
    %a %b %e %H:%M:%S %Y So i have changed my "$before line" to:

    $before = Time::Piece->strptime("Thu May 1 10:00:00 2003", "%a %b %e %H:%M:%S %Y");
    This change still makes no difference.

    Time now is:Thu May 1 11:37:33 2003
    Time before:Thu May 1 10:00:00 2003
    0 years, 0 months, 0 days since , 0 hours since, 37 min since Thu May 1 10:00:00 2003

    So anybody able to offer some advice?
    Regards, Gareth

      Let me qualify this answer by saying I don't really use Time::Piece. I just downloaded, compiled, and installed it. The difference I was getting was a bit larger than yours... I was getting an 8 hour difference. Coincidentally, that's my offset from GMT. I made sure my TZ environment variable was set correctly and changed the assignment to $now to this:

      $now = locatime() + $before->tzoffset;
      And that seemed to do the trick. You might try the same change and find that it magically works.

      By the way, you have a couple other little errors, I think. You subtract $hours * ONE_HOUR before you determine $hours and you neglect to subtract $days * ONE_DAY. You also add an 'S' on to the constant for ONE_MINUTE. And there doesn't appear to be a constant for ONE_SECOND, which only makes sense if you think about it. It just so happens that none of these would have affected your testing so far.

      -sauoq
      "My two cents aren't worth a dime.";
      
        In the words and stile of a grateful man:-
        Thank you, and thank you again.

        I have made the changes that everybody kindly suggested
        And thanks to all who noticed my incorrect calculations.
        This steams from to points.
        1st, i copied bits of the code of different help notes and tried to cobble it all together.
        2nd, I'm honestly not sure how this code is working when it comes to the lines of
        $months = int($diff->months); #i know int insure an integer.
        In this case dumping the number of seconds that are left over?

        $diff -= $months * ONE_MONTH; # but this line confuses me?

        Where does the "ONE_MONTH" variable get pulled from?
        ie how do i know if i got the right name?

        is it lifted from the Time::Piece pm
        ie this bit:
        use constant 'c_sec' => 0;
        use constant 'c_min' => 1;
        use constant 'c_hour' => 2;
        use constant 'c_mday' => 3;
        use constant 'c_mon' => 4;
        use constant 'c_year' => 5;
        use constant 'c_wday' => 6;
        use constant 'c_yday' => 7;
        use constant 'c_isdst' => 8;
        use constant 'c_epoch' => 9;
        use constant 'c_islocal' => 10;

        or is my understanding of this as closes to my understanding of rocket since.

        One thing i using code and another understanding it :(

        One final thing If I may. I have copy my final code below. Can anybody see any other faults I have missed/misunderstood?

        Final I would like to thank again everybody who has help me understand this.

        Regards,
        Gareth

        #!/usr/bin/perl use Time::Piece; use Time::Seconds; $before = Time::Piece->strptime("Thu Apr 1 10:00:00 2003", "%a %b %e % +H:%M:%S %Y"); $now = localtime(time) + $before->tzoffset; print "Time now is:$now\n"; # gives time format "Thu May 1 13:11:11 +2003" print "Time before:$before\n"; # gives time format of "Thu May 1 13:1 +1:11 2003" # (dont forget timezone) $diff = $now - $before; $years = int($diff->years); $diff -= $years * ONE_YEAR; $months = int($diff->months); $diff -= $months * ONE_MONTH; $days = int($diff->days); $diff -= $days * ONE_DAYS; $hours = int($diff->hours); $diff -= $hours * ONE_HOUR; $minutes = int($diff->minutes); $diff -= $minute * ONE_MINUTE; print "$years years, $months months, $days days, $hours hours, $minute +s minutes since $before\n";