$NetBSD: timespec_get.3,v 1.9 2025/04/22 14:25:50 uwe Exp $

Copyright (c) 2016, 2025 The NetBSD Foundation, Inc.
All rights reserved.

This code is derived from software contributed to The NetBSD Foundation
by Kamil Rytarowski.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

.Dd April 22, 2025 .Dt TIMESPEC_GET 3 .Os . .Sh NAME .Nm timespec_get , .Nm timespec_getres .Nd get current calendar time . .Sh LIBRARY .Lb libc . .Sh SYNOPSIS . n time.h . .Ft int .Fn timespec_get "struct timespec *ts" "int base" . .Ft int .Fn timespec_getres "struct timespec *ts" "int base" . .Sh DESCRIPTION The .Fn timespec_get function sets the interval pointed to by .Fa ts to hold the current calendar time based on the specified time base in .Fa base . It is the .Tn ISO C equivalent to the .Tn POSIX function .Xr clock_gettime 2 .

p The base .Dv TIME_UTC returns the time since the epoch. This time is expressed in seconds and nanoseconds since midnight (0 hour), January 1, 1970. In .Nx , this corresponds to .Dv CLOCK_REALTIME .

p The base .Dv TIME_MONOTONIC returns the seconds and nanoseconds since an implementation-defined reference point in such a way that a second call will never return a value less than the first. In .Nx , this corresponds to .Dv CLOCK_MONOTONIC .

p The resolution of each timer can be queried using .Fn timespec_getres . Each successive call to this function will return the same value for a specific clock. It is the .Tn ISO C equivalent to the .Tn POSIX function .Xr clock_getres 2 . .Sh RETURN VALUES These functions return the passed non-zero value of .Fa base if successful, otherwise 0 on failure. .Sh SEE ALSO .Xr clock_gettime 2 , .Xr gettimeofday 2 , .Xr time 3 .Sh STANDARDS The .Fn timespec_get function with a .Fa base of .Dv TIME_UTC conforms to .St -isoC-2011 . The .Fa base of .Dv TIME_MONOTONIC is specified in .St -isoC-2023 .

p The .Fn timespec_getres function conforms to .St -isoC-2023 . .Sh HISTORY The .Fn timespec_get function first appeared in .Nx 8.0 .

p The .Fn timespec_getres function first appeared in .Nx 11.0 . .Sh AUTHORS .An Kamil Rytarowski Aq Mt kamil@NetBSD.org .An Nia Alarie Aq Mt nia@NetBSD.org