1 /* Work around the bug in some systems whereby gettimeofday clobbers the 2 static buffer that localtime uses for it's return value. The gettimeofday 3 function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem. 4 The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6. 5 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2, or (at your option) 10 any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software Foundation, 19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 /* written by Jim Meyering */ 22 23 #ifdef HAVE_CONFIG_H 24 # include <config.h> 25 #endif 26 27 /* Disable the definitions of these functions (from config.h) 28 so we can use the library versions here. */ 29 #undef gettimeofday 30 #undef gmtime 31 #undef localtime 32 #undef tzset 33 34 #include <sys/types.h> 35 36 #if TIME_WITH_SYS_TIME 37 # include <sys/time.h> 38 # include <time.h> 39 #else 40 # if HAVE_SYS_TIME_H 41 # include <sys/time.h> 42 # else 43 # include <time.h> 44 # endif 45 #endif 46 47 #include <stdlib.h> 48 49 static struct tm *localtime_buffer_addr; 50 51 /* This is a wrapper for localtime. It is used only on systems for which 52 gettimeofday clobbers the static buffer used for localtime's result. 53 54 On the first call, record the address of the static buffer that 55 localtime uses for its result. */ 56 57 struct tm * 58 rpl_localtime (const time_t *timep) 59 { 60 struct tm *tm = localtime (timep); 61 62 if (! localtime_buffer_addr) 63 localtime_buffer_addr = tm; 64 65 return tm; 66 } 67 68 /* Same as above, since gmtime and localtime use the same buffer. */ 69 struct tm * 70 rpl_gmtime (const time_t *timep) 71 { 72 struct tm *tm = gmtime (timep); 73 74 if (! localtime_buffer_addr) 75 localtime_buffer_addr = tm; 76 77 return tm; 78 } 79 80 /* This is a wrapper for gettimeofday. It is used only on systems for which 81 gettimeofday clobbers the static buffer used for localtime's result. 82 83 Save and restore the contents of the buffer used for localtime's result 84 around the call to gettimeofday. */ 85 86 int 87 rpl_gettimeofday (struct timeval *tv, struct timezone *tz) 88 { 89 struct tm save; 90 int result; 91 92 if (! localtime_buffer_addr) 93 { 94 time_t t = 0; 95 localtime_buffer_addr = localtime (&t); 96 } 97 98 save = *localtime_buffer_addr; 99 result = gettimeofday (tv, tz); 100 *localtime_buffer_addr = save; 101 102 return result; 103 } 104 105 /* This is a wrapper for tzset. It is used only on systems for which 106 tzset may clobber the static buffer used for localtime's result. 107 Save and restore the contents of the buffer used for localtime's 108 result around the call to tzset. */ 109 void 110 rpl_tzset (void) 111 { 112 struct tm save; 113 114 if (! localtime_buffer_addr) 115 { 116 time_t t = 0; 117 localtime_buffer_addr = localtime (&t); 118 } 119 120 save = *localtime_buffer_addr; 121 tzset (); 122 *localtime_buffer_addr = save; 123 } 124