Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SET system time

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Anyone have a good way to set the system time...preferably, platform independently? In specific, I need to take the current date, and adjust the time + or - an hour.

Thanks in advance!
 
Do you need to actually modify the system time, so that when your script quits running the clock on the system will show a different time + or - an hour?

If you only need an hour difference in time for your script, just add or subtract from the value you get from time().

Code:
my $now = time();
print "right now: " . localtime($now) . "\n";

my $future = $now + 60*60; # add an hour
print "an hour from now: " . localtime($future) . "\n";

my $past = $now - 60*60; # minus an hour
print "an hour ago: " . localtime($past) . "\n";

Most other time functions besides localtime use a value like what time() returns, so you can just modify the return value of time() in most cases. 60*60's logic is "60 seconds = 1 minute * 60 = 1 hour".

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Actually, I'm looking to change the actual system time...so that the system clock has changed after the script runs.
 
You probably won't be able to do this in a system-independent way.

In Linux it involves copying a file from /usr/share/zoneinfo where /etc/localtime is (a lot of tutorials say to symlink /etc/localtime to a file there, but I've run into problems with that in the past, for instance if you have /etc/localtime as a symlink and then try to copy (not symlink) another time zone file to it, it will actually *overwrite* the time zone it used to be linked to, instead of breaking the link).

Example, set to Pacific time,

Code:
cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
OR
cp /usr/share/zoneinfo/PST8PDT /etc/localtime

On Windows it would likely involve using the Win32 API to change the time, but I don't have any details on how this is done.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
I think the OP meant to change the time, not the time zone... i.e. using the date MMDDHHMM command for example.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top