Jump to content

Mismatched Time Zones


editor
 Share

Recommended Posts

I have a perl script that records member logins on my Web site. Here's part of the code:

_____

 

open(FILE, ">>$log_file");

{($sec,$min,$hour,$day,$month) = localtime();

$month++;

if ($hour < 10)

{ $hour = "0$hour"; }

if ($min < 10)

{ $min = "0$min"; }

}

if ($password ne "xxxx") {

print FILE "$month/$day $hour:$min $password $ENV{'REMOTE_ADDR'}\n";

close(FILE);

_____

 

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?

Link to comment
Share on other sites

Try this code out and see if it works for you:

>use DateTime;
open(FILE, ">>$log_file");
my $dt = DateTime->now(time_zone => 'America/Chicago');
if ($password ne "xxxx") {
print FILE $dt->strftime("%m/%d %H:%M") . " $password $ENV{'REMOTE_ADDR'}\n";
close(FILE);

I appreciate the help. However, the modified script generates a "Premature end of script headers" error.

Link to comment
Share on other sites

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.

 

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.

Link to comment
Share on other sites

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.

I submitted the ticket, the modules were installed swiftly, and the script now works beautifully! :clapping:

 

Thanks again for all your help!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...