bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* new module 'timespec_get'
@ 2021-01-02 12:18 Bruno Haible
  0 siblings, 0 replies; only message in thread
From: Bruno Haible @ 2021-01-02 12:18 UTC (permalink / raw)
  To: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 1231 bytes --]

One of the glibc functions that was added in 2012 already is actually
specified in ISO C 11: 'timespec_get'. This set of patches implements it,
as a thin wrapper around the 'gettime' module.


2021-01-02  Bruno Haible  <bruno@clisp.org>

	timespec_get: Add tests.
	* tests/test-timespec_get.c: New file.
	* modules/timespec_get-tests: New file.

	timespec_get: New module.
	* lib/time.in.h (timespec_get): New declaration.
	* lib/timespec_get.c: New file.
	* m4/timespec_get.m4: New file.
	* m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize
	GNULIB_TIMESPEC_GET, HAVE_TIMESPEC_GET.
	* modules/time (Makefile.am): Substitute GNULIB_TIMESPEC_GET,
	HAVE_TIMESPEC_GET.
	* modules/timespec_get: New file.
	* tests/test-time-c++.cc (timespec_get): Check signature.
	* doc/glibc-functions/timespec_get.texi: Mention the new module.

2021-01-01  Bruno Haible  <bruno@clisp.org>

	time: Define TIME_UTC.
	* lib/time.in.h (TIME_UTC, GNULIB_defined_TIME_UTC): New macros.
	* m4/time_h.m4 (gl_HEADER_TIME_H_BODY): Set TIME_H_DEFINES_TIME_UTC.
	* modules/time (Makefile.am): Substitute TIME_H_DEFINES_TIME_UTC.
	* tests/test-time.c: Check that TIME_UTC is defined and a positive
	integer.
	* doc/posix-headers/time.texi: Mention TIME_UTC.


