This was originally to Dave directly. A second mail with a tentative patch that somewhat "fixes" the problem discussed below follows in a second mail (that I also sent to Dave directly and will forward separately). Please comment but CC me, as I don't follow the list!

-max

-------- Original Message --------
Subject: DateTime::Duration comparisions
Date: Tue, 11 Nov 2003 14:49:04 +0100
From: Max Maischein <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]

Hello Dave,

I just dabbled a bit with DateTime, comparisions and
DateTime::Durations, and I found some things that I don't understand or
did not find in the documentation:

The delta_* methods are not really usefull, since in 90% of the time, I
just want to know if one duration is longer than another - having to do
several comparisions just to find that out is tedious.

I saw in the CHANGES file that you added some other delta_* methods, but
didn't find them in the documentation, so I don't know about these, and
was too lazy to delve into the source for this :-)

I wrote a test file that shows some ugly bugs when dealing with my naive
comparision, most notably that ($dt-$dt)->is_zero() is false. I don't
know why you implemented is_zero() that way, so I can't propose any good
change. My approach would be to make is_zero() not only check the sign
but also check whether all values are zero.

I wrote a simple DateTime::Duration comparision and hacked it into
DateTime::Duration, but my tests fail and I don't understand enough of
the DateTime mechanics to fix them. I saw some references to the
DateTime::Duration problems on the mailing list, but I saw that you
considered removing the comparision operators completely from
DateTime::Duration - I think this would be bad, but I only ended up
looking at DateTime::Duration, as I could not find an easy way to look
at two DateTime objects and find out which one was later, as comparision
for those also isn't implemented (or, once again, I'm too stupid to read
the docs properly).

Can you please take a look at my comparision patch, the included tests
(especially the is_zero() test is bothering me), and tell me where I am
wrong :-)

Thanks for writing DateTime,
-max

--- DateTime.pm
sub _cmp_datetime {
  my ($left,$right,$reverse) = @_;
  ($left,$right) = ($right,$left) if $reverse;
  my $diff = $right - $left;
  warn sprintf "Left  is  %s days and %s minutes.\n",
$left->delta_days,  $left->delta_minutes;
  warn sprintf "Right is  %s days and %s minutes.\n",
$right->delta_days, $right->delta_minutes;
  warn "positive"
    if $diff->is_positive;
  warn "negative"
    if $diff->is_negative;
  return 0 if $diff->is_zero;
  return 1 if $diff->is_positive;
  return -1 if $diff->is_negative;
  die "This should never happen";
};
---
The test file
---
#!/usr/bin/perl -w
use strict;
use Test::More tests => 7;
use DateTime;
use Data::Dumper;

my $one_hour = DateTime::Duration->new( hours => 1 );

my $one_day_plus_something = DateTime->from_epoch(epoch =>
24*3600+19*60) - DateTime->from_epoch( epoch => 0);

ok( $one_day_plus_something > $one_hour, "One day plus something takes
longer than one hour" )
  or dump_duration( $one_day_plus_something, $one_hour);
is( $one_hour, $one_hour, "One hour equals itself" )
  or dump_duration($one_hour);
ok( $one_hour == $one_hour, "One hour equals itself (via ==)" )
  or dump_duration($one_hour);
is( $one_day_plus_something, $one_day_plus_something, "One day plus
something equals itself" )
  or dump_duration($one_day_plus_something);
ok( ($one_day_plus_something - $one_day_plus_something)->is_zero, "A
difference subtracted from itself is zero" )
  or dump_duration($one_day_plus_something - $one_day_plus_something);
ok( $one_day_plus_something == $one_day_plus_something, "One day plus
something equals itself (via ==)" )
  or dump_duration($one_day_plus_something);

ok( $one_hour < $one_day_plus_something, "One hour is shorter than one
day plus something" );

sub dump_duration {
  diag Dumper($_) for @_;
};






Reply via email to