Good info on using a Set.  I meant to look at that after looking at the
FAQ but wasn't familiar with using them and didn't want to waste too
much time.

Hiding the rounding error by replacing the last value with $end itself
was the solution in my second sub-routine, but it might not have been
obvious.  The map only ran for 0..($n-1) instead of 0..$n.

Bobby

-----Original Message-----
From: Flavio S. Glock [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 08, 2007 9:26 AM
To: [email protected]
Subject: Re: Simple math: partitioning the time between two DateTimes

2007/8/7, J. David Blackstone <[EMAIL PROTECTED]>:
>   Given two DateTime objects as input, and an integer $N, I want to
> partition the time in between them into $N equal intervals and return
> an array of $N + 1 evenly-spaced DateTimes that begins with the first
> input DateTime and ends with the last input DateTime.

you can hide the complexity with a Set:


  use DateTime::Event::ICal;

  my $start = DateTime->now;
  my $end = DateTime->new( year => 2008, month => 7, day => 12 );
  my $n = 3;

  my $split = DateTime::Event::ICal->recur(
    dtstart => $start,
    freq => secondly,
    interval => int( ($end->hires_epoch() - $start->hires_epoch()) / $n
),
    until => $end,
  );
  print join("\n",  @{[ $split->as_list ]} ), "\n";


  # you can hide the rounding error by replacing the last value with
$end itself.

  print "rounded:\n";
  @values =  @{[ $split->as_list ]};
  $values[-1] = $end;
  print join("\n",  @values ), "\n";

- Flavio S. Glock

Reply via email to