use strict;
use warnings;
use Time::Local qw(timelocal);
sub localdate2epoch {
my $date = shift;
# insert some sanity checks and normalisation (whitespace removal, e
+tc.)
my ($year, $mon, $mday, $hour, $min, $sec) = split /[-\s:]+/, $date
+;
# insert more sanity checks
# .oO( get the impression here, it's better to use a module ;-)
$year -= 1900; # correct year (2011 = 1900 + 111);
$mon -= 1; # correct month (0..11)
return timelocal( $sec, $min, $hour, $mday, $mon, $year );
}
my $date = "2011-06-05 11:00:00";
my $now_in_secs = localdate2epoch $date;
print "In : $date\n";
print "epo: $now_in_secs\n";
print "Out: ", scalar localtime $now_in_secs, "\n";
__END__
In : 2011-06-05 11:00:00
epo: 1307264400
Out: Sun Jun 5 11:00:00 2011
|