Skip to content

Instantly share code, notes, and snippets.

@iandexter
Created November 15, 2013 06:53
Show Gist options
  • Save iandexter/7480233 to your computer and use it in GitHub Desktop.
Save iandexter/7480233 to your computer and use it in GitHub Desktop.
Convert timestamps to epoch time.
use Time::Local;
sub toepoch {
my $timestamp = shift;
my ($mon, $mday, $year, $hour, $min, $sec, $ampm) =
split(/[\/\s:]/, $timestamp);
$hour = ($ampm =~ /PM/ && $hour < 12) ? $hour+12 : $hour;
return timelocal($sec,$min,$hour,$mday,$mon-1,$year);
}
sub toepoch {
my $timestamp = shift;
my %months = qw(
jan 1 feb 2 mar 3 apr 4 may 5 jun 6
jul 7 aug 8 sep 9 oct 10 nov 11 dec 12
);
my ($weekday, $mday, $month, $year, $hour, $min, $sec, $zone) =
split(/[\/\s:]/, $timestamp);
my $mon = $months{ lc substr($month, 0, 3) };
return timelocal($sec,$min,$hour,$mday,$mon-1,$year);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment