Mismatched Time Zones

liunx

Guest
I have a perl script that records member logins on my Web site. Here's part of the code:<br />_____<br /><br />open(FILE, ">>$log_file");<br />{($sec,$min,$hour,$day,$month) = localtime();<br />$month++;<br />if ($hour < 10) <br />{ $hour = "0$hour"; }<br /> if ($min < 10) <br />{ $min = "0$min"; }<br />}<br />if ($password ne "xxxx") {<br />print FILE "$month/$day $hour:$min $password $ENV{'REMOTE_ADDR'}\n";<br />close(FILE);<br />_____<br /><br />The problem is that the time printed to the log file for each login is one hour ahead of the time where I live. Presumably, localtime() on the server where my Web site resides (Server 24) is Eastern Time, whereas I live in the Central Time zone. What changes could I make to the script so that the time it prints to the log file matches my own time zone?<!--content-->
Try this code out and see if it works for you:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->use DateTime;<br />open(FILE, ">>$log_file");<br />my $dt = DateTime->now(time_zone => 'America/Chicago');<br />if ($password ne "xxxx") {<br />print FILE $dt->strftime("%m/%d %H:%M") . " $password $ENV{'REMOTE_ADDR'}\n";<br />close(FILE);<!--c2--></div><!--ec2--><!--content-->
<!--quoteo(post=155145:date=Nov 11 2005, 04:33 AM:name=TCH-David)--><div class='quotetop'>QUOTE(TCH-David @ Nov 11 2005, 04:33 AM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=155145"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Try this code out and see if it works for you:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->use DateTime;<br />open(FILE, ">>$log_file");<br />my $dt = DateTime->now(time_zone => 'America/Chicago');<br />if ($password ne "xxxx") {<br />print FILE $dt->strftime("%m/%d %H:%M") . " $password $ENV{'REMOTE_ADDR'}\n";<br />close(FILE);<!--c2--></div><!--ec2--><!--QuoteEnd--></div><!--QuoteEEnd--><br />I appreciate the help. However, the modified script generates a "Premature end of script headers" error.<!--content-->
Silly me! I tested the code on my local PC, where it worked fine, and I thought it would work just as well on the TCH server without further testing.<br /><br />I had to have the Help Desk install the DateTime and DateTime::TimeZone modules on my server before the code would work correctly. Submit a ticket to the Help Desk and ask them to install the DateTime and DateTime::TimeZone perl modules on your server, then try your script again.<!--content-->
<!--quoteo(post=155235:date=Nov 11 2005, 03:33 PM:name=TCH-David)--><div class='quotetop'>QUOTE(TCH-David @ Nov 11 2005, 03:33 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=155235"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Submit a ticket to the Help Desk and ask them to install the DateTime and DateTime::TimeZone perl modules on your server, then try your script again.<!--QuoteEnd--></div><!--QuoteEEnd--><br />I submitted the ticket, the modules were installed swiftly, and the script now works beautifully! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/clapping.gif" style="vertical-align:middle" emoid=":clapping:" border="0" alt="clapping.gif" /><br /><br />Thanks again for all your help!<!--content-->
 
Back
Top