> * Some which should obey TZ, just that they should ignore the values set by > Cygwin (instead of exhibiting garbage behaviour): > > localtime, _localtime* > https://msdn.microsoft.com/en-us/library/bf12f0hc.aspx > https://msdn.microsoft.com/en-us/library/a442x3ye.aspx > http://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html
This patch does it. 2017-04-30 Bruno Haible <br...@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. diff --git a/doc/posix-functions/localtime.texi b/doc/posix-functions/localtime.texi index 1d6acdb..74d4a6a 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 0000000..07b532a --- /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 47087d8..2587cdd 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 0000000..deb54a3 --- /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 fdd819b..2eaf3ae 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 0000000..5da46a9 --- /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 eff6132..4bbfbd4 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' \