[-- Attachment #2: 0001-time-Define-TIME_UTC.patch --]
[-- Type: text/x-patch, Size: 5066 bytes --]

From 2d4f9d5c5a0c6dbdc2fbe34c42fe9cd74f162af3 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 2 Jan 2021 00:14:05 +0100
Subject: [PATCH 1/3] time: Define TIME_UTC.

* lib/time.in.h (TIME_UTC, GNULIB_defined_TIME_UTC): New macros.
* m4/time_h.m4 (gl_HEADER_TIME_H_BODY): Set TIME_H_DEFINES_TIME_UTC.
* modules/time (Makefile.am): Substitute TIME_H_DEFINES_TIME_UTC.
* tests/test-time.c: Check that TIME_UTC is defined and a positive
integer.
* doc/posix-headers/time.texi: Mention TIME_UTC.
---
 ChangeLog                   | 10 ++++++++++
 doc/posix-headers/time.texi |  6 ++++--
 lib/time.in.h               |  8 ++++++++
 m4/time_h.m4                | 18 +++++++++++++++++-
 modules/time                |  1 +
 tests/test-time.c           |  4 ++++
 6 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 16fafe4..f222500 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2021-01-01  Bruno Haible  <bruno@clisp.org>
 
+	time: Define TIME_UTC.
+	* lib/time.in.h (TIME_UTC, GNULIB_defined_TIME_UTC): New macros.
+	* m4/time_h.m4 (gl_HEADER_TIME_H_BODY): Set TIME_H_DEFINES_TIME_UTC.
+	* modules/time (Makefile.am): Substitute TIME_H_DEFINES_TIME_UTC.
+	* tests/test-time.c: Check that TIME_UTC is defined and a positive
+	integer.
+	* doc/posix-headers/time.texi: Mention TIME_UTC.
+
+2021-01-01  Bruno Haible  <bruno@clisp.org>
+
 	stddef: Try harder to get max_align_t defined on OpenBSD, part 2.
 	* modules/stddef (Depends-on): Add extensions.
 
diff --git a/doc/posix-headers/time.texi b/doc/posix-headers/time.texi
index 980212c..9d25ed2 100644
--- a/doc/posix-headers/time.texi
+++ b/doc/posix-headers/time.texi
@@ -9,11 +9,13 @@ Portability problems fixed by Gnulib:
 @itemize
 @item
 @samp{struct timespec} is not defined on some platforms.
-
+@item
+The macro @code{TIME_UTC} is not defined on many platforms:
+glibc 2.15, Mac OS X 10.13, FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.0, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, Cygwin 2.9, mingw, MSVC 14, Android 9.0.
 @item
 Some platforms provide a @code{NULL} macro that cannot be used in arbitrary
 expressions:
-NetBSD 5.0
+NetBSD 5.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/time.in.h b/lib/time.in.h
index 9ed57e4..33c4dad 100644
--- a/lib/time.in.h
+++ b/lib/time.in.h
@@ -101,6 +101,14 @@ struct __time_t_must_be_integral {
 #  define GNULIB_defined_struct_time_t_must_be_integral 1
 # endif
 
+/* Define TIME_UTC, a positive integer constant used for timespec_get().  */
+# if ! @TIME_H_DEFINES_TIME_UTC@
+#  if !GNULIB_defined_TIME_UTC
+#   define TIME_UTC 1
+#   define GNULIB_defined_TIME_UTC 1
+#  endif
+# 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>.  */
diff --git a/m4/time_h.m4 b/m4/time_h.m4
index 07e6967..439ee74 100644
--- a/m4/time_h.m4
+++ b/m4/time_h.m4
@@ -2,7 +2,7 @@
 
 # Copyright (C) 2000-2001, 2003-2007, 2009-2021 Free Software Foundation, Inc.
 
-# serial 13
+# serial 14
 
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
@@ -25,6 +25,22 @@ AC_DEFUN([gl_HEADER_TIME_H_BODY],
   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_TIMESPEC])
 
   AC_REQUIRE([AC_C_RESTRICT])
+
+  AC_CACHE_CHECK([for TIME_UTC in <time.h>],
+    [gl_cv_time_h_has_TIME_UTC],
+    [AC_COMPILE_IFELSE(
+       [AC_LANG_PROGRAM(
+          [[#include <time.h>
+          ]],
+          [[static int x = TIME_UTC; x++;]])],
+       [gl_cv_time_h_has_TIME_UTC=yes],
+       [gl_cv_time_h_has_TIME_UTC=no])])
+  if test $gl_cv_time_h_has_TIME_UTC = yes; then
+    TIME_H_DEFINES_TIME_UTC=1
+  else
+    TIME_H_DEFINES_TIME_UTC=0
+  fi
+  AC_SUBST([TIME_H_DEFINES_TIME_UTC])
 ])
 
 dnl Check whether 'struct timespec' is declared
diff --git a/modules/time b/modules/time
index 2f91fa3..2932375 100644
--- a/modules/time
+++ b/modules/time
@@ -58,6 +58,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
 	      -e 's|@''SYS_TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \
 	      -e 's|@''TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \
 	      -e 's|@''UNISTD_H_DEFINES_STRUCT_TIMESPEC''@|$(UNISTD_H_DEFINES_STRUCT_TIMESPEC)|g' \
+	      -e 's|@''TIME_H_DEFINES_TIME_UTC''@|$(TIME_H_DEFINES_TIME_UTC)|g' \
 	      -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
 	      -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
 	      -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
diff --git a/tests/test-time.c b/tests/test-time.c
index a3a3276..e5a4522 100644
--- a/tests/test-time.c
+++ b/tests/test-time.c
@@ -34,6 +34,10 @@ pid_t t2;
    per POSIX 2008.  */
 verify (sizeof NULL == sizeof (void *));
 
