SSS said:
When i run the following script, I get error:
Day '31' out of range 1..28 at exp.pl line 18
perldoc -f localtime
localtime EXPR
localtime
Converts a time as returned by the time function to a
9-element list with the time analyzed for the local time
zone. Typically used as follows:
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
All list elements are numeric, and come straight out of the
C `struct tm'. $sec, $min, and $hour are the seconds,
minutes, and hours of the specified time.
$mday is the day of the month, and $mon is themonth
itself, in the range 0..11 with 0 indicating January and 11
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
indicating December. This makes it easy to get a month
^^^^^^^^^^^^^^^^^^^
name from a list:
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct
Nov Dec );
print "$abbr[$mon] $mday";
# $mon=9, $mday=18 gives "Oct 18"
$year is the number of years since 1900, not just the last
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
two digits of the year. That is, $year is 123 in year
2023. The proper way to get a complete 4-digit year is
simply:
$year += 1900;
Otherwise you create non-Y2K-compliant programs--and you
wouldn't want to do that, would you?
use strict;
use warnings;
use Time::Local;
my $date_string = '2011-01-31 17:30:55';
my ($date, $time) = split /\s+/, $string;
my ($y, $m, $d) = split /-/, $date;
my ($hr, $min, $sec) = split /:/, $time;
print timelocal($sec,$min,$hr,$d,$m,$y);
my ( $y, $m, $d, $hr, $min, $sec ) = $date_string =~ /\d+/g
print timelocal( $sec, $min, $hr, $d, $m - 1, $y - 1900 );
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein