[ moving this onto the datetime list because I know it's not a bug ... ]
On Mon, 1 Nov 2004, [EMAIL PROTECTED] via RT wrote:
I think there may be some confusion here. When you just subtract two dates you get a duration that has all possible units it could have. The in_units method can only convert between two units which have a fixed conversion (7 days = 1 week, 12 months = 1 year, etc). It cannot convert between months & days, because these are not fixed.
But it doesn't give me the right number of days.
Yes it does.
It says:
* 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 inte- gral, 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)
Note that the numbers returned by this method may not match the values given to the constructor.
As I read that, in_units( 'days' ) should tell me how many units of 24 hours there are in the period but it doesn't.
It does, but it doesn't convert months to days. How could it do that? Each month has a different number of days.
Likewise, in_units( 'weeks' ) should tell me how many whole weeks are in the period and in_units( 'weeks', 'days' ) should give me the number of whole weeks and the remaining days.
It does. But if the duration has 2 months & 8 days, then it only has 1 week, not 9 (or 10).
in fact it seems to ignore weeks entirely and give me the remaining days after the months have been taken off *always* (but how can it know unless the duration has some concept of "place" in time ?), and not the total number of days in the period as I would expect from those docs.
Actually, that's backwards. It could only know how to convert months to days (in the duration) if the duration had an "anchor" from which to calculate. This anchor exists when you did your date subtraction originally, so by default you get a duration with months, days, minutes, seconds, & nanoseconds.
If you just want days, you can use the delta_days() method.
I actually found another way around it in this case but fixing it would be good for future work.
There's nothing to fix. This is all documented in the "How Date Math is Done" section of the DateTime.pm docs. I think the docs for in_unit in DateTime::Duration could be improved so as to be less confusing, however.
-dave
/*=========================== VegGuide.Org Your guide to all that's veg. ===========================*/