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

Getting Time to work

by Anonymous Monk
on Aug 06, 2002 at 14:23 UTC ( [id://188037]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting error saying:
Day '' out of range 1..31 at C:\Perl\bin\timr3.pl line 29
I was hoping a more experienced person can direct me what I am doing wrong on this NT script??
use Time::Local; my %month_num = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11, ); sub get_seconds { my ($date, $time) = @_; my ($day, $mon, $yr) = split /\//, $date; my ($hr, $min, $sec) = split /:/, $time; $mon = $month_num{$mon}; $yr = $yr - 1900; my $data = timelocal($sec, $min, $hr, $day, $mon, $yr); print "$data\n"; } &get_seconds;

Replies are listed 'Best First'.
Re: Getting Time to work
by grinder (Bishop) on Aug 06, 2002 at 14:34 UTC
    This probably means you are passing garbage to get_seconds, and it just so happens that $day is the first thing that is checked by timelocal. You appear (at least in this snippet) to be calling get_seconds without any arguments, although, since you are using the & hack to call it, I really can't be sure. More proof that & is evil.

    The month_num hash is pretty cute, but the value of $month is usually meant to be used as an offset into an array. So you would write something like:

    $mon = (qw/Jan Feb Mar ... Nov Dec/)[$mon];

    In those other cases where you really do need a hash as you have it here, enclose both the hash and the sub that accesses it in curly brackets. That way you reduce the scope of the hash, and it can't be modified (read: trashed) by something else by accident.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Getting Time to work
by arturo (Vicar) on Aug 06, 2002 at 14:29 UTC

    The subroutine get_seconds isn't being passed any parameters. It looks like what it wants is two strings, the first in the form DD/MM/YYYY and the second in the form HH:MI:SS.

    Try calling it like so: get_seconds("01/01/2001", "12:00:00");

    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

Re: Getting Time to work
by demerphq (Chancellor) on Aug 06, 2002 at 15:41 UTC
    I was hoping a more experienced person can direct me what I am doing wrong on this NT script??

    A: No strictures, no warnings, no parameter validation.

    A subroutine should always check its parameters. If you use Carp::Assert you can turn it off for production and on for development. If you had basic parameter validation then it would be clear to you that no parameters were being passed to the subroutine.

    The merits of strict and warnings are discussed in so many nodes on this site it isnt funny. Peruse them, especially Use strict warnings and diagnostics or die

    Also grinders comments are spot on.

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

Log In?
Username:
Password:

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

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

    No recent polls found