Joshua Hoblitt said:
> There must be a way to express the same semantic
> meaning with fewer lines of code

A slightly smaller version - specify days and hours
in the same constructor.

- Flavio S. Glock

---
#!/usr/local/bin/perl -w

use strict;
use DateTime;
use DateTime::Span;
use DateTime::SpanSet;
use DateTime::Event::Recurrence;
use Carp;

# Make a set representing mondays to fridays, with
# the working hour restriction
my $working_days = 
    DateTime::SpanSet->from_sets
      (start_set => DateTime::Event::Recurrence-> 
           weekly( days => [1..6],hours => [9,  1]),
         end_set => DateTime::Event::Recurrence-> 
           weekly( days => [1..6],hours => [12, 5]),
      );

# Make the date range
my $date_range = DateTime::Span->from_datetimes
    (start => DateTime->new(
        year => 2003, month => 7, day => 1),
     end   => DateTime->new(
        year => 2003, month => 7, day => 18)); 

# Build the spanset of legal times
my $legal = $working_days->intersection($date_range);

## Test code
my $iter = $legal->iterator();
while ( my $dt = $iter->next ) {   
    printf "%s to %s\n", 
           $dt->start->datetime,
           $dt->end->time;
};
##

# Now test the date
my $dt = DateTime->new(year => 2003);
croak "Bad date range" unless $legal->contains($dt);


Reply via email to