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

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

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

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 (Chaplain) 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!
        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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964491]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-01-17 11:53 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (198 votes). Check out past polls.