Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by fglock (Vicar)
on Apr 11, 2012 at 12:22 UTC ( #964530=note: print w/ replies, xml ) Need Help??


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

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"; };


Comment on Re^5: A question to iterator in Datetime::Event::Recurrence
Download Code
Re^6: A question to iterator in Datetime::Event::Recurrence
by vagabonding electron (Hermit) on Apr 11, 2012 at 13:02 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2015-07-06 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (73 votes), past polls