RE: DateTime Subtraction

2005-01-27 Thread Hill, Ronald
Hi Todd,

Todd Goldenbaum wrote:
 
 I'm curious now though, what is $duration-days used for,
 if not for expressing the length in days of a $duration?
 
From the Docs:

These methods return numbers indicating how many of the given unit
the object represents, after having taken away larger units.
   
The key here is after having taken away larger units
using your example you were expecting the days unit to
be set to 18 however, that can also be expressed as 2 weeks
and 4 days. Which is what DateTime is doing:

 
use strict;
use warnings;
use DateTime;
use DateTime::Format::MySql;


my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05' );
my $end_date = DateTime::Format::MySQL-parse_date( '2005-01-23' );

my $dur = $start_date-delta_days( $end_date );

print Number of weeks is:,$dur-weeks ,\n;
print Number of days is:, $dur-days;

prints
F:\scriptsmy.pl
Number of weeks is:2
Number of days is:4
F:\scripts
So what is needed is a way to tell DateTime to convert all units
to just one, in your case days. Hence the in_units method
needs to be used. I hope this helps.

Thanks

Ron Hill


Re: DateTime Subtraction

2005-01-27 Thread Todd Goldenbaum
Ron, that makes perfect sense.  Thanks!
Todd
On Jan 27, 2005, at 7:06 AM, Hill, Ronald wrote:
Hi Todd,
Todd Goldenbaum wrote:
I'm curious now though, what is $duration-days used for,
if not for expressing the length in days of a $duration?
From the Docs:
These methods return numbers indicating how many of the given unit
the object represents, after having taken away larger units.
The key here is after having taken away larger units
using your example you were expecting the days unit to
be set to 18 however, that can also be expressed as 2 weeks
and 4 days. Which is what DateTime is doing:
use strict;
use warnings;
use DateTime;
use DateTime::Format::MySql;
my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05' );
my $end_date = DateTime::Format::MySQL-parse_date( '2005-01-23' );
my $dur = $start_date-delta_days( $end_date );
print Number of weeks is:,$dur-weeks ,\n;
print Number of days is:, $dur-days;
prints
F:\scriptsmy.pl
Number of weeks is:2
Number of days is:4
F:\scripts
So what is needed is a way to tell DateTime to convert all units
to just one, in your case days. Hence the in_units method
needs to be used. I hope this helps.
Thanks
Ron Hill



Re: DateTime Subtraction

2005-01-27 Thread Dave Rolsky
On Wed, 26 Jan 2005, Leonardo Herrera wrote:
Todd Goldenbaum wrote:
I see now, that did the trick.  My mistake was assuming that 
$duration-days would give the same result as $duration-delta_days( 
$start_date ) or $duration-in_units('days').

I'm curious now though, what is $duration-days used for, if not for 
expressing the length in days of a $duration?
I *believe* it is so you can say X months, Y days and Z hours or things 
like that.
More like 2 weeks and 4 days.  If I'd known Rick Measham was going to 
write DateTime::Format::Duration, which does a much better job of turning 
durations into different outputs, I'd never have put these methods in, and 
only put in the minimum needed for math.

Oh well, hindsight and all that.  I think I'll add a big pointer to 
DT::F::Duration in the DT::Duration docs though.

-dave
/*===
VegGuide.Orgwww.BookIRead.com
Your guide to all that's veg.   My book blog
===*/


Re: DateTime Subtraction

2005-01-26 Thread Todd Goldenbaum
On Jan 25, 2005, at 8:57 AM, Hill, Ronald wrote:
Hi Todd,
Todd Goldenbaum wrote:
Hello,
I'm trying to find the difference betwen two datetime
objects, but I'm
getting some unexpected results.  I start with two dates out of mysql
(code simplified  a bit for this example):
   my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05'
   ); my $end_date = DateTime::Format::MySQL-parse_date(
'2005-01-23' );
Then I use one of these two techniques to perform the subtraction:
   my $duration = $start_date-delta_days( $end_date );
   my $duration = $end_date - $start_date;
