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


In reply to Time::Piece wierdness - sanity check please! by jdtoronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.