Altering the system clock

J

jny0

Hi,

I've submitted this in c.moderated as well (but it hasn't yet made it
to be posted), so sorry if you're thinking you've read this already.

I need some code to write a new time to the system clock. I can draw
the time from it easily enough, but need to be able to sync the system
with a server time. How can I do this?

Regards,
JNY
 
S

Seebs

I've submitted this in c.moderated as well (but it hasn't yet made it
to be posted), so sorry if you're thinking you've read this already.

It got rejected.
I need some code to write a new time to the system clock. I can draw
the time from it easily enough, but need to be able to sync the system
with a server time. How can I do this?

You can't in portable C. A way to do it may exist on any given systems,
but on many systems, the answer will be "you can't, have the system
administrator do it for you."

-s
 
N

Nobody

I need some code to write a new time to the system clock. I can draw
the time from it easily enough, but need to be able to sync the system
with a server time. How can I do this?

You should assume that you cannot do this.

If you just need to synchronise your application to a remote server, you
should leave the system clock alone and just store the difference between
the local system time and the server's time.

On Unix, adjusting the system clock typically requires superuser privilege
(or CAP_SYS_TIME on systems with capabilities). But that's only the
minimum kernel-imposed requirement; there may be additional constraints.

E.g. if the system is running ntpd, ntpd will try to maintain the system
clock, and will try to correct any adjustments which you make to it. Also,
there may be reasons why the system needs to be accurately (and
demonstrably) synchronised to a known time source, and messing with that
may result in contractual or even statutory liability (even if they clock
remains accurate, you may not be able to adequately prove it).

Making instantaneous adjustments to the system clock is a very bad idea,
as it will break anything which tries to perform rate-of-change
calculations. If the time goes backwards, files will suddenly have
timestamps in the future.

For this reason, most modern Unix systems have interfaces such as
adjtime(), where you specify a desired change and the kernel makes the
system clock run slightly (like, 1%) fast or slow until the desired change
has been completed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top