Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Seeking an example of Time::Period usage

by jking (Novice)
on Dec 21, 2006 at 18:50 UTC ( [id://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
    Time::Period:
    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).

      Cheers,
      Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found