Add wrappers for gmtime and tzset, too.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 14 Aug 2003 21:59:28 +0000 (21:59 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 14 Aug 2003 21:59:28 +0000 (21:59 +0000)
lib/gettimeofday.c
m4/gettimeofday.m4
m4/tzset.m4 [new file with mode: 0644]
modules/tzset [new file with mode: 0644]

index 0d259be..1ebe4d0 100644 (file)
@@ -1,7 +1,8 @@
 /* Work around the bug in some systems whereby gettimeofday clobbers the
    static buffer that localtime uses for it's return value.  The gettimeofday
    function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
-   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
+   The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
+   Copyright (C) 2001, 2002, 2003 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
 
 #include <config.h>
 
-/* Disable the definitions of gettimeofday and localtime (from config.h)
+/* Disable the definitions of these functions (from config.h)
    so we can use the library versions here.  */
 #undef gettimeofday
+#undef gmtime
 #undef localtime
+#undef tzset
 
 #include <sys/types.h>
 
@@ -60,6 +63,18 @@ rpl_localtime (const time_t *timep)
   return tm;
 }
 
+/* Same as above, since gmtime and localtime use the same buffer.  */
+struct tm *
+rpl_gmtime (const time_t *timep)
+{
+  struct tm *tm = gmtime (timep);
+
+  if (! localtime_buffer_addr)
+    localtime_buffer_addr = tm;
+
+  return tm;
+}
+
 /* This is a wrapper for gettimeofday.  It is used only on systems for which
    gettimeofday clobbers the static buffer used for localtime's result.
 
@@ -84,3 +99,23 @@ rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
 
   return result;
 }
+
+/* This is a wrapper for tzset. It is used only on systems for which
+   tzset may clobber the static buffer used for localtime's result.
+   Save and restore the contents of the buffer used for localtime's
+   result around the call to tzset.  */
+void
+rpl_tzset (void)
+{
+  struct tm save;
+
+  if (! localtime_buffer_addr)
+    {
+      time_t t = 0;
+      localtime_buffer_addr = localtime (&t);
+    }
+
+  save = *localtime_buffer_addr;
+  tzset ();
+  *localtime_buffer_addr = save;
+}
index 836a4b9..e7a5a6d 100644 (file)
@@ -1,4 +1,4 @@
-#serial 3
+#serial 4
 
 dnl From Jim Meyering.
 dnl
@@ -58,9 +58,8 @@ main ()
         jm_cv_func_gettimeofday_clobber=yes)
   ])
   if test $jm_cv_func_gettimeofday_clobber = yes; then
-    AC_LIBOBJ(gettimeofday)
-    AC_DEFINE(localtime, rpl_localtime,
-      [Define to rpl_localtime if the replacement function should be used.])
+    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
+
     AC_DEFINE(gettimeofday, rpl_gettimeofday,
       [Define to rpl_gettimeofday if the replacement function should be used.])
     AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME_BUFFER, 1,
@@ -69,6 +68,14 @@ main ()
   fi
 ])
 
+AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
+  AC_LIBOBJ(gettimeofday)
+  AC_DEFINE(gmtime, rpl_gmtime,
+    [Define to rpl_gmtime if the replacement function should be used.])
+  AC_DEFINE(localtime, rpl_localtime,
+    [Define to rpl_localtime if the replacement function should be used.])
+])
+
 # Prerequisites of lib/gettimeofday.c.
 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
   AC_REQUIRE([AC_HEADER_TIME])
diff --git a/m4/tzset.m4 b/m4/tzset.m4
new file mode 100644 (file)
index 0000000..6df4eb7
--- /dev/null
@@ -0,0 +1,62 @@
+#serial 1
+# See if we have a working tzset function.
+# If so, arrange to compile the wrapper function.
+# For at least Solaris 2.5.1 and 2.6, this is necessary
+# because tzset can clobber the contents of the buffer
+# used by localtime.
+
+# Written by Paul Eggert and Jim Meyering.
+
+AC_DEFUN([gl_FUNC_TZSET_CLOBBER],
+[
+  AC_REQUIRE([AC_HEADER_TIME])
+  AC_CACHE_CHECK([whether tzset clobbers localtime buffer],
+                 gl_cv_func_tzset_clobber,
+  [
+  AC_RUN_IFELSE([AC_LANG_SOURCE([[
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+#include <stdlib.h>
+
+int
+main ()
+{
+  time_t t1 = 853958121;
+  struct tm *p, s;
+  putenv ("TZ=GMT0");
+  p = localtime (&t1);
+  s = *p;
+  putenv ("TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00");
+  tzset ();
+  exit (p->tm_year != s.tm_year
+        || p->tm_mon != s.tm_mon
+        || p->tm_mday != s.tm_mday
+        || p->tm_hour != s.tm_hour
+        || p->tm_min != s.tm_min
+        || p->tm_sec != s.tm_sec);
+}
+  ]])],
+       [gl_cv_func_tzset_clobber=no],
+       [gl_cv_func_tzset_clobber=yes],
+       [gl_cv_func_tzset_clobber=yes])])
+
+  AC_DEFINE(HAVE_RUN_TZSET_TEST, 1,
+    [Define to 1 if you have run the test for working tzset.])
+
+  if test $gl_cv_func_tzset_clobber = yes; then
+    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
+
+    AC_DEFINE(tzset, rpl_tzset,
+      [Define to rpl_tzset if the wrapper function should be used.])
+    AC_DEFINE(TZSET_CLOBBERS_LOCALTIME_BUFFER, 1,
+      [Define if tzset clobbers localtime's static buffer.])
+  fi
+])
diff --git a/modules/tzset b/modules/tzset
new file mode 100644 (file)
index 0000000..c0e9304
--- /dev/null
@@ -0,0 +1,20 @@
+Description:
+tzset - initialize time conversion information
+
+Files:
+m4/tzset.m4
+
+Depends-on:
+gettimeofday
+
+configure.ac:
+gl_FUNC_TZSET_CLOBBER
+
+Makefile.am:
+
+Include:
+<time.h>
+
+Maintainer:
+Jim Meyering
+