Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

DateTime::Duration not adding seconds appropriately

by biohisham (Priest)
on Sep 01, 2013 at 10:26 UTC ( [id://1051781]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks after a wee while. Well, I have a weird situation with DateTime::Duration when I am adding values of the format [hh:mm::ss] together. The normalization of the seconds into minutes after 59 seems to be not doing what should exactly be done when the total of the seconds is over 59. In my case seconds just get added up well beyond this rather than be converted to minutes. However, minutes seamlessly get converted into hours with no glitches. I tried a few tricks in the documentation of DateTime::Duration regarding the addition of [hh:mm::ss] values but all producing the same result.

Is this a behavior to expect with this module ? or it got to do with the way I wrote my program. And since it is one of the very robust modules out there I still saw that many other similar modules offering date and time calculation don't weigh in such straightforward additions and subtraction of values

Here's a functioning code that reproduces the case I am describing. After adding up all the hh:mm::ss values I get this output Total Time: 11:57:300

use strict; use warnings; use DateTime::Duration; my $duration = DateTime::Duration->new(); while(<DATA>){ chomp; my ($hour, $minute, $second); my @time = split(':',$_); if(scalar @time == 3){ ($hour, $minute, $second) = @time; $duration->add(hours=>$hour, minutes=>$minute, seconds=>$secon +d); }elsif(scalar @time == 2){ $hour =0; ($minute, $second) = @time; $duration->add(hours=>$hour, minutes=>$minute, seconds=>$s +econd); } } print "Total Time: ", join(':',$duration->in_units('hours', 'minutes', +'seconds')),$/; __DATA__ 5:43:05 01:18 11:25 36:31 17:14 20:14 07:55 06:50 00:40 23:33 1:18:02 2:55:13

David R. Gergen said "We know that second terms have historically been marred by hubris and by scandal." and I am a two y.o. monk today :D, June,12th, 2011...

Replies are listed 'Best First'.
Re: DateTime::Duration not adding seconds appropriately
by ww (Archbishop) on Sep 01, 2013 at 11:57 UTC

    There's definitely a doc_smell here, but could the part I've emphasized (from perldoc DateTime:Duration) the documentation for this problem?

    $dur->in_units( ... )
      Returns the length of the duration in the units (any of those that can
      be passed to "new") given as arguments. All lengths are integral, but
      may be negative. Smaller units are computed from what remains after
      taking away the larger units given, so for example:
    
        my $dur = DateTime::Duration->new( years => 1, months => 15 );
    
        $dur->in_units( 'years' );            # 2
        $dur->in_units( 'months' );           # 27
        $dur->in_units( 'years', 'months' );  # (2, 3)
        $dur->in_units( 'weeks', 'days' );    # (0, 0) !
    
      The last example demonstrates that there will not be any conversion
      between units which don't have a fixed conversion rate. 
    The only conversions possible are:
    
      *       years <=> months
    
      *       weeks <=> days
    
      *       hours <=> minutes
    
      *       seconds <=> nanoseconds
    
      For the explanation of why this is the case, please see the How DateTime
      Math Works section of the DateTime.pm documentation

    My speculation here seems almost non-sensical ... except for the module author's repeated comment, "...if you are trying to generate output suitable for humans, use the "DateTime::Format::Duration" module."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1051781]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2025-01-14 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (38 votes). Check out past polls.