From 4bc76593d509827588081e32ebd75a5e34d1c374 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 23 Jul 2015 17:44:19 -0700 Subject: [PATCH] time_rz: new module * MODULES.html.sh: Add time_rz. * lib/time_rz.c, m4/time_rz.m4, modules/time_rz: New files. * lib/time.in.h (timezone_t, tzalloc, tzfree, localtime_rz, mktime_z): New decls if _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@. * m4/sys_time_h.m4 (gl_HEADER_SYS_TIME_H_DEFAULTS): New var HAVE_TIMEZONE_T (default 0). * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): New var GNULIB_TIME_RZ (default 0). * modules/time (time.h): Substitute the new vars. --- ChangeLog | 11 ++ MODULES.html.sh | 1 + lib/time.in.h | 19 +++ lib/time_rz.c | 336 +++++++++++++++++++++++++++++++++++++++++++++++ m4/sys_time_h.m4 | 1 + m4/time_h.m4 | 1 + m4/time_rz.m4 | 21 +++ modules/time | 2 + modules/time_rz | 44 +++++++ 9 files changed, 436 insertions(+) create mode 100644 lib/time_rz.c create mode 100644 m4/time_rz.m4 create mode 100644 modules/time_rz diff --git a/ChangeLog b/ChangeLog index 775f5f203e..49993624c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2015-07-23 Paul Eggert + time_rz: new module + * MODULES.html.sh: Add time_rz. + * lib/time_rz.c, m4/time_rz.m4, modules/time_rz: New files. + * lib/time.in.h (timezone_t, tzalloc, tzfree, localtime_rz, mktime_z): + New decls if _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@. + * m4/sys_time_h.m4 (gl_HEADER_SYS_TIME_H_DEFAULTS): + New var HAVE_TIMEZONE_T (default 0). + * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): + New var GNULIB_TIME_RZ (default 0). + * modules/time (time.h): Substitute the new vars. + flexmember: license is now unlimited * modules/flexmember (License): Change to unlimited, since its only source file gives an unlimited license. diff --git a/MODULES.html.sh b/MODULES.html.sh index 7835e9db74..5b2c459fb2 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1725,6 +1725,7 @@ func_all_modules () func_begin_table func_module fprintftime func_module strftime + func_module time_rz func_end_table element="Extra functions based on ANSI C 89" diff --git a/lib/time.in.h b/lib/time.in.h index c374aebb96..a5cb55d6b0 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -231,6 +231,25 @@ _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf, _GL_CXXALIASWARN (strptime); # endif +# if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@ +typedef struct tm_zone *timezone_t; +_GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name)); +_GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name)); +_GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz)); +_GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz)); +_GL_FUNCDECL_SYS (localtime_rz, struct tm *, + (timezone_t __tz, time_t const *restrict __timer, + struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3))); +_GL_CXXALIAS_SYS (localtime_rz, struct tm *, + (timezone_t __tz, time_t const *restrict __timer, + struct tm *restrict __result)); +_GL_FUNCDECL_SYS (mktime_z, time_t, + (timezone_t __tz, struct tm *restrict __result) + _GL_ARG_NONNULL ((2))); +_GL_CXXALIAS_SYS (mktime_z, time_t, + (timezone_t __tz, struct tm *restrict __result)); +# endif + /* Convert TM to a time_t value, assuming UTC. */ # if @GNULIB_TIMEGM@ # if @REPLACE_TIMEGM@ diff --git a/lib/time_rz.c b/lib/time_rz.c new file mode 100644 index 0000000000..db1a382af6 --- /dev/null +++ b/lib/time_rz.c @@ -0,0 +1,336 @@ +/* Time zone functions such as tzalloc and localtime_rz + + Copyright 2015 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 2, 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 . */ + +/* Written by Paul Eggert. */ + +/* Although this module is not thread-safe, any races should be fairly + rare and reasonably benign. For complete thread-safety, use a C + library with a working timezone_t type, so that this module is not + needed. */ + +#include + +#include + +#include +#include +#include +#include +#include + +#if !HAVE_TZSET +static void tzset (void) { } +#endif + +/* A time zone rule. */ +struct tm_zone +{ + /* More abbreviations, should they be needed. Their TZ_IS_SET + members are zero. */ + timezone_t next; + + /* If nonzero, the rule represents the TZ environment variable set + to the first "abbreviation" (this may be the empty string). + Otherwise, it represents an unset TZ. */ + char tz_is_set; + + /* A sequence of null-terminated strings packed next to each other. + The strings are followed by an extra null byte. If TZ_IS_SET, + there must be at least one string and the first string (which is + actually a TZ environment value value) may be empty. Otherwise + all strings must be nonempty. + + Abbreviations are stored here because otherwise the values of + tm_zone and/or tzname would be dead after changing TZ and calling + tzset. Abbreviations never move once allocated, and are live + until tzfree is called. */ + char abbrs[FLEXIBLE_ARRAY_MEMBER]; +}; + +/* The approximate size to use for small allocation requests. This is + the largest "small" request for the GNU C library malloc. */ +enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; + +/* Minimum size of the ABBRS member of struct abbr. ABBRS is larger + only in the unlikely case where an abbreviation longer than this is + used. */ +enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) }; + +static char const TZ[] = "TZ"; + +/* Magic cookie timezone_t value, for local time. It differs from + NULL and from all other timezone_t values. Only the address + matters; the pointer is never dereferenced. */ +static timezone_t local_tz = (timezone_t) 1; + +#if HAVE_TM_ZONE || HAVE_TZNAME + +/* Return true if the values A and B differ according to the rules for + tm_isdst: A and B differ if one is zero and the other positive. */ +static bool +isdst_differ (int a, int b) +{ + return !a != !b && 0 <= a && 0 <= b; +} + +/* Return true if A and B are equal. */ +static int +equal_tm (const struct tm *a, const struct tm *b) +{ + return ! ((a->tm_sec ^ b->tm_sec) + | (a->tm_min ^ b->tm_min) + | (a->tm_hour ^ b->tm_hour) + | (a->tm_mday ^ b->tm_mday) + | (a->tm_mon ^ b->tm_mon) + | (a->tm_year ^ b->tm_year) + | isdst_differ (a->tm_isdst, b->tm_isdst)); +} + +#endif + +/* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this + includes its trailing null byte). Append an extra null byte to + mark the end of ABBRS. */ +static void +extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size) +{ + memcpy (abbrs, abbr, abbr_size); + abbrs[abbr_size + 1] = '\0'; +} + +/* Return a newly allocated time zone for NAME, or NULL on failure. + As a special case, return a nonzero constant for wall clock time, a + constant that survives freeing. */ +timezone_t +tzalloc (char const *name) +{ + size_t name_size = name ? strlen (name) + 1 : 0; + size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1; + timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size); + if (tz) + { + tz->next = NULL; + tz->tz_is_set = !!name; + extend_abbrs (tz->abbrs, name, name_size); + } + return tz; +} + +/* Save into TZ any nontrivial time zone abbreviation used by TM, + and update *TM or tzname if they point to the abbreviation. + Return true if successful, false (setting errno) otherwise. */ +static bool +save_abbr (timezone_t tz, struct tm *tm) +{ +#if HAVE_TM_ZONE || HAVE_TZNAME + char const *zone = NULL; + char **tzname_zone = NULL; + char *zone_copy = (char *) ""; +# if HAVE_TM_ZONE + zone = tm->tm_zone; +# endif +# if HAVE_TZNAME + if (! (zone && *zone) && 0 <= tm->tm_isdst) + zone = *(tzname_zone = &tzname[0 < tm->tm_isdst]); +# endif + + /* No need to replace null zones, or zones within the struct tm. */ + if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1))) + return true; + + if (*zone) + { + zone_copy = tz->abbrs; + + while (strcmp (zone_copy, zone) != 0) + { + if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set))) + { + size_t zone_size = strlen (zone) + 1; + if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy) + extend_abbrs (zone_copy, zone, zone_size); + else + { + tz = tz->next = tzalloc (zone); + if (!tz) + return false; + tz->tz_is_set = 0; + zone_copy = tz->abbrs; + } + break; + } + + zone_copy += strlen (zone_copy) + 1; + if (!*zone_copy && tz->next) + { + tz = tz->next; + zone_copy = tz->abbrs; + } + } + } + + /* Replace the zone name so that its lifetime matches that of TZ. */ +# if HAVE_TM_ZONE + if (!tzname_zone) + tm->tm_zone = zone_copy; +# endif +# if HAVE_TZNAME + if (tzname_zone) + *tzname_zone = zone_copy; +# endif +#endif + return true; +} + +/* Free a time zone. */ +void +tzfree (timezone_t tz) +{ + if (tz != local_tz) + while (tz) + { + timezone_t next = tz->next; + free (tz); + tz = next; + } +} + +/* Get and set the TZ environment variable. These functions can be + overridden by programs like Emacs that manage their own environment. */ + +#ifndef getenv_TZ +static char * +getenv_TZ (void) +{ + return getenv (TZ); +} +#endif + +#ifndef setenv_TZ +static int +setenv_TZ (char const *tz) +{ + return tz ? setenv (TZ, tz, 1) : unsetenv (TZ); +} +#endif + +/* Change the environment to match the specified timezone_t value. + Return true if successful, false (setting errno) otherwise. */ +static bool +change_env (timezone_t tz) +{ + if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0) + return false; + tzset (); + return true; +} + +/* Temporarily set the time zone to TZ, which must not be null. + Return LOCAL_TZ if the time zone setting is already correct. + Otherwise return a newly allocated time zone representing the old + setting, or NULL (setting errno) on failure. */ +static timezone_t +set_tz (timezone_t tz) +{ + char *env_tz = getenv_TZ (); + if (env_tz + ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0 + : !tz->tz_is_set) + return local_tz; + else + { + timezone_t old_tz = tzalloc (env_tz); + if (!old_tz) + return old_tz; + if (! change_env (tz)) + { + int saved_errno = errno; + tzfree (old_tz); + errno = saved_errno; + return NULL; + } + return old_tz; + } +} + +/* Restore an old setting returned by set_tz. It must not be null. + Return true (preserving errno) if successful, false (setting errno) + otherwise. */ +static bool +revert_tz (timezone_t tz) +{ + if (tz == local_tz) + return true; + else + { + int saved_errno = errno; + bool ok = change_env (tz); + if (!ok) + saved_errno = errno; + tzfree (tz); + errno = saved_errno; + return ok; + } +} + +/* Use time zone TZ to compute localtime_r (T, TM). */ +struct tm * +localtime_rz (timezone_t tz, time_t const *t, struct tm *tm) +{ + if (!tz) + return gmtime_r (t, tm); + else + { + timezone_t old_tz = set_tz (tz); + if (old_tz) + { + tm = localtime_r (t, tm); + if (tm && !save_abbr (tz, tm)) + tm = NULL; + if (revert_tz (old_tz)) + return tm; + } + return NULL; + } +} + +/* Use time zone TZ to compute mktime (TM). */ +time_t +mktime_z (timezone_t tz, struct tm *tm) +{ + if (!tz) + return timegm (tm); + else + { + timezone_t old_tz = set_tz (tz); + if (old_tz) + { + time_t t = mktime (tm); +#if HAVE_TM_ZONE || HAVE_TZNAME + time_t badtime = -1; + struct tm tm_1; + if ((t != badtime + || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1))) + && !save_abbr (tz, tm)) + t = badtime; +#endif + if (revert_tz (old_tz)) + return t; + } + return -1; + } +} diff --git a/m4/sys_time_h.m4 b/m4/sys_time_h.m4 index 50133b9ff9..28c8b1acbb 100644 --- a/m4/sys_time_h.m4 +++ b/m4/sys_time_h.m4 @@ -105,6 +105,7 @@ AC_DEFUN([gl_HEADER_SYS_TIME_H_DEFAULTS], HAVE_GETTIMEOFDAY=1; AC_SUBST([HAVE_GETTIMEOFDAY]) HAVE_STRUCT_TIMEVAL=1; AC_SUBST([HAVE_STRUCT_TIMEVAL]) HAVE_SYS_TIME_H=1; AC_SUBST([HAVE_SYS_TIME_H]) + HAVE_TIMEZONE_T=0; AC_SUBST([HAVE_TIMEZONE_T]) REPLACE_GETTIMEOFDAY=0; AC_SUBST([REPLACE_GETTIMEOFDAY]) REPLACE_STRUCT_TIMEVAL=0; AC_SUBST([REPLACE_STRUCT_TIMEVAL]) ]) diff --git a/m4/time_h.m4 b/m4/time_h.m4 index d9c41a48f3..754b469a0d 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -109,6 +109,7 @@ AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS], GNULIB_STRPTIME=0; AC_SUBST([GNULIB_STRPTIME]) GNULIB_TIMEGM=0; AC_SUBST([GNULIB_TIMEGM]) GNULIB_TIME_R=0; AC_SUBST([GNULIB_TIME_R]) + GNULIB_TIME_RZ=0; AC_SUBST([GNULIB_TIME_RZ]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_LOCALTIME_R=1; AC_SUBST([HAVE_DECL_LOCALTIME_R]) HAVE_NANOSLEEP=1; AC_SUBST([HAVE_NANOSLEEP]) diff --git a/m4/time_rz.m4 b/m4/time_rz.m4 new file mode 100644 index 0000000000..0c1f2c3736 --- /dev/null +++ b/m4/time_rz.m4 @@ -0,0 +1,21 @@ +dnl Time zone functions: tzalloc, localtime_rz, etc. + +dnl Copyright (C) 2015 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 Written by Paul Eggert. + +AC_DEFUN([gl_TIME_RZ], +[ + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS]) + AC_REQUIRE([AC_STRUCT_TIMEZONE]) + AC_CHECK_FUNCS_ONCE([tzset]) + + AC_CHECK_TYPES([timezone_t], [], [], [[#include ]]) + if test "$ac_cv_type_timezone_t" = yes; then + HAVE_TIMEZONE_T=1 + fi +]) diff --git a/modules/time b/modules/time index 55edc2572f..d12add0911 100644 --- a/modules/time +++ b/modules/time @@ -36,10 +36,12 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_STRPTIME''@/$(GNULIB_STRPTIME)/g' \ -e 's/@''GNULIB_TIMEGM''@/$(GNULIB_TIMEGM)/g' \ -e 's/@''GNULIB_TIME_R''@/$(GNULIB_TIME_R)/g' \ + -e 's/@''GNULIB_TIME_RZ''@/$(GNULIB_TIME_RZ)/g' \ -e 's|@''HAVE_DECL_LOCALTIME_R''@|$(HAVE_DECL_LOCALTIME_R)|g' \ -e 's|@''HAVE_NANOSLEEP''@|$(HAVE_NANOSLEEP)|g' \ -e 's|@''HAVE_STRPTIME''@|$(HAVE_STRPTIME)|g' \ -e 's|@''HAVE_TIMEGM''@|$(HAVE_TIMEGM)|g' \ + -e 's|@''HAVE_TIMEZONE_T''@|$(HAVE_TIMEZONE_T)|g' \ -e 's|@''REPLACE_GMTIME''@|$(REPLACE_GMTIME)|g' \ -e 's|@''REPLACE_LOCALTIME''@|$(REPLACE_LOCALTIME)|g' \ -e 's|@''REPLACE_LOCALTIME_R''@|$(REPLACE_LOCALTIME_R)|g' \ diff --git a/modules/time_rz b/modules/time_rz new file mode 100644 index 0000000000..765e9e4ecc --- /dev/null +++ b/modules/time_rz @@ -0,0 +1,44 @@ +Description: +Reentrant time zone functions: localtime_rz, mktime_z, etc. + +Comment: +This implements the NetBSD-inspired extensions to , which +defines a type timezone_t and associated allocation functions tzalloc +and tzfree, along with two functions localtime_rz and mktime_z that +are like localtime_r and mktime except they have a new leading +timezone_t argument. Time zone abbreviation strings have lifetimes +equal to the corresponding struct tm or timezone_t object (whichever +is less). tzalloc (X) yields a time zone object equivalent to setting +the TZ environment variable to X. tzalloc (NULL) is the same as an +unset TZ environment variable. (timezone_t) 0 stands for UTC. + +Files: +lib/time_rz.c +m4/time_rz.m4 + +Depends-on: +extensions +flexmember [test "$HAVE_TIMEZONE_T" = 0] +setenv [test "$HAVE_TIMEZONE_T" = 0] +stdbool [test "$HAVE_TIMEZONE_T" = 0] +time_r [test "$HAVE_TIMEZONE_T" = 0] +timegm [test "$HAVE_TIMEZONE_T" = 0] +unsetenv [test "$HAVE_TIMEZONE_T" = 0] + +configure.ac: +gl_TIME_RZ +if test "$HAVE_TIMEZONE_T" = 0; then + AC_LIBOBJ([time_rz]) +fi +gl_TIME_MODULE_INDICATOR([time_rz]) + +Makefile.am: + +Include: + + +License: +LGPLv2+ + +Maintainer: +Paul Eggert -- 2.39.5