+/* Check that TIME_UTC is defined and a positive integer.  */
+int t3 = TIME_UTC;
+verify (TIME_UTC > 0);
+
 int
 main (void)
 {
-- 
2.7.4


[-- Attachment #3: 0002-timespec_get-New-module.patch --]
[-- Type: text/x-patch, Size: 9335 bytes --]

From caf114a384a66c506ad1304197264cfac64f1fbc Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 2 Jan 2021 13:14:08 +0100
Subject: [PATCH 2/3] timespec_get: New module.

* lib/time.in.h (timespec_get): New declaration.
* lib/timespec_get.c: New file.
* m4/timespec_get.m4: New file.
* m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize
GNULIB_TIMESPEC_GET, HAVE_TIMESPEC_GET.
* modules/time (Makefile.am): Substitute GNULIB_TIMESPEC_GET,
HAVE_TIMESPEC_GET.
* modules/timespec_get: New file.
* tests/test-time-c++.cc (timespec_get): Check signature.
* doc/glibc-functions/timespec_get.texi: Mention the new module.
---
 ChangeLog                             | 14 ++++++++++++++
 doc/glibc-functions/timespec_get.texi |  8 ++++----
 lib/time.in.h                         | 11 +++++++++++
 lib/timespec_get.c                    | 33 +++++++++++++++++++++++++++++++++
 m4/time_h.m4                          |  4 +++-
 m4/timespec_get.m4                    | 18 ++++++++++++++++++
 modules/time                          |  2 ++
 modules/timespec_get                  | 32 ++++++++++++++++++++++++++++++++
 tests/test-time-c++.cc                |  4 ++++
 9 files changed, 121 insertions(+), 5 deletions(-)
 create mode 100644 lib/timespec_get.c
 create mode 100644 m4/timespec_get.m4
 create mode 100644 modules/timespec_get

diff --git a/ChangeLog b/ChangeLog
index f222500..899f7a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2021-01-02  Bruno Haible  <bruno@clisp.org>
+
+	timespec_get: New module.
+	* lib/time.in.h (timespec_get): New declaration.
+	* lib/timespec_get.c: New file.
+	* m4/timespec_get.m4: New file.
+	* m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize
+	GNULIB_TIMESPEC_GET, HAVE_TIMESPEC_GET.
+	* modules/time (Makefile.am): Substitute GNULIB_TIMESPEC_GET,
+	HAVE_TIMESPEC_GET.
+	* modules/timespec_get: New file.
+	* tests/test-time-c++.cc (timespec_get): Check signature.
+	* doc/glibc-functions/timespec_get.texi: Mention the new module.
+
 2021-01-01  Bruno Haible  <bruno@clisp.org>
 
 	time: Define TIME_UTC.
diff --git a/doc/glibc-functions/timespec_get.texi b/doc/glibc-functions/timespec_get.texi
index 8113a9b..c5cf236 100644
--- a/doc/glibc-functions/timespec_get.texi
+++ b/doc/glibc-functions/timespec_get.texi
@@ -2,15 +2,15 @@
 @subsection @code{timespec_get}
 @findex timespec_get
 
-Gnulib module: ---
+Gnulib module: timespec_get
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.15, Mac OS X 10.13, FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.0, Minix 3.1.8, 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 many platforms:
-glibc 2.15, Mac OS X 10.13, FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.0, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, Cygwin 2.9, mingw, MSVC 14, Android 9.0.
 @end itemize
diff --git a/lib/time.in.h b/lib/time.in.h
index 33c4dad..4da1172 100644
--- a/lib/time.in.h
+++ b/lib/time.in.h
@@ -109,6 +109,17 @@ struct __time_t_must_be_integral {
 #  endif
 # endif
 
+/* Set *TS to the current time, and return BASE.
+   Upon failure, return 0.  */
+# if @GNULIB_TIMESPEC_GET@
+#  if ! @HAVE_TIMESPEC_GET@
+_GL_FUNCDECL_SYS (timespec_get, int, (struct timespec *ts, int base)
+                                     _GL_ARG_NONNULL ((1)));
+#  endif
+_GL_CXXALIAS_SYS (timespec_get, int, (struct timespec *ts, int base));
+_GL_CXXALIASWARN (timespec_get);
+# 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>.  */
diff --git a/lib/timespec_get.c b/lib/timespec_get.c
new file mode 100644
index 0000000..d738c12
--- /dev/null
+++ b/lib/timespec_get.c
@@ -0,0 +1,33 @@
+/* timespec_get() function.
+   Copyright (C) 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/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <time.h>
+
+#include "timespec.h"
+
+int
+timespec_get (struct timespec *ts, int base)
+{
+  if (base == TIME_UTC)
+    {
+      gettime (ts);
+      return base;
+    }
+  return 0;
+}
diff --git a/m4/time_h.m4 b/m4/time_h.m4
index 439ee74..b6a1aa3 100644
--- a/m4/time_h.m4
+++ b/m4/time_h.m4
@@ -2,7 +2,7 @@
 
 # Copyright (C) 2000-2001, 2003-2007, 2009-2021 Free Software Foundation, Inc.
 
-# serial 14
+# serial 15
 
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
@@ -129,6 +129,7 @@ AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS],
   GNULIB_STRFTIME=0;                     AC_SUBST([GNULIB_STRFTIME])
   GNULIB_STRPTIME=0;                     AC_SUBST([GNULIB_STRPTIME])
   GNULIB_TIMEGM=0;                       AC_SUBST([GNULIB_TIMEGM])
+  GNULIB_TIMESPEC_GET=0;                 AC_SUBST([GNULIB_TIMESPEC_GET])
   GNULIB_TIME_R=0;                       AC_SUBST([GNULIB_TIME_R])
   GNULIB_TIME_RZ=0;                      AC_SUBST([GNULIB_TIME_RZ])
   GNULIB_TZSET=0;                        AC_SUBST([GNULIB_TZSET])
@@ -139,6 +140,7 @@ AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS],
   HAVE_NANOSLEEP=1;                      AC_SUBST([HAVE_NANOSLEEP])
   HAVE_STRPTIME=1;                       AC_SUBST([HAVE_STRPTIME])
   HAVE_TIMEGM=1;                         AC_SUBST([HAVE_TIMEGM])
+  HAVE_TIMESPEC_GET=1;                   AC_SUBST([HAVE_TIMESPEC_GET])
   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.
diff --git a/m4/timespec_get.m4 b/m4/timespec_get.m4
new file mode 100644
index 0000000..a4a3b78
--- /dev/null
+++ b/m4/timespec_get.m4
@@ -0,0 +1,18 @@
+# timespec_get.m4 serial 1
+dnl Copyright (C) 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_GET],
+[
+  AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
+
+  dnl Persuade OpenBSD <time.h> to declare timespec_get().
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+  AC_CHECK_FUNCS_ONCE([timespec_get])
+  if test $ac_cv_func_timespec_get != yes; then
+    HAVE_TIMESPEC_GET=0
+  fi
+])
diff --git a/modules/time b/modules/time
index 2932375..c0fb27d 100644
--- a/modules/time
+++ b/modules/time
@@ -36,6 +36,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
 	      -e 's/@''GNULIB_STRFTIME''@/$(GNULIB_STRFTIME)/g' \
 	      -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_TIME_R''@/$(GNULIB_TIME_R)/g' \
 	      -e 's/@''GNULIB_TIME_RZ''@/$(GNULIB_TIME_RZ)/g' \
 	      -e 's/@''GNULIB_TZSET''@/$(GNULIB_TZSET)/g' \
@@ -44,6 +45,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
 	      -e 's|@''HAVE_NANOSLEEP''@|$(HAVE_NANOSLEEP)|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_TIMEZONE_T''@|$(HAVE_TIMEZONE_T)|g' \
 	      -e 's|@''REPLACE_CTIME''@|$(REPLACE_CTIME)|g' \
 	      -e 's|@''REPLACE_GMTIME''@|$(REPLACE_GMTIME)|g' \
diff --git a/modules/timespec_get b/modules/timespec_get
new file mode 100644
index 0000000..51efed3
--- /dev/null
+++ b/modules/timespec_get
@@ -0,0 +1,32 @@
+Description:
+timespec_get() function: return the current time with up to nanosecond resolution
+
+Files:
+lib/timespec_get.c
+m4/timespec_get.m4
+
+Depends-on:
+time
+extensions
+gettime         [test $HAVE_TIMESPEC_GET = 0]
+
+configure.ac:
+gl_FUNC_TIMESPEC_GET
+if test $HAVE_TIMESPEC_GET = 0; then
+  AC_LIBOBJ([timespec_get])
+fi
+gl_TIME_MODULE_INDICATOR([timespec_get])
+
+Makefile.am:
+
+Include:
+<time.h>
+
+Link:
+$(LIB_CLOCK_GETTIME)
+
+License:
+LGPL
+
+Maintainer:
+all
diff --git a/tests/test-time-c++.cc b/tests/test-time-c++.cc
index 07fff99..cdcd61e 100644
--- a/tests/test-time-c++.cc
+++ b/tests/test-time-c++.cc
@@ -24,6 +24,10 @@
 #include "signature.h"
 
 
+#if GNULIB_TEST_TIMESPEC_GET
+SIGNATURE_CHECK (GNULIB_NAMESPACE::timespec_get, int, (struct timespec *, int));
+#endif
+
 #if GNULIB_TEST_NANOSLEEP
 SIGNATURE_CHECK (GNULIB_NAMESPACE::nanosleep, int,
                  (struct timespec const *, struct timespec *));
-- 
2.7.4


[-- Attachment #4: 0003-timespec_get-Add-tests.patch --]
[-- Type: text/x-patch, Size: 3156 bytes --]

From 880a723a4976abf6ae22731bd75416b938157f30 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 2 Jan 2021 13:15:06 +0100
Subject: [PATCH 3/3] timespec_get: Add tests.

* tests/test-timespec_get.c: New file.
* modules/timespec_get-tests: New file.
---
 ChangeLog                  |  4 ++++
 modules/timespec_get-tests | 13 +++++++++++
 tests/test-timespec_get.c  | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
 create mode 100644 modules/timespec_get-tests
 create mode 100644 tests/test-timespec_get.c

diff --git a/ChangeLog b/ChangeLog
index 899f7a1..5d5c05a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2021-01-02  Bruno Haible  <bruno@clisp.org>
 
+	timespec_get: Add tests.
+	* tests/test-timespec_get.c: New file.
+	* modules/timespec_get-tests: New file.
+
 	timespec_get: New module.
 	* lib/time.in.h (timespec_get): New declaration.
 	* lib/timespec_get.c: New file.
diff --git a/modules/timespec_get-tests b/modules/timespec_get-tests
new file mode 100644
index 0000000..f13e199
--- /dev/null
+++ b/modules/timespec_get-tests
@@ -0,0 +1,13 @@
+Files:
+tests/test-timespec_get.c
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-timespec_get
+check_PROGRAMS += test-timespec_get
+test_timespec_get_LDADD = $(LDADD) @LIB_CLOCK_GETTIME@
diff --git a/tests/test-timespec_get.c b/tests/test-timespec_get.c
new file mode 100644
index 0000000..65555b3
--- /dev/null
+++ b/tests/test-timespec_get.c
@@ -0,0 +1,54 @@
+/* Test of timespec_get() function.
+   Copyright (C) 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 Bruno Haible.  */
+
+#include <config.h>
+
+#include <time.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (timespec_get, int, (struct timespec *ts, int base));
+
+#include "macros.h"
+
+int
+main (void)
+{
+  struct timespec ts1;
+  time_t tt2;
+  struct timespec ts3;
+  time_t tt4;
+
+  ASSERT (timespec_get (&ts1, TIME_UTC));
+  tt2 = time (NULL);
+  ASSERT (timespec_get (&ts3, TIME_UTC));
+  tt4 = time (NULL);
+
+  /* Verify monotonicity of timespec_get().  */
+  ASSERT (ts1.tv_sec < ts3.tv_sec
+          || (ts1.tv_sec == ts3.tv_sec && ts1.tv_nsec <= ts3.tv_nsec));
+
+  /* Verify monotonicity of time().  */
+  ASSERT (tt2 <= tt4);
+
+  /* Verify that the tv_sec field of the result is the same as time(NULL).  */
+  ASSERT (ts1.tv_sec <= tt2);
+  ASSERT (tt2 <= ts3.tv_sec);
+  ASSERT (ts3.tv_sec <= tt4);
+
+  return 0;
+}
-- 
2.7.4


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-01-02 12:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-02 12:18 new module 'timespec_get' Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).