Re: How to convert timestamp to epoch?



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

.



Relevant Pages

  • separating address elements in one cell
    ... There are a handlful of data tools available for parsing ... do not have a consistent delimiter. ... or offer recommendations. ...
    (microsoft.public.excel.misc)
  • Re: For those crossing the border with electronics
    ... the package. ... When I was writing sentencing recommendations, we were given a copy of the District Attorney's file as a place to start our research. ... One of the pages that the Assistant DA had to fill out was called a "Dump Sheet". ... Among the rest of the info was a line for "Reason for Plea Bargain". ...
    (rec.outdoors.rv-travel)
  • Re: Corrupt package "database"
    ... opinions and recommendations. ... for a corrupt package database (according to Sun ... planning to install the latest recommended patch cluster unconditionally ...
    (SunManagers)
  • Re: Deferred imports
    ... "startup needs" of that package met. ... global myDeferredModule ... pypy has a neat lazy loading importer, you could see how they implement ... class defer: ...
    (comp.lang.python)
  • Terrorist attack in Bombay
    ... extra channels to his Sky TV package, or defer a payment on his car ...
    (uk.legal)