G'day Marco, 
 
Seems to me that this wheel has already been invented and is called 
DateTime::Event::Recurrence. Here's your code: 
 
use Date::Iterator; 
my $i1 = Date::Iterator->new(from => [2003,11,1], to =>  [2003,11,30]) ; 
my @dates; 
push @dates,$_ while $_ = $i1->next ; 
 
# (2003, 11, 01) 
# ... 
# (2003, 11, 30) 
 
 
And here is the DateTime::Event::Recurrence version: 
 
use DateTime; 
use DateTime::Event::Recurrence; 
my @dates = DateTime::Event::Recurrence->daily->as_list(  
        start => DateTime->new( year => 2003, month => 11, day => 1), 
        end  => DateTime->new( year => 2003, month => 11, day => 30), 
); 
# 2003-11-01 
# ... 
# 2003-11-30 
 
You also say that the task should allow the user to specify the 'step' or the number 
of days 
in the delta between each returned value: 
 
my @dates = daily DateTime::Event::Recurrence( 
        interval => 7, 
        start => DateTime->new( year => 2003, month => 11, day => 1), 
)->as_list( 
        start => DateTime->new( year => 2003, month => 11, day => 1), 
        end  => DateTime->new( year => 2003, month => 11, day => 30), 
); 
# 2003-11-01 
# 2003-11-08 
# 2003-11-15 
# 2003-11-22 
# 2003-11-29 
 
 
But what if we take it one step further in another direction and want it to return 
weekly values 
again, but this time they should just be Sundays, rather than 
seven-days-after-the-start: 
 
my @dates = DateTime::Event::Recurrence->weekly( 
        days => 7,  # 7 = Sunday 
        start => DateTime->new( year => 2003, month => 11, day => 1), 
)->as_list(  
        start => DateTime->new( year => 2003, month => 11, day => 1), 
        end  => DateTime->new( year => 2003, month => 11, day => 30), 
); 
# 2003-11-02 
# 2003-11-09 
# 2003-11-16 
# 2003-11-23 
# 2003-11-30 
 
While your solution works, there's already a good, easy way to do it. And the existing 
way 
can do a lot more besides (including iterations of periods other than day-based) 
 
I'm not saying don't release it to CPAN, just that there's ways already on CPAN for 
doing 
this. 
 
Cheers! 
Rick Measham 
 
PS. Note that the above DateTime routines return lists of DateTime objects. These mean 
you can do all sort of crazy stuff with them, including formatting them in any way you 
please. However to get a list like your routine (if you REALLY need a list) then use 
something like: 
foreach (@dates) { $_ = [ $_->year, $_->month, $_->day ] } 
 
 

Reply via email to