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

Esteemed monks,

I have an application where I am usinf Time::Piece to parse times and do some calculations based on them. But today I discovered a wierdness which leads me to believe that the module does not understand Time at all!

If I take a localtime and create an object $t, then I take a string and parse it using either the

Time::Piece->from_mysql_datetime()
method or the
Time::Piece->strptime()
method, then next I subtract one from the other. I always get a difference which is 5 hours too great! The following code will demonstrate the problem (Well I hope it will on your machine!) I just need a sanity check form someone before I send this to the module author.

The code

#!/usr/local/bin/perl use strict; use warnings; use Time::Piece::MySQL; use Time::Seconds; use Data::Dumper; my $time = localtime; my $future_time = $time + ONE_HOUR; my $diff = $future_time - $time; print "Local time: " . $time . "\n"; print "Future time: " . $future_time . "\n"; print "Difference: " . $diff->hours . "hours \n"; print Dumper( $time, $future_time, $diff); my $test_time = Time::Piece->from_mysql_datetime( '2004-03-05 20:30:00 +' ); print Dumper $test_time; print "Test time: " . $test_time . "\n"; $diff = $test_time - $time; print $diff->minutes . " minutes\n"; $test_time = Time::Piece->strptime( '2004-03-05 20:30:00', '%Y-%m-%d % +H:%M:%S' ); print Dumper $test_time; print "Test time: " . $test_time . "\n"; $diff = $test_time - $time; print $diff->minutes . " minutes\n";
My sample output:
[root@posiedon3 johnday]# perl timepiece.pl Local time: Fri Mar 5 20:44:17 2004 Future time: Fri Mar 5 21:44:17 2004 Difference: 1hours $VAR1 = bless( [ 17, 44, 20, 5, 2, 104, 5, 64, 0, 1078537457, 1 ], 'Time::Piece' ); $VAR2 = bless( [ 17, 44, 21, 5, 2, 104, 5, 64, 0, 1078541057, 1 ], 'Time::Piece' ); $VAR3 = bless( do{\(my $o = 3600)}, 'Time::Seconds' ); $VAR1 = bless( [ 0, 30, 20, 5, 2, 104, 5, 64, 0, undef, 0, 0 ], 'Time::Piece' ); Test time: Fri Mar 5 20:30:00 2004 -314.283333333333 minutes $VAR1 = bless( [ 0, 30, 20, 5, 2, 104, 5, 64, 0, undef, 0, 0 ], 'Time::Piece' ); Test time: Fri Mar 5 20:30:00 2004 -314.283333333333 minutes
Could someone please check what I have done? Many thanks!

jdtoronto

Replies are listed 'Best First'.
Re: Time::Piece wierdness - sanity check please!
by tachyon (Chancellor) on Mar 06, 2004 at 02:00 UTC

    You are trying to use the string returned by a scalar context call to locatime() as the integer unix epoch time you really mean. You can't add ONE_HOUR to 'Sat Mar 6 01:55:26 2004' and get what you want!

    my $time = localtime; # you mean $time = time(); print $time, $/; $time = time(); print $time, $/; __DATA__ Sat Mar 6 01:55:26 2004 1078538126

    cheers

    tachyon

      Time::Piece over-rides localtime and gmtime, ONE_HOUR is a constant exported by Time::Seconds.

      $t = localtime - returns an object which, when stringified, behaves the same as the original localtime.

      The time arithmetic is not the problem, the problem is when you parse a datetime you get a value which is being treated as though it were GMT. Hence the difference values being calculated later which are 5 hours greater than the actual difference between the times.

      jdtoronto

        Yes, I get equivalent results -- I seem to be in the same time zone as you, and get a similar 5-hour "padding" of the difference between "localtime" and a given time string passed to either "from_mysql_datetime" or "strptime". Regarding this latter method, the Time::Piece man page says "see the [unix] strptime man page", and when I looked at that (on MacOSX BSD), it said "The resulting values will be relative to the local time zone." But it looks like Time::Piece is not treating it as such -- instead, it's treating the values as relative to GMT -- and so is behaving badly (or at least counter to apparent intention, which is bad).