* lib/time.in.h (timespec_getres): New decl.
* lib/timespec_getres.c, m4/timespec_getres.m4:
* modules/timespec_getres, modules/timespec_getres-tests:
* tests/test-timespec_getres.c:
New files.
* m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS, gl_TIME_H_DEFAULTS):
* modules/time (time.h):
Support timespec_getres.
2021-12-28 Paul Eggert <eggert@cs.ucla.edu>
+ timespec_getres: new module
+ * lib/time.in.h (timespec_getres): New decl.
+ * lib/timespec_getres.c, m4/timespec_getres.m4:
+ * modules/timespec_getres, modules/timespec_getres-tests:
+ * tests/test-timespec_getres.c:
+ New files.
+ * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS, gl_TIME_H_DEFAULTS):
+ * modules/time (time.h):
+ Support timespec_getres.
+
gettime-res: new module
* lib/gettime-res.c, modules/gettime-res: New files.
* lib/timespec.h (gettime_res): New decl.
--- /dev/null
+@node timespec_getres
+@subsection @code{timespec_getres}
+@findex timespec_getres
+
+Gnulib module: timespec_get
+
+Portability problems fixed by Gnulib:
+@itemize
+@item
+This function is missing on many platforms:
+glibc 2.33, macOS 12, FreeBSD 13.0, NetBSD 9.2, OpenBSD 7.0, Minix 3.3.0, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, Cygwin 2.9, mingw, MSVC 14, Android 9.0.
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
ISO C2x specification:@* @url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf} section 7.27.2.6
-Gnulib module: ---
+Gnulib module: timespec_getres
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+glibc 2.33, macOS 12, FreeBSD 13.0, NetBSD 9.2, OpenBSD 7.0, Minix 3.3.0, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, Cygwin 2.9, mingw, MSVC 14, Android 9.0.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-glibc 2.33, macOS 11.1, FreeBSD 13.0, NetBSD 9.0, OpenBSD 6.7, Minix 3.3.0, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.4, Cygwin 2.9, mingw, MSVC 14, Android 9.0.
@end itemize
_GL_CXXALIASWARN (timespec_get);
# endif
+/* Set *TS to the current time resolution, and return BASE.
+ Upon failure, return 0. */
+# if @GNULIB_TIMESPEC_GETRES@
+# if ! @HAVE_TIMESPEC_GETRES@
+_GL_FUNCDECL_SYS (timespec_getres, int, (struct timespec *ts, int base)
+ _GL_ARG_NONNULL ((1)));
+# endif
+_GL_CXXALIAS_SYS (timespec_getres, int, (struct timespec *ts, int base));
+_GL_CXXALIASWARN (timespec_getres);
+# endif
+
/* Sleep for at least RQTP seconds unless interrupted, If interrupted,
return -1 and store the remaining time into RMTP. See
<https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
--- /dev/null
+/* Get resolution of a time base.
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <time.h>
+
+#include "timespec.h"
+
+/* Set TS to resolution of time base BASE.
+ Return BASE if successful, 0 otherwise. */
+int
+timespec_getres (struct timespec *ts, int base)
+{
+ if (base == TIME_UTC)
+ {
+#if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
+ clock_getres (CLOCK_REALTIME, ts);
+#else
+ long int res = gettime_res ();
+ ts->tv_sec = res / TIMESPEC_HZ;
+ ts->tv_nsec = res % TIMESPEC_HZ;
+#endif
+ return base;
+ }
+ return 0;
+}
# Copyright (C) 2000-2001, 2003-2007, 2009-2021 Free Software Foundation, Inc.
-# serial 18
+# serial 19
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRPTIME])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMEGM])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMESPEC_GET])
+ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMESPEC_GETRES])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIME_R])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIME_RZ])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TZSET])
HAVE_STRPTIME=1; AC_SUBST([HAVE_STRPTIME])
HAVE_TIMEGM=1; AC_SUBST([HAVE_TIMEGM])
HAVE_TIMESPEC_GET=1; AC_SUBST([HAVE_TIMESPEC_GET])
+ HAVE_TIMESPEC_GETRES=1; AC_SUBST([HAVE_TIMESPEC_GETRES])
dnl Even GNU libc does not have timezone_t yet.
HAVE_TIMEZONE_T=0; AC_SUBST([HAVE_TIMEZONE_T])
dnl If another module says to replace or to not replace, do that.
--- /dev/null
+# Test whether timespec_getres works.
+dnl Copyright 2021 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_TIMESPEC_GETRES],
+[
+ AC_REQUIRE([gl_TIME_H_DEFAULTS])
+
+ dnl Might be needed for the same reason timespec_get needs it.
+ AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+ AC_CHECK_FUNCS_ONCE([timespec_getres])
+ if test $ac_cv_func_timespec_getres != yes; then
+ HAVE_TIMESPEC_GETRES=0
+ fi
+])
-e 's/@''GNULIB_STRPTIME''@/$(GNULIB_STRPTIME)/g' \
-e 's/@''GNULIB_TIMEGM''@/$(GNULIB_TIMEGM)/g' \
-e 's/@''GNULIB_TIMESPEC_GET''@/$(GNULIB_TIMESPEC_GET)/g' \
+ -e 's/@''GNULIB_TIMESPEC_GETRES''@/$(GNULIB_TIMESPEC_GETRES)/g' \
-e 's/@''GNULIB_TIME_R''@/$(GNULIB_TIME_R)/g' \
-e 's/@''GNULIB_TIME_RZ''@/$(GNULIB_TIME_RZ)/g' \
-e 's/@''GNULIB_TZSET''@/$(GNULIB_TZSET)/g' \
-e 's|@''HAVE_STRPTIME''@|$(HAVE_STRPTIME)|g' \
-e 's|@''HAVE_TIMEGM''@|$(HAVE_TIMEGM)|g' \
-e 's|@''HAVE_TIMESPEC_GET''@|$(HAVE_TIMESPEC_GET)|g' \
+ -e 's|@''HAVE_TIMESPEC_GETRES''@|$(HAVE_TIMESPEC_GETRES)|g' \
-e 's|@''HAVE_TIMEZONE_T''@|$(HAVE_TIMEZONE_T)|g' \
-e 's|@''REPLACE_CTIME''@|$(REPLACE_CTIME)|g' \
-e 's|@''REPLACE_GMTIME''@|$(REPLACE_GMTIME)|g' \
--- /dev/null
+Description:
+timespec_getres: return the timestamp resolution
+
+Files:
+lib/timespec_getres.c
+m4/timespec_getres.m4
+
+Depends-on:
+extensions
+time
+gettime-res [test $HAVE_TIMESPEC_GETRES = 0]
+
+configure.ac:
+AC_REQUIRE([gl_FUNC_TIMESPEC_GETRES])
+if test $HAVE_TIMESPEC_GETRES = 0; then
+ AC_LIBOBJ([timespec_getres])
+fi
+gl_TIME_MODULE_INDICATOR([timespec_getres])
+
+Makefile.am:
+
+Include:
+<time.h>
+
+Link:
+$(LIB_CLOCK_GETTIME)
+
+License:
+LGPL
+
+Maintainer:
+all
--- /dev/null
+Files:
+tests/test-timespec_getres.c
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-timespec_getres
+check_PROGRAMS += test-timespec_getres
+test_timespec_getres_LDADD = $(LDADD) @LIB_CLOCK_GETTIME@
--- /dev/null
+/* Test timespec_getres.
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert. */
+
+#include <config.h>
+
+#include <time.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (timespec_getres, int, (struct timespec *ts, int base));
+
+#include "macros.h"
+
+int
+main (void)
+{
+ struct timespec ts;
+ ASSERT (timespec_getres (&ts, TIME_UTC) == TIME_UTC);
+
+ /* No clock resolution should be worse than 1 s or better than 1 ns. */
+ ASSERT (ts.tv_sec == 0
+ ? 0 < ts.tv_nsec && ts.tv_nsec < 1000000000
+ : ts.tv_sec == 1 && ts.tv_nsec == 0);
+
+ return 0;
+}