Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
My first post on pm. I've read all the docs and asked about documentation on Time::Period in the cb; I've googled and experimented... I still need a little bit of help. I'm trying to create a script that iterates over a time period and prints out a start and end time for each day in the range. The end result will hopefully be a textfile that resembles something like this:
2006-09-22 9:22am - 6:45pm 2006-09-25 9:20am - 6:15pm
In the following script, $return always returns -1. In the Time::Period pod, this means that inPeriod() is not getting a properly formed value as it's input. Here's my code:
#!/usr/bin/perl -w use strict; use DateTime; use DateTime::Span; use DateTime::Set; use Time::Period; my $start_date = DateTime->new( year => 2006, month => 9, day => 10, hour => 9 ); my $end_date = DateTime->new( year => 2006, month => 12, day => 20, hour => 18 ); my $span = DateTime::Span->from_datetimes( start => $start_date, end = +> $end_date ); my $days = DateTime::Set->from_recurrence ( span => $span, recurrence => sub { return $_[0]->truncate( to => 'day' )->add( days => 1 ) }, ); my $iter = $days->iterator; while (my $dt = $iter->next) { my $result = inPeriod( $dt->datetime, "wd {Mon-Fri}" ); if ($result) { printf("TRUE: %s -- \$result: %d\n", $dt->ymd, $result); } }

In reply to Seeking an example of Time::Period usage by jking

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2020-02-28 09:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (123 votes). Check out past polls.

      Notices?