From 16778a29111e48e1f8f4afa07f77cfc3651c6040 Mon Sep 17 00:00:00 2001 From: Bruno Haible <bruno@clisp.org> Date: Sun, 30 Apr 2017 13:32:40 +0200 Subject: [PATCH] localtime: New module. * lib/time.in.h (localtime): Declare also if requested by module 'localtime'. * lib/localtime.c: New file. * m4/localtime.m4: New file. * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize GNULIB_LOCALTIME. * modules/time (Makefile.am): Substitute GNULIB_LOCALTIME. * modules/localtime: New file. * doc/posix-functions/localtime.texi: Mention the new module. --- ChangeLog | 12 ++++++++ doc/posix-functions/localtime.texi | 8 +++--- lib/localtime.c | 45 ++++++++++++++++++++++++++++++ lib/time.in.h | 2 +- m4/localtime.m4 | 14 ++++++++++ m4/time_h.m4 | 1 + modules/localtime | 27 ++++++++++++++++++ modules/time | 1 + 8 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 lib/localtime.c create mode 100644 m4/localtime.m4 create mode 100644 modules/localtime diff --git a/ChangeLog b/ChangeLog index 17c3bf6e84..af7f731c4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2017-04-30 Bruno Haible <bruno@clisp.org> + + localtime: New module. + * lib/time.in.h (localtime): Declare also if requested by module + 'localtime'. + * lib/localtime.c: New file. + * m4/localtime.m4: New file. + * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize GNULIB_LOCALTIME. + * modules/time (Makefile.am): Substitute GNULIB_LOCALTIME. + * modules/localtime: New file. + * doc/posix-functions/localtime.texi: Mention the new module. + 2017-04-30 Bruno Haible <bruno@clisp.org> ctime: New module. diff --git a/doc/posix-functions/localtime.texi b/doc/posix-functions/localtime.texi index 1d6acdba8f..74d4a6a48e 100644 --- a/doc/posix-functions/localtime.texi +++ b/doc/posix-functions/localtime.texi @@ -4,18 +4,18 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/localtime.html} -Gnulib module: --- +Gnulib module: localtime Portability problems fixed by Gnulib: @itemize +@item +On native Windows platforms (mingw, MSVC), this function works incorrectly +when the environment variable @code{TZ} has been set by Cygwin. @end itemize Portability problems not fixed by Gnulib: @itemize @item -On native Windows platforms (mingw, MSVC), this function works incorrectly -when the environment variable @code{TZ} has been set by Cygwin. -@item On some platforms, this function returns nonsense values for unsupported arguments (like @math{2^56}), rather than failing: FreeBSD 10. diff --git a/lib/localtime.c b/lib/localtime.c new file mode 100644 index 0000000000..07b532aec9 --- /dev/null +++ b/lib/localtime.c @@ -0,0 +1,45 @@ +/* Work around platform bugs in localtime. + Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <time.h> + +/* Keep consistent with gettimeofday.c! */ +#if !(GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME) + +# include <stdlib.h> +# include <string.h> + +# undef localtime + +struct tm * +rpl_localtime (const time_t *tp) +{ +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + /* If the environment variable TZ has been set by Cygwin, neutralize it. + The Microsoft CRT interprets TZ differently than Cygwin and produces + incorrect results if TZ has the syntax used by Cygwin. */ + const char *tz = getenv ("TZ"); + if (tz != NULL && strchr (tz, '/') != NULL) + _putenv ("TZ="); +# endif + + return localtime (tp); +} + +#endif diff --git a/lib/time.in.h b/lib/time.in.h index 47087d8da6..2587cdd1ea 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -187,7 +187,7 @@ _GL_CXXALIASWARN (gmtime_r); /* Convert TIMER to RESULT, assuming local time and UTC respectively. See <http://www.opengroup.org/susv3xsh/localtime.html> and <http://www.opengroup.org/susv3xsh/gmtime.html>. */ -# if @GNULIB_GETTIMEOFDAY@ +# if @GNULIB_LOCALTIME@ || @GNULIB_GETTIMEOFDAY@ # if @REPLACE_LOCALTIME@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) # undef localtime diff --git a/m4/localtime.m4 b/m4/localtime.m4 new file mode 100644 index 0000000000..deb54a37b2 --- /dev/null +++ b/m4/localtime.m4 @@ -0,0 +1,14 @@ +# localtime.m4 serial 1 +dnl Copyright (C) 2017 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_LOCALTIME], +[ + AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + case "$host_os" in + mingw*) REPLACE_LOCALTIME=1 ;; + esac +]) diff --git a/m4/time_h.m4 b/m4/time_h.m4 index fdd819bc59..2eaf3aeb3a 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -106,6 +106,7 @@ AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS], [ GNULIB_CTIME=0; AC_SUBST([GNULIB_CTIME]) GNULIB_MKTIME=0; AC_SUBST([GNULIB_MKTIME]) + GNULIB_LOCALTIME=0; AC_SUBST([GNULIB_LOCALTIME]) GNULIB_NANOSLEEP=0; AC_SUBST([GNULIB_NANOSLEEP]) GNULIB_STRPTIME=0; AC_SUBST([GNULIB_STRPTIME]) GNULIB_TIMEGM=0; AC_SUBST([GNULIB_TIMEGM]) diff --git a/modules/localtime b/modules/localtime new file mode 100644 index 0000000000..5da46a9550 --- /dev/null +++ b/modules/localtime @@ -0,0 +1,27 @@ +Description: +localtime() function: convert time to broken-down local time. + +Files: +lib/localtime.c +m4/localtime.m4 + +Depends-on: +time + +configure.ac: +gl_FUNC_LOCALTIME +if test $REPLACE_LOCALTIME = 1; then + AC_LIBOBJ([localtime]) +fi +gl_TIME_MODULE_INDICATOR([localtime]) + +Makefile.am: + +Include: +<time.h> + +License: +LGPLv2+ + +Maintainer: +all diff --git a/modules/time b/modules/time index eff6132fd2..4bbfbd4578 100644 --- a/modules/time +++ b/modules/time @@ -32,6 +32,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''NEXT_TIME_H''@|$(NEXT_TIME_H)|g' \ -e 's/@''GNULIB_CTIME''@/$(GNULIB_CTIME)/g' \ -e 's/@''GNULIB_GETTIMEOFDAY''@/$(GNULIB_GETTIMEOFDAY)/g' \ + -e 's/@''GNULIB_LOCALTIME''@/$(GNULIB_LOCALTIME)/g' \ -e 's/@''GNULIB_MKTIME''@/$(GNULIB_MKTIME)/g' \ -e 's/@''GNULIB_NANOSLEEP''@/$(GNULIB_NANOSLEEP)/g' \ -e 's/@''GNULIB_STRPTIME''@/$(GNULIB_STRPTIME)/g' \ -- 2.39.5