C timespec_getres() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C23 timespec_getres() function reports the resolution associated with a supported time base. With TIME_UTC, the result describes the granularity of calendar-time readings.

The function stores the resolution in a struct timespec when the destination is not NULL. It is new in C23, so compiler and library support may lag behind language-mode support. Include <time.h>.

Syntax

Start with this concrete timespec_getres() call:

C
timespec_getres(&resolution, TIME_UTC);
CallWhat it does
timespec_getres(&resolution, TIME_UTC);Stores the resolution of TIME_UTC in resolution.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
int timespec_getres(struct timespec *ts, int base);

Parameters

ParameterDescription
tsPointer to a writable resolution object; C23 permits NULL when only support is queried.
baseTime base whose resolution is requested, normally TIME_UTC.

Return Value

Return ValueDescription
TIME_UTCThe base is supported and its resolution was queried.
0The requested base is unsupported.

C Examples

Example 1 — Query TIME_UTC Resolution

This example demonstrates timespec_getres() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    struct timespec resolution;
    if (timespec_getres(&resolution, TIME_UTC) == TIME_UTC)
        printf("resolution: %lld s, %ld ns\n",
               (long long)resolution.tv_sec, resolution.tv_nsec);
    return 0;
}

How It Works

  1. Step 1. Create storage for the resolution.
  2. Step 2. Ask about the standard calendar-time base.
  3. Step 3. Confirm that the returned base indicates support.
  4. Step 4. Read the seconds and nanoseconds that describe one resolution unit.

Example 2 — Convert Resolution to Nanoseconds

This example demonstrates timespec_getres() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    struct timespec r;
    if (timespec_getres(&r, TIME_UTC) == TIME_UTC) {
        long long ns = (long long)r.tv_sec * 1000000000LL + r.tv_nsec;
        printf("resolution: %lld ns\n", ns);
    }
    return 0;
}

How It Works

  1. Step 1. Query resolution into a complete timespec.
  2. Step 2. Convert whole seconds to nanoseconds.
  3. Step 3. Add the already fractional nanosecond field.
  4. Step 4. Print one combined unit that is convenient for comparison.

Example 3 — Check Base Support

This example demonstrates timespec_getres() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    int supported = timespec_getres(NULL, TIME_UTC);
    if (supported == TIME_UTC)
        printf("TIME_UTC is supported\n");
    else
        printf("TIME_UTC is unavailable\n");
    return 0;
}

How It Works

  1. Step 1. Pass NULL because no resolution value is needed.
  2. Step 2. Request support information for TIME_UTC.
  3. Step 3. Compare the return value with the requested base.
  4. Step 4. Report support without dereferencing any result object.

Frequently Asked Questions

timespec_getres() queries the resolution of a C23 time base.
Include <time.h> before calling timespec_getres().
On success it returns the base value after storing the resolution. On failure it returns 0. This API needs C23 support.
This function requires C23 library support. Resolution is the clock's granularity, not a promise that every reading is exact to that amount.
It is an ISO C23 function, so it requires a recent C library.

Next: C time.h Functions

Continue through the C time.h function tutorials.

C time.h Functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful