Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Monday July 10, 2006
08:45 AM

DateTime::Interval does not DWIM

[ #30240 ]

So I have these two DateTime objects, and I subtract them to get an interval. Great! Now I want to find out how long that interval is in minutes.

Uh, you can't, because my interval spans nearly three days' worth of time. And since in some weird edge cases a day might not equal 24 hours, DateTime::Interval therefore provides no method at all for letting me get the length of my interval in minutes making standard assumptions.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The annoying workaround is to do $duration->days * 24 + $duration->minutes or something similar. I'd rather it gave me that automatically and warned about any potential consequences, either that or worked it out correctly. Also it's called DateTime::Duration not interval iirc
    --

    @JAPH = qw(Hacker Perl Another Just);
    print reverse @JAPH;
  • Hello,

    You might want to consider something along these lines.

    use strict;
    use warnings;
    use DateTime;
    use DateTime::Format::Duration;
    use Data::Dumper;

    my $dt1 = DateTime->now(
              time_zone => 'UTC',
             );

    my $dt2 = DateTime->new(
            year  => 2006,
            month => 10,
            day => 31,
            time_zone =>'UTC',
        );

    my $delta = $dt1-
  • There's a module called DateTime::Format::Duration that should do what you want. Unfortunately, it needs some work to pass its tests with the latest DateTime.pm.
    • The last time I checked (this morning), it did not do what I wanted. It could tell me the minutes component of the interval, but it could not convert the days, hours, and minutes to a total number of minutes. Unless I'm missing something, and if so, please point it out, because you will really help me.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • Have you seen my post on this?
        • Yes, I did, and thank you! I'd discovered delta_ms by that time. However, it still does not really DWIM. If I have real Date objects, I should be able to subtract them and get an interval that can be converted to whatever resolution or units I want. The problem then is that I have to do something unintuitive with my DateTimes: intuitively, I had written

          $end - $start

          and expected to get something that I could express as minutes. I shouldn't have to perform something other than subtraction in order to

          --
          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • I think you'd be best served by writing a message to datetime@perl.org outlining what you want to do.