Blame gnulib/lib/localtime-buffer.c

Packit 06dd63
/* Provide access to the last buffer returned by localtime() or gmtime().
Packit 06dd63
Packit 06dd63
   Copyright (C) 2001-2003, 2005-2007, 2009-2019 Free Software Foundation, Inc.
Packit 06dd63
Packit 06dd63
   This program is free software; you can redistribute it and/or modify
Packit 06dd63
   it under the terms of the GNU Lesser General Public License as published by
Packit 06dd63
   the Free Software Foundation; either version 2.1, or (at your option)
Packit 06dd63
   any later version.
Packit 06dd63
Packit 06dd63
   This program is distributed in the hope that it will be useful,
Packit 06dd63
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 06dd63
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 06dd63
   GNU Lesser General Public License for more details.
Packit 06dd63
Packit 06dd63
   You should have received a copy of the GNU Lesser General Public License
Packit 06dd63
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 06dd63
Packit 06dd63
/* written by Jim Meyering */
Packit 06dd63
Packit 06dd63
#include <config.h>
Packit 06dd63
Packit 06dd63
/* Specification.  */
Packit 06dd63
#include "localtime-buffer.h"
Packit 06dd63
Packit 06dd63
#if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
Packit 06dd63
Packit 06dd63
static struct tm tm_zero_buffer;
Packit 06dd63
struct tm *localtime_buffer_addr = &tm_zero_buffer;
Packit 06dd63
Packit 06dd63
/* This is a wrapper for localtime.
Packit 06dd63
Packit 06dd63
   On the first call, record the address of the static buffer that
Packit 06dd63
   localtime uses for its result.  */
Packit 06dd63
Packit 06dd63
struct tm *
Packit 06dd63
rpl_localtime (time_t const *timep)
Packit 06dd63
#undef localtime
Packit 06dd63
{
Packit 06dd63
  struct tm *tm = localtime (timep);
Packit 06dd63
Packit 06dd63
  if (localtime_buffer_addr == &tm_zero_buffer)
Packit 06dd63
    localtime_buffer_addr = tm;
Packit 06dd63
Packit 06dd63
  return tm;
Packit 06dd63
}
Packit 06dd63
Packit 06dd63
/* Same as above, since gmtime and localtime use the same buffer.  */
Packit 06dd63
struct tm *
Packit 06dd63
rpl_gmtime (time_t const *timep)
Packit 06dd63
#undef gmtime
Packit 06dd63
{
Packit 06dd63
  struct tm *tm = gmtime (timep);
Packit 06dd63
Packit 06dd63
  if (localtime_buffer_addr == &tm_zero_buffer)
Packit 06dd63
    localtime_buffer_addr = tm;
Packit 06dd63
Packit 06dd63
  return tm;
Packit 06dd63
}
Packit 06dd63
Packit 06dd63
#endif