Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

timelocal error when executed twice.

by Anonymous Monk
on Oct 19, 2009 at 17:17 UTC ( [id://802039]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Brothers. I'm having trouble with timelocal.
my $sec = 0; my $min =0; my $hr = 0; $epoch = timelocal( $sec, $min, $hr, $d, $m, $y-=1900 ) + $plus * 8640 +0; print "Date $sec, $min, $hr, $y, $m, $d = $epoch\n"; $sec = 59; $min = 59; $hr = 23; $epoch = timelocal( $sec, $min, $hr, $d, $m, $y-=1900 ) + $plus * 8640 +0; # line 51 print "Date $sec, $min, $hr, $y, $m, $d = $epoch\n";
Produces output of
Date 0, 0, 0, 109, 8, 2 = 1251864000 Cannot handle date (59, 59, 23, 2, 8, 109) at /cygdrive/c/Documents an +d Settings/nwatson/My Documents/bin/foo line 51
Why the error? Even when both timelocal calls are identical the error persists.

Replies are listed 'Best First'.
Re: timelocal error when executed twice.
by almut (Canon) on Oct 19, 2009 at 17:48 UTC

    Don't use $y-=1900, use $y-1900 instead.  As you have it, your value will be decremented twice, yielding an invalid year the second time.  (I'm supposing you're setting $y once somewhere at the beginning — though you haven't shown it...)

    P.S.: In case anyone wonders (as I did) why when the module croaks the year value is reported as 109 (though it isn't)...  that's because the module does:

    ... $year += 1900; croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year +)";
      Almut ++ you're right. The '-=1900' was the problem. Thanks.
Re: timelocal error when executed twice.
by stonecolddevin (Parson) on Oct 19, 2009 at 18:55 UTC

    Why aren't you using DateTime for this?

    mtfnpy

Re: timelocal error when executed twice.
by markkawika (Monk) on Oct 19, 2009 at 17:28 UTC
    I get completely different output:
    mpettit:~$ ./monk.pl Undefined subroutine &main::timelocal called at ./monk.pl line 6.
    If I use Time::Local (assuming that's the module you're including that is providing you with timelocal(), I still get different output:
    mpettit:~$ ./monk.pl Day '' out of range 1..31 at ./monk.pl line 7
Re: timelocal error when executed twice.
by bv (Friar) on Oct 19, 2009 at 17:40 UTC

    I cannot reproduce using this code:

    print timelocal(59, 59, 23, 2, 8, 109)

    What version of Perl are you using? Time::Local? Do this and tell us what you see:

    $ perl -v $ perl -MTime::Local -le 'print $Time::Local::VERSION'

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
Re: timelocal error when executed twice.
by bichonfrise74 (Vicar) on Oct 19, 2009 at 17:31 UTC
    The timelocal calls are not identical, you have changed the variables. Can you show the actual code?
Re: timelocal error when executed twice.
by ikegami (Patriarch) on Oct 19, 2009 at 23:22 UTC

    By the way, not all days have 86400 seconds. Use DateTime or some other module to do your arithmetic.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-10-13 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.