Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Seeking an example of Time::Period usage

by jking (Novice)
on Dec 21, 2006 at 18:50 UTC ( #591160=perlquestion: print w/replies, xml ) Need Help??
jking has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Seeking an example of Time::Period usage
by imp (Priest) on Dec 21, 2006 at 19:08 UTC
    The time is specified as per the time() function, which is assumed to be the number of non-leap seconds since January 1, 1970.
    DateTime::datetime returns a formatted string, e.g. 2006-09-11T00:00:00. You should be using $dt->epoch instead.
      Works great. Thanks. I totally missed that and now I see it after you've pointed it out. Cheers.
Re: Seeking an example of Time::Period usage
by brusimm (Pilgrim) on Dec 21, 2006 at 20:35 UTC
    Hi, I am attempting to learn Perl. I would like to ask where in the script dt->epoch would go?

    I do not quite understand the remedy. Thank you.

      I would like to ask where in the script dt->epoch would go?

      You would replace $dt->datetime with $dt->epoch (a few lines up from the end of the original code).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://591160]
Approved by Corion
erix wonders... how is the neurocent doing these days?
[marto]: Invest in my crypto currency, derpcoin
[erix]: ( and only a single italian in the Giro top 10 -- surely you guys are not trying hard enough! )
[marto]: it does literally nothing, solves no problems that aren't already addressed, but it's real easy to mine, because nobody is doing it
[LanX]: is producing broken links for me ...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2018-05-25 15:52 GMT
Find Nodes?
    Voting Booth?