Re: How to convert timestamp to epoch?
- From: TonyV <kingskippus@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 12:15:24 -0700
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:
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);
if ($ts =~ m{^(\d{1,2})-(\w{1,3})-(\d{1,2}) (\d{1,2})\.(\d{1,2})\.
(\d{1,2})\.\d+ ([AP])M$}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; }
}
Check to make sure your return value is non-zero, and you're in
business.
You might also want to check out the Date::Manip package, as I
understand that it was designed to do this type of parsing. I don't
use it myself, so I'll defer to others' recommendations for it, but
here it is:
http://search.cpan.org/dist/Date-Manip/Manip.pod
.
- Follow-Ups:
- Re: How to convert timestamp to epoch?
- From: TonyV
- Re: How to convert timestamp to epoch?
- References:
- How to convert timestamp to epoch?
- From: void.no.spam.com@xxxxxxxxx
- How to convert timestamp to epoch?
- Prev by Date: FAQ 8.13 How do I trap control characters/signals?
- Next by Date: Re: How to convert timestamp to epoch?
- Previous by thread: How to convert timestamp to epoch?
- Next by thread: Re: How to convert timestamp to epoch?
- Index(es):
Relevant Pages
|