What is the difference between clock_t, time_t and struct tm?

Cristi picture Cristi · Sep 2, 2012 · Viewed 19.8k times · Source

What is the difference between clock_t, time_t and struct tm?

struct tm looks like this:

struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};

But how does clock_t and time_t look like?

Answer

user149341 picture user149341 · Sep 3, 2012

time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time.

clock_t is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly the computer's bootup, but no guarantees, as it may roll over quite often). There are CLOCKS_PER_SEC clock ticks per second; the value of this constant can vary between different operating systems, but it is usually around 100. It is sometimes used for timing purposes, but it is not very good at it due to its relatively low resolution. gettimeofday's struct timeval is much better for timing purposes.

struct tm is a calendar date and time. It may not represent any real point in time (e.g, you can have a struct tm that says it is February 31st, or Dodecember 0st). It does not include a time zone, so it is not absolute. It is typically used when converting to or from human-readable representations of the date and time.