Re: DT::Duration overloads

2004-06-09 Thread Matt Sisk
Dave Rolsky wrote:
Also, I'd like to point out that this really doesn't have as much to do
with the internals as it does with the nature of date & time math.
You cannot expect to understand date math without understanding that it's
not possible to convert between various units of date/time, in particular
from months to days or vice versa.  This would be a problem regardless of
the internals, right?
 

I do understand the ambiguities when you are dealing with various forms 
of durations that are not anchored to an actual point in time. As I said 
in my last message, however, we're talking about two absolute points in 
time -- no ambiguity.

Rephrased, the question is again: what is the midpoint of a span? (and 
if we come up with a good approach to this problem, it should solve the 
general class of problems having to do with portioning out spans).

It's not immediately apparent to me what the solution is -- I know what 
I want to do, and would prefer it to be fairly easy and straightforward. 
This seems to be an area of the interface that could use some brushing 
up rather than some intrinsic ambiguity of date math.

(I'm willing to meditate on possible solutions myself -- I'm not merely 
asking you to solve it for me. However, I just thought I might be 
missing something obvious)

Thanks again,
Matt


Re: DT::Duration overloads

2004-06-09 Thread Matt Sisk
Dave Rolsky wrote:
Well, if you just want the _date_, it's pretty easy.
my $dur = $dt1->delta_days($dt2);
# or use Math::Round if you want
my $mid = $dt1->add( days => int( $dur->delta_days / 2 ) );
If you want to account for the time then it gets funkier.
Hmm, indeed.
I need to approximate solar noon by deriving the midpoint between 
sunrise and sunset. I also need to approximate solar midnight by looking 
at the sunset from the prior day.

As a general problem, finding a fractional time between two points in 
time is not that unusual. I *suppose* I could convert to epoch, take the 
diff, and use that to create a new duration. But it seems unfortunate 
that I'd have to step out of the datetime API like that. The two 
endpoints are known quantities -- there is no abiguity in selecting a 
midpoint.

Is this sort of thing something that should exist in spans rather than 
expecting regular date math to handle?

Matt


Re: DT::Duration overloads

2004-06-09 Thread Dave Rolsky
On Thu, 10 Jun 2004, Matt Sisk wrote:

