On Wed, 12 Mar 2003 [EMAIL PROTECTED] wrote:

> DateTime::Set->new( recurrence => $string );

>  where $string is any of the parameters accepted by both
> DateTime::truncate() and DateTime::add(), such as 'day', 'year'.

> If somebody really wants other recurrences, they could create their own
> DateTime-derived class and override truncate and add. I think this
> approach is cleaner and more 'OO' than using callbacks.

Bleah, that's no fun.  People will want to use this for real apps, and
that's forcing them to do too much work.  For example, I'd imagine someone
wanting to specify a recurrence callbacks that returns the next business
day.

There's nothing non-OO about callbacks, though an alternate interface
might involve specifying an object and a method to call on it, as opposed
to a code reference.

Anyway, I _still_ don't understand why this is so complicated.  If I have
this recurrence:

 sub first_of_month
 {
     my $dt = shift;
     $dt->truncate( to => 'month' )->add( months => 1 );
 }

and do this:

 # no start or end param means it is infinite in either direction
 my $infinite_set = DateTime::Set->new( recurrence => \&first_of_month );

and then do this:

 my $finite_set = DateTime::Set->new
                      ( dates =>
                        [ $march_1_1900, $march_7_1905, $april_27_2003,
                          $december_1, 2050, $april_4_3130 ],
                      );

Then calculating the intersection should be easy, right?

 my $inter = $infinite_set->intersection( $finite_set );

In pseudo-code, I'd do:


 if both sets are infinite, blow up

 if one set if infinite, then ...

    foreach my $dt (datetimes in finite set)

      # optimization
      next if $last_dt && $last_dt > $dt

      $dt_minus_1 = datetime - one second

      $next_dt = _next_ datetime in infinite set _after_ $dt_minus_1

      if $dt == $next_dt

        record as intersecting

      $last_dt = $next_dt


This has a couple of assumptions built in, namely that internally we
probably need to convert datetime objects used in sets to the UTC time
zone (to avoid a possible exception when subtracting one second), and that
we don't support floating point seconds.


-dave

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

Reply via email to