By the way:
  Altitide --> Altitude

Hill, Ronald wrote:
> What I wanted to accomplish is to have the module accept a
> DateTime::Set object compute the rise/set times and return a new
> DateTime::Set object. Here is what I have done:

> sub as_set {
>         my $self = shift;
>         my $dt = shift;
>         croak( "Dates need to be DateTime::Set objects (" . ref($dt) . ")" )
>       unless ( ref($dt) eq 'DateTime::Set' );
>       my @set = ();
>       my $iter = $dt->iterator;
>       while (my $tmp_dt = $iter->next )  {
>             my($tmp_rise,$tmp_set) = sunrise( $self, $tmp_dt);
>             push(@set, $tmp_rise);
>               push(@set, $tmp_set);
>     }
>         return DateTime::Set->new( dates =>[sort @set] );
> 
> }
 

You could build a recurrence:

  sub sunrise_as_set {
    my $self = shift;
    my $class = ref($self);
    return DateTime::Set->new( recurrence => $class::following_sunrise
);
  }

  sub sunset_as_set {
    my $self = shift;
    my $class = ref($self);
    return DateTime::Set->new( recurrence => $class::following_sunset );
  }

Now, since you have rise/set times, you can build a "DateTime::SpanSet",
which is a set of intervals.

We haven't finished the discussion on what is the syntax for creating 
a DateTime::SpanSet, but it would be something like this:

  sub daytime_as_spanset {
    my $self = shift;
    my $class = ref($self);
    my $sunrises = $self->sunrise_as_set;
    my $sunsets  = $self->sunset_as_set;
    return $sunrises->until( $sunsets );
  }
    
  sub nighttime_as_spanset {
    my $self = shift;
    my $class = ref($self);
    my $sunrises = $self->sunrise_as_set;
    my $sunsets  = $self->sunset_as_set;
    return $sunsets->until( $sunrises );
  }

> Having done this, I was able to write a perl script using the easter and
> sunrise
> module like this:

> my $easter_sunday = DateTime::Event::Easter->new();
> 
> my $sunrise = DateTime::Event::Sunrise ->new(
>                  Longitude =>'-118' ,
>                      Latitude => '33',
> );
> 
> my $dt1 = DateTime->new( year   => 2000,
>                  month  => 1,
>                  day    => 1,
>                   );
> 
> my $dt2 = DateTime->new( year   => 2050,
>                  month  => 12,
>                  day    => 1,
>                   );
> 

  my $dt_span = DateTime::Span->new( begin => $dt1, end => $dt2 );

> my $easter = $easter_sunday->as_set(from=>$dt1, to=>$dt2, inclusive=>1);

  # I would expect $easter_sunday->as_spanset to return 
  # the time from sunday 00:00 until 23:59
  $easter = $easter_sunday->as_spanset->intersection( $dt_span );

> my $set2 = $sunrise->as_set($easter);

  my $set2 = $sunrise->daytime_as_spanset->intersection ( $easter );

> my $iter = $set2->iterator;
> while ( my $dt = $iter->next ) {
>     $dt->set_time_zone( 'America/Los_Angeles' );
>     print $dt->datetime ."\n";
> }

I think we could just have a 
  $set = $set->time_zone( 'America/Los_Angeles' );
method (but we don't have it yet).

All this might seem a bit strange, but the idea of using sets is that
you don't have to use iterators -- you use set operations instead.
(you can also 'print $set' )

Now show it:

  my $iter = $set2->iterator;
  while ( my $dt = $iter->next ) {
     print $dt->begin->datetime ." until ". $dt->end->datetime ."\n";
  }

Note: I haven't tested all this yet, and some of the syntax is not
implemented.
All suggestions are very welcome.

- Flavio S. Glock

Reply via email to