DateTime::Interval does not DWIM

jdavidb on 2006-07-10T13:45:31

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.


annoying workaround

TeeJay on 2006-07-10T15:48:59

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

DateTime::Duration does not DWIM

monsterzero on 2006-07-10T17:13:33

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->delta_ms($dt2);

print Dumper($delta), "\n";
my $d = DateTime::Format::Duration->new(
        pattern => '%M minutes'
);
print $d->format_duration($delta);

Here I used the delta_ms method (see the docs) to create the duration. Also don't forget to use the format_duration method for getting at the value of minutes :-)

I hope this helps.

DateTime::Format::Duration

autarch on 2006-07-10T19:59:10

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.

Re:DateTime::Format::Duration

jdavidb on 2006-07-10T20:38:42

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.

Re:DateTime::Format::Duration

monsterzero on 2006-07-10T20:49:25

Have you seen my post on this?

Re:DateTime::Format::Duration

jdavidb on 2006-07-10T20:58:59

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 get such a result.

But at least it is possible, and for that I am grateful. :)

Re:DateTime::Format::Duration

autarch on 2006-07-10T20:55:41

I think you'd be best served by writing a message to datetime@perl.org outlining what you want to do.