*** empty log message ***
[gnulib.git] / lib / gettimeofday.c
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    Copyright (C) 2001 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* written by Jim Meyering */
21
22 #include <config.h>
23
24 /* Disable the definition of gettimeofday (from config.h) so we can use
25    the library version.  */
26 #undef gettimeofday
27
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <stdlib.h>
42 #include "gtod.h"
43
44 static struct tm *localtime_buffer_addr;
45
46 void
47 GTOD_init (void)
48 {
49   time_t t = 0;
50   localtime_buffer_addr = localtime (&t);
51 }
52
53 /* This is a wrapper for gettimeofday.  It is used only on systems for which
54    gettimeofday clobbers the static buffer used for localtime's result.
55
56    Save and restore the contents of the buffer used for localtime's result
57    around the call to gettimeofday.  */
58
59 int
60 rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
61 {
62   struct tm save;
63   if (! localtime_buffer_addr)
64     abort ();
65
66   save = *localtime_buffer_addr;
67   result = gettimeofday (tv, tz);
68   *localtime_buffer_addr = save;
69
70   return result;
71 }