Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

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


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

Thank you very much!

Ditch DateTime::Event::Recurrence :)

Now after I have tried so much to familiarize to it? :-)

The task is to calculate the bed occupancies in a unit to every certain hour. The start and the end points are known, the iterator generates the hours and then I use $hash{$dt->datetime()}{$id}[0] = 1 for each hour (to sum it later).
The print was used just to debug.
The way around could be to use

while ( my $dt = $it->next() ) { print $dt->datetime(), "\n" if ($dt->hour > 6 and $dt->hour < 23); }
but since I am learning I would like to use the mainstream and not the way around.
Thanks again!
VE


Comment on Re^2: A question to iterator in Datetime::Event::Recurrence
Select or Download Code
Re^3: A question to iterator in Datetime::Event::Recurrence
by Anonymous Monk on Apr 11, 2012 at 10:25 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://964495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-10-23 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls