Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: A question to iterator in Datetime::Event::Recurrence

by vagabonding electron (Hermit)
on Apr 11, 2012 at 12:05 UTC ( #964526=note: print w/ replies, xml ) Need Help??


in reply to Re^3: A question to iterator in Datetime::Event::Recurrence
in thread A question to iterator in Datetime::Event::Recurrence

Aye-aye, sir! :-)

#!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Format::Strptime; use Datetime::Set; # use Data::Dumper; my $strp = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %T', ); my $datf = qq{2012-01-01 04:00:00}; my $datt = qq{2012-01-02 23:00:00}; my $start = $strp->parse_datetime($datf); my $end = $strp->parse_datetime($datt); while( $start < $end ){ print "$start\n" if $start->hour > 6 and $start->hour < 23; $start->add( hours => 1 ); }
I had bad luck with an attempt over $set = DateTime::Set->from_recurrence however.
This one:
$set = DateTime::Set->from_recurrence( after => $start, before => $end, recurrence => sub { return $_[0]->truncate( to => 'day' )->add( hours => 1 ) }, ); my $iter = $set->iterator; while ( my $dt = $iter->next ) { print $dt->ymd; };
produced an infinite loop with the error message "iterator can't find a previous value". I intended to add later
my $set2 = $set->grep( sub { return ( $_->hour > 7 or $_->hour < 23); } ); my $iter = $set2->iterator; while ( my $dt = $iter->next ) { print $dt->ymd; };

Where is my mistake?
Many thanks again - also for answering my previous question!
VE


Comment on Re^4: A question to iterator in Datetime::Event::Recurrence
Select or Download Code
Re^5: A question to iterator in Datetime::Event::Recurrence
by fglock (Vicar) on Apr 11, 2012 at 12:22 UTC

    The "recurrence" function should return the "next" recurrence. This is one way to implement it:

    #!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Format::Strptime; use Datetime::Set; # use Data::Dumper; my $strp = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %T', ); my $datf = qq{2012-01-01 04:00:00}; my $datt = qq{2012-01-02 23:00:00}; my $start = $strp->parse_datetime($datf); my $end = $strp->parse_datetime($datt); my $iter; my $set = DateTime::Set->from_recurrence( after => $start, before => $end, recurrence => sub { return $_[0]->add( hours => 1 )->truncate( to => 'hour' ) }, ); print "first set:\n"; $iter = $set->iterator; while ( my $dt = $iter->next ) { print $dt->datetime, "\n"; }; my $set2 = $set->grep( sub { return ( $_->hour > 7 && $_->hour < 23); } ); print "second set:\n"; $iter = $set2->iterator; while ( my $dt = $iter->next ) { print $dt->datetime, "\n"; };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964526]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-07-31 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls