Problems with timelocal

Hi all;

I've inherited some PERL scripts that produce the reports for our Netbackup daily processes. This is running on a Windows 2003 SP1 Standard edition server. One of the reports quit working July 18, 2007. I ran the script from the command line and received the error:
Month '-1' out of range 0..11 at LP_Date.pm line 214

The sub that line 214 is in is as follows:

#########
# this subroutine is given a human-readable
# date (MM/DD/YY HH:MM:SS) and returns the
# equivalent epoch-style date (10 digits,
# seconds since 1970)
# e.g. if it is passed  "02/12/99 00:00:17",
# it will return "0918799217"
#########

sub time2stamp {

use Time::Local;

my ($time) = @_;

($tmp_date,$tmp_time) = split / /, $time, 2;
($tmp_month,$tmp_day,$tmp_year) = split /\//, $tmp_date, 3;
($tmp_hour,$tmp_min,$tmp_sec) = split /:/,$tmp_time, 3;

$stamp = timelocal($tmp_sec,$tmp_min,$tmp_hour,$tmp_day,$tmp_month-1,$tmp_year);
 
return $stamp;
}

I removed the -1 and received the error:
Day '' out of range 1..31 at LP_Date.pm line 214

Line 214 is $stamp = timelocal($tmp_sec,$tmp_min,$tmp_hour,$tmp_day,$tmp_month-1,$tmp_year);

This is part of a larger module, but this is where the error is occurring. I would appreciate any enlightenment to what the problem is.
davidbiAsked:
Who is Participating?
 
Adam314Commented:
sub time2stamp {
      use Time::Local;
      my ($ht) = @_;
      "02/12/99 00:00:17"
      return 0 unless $ht =~ /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/;
      my $stamp = timelocal($6,$5,$4, $2,$1-1,$3);
       
       return $stamp;
}
0
 
davidbiAuthor Commented:
Thanks for the response Adam314; I re-ran the script and still get the same error, would it help if I posted the whole module?
0
 
Adam314Commented:
I'm guessing you are passing it an invalid date then.

For debugging, try this:
sub time2stamp {
      use Time::Local;
      my ($ht) = @_;
      print "time2stamp: $ht\n";
      return 0 unless $ht =~ /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/;
      my $stamp = timelocal($6,$5,$4, $2,$1-1,$3);
       
       return $stamp;
}

what is the output with the above?
0
 
davidbiAuthor Commented:
Just re-ran with the modification to the sub and it worked! I think I ran the the old script on the other server, running the modifications that you did has worked. Thank you very much.
0
 
ozoCommented:
What was the date that was passed to the sub, and what would you want it to return in that case?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.