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

if you practice your time::local module in a month contains 31days, then, the timelocal()function report that day '31' out of range 1..30 my code is as follows:
#!/usr/bin/perl -w use strict; my @now =localtime(); my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d",$now[5]+1900,$now[4 +]+1,$now[3],$now[2],$now[1],$now[0]); my $systime = timelocal($now[0],$now[1],$now[2],$now[3],$now[4]+1,$now +[5]+1900);

Replies are listed 'Best First'.
Re: I have seen a bug of Time::Local module
by wind (Priest) on Mar 31, 2011 at 07:18 UTC

    Time::Local is meant to be the inverse of the built-in localtime, therefore its arguments are of the same format with a month in a 0-11 range.

    Additionally, you can use POSIX and strftime to get the timestamp in the format you want instead of rolling your own solution as it also takes datetime information in the same format as returned by localtime.

    #!/usr/bin/perl -w use POSIX qw(strftime); use Time::Local; use strict; my @now = localtime(); my $timestamp = strftime "%Y%m%d%H%M%S", @now; print "$timestamp\n"; my $systime = timelocal(@now); print "$systime\n";
    And if you really want to be succinct, you don't even really need Time::Local in this instance.
    my $systime = time; my $timestamp = strftime "%Y%m%d%H%M%S", localtime $systime;
      I have solved the problems using the above methods. Thank you very much for all your replies!
Re: I have seen a bug of Time::Local module
by Corion (Patriarch) on Mar 31, 2011 at 07:07 UTC
    $now[4]+1

    No. The number of the month is zero based in all the localtime and timelocal APIs. Only strftime and sprintf want the number of the month adjusted.

    Update: D'oh - I should've known that all the *time functions behave the same with regards to their input parameters.

      Not strftime. Valid use:
      strftime("%Y%m%d%H%M%S", localtime)