> What I'd like to do is simply find the midpoint, more or less, between
> two arbitrary datetimes. Off the cuff, knowing nothing about the
> internals (which I do, but I'm pretending not to) I'd think this:

Also, I'd like to point out that this really doesn't have as much to do
with the internals as it does with the nature of date & time math.

You cannot expect to understand date math without understanding that it's
not possible to convert between various units of date/time, in particular
from months to days or vice versa.  This would be a problem regardless of
the internals, right?


-dave

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


Re: DT::Duration overloads

2004-06-09 Thread Dave Rolsky
On Thu, 10 Jun 2004, Matt Sisk wrote:

> What I'd like to do is simply find the midpoint, more or less, between
> two arbitrary datetimes. Off the cuff, knowing nothing about the
> internals (which I do, but I'm pretending not to) I'd think this:
>
>   $mid = $dt1 + ($dt2 - $dt1)/2
>
> to dwim.
>
> However, as you say, things aren't really well defined the way durations
> are defined internally at the moment.
>
> So the question becomes -- if the above is not the datetime idiom for
> finding a midpoint between two datetimes, then what is?

Well, if you just want the _date_, it's pretty easy.

 my $dur = $dt1->delta_days($dt2);

 # or use Math::Round if you want
 my $mid = $dt1->add( days => int( $dur->delta_days / 2 ) );

If you want to account for the time then it gets funkier.


-dave

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


Re: DT::Duration overloads

2004-06-09 Thread Matt Sisk
Dave Rolsky wrote:
Thinking about this more, I'm considering maybe just requiring that
multiplication be passed an integer, because if you do this:
 

What I'd like to do is simply find the midpoint, more or less, between 
two arbitrary datetimes. Off the cuff, knowing nothing about the 
internals (which I do, but I'm pretending not to) I'd think this:

 $mid = $dt1 + ($dt2 - $dt1)/2
to dwim.
However, as you say, things aren't really well defined the way durations 
are defined internally at the moment.

So the question becomes -- if the above is not the datetime idiom for 
finding a midpoint between two datetimes, then what is?

Thanks,
Matt


Re: DT::Duration overloads

2004-06-09 Thread Dave Rolsky
On Wed, 9 Jun 2004, Matt Sisk wrote:

> I understand that division can be expressed as multiplication, but is
> there any particular reason why division (/) is not overloaded but
> multiplication is for durations? Then you could say:
>
>   $midpoint = ($dt2 - $dt1)/2;
>
> rather than
>
>   $midpoint = ($dt2 - $dt1) * 0.5;
>
> Small thing. Just curious.

Thinking about this more, I'm considering maybe just requiring that
multiplication be passed an integer, because if you do this:

 my $dur = DateTime::Duration->new( months => 1, days => 1, minutes => 1 );
 $dur->multiple(.5);

 print DateTime->now->add_duration($dur)->datetime;

The results are kind of weird, and certainly not what anyone would expect.


-dave

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


Re: DT::Duration overloads

2004-06-09 Thread Dave Rolsky
On Thu, 10 Jun 2004, Rick Measham wrote:

> On 10 Jun 2004, at 9:25 AM, Dave Rolsky wrote:
> > What is half a minute?  How long is half a month?
>
> $dtd = DateTime::Duration->new(
>   months  => 1,
>   minutes => 1,
> );
>
> $half_dtd = $dtd / 2;
>
> print $half_dtd->months . "\n";
> # 0.5
>
> print $half_dtd->seconds . "\n";
> # 0.5
>
> print strfduration(
>   normalise => 'ISO',
>   pattern   => '%Y years, %m months, %e days, %H hours, %M minutes, %S
> seconds',
>   duration  => $half_dtd
> );
> # 0 years, 0 months, 15 days, 0 hours, 0 minutes, 30 seconds

Great, now what should DateTime.pm do in the add_duration method?


-dave

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


Re: DT::Duration overloads

2004-06-09 Thread Rick Measham
On 10 Jun 2004, at 9:25 AM, Dave Rolsky wrote:
What is half a minute?  How long is half a month?
$dtd = DateTime::Duration->new(
months  => 1,
minutes => 1,
);
$half_dtd = $dtd / 2;
print $half_dtd->months . "\n";
# 0.5
print $half_dtd->seconds . "\n";
# 0.5
print strfduration(
	normalise => 'ISO',
	pattern   => '%Y years, %m months, %e days, %H hours, %M minutes, %S 
seconds',
	duration  => $half_dtd
);
# 0 years, 0 months, 15 days, 0 hours, 0 minutes, 30 seconds


Senior Developer
PrintSupply - Print Procurement & Supply Management
18 Greenaway Street VIC 3105
Tel: (03) 9850 3255
Fx: (03) 9850 3277
http://www.printsupply.com.au/


Re: DT::Duration overloads

2004-06-09 Thread Dave Rolsky
On Wed, 9 Jun 2004, Matt Sisk wrote:

> I understand that division can be expressed as multiplication, but is
> there any particular reason why division (/) is not overloaded but
> multiplication is for durations? Then you could say:
>
>   $midpoint = ($dt2 - $dt1)/2;
>
> rather than
>
>   $midpoint = ($dt2 - $dt1) * 0.5;
>
> Small thing. Just curious.

Well, division doesn't really work, whether you do it as multiplication or
not ;)

What is half a minute?  How long is half a month?


-dave

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


DT::Duration overloads

2004-06-09 Thread Matt Sisk
I understand that division can be expressed as multiplication, but is 
there any particular reason why division (/) is not overloaded but 
multiplication is for durations? Then you could say:

 $midpoint = ($dt2 - $dt1)/2;
rather than
 $midpoint = ($dt2 - $dt1) * 0.5;
Small thing. Just curious.
Thanks,
Matt