Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Converting between Local and Unix time problems

Reply
Thread Tools

Converting between Local and Unix time problems

 
 
pj_sammie@hotmail.com
Guest
Posts: n/a
 
      01-05-2006
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";
}

 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      01-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      01-05-2006
(E-Mail Removed) wrote:
> 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__

--
http://DavidFilmer.com

 
Reply With Quote
 
pj_sammie@hotmail.com
Guest
Posts: n/a
 
      01-05-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting time historic GMT to local time by specifying a timezone Tharinda C++ 3 08-25-2010 05:32 PM
Convert UTC DateTime to Local Time (Not My Local Time) Jonathan Wood ASP .Net 1 11-01-2009 12:08 AM
Time conversion between UTC and local time andreas.profous@googlemail.com Python 2 02-09-2008 02:54 AM
convert time string in UTC to time in local time davelist@mac.com Python 1 03-11-2007 12:57 AM
GMT time to local time, according to timezone and summer/winter time. David Joseph Bonnici Perl Misc 1 05-15-2005 09:15 PM



Advertisments