[Tutor] Setting the system time

dman dsh8290@rit.edu
Wed, 30 Jan 2002 22:41:51 -0500


On Wed, Jan 30, 2002 at 08:05:21PM +0100, Daniel Kinnaer wrote:
| 
| 
| There are various ways to _get_ the system time
| >>> time1 = time.localtime(time.time())
| >>> print time1
| (2002, 1, 30, 19, 56, 8, 2, 30, 0)

Yeah, this is easy.

| What is the way to _set_ a system time (to another value)?

What's your system and what privileges do you have?

On *nix (well, linux at least) systems the 'date' program can be used
(by root only) to set the current system time and the 'hwclock'
program can be used (also by root only) to set the hardware clock's
time and/or to update the system clock with the data from the hardware
clock.

On windows you right click on the clock in the task bar and click some
more buttons to specify the new current time.  If you have a NT
variant you must have administrator privileges to change the time.

The reason for the restriction is you don't want any Joe User to
fiddle with the clock.  After all, time marches on (from our
perspective anyways) and there is no reason to have user intervention.
The only time a human must change the clock's settings is if the clock
goes haywire for some reason (CMOS battery dies, timezone has changed
and your sytem doesn't understand that[1]).


I'm sure that each system has a programmatic interface for resetting
the clock, but that will be very system dependent and will also
require that your program run with the privileges needed for tweaking
the system's clock.  Normally programs don't try and reset time
anyways.

Why do you want to be able to set the system clock?  Is it just so you
can do some sort of calculations with time objects?  There's no need
to change the system clock for just that -- simply create the time
objects you want and pass them around.


-D


[1] This means all Windows systems -- they *assume* the hardware clock
    is in local time and changing the timezone setting in windows has
    no effect on that (it just assumes it means the local time for the
    new timezone you pick).  This contrasts with my linux boxes which
    I have (choosen to) set the hardware clock to UTC and changing the
    timezone on the system causes a new calculation of local time and
    thus localtime is always local without resetting the clock.

-- 

Better a little with righteousness
than much gain with injustice.
        Proverbs 16:8