Expecting to get a $duration of 18 days.  But with either technique,
the resulting $duration-days value is 4 instead of the expected 18.
And when I ask for $duration-hours, it gives me 0.
Further experimentation with the two dates involved reveals that
subtracting two dates that are within a few days of each
other seems to
work, but at a certain point it breaks down and gives erroneous
results.  Any idea what could be going on here?
Thanks,
Todd
There are a couple of ways to do this;
use strict;
use warnings;
use DateTime;
use DateTime::Format::MySql;
my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05' );
my $end_date = DateTime::Format::MySQL-parse_date( '2005-01-23' );
my $dur = $end_date-delta_days( $start_date );
print $dur-delta_days;
gives the correct result of 18
or
using your example above
my $duration = $end_date - $start_date;
print $duration-in_units('days');
gives the same results.
I see now, that did the trick.  My mistake was assuming that 
$duration-days would give the same result as $duration-delta_days( 
$start_date ) or $duration-in_units('days').

I'm curious now though, what is $duration-days used for, if not for 
expressing the length in days of a $duration?

Todd



Re: DateTime Subtraction

2005-01-26 Thread Leonardo Herrera
Todd Goldenbaum wrote:
I see now, that did the trick.  My mistake was assuming that 
$duration-days would give the same result as $duration-delta_days( 
$start_date ) or $duration-in_units('days').

I'm curious now though, what is $duration-days used for, if not for 
expressing the length in days of a $duration?
I *believe* it is so you can say X months, Y days and Z hours or 
things like that.

Regards,
--
Leonardo Herrera L.
mailto:[EMAIL PROTECTED]


RE: DateTime Subtraction

2005-01-25 Thread Hill, Ronald

Hi Todd,

Todd Goldenbaum wrote:
 Hello,
 
 I'm trying to find the difference betwen two datetime
 objects, but I'm
 getting some unexpected results.  I start with two dates out of mysql
 (code simplified  a bit for this example):
 
my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05'
); my $end_date = DateTime::Format::MySQL-parse_date(
 '2005-01-23' ); 
 
 Then I use one of these two techniques to perform the subtraction:
 
my $duration = $start_date-delta_days( $end_date );
 
my $duration = $end_date - $start_date;
 
 Expecting to get a $duration of 18 days.  But with either technique,
 the resulting $duration-days value is 4 instead of the expected 18.
 And when I ask for $duration-hours, it gives me 0.
 
 Further experimentation with the two dates involved reveals that
 subtracting two dates that are within a few days of each
 other seems to
 work, but at a certain point it breaks down and gives erroneous
 results.  Any idea what could be going on here?
 
 Thanks,
 Todd
There are a couple of ways to do this;
use strict;
use warnings;
use DateTime;
use DateTime::Format::MySql;


my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05' );
my $end_date = DateTime::Format::MySQL-parse_date( '2005-01-23' );

my $dur = $end_date-delta_days( $start_date );
print $dur-delta_days;

gives the correct result of 18

or

using your example above

my $duration = $end_date - $start_date;
print $duration-in_units('days');
gives the same results.


However, after checking the docs for this function. I found this:

The Cdelta_md and Cdelta_days methods truncate the duration so
that any fractional portion of a day is ignored.  The Cdelta_ms
method converts any day and month differences to minutes.

Unlike the subtraction methods, Bthese methods always return a
positive (or zero) duration.
^^^
but if I do thisuse strict;
use warnings;
use DateTime;
use DateTime::Format::MySql;


my $start_date = DateTime::Format::MySQL-parse_date( '2005-01-05' );
my $end_date = DateTime::Format::MySQL-parse_date( '2005-01-23' );

my $dur = $start_date-delta_days( $end_date );

if ($dur-is_positive) {print it's positive\n}
if ($dur-is_negative) {print it's negative\n}
print $dur-delta_days;

I get this:
F:\scriptsmy.pl
it's negative
-18

Which is correct but does not match what the docs say.
Anyway I hope this helps.

Ron Hill