timespec_get(3)

πŸ’– timespec_get(3) πŸ’–

okay bestie, welcome to the timespec_get page from the Library Functions Manual β€” yes this is a section 3 manual page and yes it's about the ISO C interface to clock and time functions. grab a boba, we're doing time today πŸ•βœ¨


🌸 NAME 🌸

timespec_get, timespec_getres β€” the ISO C interface to clock and time functions. iykyk, this is the glow-up of getting the time but make it standardised πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C library, aka libc, aka link it with -lc. she's everywhere, she's iconic, she's already in your binary πŸ’–


✨ SYNOPSIS ✨

#include <time.h>

int timespec_get(struct timespec *res, int base);
int timespec_getres(struct timespec *tp, int base);

πŸŽ€ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) πŸŽ€

okay so glibc is picky about which macros you define before you get to play with these, bestie, so listen up:

  • timespec_get(), TIME_UTC β€” you gotta have _ISOC11_SOURCE πŸ’«
  • timespec_getres(), TIME_MONOTONIC, TIME_ACTIVE, TIME_THREAD_ACTIVE β€” you gotta have _ISOC23_SOURCE πŸ’«

πŸ¦‹ DESCRIPTION πŸ¦‹

so, timespec_get() stores the current time β€” based on the time base you picked β€” into the timespec(3type) structure that res points at. it literally hands you the time. no cap 🫢

and timespec_getres()? she stores the resolution of times retrieved by timespec_get() with that specified time base into the timespec(3type) structure that tp points at β€” but only if tp is non-NULL. like, if tp is NULL she just… doesn't. πŸ™ˆ also, for any particular time base, that resolution is constant for the lifetime of the calling process. it does not change on you. periodt.

The time base base is one of these iconic options:

  • TIME_UTC β€” a system-wide time base measuring real time, aka wall-clock time. the time and resolution in this base are exactly the same as what you'd get from clock_gettime(CLOCK_REALTIME, res) and clock_getres(CLOCK_REALTIME, tp). same energy, different font πŸ’–
  • TIME_MONOTONIC β€” a time base that measures time since some unspecified point in the past, and the time within a process will never decrease, even if the system's real time clock gets set or adjusted. she only goes forward, main character energy. the time and resolution here are the same as clock_gettime(CLOCK_MONOTONIC, res) and clock_getres(CLOCK_MONOTONIC, tp) ✨
  • TIME_ACTIVE β€” a process-specific time base measuring CPU time consumed by the calling process. the time and resolution are the same as clock_gettime(CLOCK_PROCESS_CPUTIME_ID, res) and clock_getres(CLOCK_PROCESS_CPUTIME_ID, tp) πŸ”₯
  • TIME_THREAD_ACTIVE β€” a thread-specific time base measuring CPU time consumed by the calling thread. the time and resolution match clock_gettime(CLOCK_THREAD_CPUTIME_ID, res) and clock_getres(CLOCK_THREAD_CPUTIME_ID, tp) 🌈

🫢 RETURN VALUE 🫢

  • timespec_get() returns the nonzero base if it's a supported time base and the current time was successfully retrieved. otherwise it returns 0. that's it, that's the vibe check πŸ’€
  • timespec_getres() returns the nonzero base if it's a supported time base, or 0 otherwise.

πŸ’… ATTRIBUTES πŸ’…

For an explanation of the terms used in this section, see attributes(7).

  • Interface: timespec_get(), timespec_getres()
  • Attribute: Thread safety
  • Value: MT-Safe

(that means she's safe to share with all your threads. no drama, no races, we love to see it πŸ‘‘)


🌟 STANDARDS 🌟

  • timespec_get() β€” C23, POSIX.1-2024.
  • TIME_UTC β€” C23 (though ISO C doesn't specify the time epoch, so don't come for her), POSIX.1-2024.
  • timespec_getres() β€” C23.
  • TIME_MONOTONIC β€” C23.
  • TIME_ACTIVE β€” C23.
  • TIME_THREAD_ACTIVE β€” C23.

πŸ“œ HISTORY πŸ“œ

  • timespec_get() β€” C11, POSIX.1-2024, glibc 2.16, musl 1.1.10.
  • TIME_UTC β€” C11, POSIX.1-2024, glibc 2.16, musl 1.1.10.
  • timespec_getres() β€” C23, glibc 2.34.
  • TIME_MONOTONIC β€” C23, glibc 2.43.
  • TIME_ACTIVE β€” C23, glibc 2.43.
  • TIME_THREAD_ACTIVE β€” C23, glibc 2.43.

(the girls that came in 2.43 are the new kids and they're already slaying 🧚)


πŸ‘€ SEE ALSO πŸ‘€


and that's the whole page bestie, go forth and timestamp responsibly πŸ’–βœ¨

Linux man-pages 6.20 Β· 2026-02-11 β€” mwah, kiss kiss, this page was brought to you by that exact version and date, no cap πŸŽ€πŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: 0BSD
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms