C timespec_get() Function

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

Definition

The C11 timespec_get() function stores calendar time in a struct timespec, which has whole seconds in tv_sec and a fractional nanosecond field in tv_nsec.

Pass TIME_UTC as the time base. Success means the function returns that same base; zero means the requested base is unsupported or the time could not be obtained. Include <time.h>.

Syntax

Start with this concrete timespec_get() call:

C
timespec_get(&now, TIME_UTC);
CallWhat it does
timespec_get(&now, TIME_UTC);Stores the current TIME_UTC calendar time in now.

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_get(struct timespec *ts, int base);

Parameters

ParameterDescription
tsPointer to a writable struct timespec.
baseRequested time base; standard C11 defines TIME_UTC.

Return Value

Return ValueDescription
TIME_UTCThe requested calendar time was stored successfully.
0The base is unsupported or time acquisition failed.

C Examples

Example 1 — Read Seconds and Nanoseconds

This example demonstrates timespec_get() 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 now;
    if (timespec_get(&now, TIME_UTC) == TIME_UTC)
        printf("seconds: %lld\nnanoseconds: %ld\n",
               (long long)now.tv_sec, now.tv_nsec);
    return 0;
}

How It Works

  1. Step 1. Allocate a timespec destination.
  2. Step 2. Request the standard TIME_UTC base.
  3. Step 3. Verify that the returned base matches the request.
  4. Step 4. Display whole seconds and the fractional field separately.

Example 2 — Print a Fractional Timestamp

This example demonstrates timespec_get() 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 now;
    if (timespec_get(&now, TIME_UTC) == TIME_UTC)
        printf("%lld.%09ld\n", (long long)now.tv_sec, now.tv_nsec);
    return 0;
}

How It Works

  1. Step 1. Capture one calendar-time reading.
  2. Step 2. Cast the seconds field for a simple portable display.
  3. Step 3. Print nanoseconds with exactly nine digits and leading zeros.
  4. Step 4. The dot joins the whole and fractional parts of the same reading.

Example 3 — Measure a Calendar Interval

This example demonstrates timespec_get() 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 a, b;
    timespec_get(&a, TIME_UTC);
    for (volatile long i = 0; i < 1000000; ++i) { }
    timespec_get(&b, TIME_UTC);
    double seconds = (double)(b.tv_sec-a.tv_sec) +
                     (b.tv_nsec-a.tv_nsec)/1000000000.0;
    printf("elapsed: %.6f s\n", seconds);
    return 0;
}

How It Works

  1. Step 1. Take a reading before the sample work.
  2. Step 2. Take a second reading afterward.
  3. Step 3. Subtract both seconds and nanoseconds, allowing the fractional term to be negative during a borrow.
  4. Step 4. Combine the parts as a double; this is calendar elapsed time, not CPU time.

Frequently Asked Questions

timespec_get() gets C11 calendar time with seconds and nanoseconds.
Include <time.h> before calling timespec_get().
On success it returns the base value (for example TIME_UTC) after filling the timespec. On failure it returns 0.
Nanosecond units do not guarantee nanosecond accuracy. The actual clock resolution is implementation- and system-dependent.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: timespec_getres()

Continue through the C time.h function tutorials.

timespec_getres() →

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