Re: Subtracting two UTC/time_t timestamps under POSIX

From: Markus Kuhn <Markus.Kuhn_at_CL.CAM.AC.UK>
Date: Thu, 30 Jan 2003 21:07:44 +0000

John Cowan wrote on 2003-01-30 20:36 UTC:
> > As far as I remember in the code, and in practice,
> > time_t was just a cute way to encode a date;
> > nothing more.
>
> You didn't routinely subtract two time_t values to get elapsed time in seconds?

For that purpose, having a leap-second-free second count is not the only
requirement. The far more important requirement is that you use a clock
that can't be adjusted by the sys-admins or by time daemons, and that is
available from startup, even before the system learns about UTC over
some external channel.

For this reason, POSIX offers today two separate clocks:

  CLOCK_REALTIME is supposed to give the system's best available
                   approximation of UTC (that's what you want to
                   convert to hh:mm:ss timestamps and local time)

  CLOCK_MONOTONIC is supposed to progress with the system's best
                   available approximation of the SI second (that's
                   what you want to use for measuring time intervals)

If you know TAI from boot-time, you can feed that into CLOCK_MONOTONIC,
but it can equivalently also just be the number of seconds since
power-up. If you have a reference clock, you can frequency control
CLOCK_MONOTONIC, but you must not phase-control it.

Literature:

  http://www.opengroup.org/onlinepubs/007904975/basedefs/time.h.html

Markus

--
Markus Kuhn, Computer Lab, Univ of Cambridge, GB
http://www.cl.cam.ac.uk/~mgk25/ | __oo_O..O_oo__
Received on Thu Jan 30 2003 - 13:07:53 PST

This archive was generated by hypermail 2.3.0 : Sat Sep 04 2010 - 09:44:54 PDT