Blame lib/localtime-buffer.c

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