Blame src/gl/tzset.c

Packit Service 4684c1
/* Provide tzset for systems that don't have it or for which it's broken.
Packit Service 4684c1
Packit Service 4684c1
   Copyright (C) 2001-2003, 2005-2007, 2009-2020 Free Software Foundation, Inc.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software; you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 3, or (at your option)
Packit Service 4684c1
   any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU General Public License
Packit Service 4684c1
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
/* written by Jim Meyering */
Packit Service 4684c1
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
Packit Service 4684c1
/* Specification.  */
Packit Service 4684c1
#include <time.h>
Packit Service 4684c1
Packit Service 4684c1
#include "localtime-buffer.h"
Packit Service 4684c1
Packit Service 4684c1
/* This is a wrapper for tzset, for systems on which tzset may clobber
Packit Service 4684c1
   the static buffer used for localtime's result.
Packit Service 4684c1
   Work around the bug in some systems whereby tzset clobbers the
Packit Service 4684c1
   static buffer that localtime uses for its return value.  The
Packit Service 4684c1
   tzset function from Solaris 2.5, 2.5.1, and 2.6 has this problem.  */
Packit Service 4684c1
Packit Service 4684c1
void
Packit Service 4684c1
tzset (void)
Packit Service 4684c1
#undef tzset
Packit Service 4684c1
{
Packit Service 4684c1
#if TZSET_CLOBBERS_LOCALTIME
Packit Service 4684c1
  /* Save and restore the contents of the buffer used for localtime's
Packit Service 4684c1
     result around the call to tzset.  */
Packit Service 4684c1
  struct tm save = *localtime_buffer_addr;
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#if defined _WIN32 && ! defined __CYGWIN__
Packit Service 4684c1
  /* Rectify the value of the environment variable TZ.
Packit Service 4684c1
     There are four possible kinds of such values:
Packit Service 4684c1
       - Traditional US time zone names, e.g. "PST8PDT".  Syntax: see
Packit Service 4684c1
         <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>
Packit Service 4684c1
       - Time zone names based on geography, that contain one or more
Packit Service 4684c1
         slashes, e.g. "Europe/Moscow".
Packit Service 4684c1
       - Time zone names based on geography, without slashes, e.g.
Packit Service 4684c1
         "Singapore".
Packit Service 4684c1
       - Time zone names that contain explicit DST rules.  Syntax: see
Packit Service 4684c1
         <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>
Packit Service 4684c1
     The Microsoft CRT understands only the first kind.  It produces incorrect
Packit Service 4684c1
     results if the value of TZ is of the other kinds.
Packit Service 4684c1
     But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value
Packit Service 4684c1
     of the second kind for most geographies, or of the first kind in a few
Packit Service 4684c1
     other geographies.  If it is of the second kind, neutralize it.  For the
Packit Service 4684c1
     Microsoft CRT, an absent or empty TZ means the time zone that the user
Packit Service 4684c1
     has set in the Windows Control Panel.
Packit Service 4684c1
     If the value of TZ is of the third or fourth kind -- Cygwin programs
Packit Service 4684c1
     understand these syntaxes as well --, it does not matter whether we
Packit Service 4684c1
     neutralize it or not, since these values occur only when a Cygwin user
Packit Service 4684c1
     has set TZ explicitly; this case is 1. rare and 2. under the user's
Packit Service 4684c1
     responsibility.  */
Packit Service 4684c1
  const char *tz = getenv ("TZ");
Packit Service 4684c1
  if (tz != NULL && strchr (tz, '/') != NULL)
Packit Service 4684c1
    _putenv ("TZ=");
Packit Service 4684c1
Packit Service 4684c1
  /* On native Windows, tzset() is deprecated.  Use _tzset() instead.  See
Packit Service 4684c1
     <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-tzset>
Packit Service 4684c1
     <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>  */
Packit Service 4684c1
  _tzset ();
Packit Service 4684c1
#elif HAVE_TZSET
Packit Service 4684c1
  tzset ();
Packit Service 4684c1
#else
Packit Service 4684c1
  /* Do nothing.  Avoid infinite recursion.  */
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#if TZSET_CLOBBERS_LOCALTIME
Packit Service 4684c1
  *localtime_buffer_addr = save;
Packit Service 4684c1
#endif
Packit Service 4684c1
}