On Sat, 8 Nov 2003, Jonathan Swartz wrote:

> I was trying to compare two durations, and came up with some surprises.
>
>   DB<1> use DateTime;
>
>   DB<2> $hour = DateTime::Duration->new(hours=>1);
>
>   DB<3> $minute = DateTime::Duration->new(minutes=>1);
>
>   # Try comparing $hour and $minute
>   DB<4> print ($hour < $minute)
> 1   # Whaaaa??

You just compared the numeric values of the two refs.

>   DB<10> $d4 = $minute - $hour;
>
>   DB<11> print $d4->is_positive
> 1   # Whaaaaa??

I think I need to rework this class a little bit.  The
is_negative/positive/zero methods all make sense for durations constructed
via new(), because they are not allowed to mix positive and negative
units.  But by using the add & subtract methods/overloading, it is
possible to construct an object which has both.

One solution would be to simply make these methods return false for
objects which we can't be sure about (anything which has had math done to
it and now has mixed units), so both is_positive and is_negative would
return false.

The other might be to simply remove these methods, since I'm not sure how
useful they are.

> I couldn't find anything about this in the documentation. I can understand
> after some reflection why you shouldn't be able to compare durations (e.g.
> how would you compare 30 days to 1 month), but it seems like this should be
> documented, and using comparison operator should fail. Let me know if I'm
> missed something obvious...

Well, comparison fails because it's not overloaded at all, but I'll add an
explicit note about that to the docs.


-dave

/*=======================
House Absolute Consulting
www.houseabsolute.com
=======================*/

Reply via email to