Re: How to convert timestamp to epoch?
- From: "void.no.spam.com@xxxxxxxxx" <void.no.spam.com@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 12:51:58 -0700
On Oct 30, 3:27 pm, TonyV <kingskip...@xxxxxxxxx> wrote:
On Oct 30, 3:15 pm, TonyV <kingskip...@xxxxxxxxx> wrote:
On Oct 30, 2:45 pm, "void.no.spam....@xxxxxxxxx"
<void.no.spam....@xxxxxxxxx> wrote:
If I get timestamps in the following format:
22-OCT-07 06.16.44.160000 PM
22-OCT-07 08.16.02.686000 AM
What is the easiest way for me to convert it into the number of
seconds since 1970?
I would probably make it a function:
Gah, here's a more word-wrap-friendly version:
use Time::Local;
my $ts_to_convert = '22-jun-07 06.16.44.160000 PM';
print timestamp_to_epoch($ts_to_convert);
sub timestamp_to_epoch {
my ($ts) = @_;
my %month = (
'JAN' => 0, 'FEB' => 1, 'MAR' => 2, 'APR' => 3,
'MAY' => 4, 'JUN' => 5, 'JUL' => 6, 'AUG' => 7,
'SEP' => 8, 'OCT' => 9, 'NOV' => 10, 'DEC' => 11);
my $regex = '^(\d{1,2})-(\w{1,3})-(\d{1,2}) ';
$regex .= '(\d{1,2})\.(\d{1,2})\.(\d{1,2})';
$regex .= '\.\d+ ([AP])M$';
if ($ts =~ m{$regex}i) {
my ($dd, $mo, $yy, $hh, $mm, $ss, $ap) =
($1, $2, $3, $4, $5, $6, $7);
$yy += 2000; $mo = $month{uc($mo)};
$hh += 12 if (uc($ap) eq 'P');
return timelocal($ss, $mm, $hh, $dd, $mo, $yy);
}
else { return 0; }
}
Thank you for writing that up.
I did have to change one line to get it to work:
sub timestamp_to_epoch {
my ($ts) = @_;
my %month = (
'JAN' => 0, 'FEB' => 1, 'MAR' => 2, 'APR' => 3,
'MAY' => 4, 'JUN' => 5, 'JUL' => 6, 'AUG' => 7,
'SEP' => 8, 'OCT' => 9, 'NOV' => 10, 'DEC' => 11);
my $regex = '^(\d{1,2})-(\w{1,3})-(\d{1,2}) ';
$regex .= '(\d{1,2})\.(\d{1,2})\.(\d{1,2})';
$regex .= '\.\d+ ([AP])M$';
if ($ts =~ m{$regex}i) {
my ($dd, $mo, $yy, $hh, $mm, $ss, $ap) =
($1, $2, $3, $4, $5, $6, $7);
$yy += 2000; $mo = $month{uc($mo)};
$hh += 12 if (uc($ap) eq 'P' && $hh != 12);
return timelocal($ss, $mm, $hh, $dd, $mo, $yy);
}
else { return 0; }
}
.
- Follow-Ups:
- Re: How to convert timestamp to epoch?
- From: void.no.spam.com@xxxxxxxxx
- Re: How to convert timestamp to epoch?
- References:
- How to convert timestamp to epoch?
- From: void.no.spam.com@xxxxxxxxx
- Re: How to convert timestamp to epoch?
- From: TonyV
- Re: How to convert timestamp to epoch?
- From: TonyV
- How to convert timestamp to epoch?
- Prev by Date: Re: How to convert timestamp to epoch?
- Next by Date: Re: Regular Expression and Useage
- Previous by thread: Re: How to convert timestamp to epoch?
- Next by thread: Re: How to convert timestamp to epoch?
- Index(es):