Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by vagabonding electron (Hermit)
on Apr 11, 2012 at 09:31 UTC ( #964486=perlquestion: print w/ replies, xml ) Need Help??
vagabonding electron has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I try to iterate over the generated set of datetimes in the following way:
#!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Event::Recurrence; use DateTime::Format::Strptime; 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 $day_set = DateTime::Event::Recurrence->hourly(); my $it = $day_set->iterator ( start => $start->truncate(to=>"hour"), before => $end->truncate(to=>"hour"), ); while ( my $dt = $it->next() ) { print $dt->datetime(), "\n"; }
This works.
What I need however is to generate only the hours between 07:00:00 and 22:00 :00.
I tried
my $day_set = DateTime::Event::Recurrence->hourly( hours => [7, 22], );
but this brings an error message "invalid argument 'hours' " - apparently one can use only the next low time step since it works with minutes=>[10, 30] for example.
I have not much experience with Datetime modules yet.
Please give me a hint.
Thanks in advance.
VE

Comment on A question to iterator in Datetime::Event::Recurrence
Select or Download Code
Re: A question to iterator in Datetime::Event::Recurrence
by Anonymous Monk on Apr 11, 2012 at 09:56 UTC

    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 ); }
      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
Re: A question to iterator in Datetime::Event::Recurrence
by fglock (Vicar) on Apr 11, 2012 at 12:07 UTC

    The parameters specify the occurrences "inside" the base unit:

    my $day_set = DateTime::Event::Recurrence->daily( hours => [ 7 .. 22 ] );

    This may be a bit unexpected, I agree it should be better documented.

      Now tested it.
      The code
      #!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Event::Recurrence; use DateTime::Format::Strptime; 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 $day_set = DateTime::Event::Recurrence->daily( hours => [7, 22], ); my $it = $day_set->iterator ( start => $start->truncate(to=>"hour"), before => $end->truncate(to=>"hour"), ); while ( my $dt = $it->next() ) { print $dt->datetime(), "\n"; }
      prints:
      2012-01-01T07:00:00 2012-01-01T22:00:00 2012-01-02T07:00:00 2012-01-02T22:00:00

      The desired output should be:
      2012-01-01T08:00:00 2012-01-01T09:00:00 2012-01-01T10:00:00 2012-01-01T11:00:00 2012-01-01T12:00:00 2012-01-01T13:00:00 2012-01-01T14:00:00 2012-01-01T15:00:00 2012-01-01T16:00:00 2012-01-01T17:00:00 2012-01-01T18:00:00 2012-01-01T19:00:00 2012-01-01T20:00:00 2012-01-01T21:00:00 2012-01-01T22:00:00 2012-01-02T08:00:00 2012-01-02T09:00:00 2012-01-02T10:00:00 2012-01-02T11:00:00 2012-01-02T12:00:00 2012-01-02T13:00:00 2012-01-02T14:00:00 2012-01-02T15:00:00 2012-01-02T16:00:00 2012-01-02T17:00:00 2012-01-02T18:00:00 2012-01-02T19:00:00 2012-01-02T20:00:00 2012-01-02T21:00:00 2012-01-02T22:00:00
      which is produced by your code from Re^5: A question to iterator in Datetime::Event::Recurrence but not the above code.
      ... This assumed that I understood everything correctly :-)
      Greetings, VE

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-07-04 16:13 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 (60 votes), past polls