Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Don't ask to ask, just ask
 
PerlMonks  

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

by Anonymous Monk
on Apr 11, 2012 at 10:25 UTC ( #964496=note: print w/ replies, xml ) Need Help??


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

Well I do remember that from your previous question ( Calculation of the time overlapping ), but it doesn't change my impressions, ditch DateTime::Event::Recurrence, learn your way around DateTime::Set


Comment on Re^3: A question to iterator in Datetime::Event::Recurrence
Re^4: A question to iterator in Datetime::Event::Recurrence
by vagabonding electron (Friar) on Apr 11, 2012 at 12:05 UTC
    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

      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://964496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-04-18 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls