Converting between Local and Unix time problems

P

pj_sammie

I am trying to convert a localtime generated in a log file, to unix
time for storage in a db. In the script below, it properly converts if
I generate a localtime within the script. But, it won't properly
convert when using a sample line from the log file. I am stumped...

Thanks for any help.

CODE
---------
use Time::Local;

# Doesn't convert propely
$thetimel = "Tue Sep 28 17:12:13 2004";
print "$thetimel\n";
&convert_time;

# Does convert properly
$currenttime = time();
$thetimel = localtime($currenttime);
print "$thetimel\n";
&convert_time;

sub convert_time {
if ($thetimel =~ /\w+\W+(\w+)\W+(\d+)\W+(\d+):(\d+):(\d+)\W+(\d+)/)
{
$mon = $1;
$mday = $2;
$hours = $3;
$min = $4;
$sec = $5;
$year = $6;
}

$thetimes = timelocal($sec, $min, $hours, $mday, $mon, $year);

print "$sec, $min, $hours, $mday, $mon, $year\n";
print "$thetimel => $thetimes\n";
print "$thetimes => " . localtime($thetimes) . "\n\n";
}
 
B

Brian Wakem

I am trying to convert a localtime generated in a log file, to unix
time for storage in a db. In the script below, it properly converts if
I generate a localtime within the script. But, it won't properly
convert when using a sample line from the log file. I am stumped...

Thanks for any help.

CODE
---------
use Time::Local;

# Doesn't convert propely
$thetimel = "Tue Sep 28 17:12:13 2004";
print "$thetimel\n";
&convert_time;

# Does convert properly
$currenttime = time();
$thetimel = localtime($currenttime);
print "$thetimel\n";
&convert_time;

sub convert_time {
if ($thetimel =~ /\w+\W+(\w+)\W+(\d+)\W+(\d+):(\d+):(\d+)\W+(\d+)/)
{
$mon = $1;
$mday = $2;
$hours = $3;
$min = $4;
$sec = $5;
$year = $6;
}

$thetimes = timelocal($sec, $min, $hours, $mday, $mon, $year);

print "$sec, $min, $hours, $mday, $mon, $year\n";
print "$thetimel => $thetimes\n";
print "$thetimes => " . localtime($thetimes) . "\n\n";
}


timelocal() expects $mon to be in the range 0..11, but you are giving it
'Sep', which is numerically equal to 0, so you end up with a date in Jan.
It seems to work for the current date as it is January. It wont work next
month.
 
U

usenet

I am trying to convert a localtime generated in a log file, to unix
time for storage in a db.

You're going to a lot of trouble there, my friend. Have you considered
something like this instead? (use "%o" instead of "%s" if you want
localtime instead of UTC):

#!/usr/bin/perl

use Date::Manip;
print UnixDate("Tue Sep 28 17:12:13 2004", "%s");

__END__
 
P

pj_sammie

Thanks for the quick replies. I did not know about Date::Manip, and
yeah that's a lot cleaner, simpler, easier etc. I think I'll go with
that :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top