liveBook · Manning

Struct timespec

As explained in Modern C

struct timespec is a C structure type that represents a time interval or timestamp with integral seconds and fractional nanoseconds.

struct timespec represents a time interval or timestamp with integral seconds and fractional nanoseconds. Its tv_nsec member supplies precision beyond time_t, whose granularity is seconds. section 8.5

Obtaining a precise timestamp

struct timespec ts;\ntimespec_get(&ts, TIME_UTC);\n\nThe timespec_get call writes the result into ts. TIME_UTC is the only base defined by the C standard, and its result should be consistent with time. section 8.5

Keep these distinctions in mind:

  • Use struct timespec when second-level precision is insufficient. section 8.5
  • Platform-specific base values may designate clocks other than wall-clock time, including monotonic or processor-time clocks. section 8.5

How Modern C covers it

How deep it goes
Defines nanosecond precision and timespec_get usage, but does not explain representation or arithmetic internals.
Examples
Uses a timespec_get declaration and contrasts wall-clock, monotonic, and processor-time clocks.
What it assumes
Assumes basic C structures and function calls while contrasting the type with time_t and clock_t.
What you can do after
Operate timespec_get with TIME_UTC to obtain a timestamp containing nanosecond precision.
How it connects
Connects time_t, timespec_get, clock_t, TIME_UTC, and monotonic clocks.

Also explained in

Compare how 2 books explain struct timespec →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.