Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the DateTime CPAN distribution.

Report information
The Basics
Id: 8160
Status: resolved
Priority: 0/
Queue: DateTime

People
Owner: Nobody in particular
Requestors: simonw [...] simonwilcox.co.uk
Cc:
AdminCc:

Bug Information
Severity: Critical
Broken in: 0.22
Fixed in: (no value)



Subject: DateTime::Duration reports incorrect weeks
According to the docs, the following code should return 105 (or maybe 104 but that's why I need the code to work :) weeks but it in fact returns 4, which I think is the difference after subtracting months but it's suppoed to be just the number of weeks in total. Months seems about right but Days is also wrong, representing the number of days in the weeks that it thinks it has. Simon. simonw@kryten:~/perl/svg$ cat dtbug.pl #! /usr/bin/perl use DateTime; my $start = DateTime->new( year => 2002, month => 9, day => 3 ); my $end = DateTime->new( year => 2004, month => 8, day => 31 ); my $diff = $end - $start; print "First : ".$start->dmy."\n"; print "Latest : ".$end->dmy."\n"; print "Diff : ".$diff->in_units( 'months' )." months\n"; print "Diff : ".$diff->in_units( 'weeks' )." weeks\n"; print "Diff : ".$diff->in_units( 'days' )." days\n"; simonw@kryten:~/perl/svg$ perl dtbug.pl First : 03-09-2002 Latest : 31-08-2004 Diff : 23 months Diff : 4 weeks Diff : 28 days simonw@kryten:~/perl/svg$
Date: Mon, 1 Nov 2004 20:16:05 -0600 (CST)
From: Dave Rolsky <autarch [...] urth.org>
To: Guest via RT <bug-DateTime [...] rt.cpan.org>
Subject: Re: [cpan #8160] DateTime::Duration reports incorrect weeks
RT-Send-Cc:
On Thu, 28 Oct 2004, Guest via RT wrote: Show quoted text
> According to the docs, the following code should return 105 (or maybe > 104 but that's why I need the code to work :) weeks but it in fact > returns 4, which I think is the difference after subtracting months but > it's suppoed to be just the number of weeks in total. Months seems about > right but Days is also wrong, representing the number of days in the > weeks that it thinks it has.
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. There are a couple solutions: 1. Use the delta_days method do produce a duration object which _only_ contains days. 2. Use DateTime::Format::Duration. If you could point me at the point in the docs that made you think what you wanted to would work that'd be great, because then I can try to make that part clearer. -dave /*=========================== VegGuide.Org Your guide to all that's veg. ===========================*/