http://www.perlmonks.org?node_id=964491


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

Please give me a hint.

Ditch DateTime::Event::Recurrence :) or explain your requirements better, my impression, you want some math

print " ###### \n"; while( $start < $end ){ print "$start\n"; $start->add( hours => 1 ); }

Replies are listed 'Best First'.
Re^2: A question to iterator in Datetime::Event::Recurrence
by vagabonding electron (Curate) on Apr 11, 2012 at 10:14 UTC
    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
        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