edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31871

Is clock_gettime a UTC time, or local time zone?

Does the function clock_gettime return a timestamp measured from the epoch in UTC or in the local time zone?

I know that time is supposed to be from the UTC epoch, but I can't find any reference saying the same is true of clock_gettime.

Upvotes: 9

Views: 15249

Answers (4)

Kingsley
Kingsley

Reputation: 14906

Just to flesh these answers out a bit:

clock_gettime(), the POSIX standard function takes a first parameter for the clock-identifier.

At the time of writing, a common identifier is CLOCK_REALTIME which does indeed fetch the UTC Epoch Time - assuming the UNIX clock is correctly set.

There is also CLOCK_MONOTONIC, which is a clock set from an unspecified time-point in the past. Typically I've seen this implemented as time since process invocation.

And the obligatory example:

struct timespec epochTime;
struct timespec otherTime;

// fetch the system time in UTC
clock_gettime( CLOCK_REALTIME, &epochTime );  // CLOCK_REALTIME returns 1970 Epoch Time

// Fetch some equally-valid time, but with an unknown start
// But relative comparisons are absolutely OK
clock_gettime( CLOCK_MONOTONIC, &otherTime );

Upvotes: 2

Actually, the question does not make a lot of sense. See time(7) for an overview of time-related functions.

Any clock device returns a time measured from some origin event in the past. Unix (and Posix) convention is to measure it from the Epoch (start of 1970, as aix's answer explains).

Displaying some time in UTC, or local time, or using the French revolutionary calendar, or using the Maya calendar, or any other calendar from whatever culture you are interested in, does not change that time. Only the display (or shown form) of that time changes.

It is a bit like saying that two, deux, 1+1, or 2, or 10b -with b meaning binary- are all representations of the same number.

Back to the question, the man page of clock_gettime gives the precise answer to the question. It depends upon the clk_id you are asking for, and for CLOCK_REALTIME, the time is measured since the Unix Epoch. For other clocks (e.g. CLOCK_MONOTONIC), the used origin is not specified.

(I'm quite sadly surprised by the number of questions here which could be answered very quickly by looking into the man. I don't understand the logic of people taking more time to ask the question here that to look -just by typing man clock_gettime on their Linux box- into the man pages).

The notion of time zone is only relevant for struct tm as returned by localtime & gmtime (and related) functions. A time (e.g. some time_t) measured from the Epoch (like the result of time(2), gettimeofday, clock_gettime with CLOCK_REALTIME) has no time zone.

The Unix Epoch is january 01, 1970 0:00 UTC (by definition of (time_t)0), In my time zone (MET= Paris/France) the same Epoch is Thu Jan 1 01:00:00 MET 1970.

Upvotes: 4

mykhal
mykhal

Reputation: 19905

it depends..

if you call clock_gettime(1), it is likely, the the epoch is the time when your linux has started.

Upvotes: -1

NPE
NPE

Reputation: 500357

To quote Wikipedia, the Unix Epoch is defined as

the time 00:00:00 UTC on 1 January 1970 (or 1970-01-01T00:00:00Z ISO 8601).

From this it follows that any reference to "the Epoch" implies UTC.

Upvotes: 16

Related Questions