Linux time: POSIX
Use the clock that answers your question
A calendar clock tells you when an event happened. A stopwatch tells you how much time passed. POSIX is a standard for common programming interfaces on Unix-like systems; it defines ways for a program to read different kinds of time. C programs get many declarations for these operations from the <time.h> header. The operating system supplies the actual clocks, and not every system supports the same options.
For example, a benchmark can read a monotonic clock before and after a job:
struct timespec start, end;
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
/* handle the error */
}
do_work();
if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
/* handle the error */
}
This is a code fragment: do_work() stands for the work being measured, and the error branches still need program-specific handling. The program measures the difference between start and end. CLOCK_MONOTONIC is for elapsed time, not a date on a calendar. A monotonic clock is useful for durations and timeouts because it is not set like the wall clock.
Calendar, elapsed, and CPU time
time_t stores a calendar-time value used by functions such as time(). A struct timespec stores seconds and nanoseconds. Nanoseconds are one billionth of a second; their use in a structure does not promise that the clock can measure time with one-nanosecond resolution or accuracy.
clock_gettime() reads a selected clock. CLOCK_REALTIME represents the system's calendar time and can be adjusted by clock-setting operations or synchronization services. Use it for event timestamps that must match a date. Comparing timestamps across computers still depends on clock synchronization and a consistent time basis such as UTC.
CLOCK_MONOTONIC measures elapsed time from an unspecified starting point. Its starting point does not identify a calendar date. It is not a settable wall clock, so it works for timeouts and durations that must ignore sudden changes to calendar time. Other clocks may be available depending on the POSIX system.
Subtract seconds as end.tv_sec - start.tv_sec and adjust by the nanosecond difference. If the ending nanoseconds are smaller, normalize the result by borrowing one second; production code must also check both clock_gettime() return values. clock_getres() reports a clock's nominal resolution, not its measurement accuracy. POSIX process and thread CPU-time clocks, where supported, measure CPU consumed by execution rather than elapsed wall time. A program can wait ten seconds for input while using very little CPU.
Linux-specific choices
Linux's CLOCK_MONOTONIC does not count time spent suspended. Linux adds CLOCK_BOOTTIME, which includes suspend time, and CLOCK_MONOTONIC_RAW, which exposes a hardware-based timeline without the same frequency adjustments. These clock IDs are Linux-specific and may not exist on other POSIX systems. Code that uses them should state the platform requirement or provide a fallback.
Time zones affect how an instant is displayed as local calendar time; they do not change a monotonic measurement. For portable C code, include the needed headers, check feature-test requirements, and choose a clock whose documented meaning fits the measurement.





