itis.guptak has asked for the wisdom of the Perl Monks concerning the following question:
Hi Everyone, I'm trying to debug a 1000 line perl code written by someone else. I run this code through a cron job. It works but with an error. The problem is with the timelocal function. I think the values I'm passing to the timelocal function are valid. Like the month is 0 for Jan etc. Day '29' out of range 1..28 Could you please help me find the reason for this error.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Timelocal out of range message
by kcott (Archbishop) on Jan 29, 2014 at 07:23 UTC | |
G'day itis.guptak, Welcome to the monastery. Please provide a short script that reproduces your problem. How to do this is explained in "How do I post a question effectively?". Without seeing any code or data, we are not able to "find the reason for this error". However, the only instance I can think of giving "Day '29' out of range 1..28" would be during February of a non-leap year. Please ensure you haven't made a simple syntax error (e.g. number and order of timelocal() arguments) by checking the Time::Local documentation. -- Ken | [reply] [d/l] |
by itis.guptak (Initiate) on Feb 03, 2014 at 07:36 UTC | |
Thanks, Ken. Let me just explain what this script does. It pulls the systems message file from a list of hosts and put the content in a single file output on a central server from where it's run as cron job. I'm pasting a portion of code that's responsible for timelocal() execution in the script. The code I'm pasting contains three subroutines: addMessage(), processFile(), getLogsSCP(). getLogsSCP() will scp the files to central server. processFile() is called within getLogsSCP() and processFile() furthur calls addMessage(). Finally it's the addMessage() that invokes timelocal(). I've also pasted some variables used for computing the parameters passed to the timelocal function. This line of the code is generating the error.
Code
| [reply] [d/l] [select] |
by kcott (Archbishop) on Feb 04, 2014 at 04:19 UTC | |
I wrote: "Please provide a short script that reproduces your problem. How to do this is explained in "How do I post a question effectively?"." Had you followed that link, you would have seen, in big, friendly letters, at the top of the screen: Paste actual code that reproduces the problem ... You haven't posted actual code. You haven't posted code that reproduces your problem. In addition, hundreds of lines of code is not a "short script": a dozen or so lines normally suffices for this sort of task. If you ever do need to post large tracts of code, data, etc., wrap it in <spoiler>...</spoiler> or <readmore>...</readmore> tags: see "Markup in the Monastery" and "Writeup Formatting Tips". What you have posted appears to have been copied from your browser. It looks like the code you posted a couple of hours before in this thread; it includes all the '+' signs at the beginning of lines (that indicate that a line wrapped) which are not part of the Perl code. If you can post a short script that shows Time::Local::timelocal() giving your "Timelocal out of range message", I'll be happy to look at it further. In writing a short script to reproduce your problem, you'll often gain insights into why your original code was having issues. Something else you can do is to add print statements to your original code to see what actual values are being passed to your functions. This can often point to where the problem lies. -- Ken | [reply] [d/l] [select] |
|
Re: Timelocal out of range message
by Corion (Patriarch) on Jan 29, 2014 at 07:27 UTC | |
Most likely you are passing in the wrong value for the month. Most likely, that value is 1, and you intend it to be for January, but 1 is for February in the Time::Local API. | [reply] |
|
Re: Timelocal out of range message
by hdb (Monsignor) on Jan 29, 2014 at 07:23 UTC | |
Read the source, Luke. The error message smells like you are trying to refer to a 29th of February in a non-leap year. Do you not get a reference to a line number in Local.pm? Check what happens there! Probably line 116? | [reply] [d/l] |
by itis.guptak (Initiate) on Feb 03, 2014 at 07:32 UTC | |
Here is the local.pm code snippet that says about out of range errors. Not sure how it's coming for day 29th?
| [reply] [d/l] |
by hdb (Monsignor) on Feb 03, 2014 at 07:40 UTC | |
In the version I have it looks like:
| [reply] [d/l] |
|
Re: Timelocal out of range message
by vinoth.ree (Monsignor) on Jan 29, 2014 at 07:40 UTC | |
You will see that Months are set in an array: my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);Therefore, when it tries to pull the number of days for the month you passed to it, it's searching it's index from 0 to 11. So when you pass in your Month as 1 (Feb), it's searching the array and thinking only 28 days are avilable and failing when you specify 29 since you are technically 'out of range'. All is well | [reply] [d/l] |
by itis.guptak (Initiate) on Feb 03, 2014 at 05:39 UTC | |
So does that mean 29 is being passed as the day of the month for Feb? I see nowhere in the script that the day of the month is passed to timelocal. Is there any way we can control this for Feb (along with a leap year)? | [reply] [d/l] |
|
Re: Timelocal out of range message
by Laurent_R (Canon) on Jan 29, 2014 at 07:46 UTC | |
| [reply] |
by itis.guptak (Initiate) on Feb 03, 2014 at 05:29 UTC | |
Thanks all for your replies! Let me just explain what this script does. It pulls the systems message file from a list of hosts and put the content in a single file output on a central server from where it's run as cron job. I'm pasting a portion of code that's responsible for timelocal() execution in the script. The code I'm pasting contains three subroutines: addMessage(), processFile(), getLogsSCP(). getLogsSCP() will scp the files to central server. processFile() is called within getLogsSCP() and processFile() furthur calls addMessage(). Finally it's the addMessage() that invokes timelocal(). I've also pasted some used for computing the parameters passed to the timelocal function. This line of the code is generating the error.
Please see the code below.
Sample output
| [reply] [d/l] [select] |