There are a bunch of modules to do Date/Time calculations. If you want to use a relatively low level module that will be installed, then something like this can be done:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
# $time = timelocal( $sec, $min, $hour, $mday, $mon, $year );
# $time = timegm( $sec, $min, $hour, $mday, $mon, $year );
my %nMonth = (Jan=>0, Feb=>1, Mar=>2, Apr=>3, May=>4, Jun=>5,
Jul=>6, Aug=>7, Sep=>8, Oct=>9, Nov=>10, Dec=>11);
foreach my $date ('Fri Jul 15 08:02:02 2016')
{
my ($textMonth,$day,$hour,$min,$sec,$year) =
$date =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/;
print "parsed results= $textMonth,$day,$hour,$min,$sec,$year\n";
my $localSecs = timelocal($sec, $min, $hour, $day, $nMonth{$textMont
+h}, $year);
print "localsecs = $localSecs\n";
my $gmSecs = timegm($sec, $min, $hour, $day, $nMonth{$textMonth}, $y
+ear);
print "gmsecs = $gmSecs\n";
}
__END__
parsed results= Jul,15,08,02,02,2016
localsecs = 1468594922
gmsecs = 1468569722
(1468569722-1468594922)/60/60=-7 hours Correct for PSDT(my curent time
+ zone)
Update: This simple code above has limitations. What happens if you want to convert a local time that occurred say in "winter time" vs current "summer time"? Something more sophisticated is needed. Perhaps the Date::Time module.
|