* lib/time.in.h (time): New declaration.
* lib/time.c: New file.
* m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Initialize GNULIB_TIME.
(gl_TIME_H_DEFAULTS): Initialize REPLACE_TIME.
* m4/time.m4: New file.
* modules/time-h (Makefile.am): Substitute GNULIB_TIME, REPLACE_TIME.
* modules/time: New file.
* tests/test-time-h-c++.cc: Check the signature of
GNULIB_NAMESPACE::time.
* doc/posix-functions/time.texi: Mention the glibc problem and the
'time' module.
+2023-03-08 Bruno Haible <bruno@clisp.org>
+
+ time: New module.
+ * lib/time.in.h (time): New declaration.
+ * lib/time.c: New file.
+ * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Initialize GNULIB_TIME.
+ (gl_TIME_H_DEFAULTS): Initialize REPLACE_TIME.
+ * m4/time.m4: New file.
+ * modules/time-h (Makefile.am): Substitute GNULIB_TIME, REPLACE_TIME.
+ * modules/time: New file.
+ * tests/test-time-h-c++.cc: Check the signature of
+ GNULIB_NAMESPACE::time.
+ * doc/posix-functions/time.texi: Mention the glibc problem and the
+ 'time' module.
+
2023-03-08 Bruno Haible <bruno@clisp.org>
time-h: Renamed from time.
POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html}
-Gnulib module: ---
+Gnulib module: time
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is not consistent with @code{gettimeofday} and @code{timespec_get}
+on some platforms:
+@c https://sourceware.org/bugzilla/show_bug.cgi?id=30200
+glibc 2.31 or newer on Linux.
+Namely, in the first 1 to 2.5 milliseconds of every second, @code{time}
+returns a value that is one less than the @code{tv_sec} part of the return
+value of @code{gettimeofday} or @code{timespec_get}.
@end itemize
Portability problems not fixed by Gnulib:
--- /dev/null
+/* Provide time() for systems for which it's broken.
+ Copyright (C) 2023 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 2.1 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/>. */
+
+/* Written by Bruno Haible. */
+
+#include <config.h>
+
+/* Specification. */
+#include <time.h>
+
+#include <stdlib.h>
+#include <sys/time.h>
+
+time_t
+time (time_t *tp)
+{
+ struct timeval tv;
+ time_t tt;
+
+ if (gettimeofday (&tv, NULL) < 0)
+ abort ();
+ tt = tv.tv_sec;
+
+ if (tp)
+ *tp = tt;
+
+ return tt;
+}
_GL_CXXALIASWARN (timespec_getres);
# endif
+/* Return the number of seconds that have elapsed since the Epoch. */
+# if @GNULIB_TIME@
+# if @REPLACE_TIME@
+# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+# define time rpl_time
+# endif
+_GL_FUNCDECL_RPL (time, time_t, (time_t *__tp));
+_GL_CXXALIAS_RPL (time, time_t, (time_t *__tp));
+# else
+_GL_CXXALIAS_SYS (time, time_t, (time_t *__tp));
+# endif
+_GL_CXXALIASWARN (time);
+# 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
+# time.m4 serial 1
+dnl Copyright (C) 2023 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.
+
+dnl From Bruno Haible.
+
+AC_DEFUN([gl_FUNC_TIME],
+[
+ AC_REQUIRE([AC_CANONICAL_HOST])
+ dnl glibc has the bug https://sourceware.org/bugzilla/show_bug.cgi?id=30200 .
+ AC_CACHE_CHECK([whether time() works],
+ [gl_cv_func_time_works],
+ [dnl Guess that it works except on glibc >= 2.31 with Linux.
+ dnl And binaries produced on glibc < 2.31 need to run fine on newer
+ dnl glibc versions as well; therefore ignore __GLIBC_MINOR__.
+ case "$host_os" in
+ linux*-gnu*)
+ AC_EGREP_CPP([Unlucky], [
+ #include <features.h>
+ #ifdef __GNU_LIBRARY__
+ #if __GLIBC__ == 2
+ Unlucky GNU user
+ #endif
+ #endif
+ ],
+ [gl_cv_func_time_works="guessing no"],
+ [gl_cv_func_time_works="guessing yes"])
+ ;;
+ *) gl_cv_func_time_works="guessing yes";;
+ esac
+ ])
+ case "$gl_cv_func_time_works" in
+ *no) REPLACE_TIME=1 ;;
+ esac
+])
+
+# Prerequisites of lib/time.c.
+AC_DEFUN([gl_PREREQ_TIME],
+[
+ :
+])
# Copyright (C) 2000-2001, 2003-2007, 2009-2023 Free Software Foundation, Inc.
-# serial 21
+# serial 22
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_NANOSLEEP])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRFTIME])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRPTIME])
+ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIME])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMEGM])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMESPEC_GET])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMESPEC_GETRES])
REPLACE_MKTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_MKTIME])
REPLACE_NANOSLEEP=GNULIB_PORTCHECK; AC_SUBST([REPLACE_NANOSLEEP])
REPLACE_STRFTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_STRFTIME])
+ REPLACE_TIME=0; AC_SUBST([REPLACE_TIME])
REPLACE_TIMEGM=GNULIB_PORTCHECK; AC_SUBST([REPLACE_TIMEGM])
REPLACE_TIMESPEC_GET=GNULIB_PORTCHECK; AC_SUBST([REPLACE_TIMESPEC_GET])
REPLACE_TZSET=GNULIB_PORTCHECK; AC_SUBST([REPLACE_TZSET])
--- /dev/null
+Description:
+time() function: return current time.
+
+Files:
+lib/time.c
+m4/time.m4
+
+Depends-on:
+time-h
+
+configure.ac:
+gl_FUNC_TIME
+gl_CONDITIONAL([GL_COND_OBJ_TIME], [test $REPLACE_TIME = 1])
+AM_COND_IF([GL_COND_OBJ_TIME], [
+ gl_PREREQ_TIME
+])
+gl_TIME_MODULE_INDICATOR([time])
+
+Makefile.am:
+if GL_COND_OBJ_TIME
+lib_SOURCES += time.c
+endif
+
+Include:
+<time.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
-e 's/@''GNULIB_NANOSLEEP''@/$(GNULIB_NANOSLEEP)/g' \
-e 's/@''GNULIB_STRFTIME''@/$(GNULIB_STRFTIME)/g' \
-e 's/@''GNULIB_STRPTIME''@/$(GNULIB_STRPTIME)/g' \
+ -e 's/@''GNULIB_TIME''@/$(GNULIB_TIME)/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|@''REPLACE_MKTIME''@|$(REPLACE_MKTIME)|g' \
-e 's|@''REPLACE_NANOSLEEP''@|$(REPLACE_NANOSLEEP)|g' \
-e 's|@''REPLACE_STRFTIME''@|$(REPLACE_STRFTIME)|g' \
+ -e 's|@''REPLACE_TIME''@|$(REPLACE_TIME)|g' \
-e 's|@''REPLACE_TIMEGM''@|$(REPLACE_TIMEGM)|g' \
-e 's|@''REPLACE_TIMESPEC_GET''@|$(REPLACE_TIMESPEC_GET)|g' \
-e 's|@''REPLACE_TZSET''@|$(REPLACE_TZSET)|g' \
SIGNATURE_CHECK (GNULIB_NAMESPACE::timespec_get, int, (struct timespec *, int));
#endif
+#if GNULIB_TEST_TIME
+SIGNATURE_CHECK (GNULIB_NAMESPACE::time, time_t, (time_t *));
+#endif
+
#if GNULIB_TEST_NANOSLEEP
SIGNATURE_CHECK (GNULIB_NAMESPACE::nanosleep, int,
(struct timespec const *, struct timespec *));