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

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


Comment on Re: A question to iterator in Datetime::Event::Recurrence
Download Code
Re^2: A question to iterator in Datetime::Event::Recurrence
by vagabonding electron (Hermit) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2014-09-16 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (32 votes), past polls