Forum Moderators: coopster & phranque

Message Too Old, No Replies

when calling the time it comes with the servers time

i need it with my local time

         

Toldo

12:20 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



i have a news website so i update the site daily
but the time that appears to the visitors is the servers time .. i need it with my local time .. how is that?

KevinADC

2:01 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



add or subtract the amount of time your server differs from your localtime. For example, you need to add three hours:

my $offset = 10800;
my $time = localtime(time+$offset);

10800 is the number of seconds in three hours.

Toldo

2:28 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



where should i add that?

KevinADC

6:36 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



err... add it whereever it's needed? I have no clue really without seeing your script, and if it's a long script I probably don't want to see it. If it's not too long post the script or a link to a text copy of the script.

crevier

10:05 pm on Sep 19, 2005 (gmt 0)

10+ Year Member


The server I use for all my personal stuff is in the eastern time zone, but I'm in central. So I just change the TZ environment variable and all my scripts behave as if they're in the central time zone. No math needed:

$ENV{'TZ'} = 'CST6CDT';

I actually have a library that I include in all my scripts, so I just set this variable in that library. Works great!

If you're not familiar with the syntax, it's the abbreviation for standard time, followed by the hours to add to get GMT, then the abbreviation for daylight savings time. For example, if you're in California, it would be: PST8PDT

(This should work fine on most Unix-like servers. Not sure about Windows.)

KevinADC

11:33 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



you use that env variable in conjunction with perl scripts? Can you give an example please.

crevier

2:00 pm on Sep 20, 2005 (gmt 0)

10+ Year Member



Sure. One of the more common uses for me is showing when a file was last updated. So I just whipped up the following script. It contains lines at the top that you can easily comment out for testing to see the difference in the result for different time zones. I included four of the American time zones along with GMT. The code can be cleaned up a bit, but you get the idea.


#!/usr/bin/perl


#$ENV{'TZ'} = 'GMT0GMT';
#$ENV{'TZ'} = 'EDT5EST';
#$ENV{'TZ'} = 'CDT6CST';
#$ENV{'TZ'} = 'MDT7MST';
$ENV{'TZ'} = 'PDT8PST';


# get the time stamp on the file
$file = '/tmp/test.html';
$stamp = (stat($file))[9];
($sec,$min,$hr,$mday,$mon,$yyyy,$wday,$yday,$isdst) = localtime($stamp);


# convert military to English
$ampm = ($hr < 12)? 'am' : 'pm';
if ($hr < 1) { $hr = 12; }
elsif ($hr > 12) { $hr -= 12; }


# make the date parts pretty
$mm = sprintf("%02d", $mon + 1);
$dd = sprintf("%02d", $mday);
$yyyy += 1900;


# make the time parts pretty
$hr = sprintf("%02d", $hr);
$min = sprintf("%02d", $min);
$sec = sprintf("%02d", $sec);


print "The file was last updated on ${mm}/${dd}/${yyyy} at ${hr}:${min}:${sec} ${ampm}\n";


exit 0;

A great implementation of this is if you have visitors who login to your site, and you know their local time zone, you can make it dynamic based on the user's preferences. (Or you can get fancy and use one of those databases that gives you a city and state based on an I.P. address, though those aren't 100% reliable.)

Note: This script works great on my Linux server. However, I just tested on Windows, and the TZ environment variable has no effect; it always displays the time in my local zone. But, knowing that it works on Linux, perhaps there is some similar solution for Windows. Hmmm.

KevinADC

8:20 pm on Sep 20, 2005 (gmt 0)

10+ Year Member



It does work on nix boxes, very cool. I had never seen this particular env variable before. It doesn't print out when I run this script on a nix box:


#!perl
print "Content-type: text/html\n\n";
foreach $key (sort keys(%ENV)) {
printf("%-10.20s: $ENV{$key}\n", $key);
}
exit;

but it does change the time output to the corresponding time zone when I run the script you posted. :)