in reply to Time::Piece errors?

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

Replies are listed 'Best First'.
Re: More Help please ? Time::Piece errors?
by sauoq (Abbot) on May 01, 2003 at 10:58 UTC

    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";