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.