Blame lib/tzset.c

Packit 8f70b4
/* Provide tzset for systems that don't have it or for which it's broken.
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 <time.h>
Packit 8f70b4
Packit 8f70b4
#include "localtime-buffer.h"
Packit 8f70b4
Packit 8f70b4
/* This is a wrapper for tzset, for systems on which tzset may clobber
Packit 8f70b4
   the static buffer used for localtime's result.
Packit 8f70b4
   Work around the bug in some systems whereby tzset clobbers the
Packit 8f70b4
   static buffer that localtime uses for its return value.  The
Packit 8f70b4
   tzset function from Solaris 2.5, 2.5.1, and 2.6 has this problem.  */
Packit 8f70b4
Packit 8f70b4
void
Packit 8f70b4
tzset (void)
Packit 8f70b4
#undef tzset
Packit 8f70b4
{
Packit 8f70b4
#if TZSET_CLOBBERS_LOCALTIME
Packit 8f70b4
  /* Save and restore the contents of the buffer used for localtime's
Packit 8f70b4
     result around the call to tzset.  */
Packit 8f70b4
  struct tm save = *localtime_buffer_addr;
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if defined _WIN32 && ! defined __CYGWIN__
Packit 8f70b4
  /* Rectify the value of the environment variable TZ.
Packit 8f70b4
     There are four possible kinds of such values:
Packit 8f70b4
       - Traditional US time zone names, e.g. "PST8PDT".  Syntax: see
Packit 8f70b4
         <https://msdn.microsoft.com/en-us/library/90s5c885.aspx>
Packit 8f70b4
       - Time zone names based on geography, that contain one or more
Packit 8f70b4
         slashes, e.g. "Europe/Moscow".
Packit 8f70b4
       - Time zone names based on geography, without slashes, e.g.
Packit 8f70b4
         "Singapore".
Packit 8f70b4
       - Time zone names that contain explicit DST rules.  Syntax: see
Packit 8f70b4
         <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>
Packit 8f70b4
     The Microsoft CRT understands only the first kind.  It produces incorrect
Packit 8f70b4
     results if the value of TZ is of the other kinds.
Packit 8f70b4
     But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value
Packit 8f70b4
     of the second kind for most geographies, or of the first kind in a few
Packit 8f70b4
     other geographies.  If it is of the second kind, neutralize it.  For the
Packit 8f70b4
     Microsoft CRT, an absent or empty TZ means the time zone that the user
Packit 8f70b4
     has set in the Windows Control Panel.
Packit 8f70b4
     If the value of TZ is of the third or fourth kind -- Cygwin programs
Packit 8f70b4
     understand these syntaxes as well --, it does not matter whether we
Packit 8f70b4
     neutralize it or not, since these values occur only when a Cygwin user
Packit 8f70b4
     has set TZ explicitly; this case is 1. rare and 2. under the user's
Packit 8f70b4
     responsibility.  */
Packit 8f70b4
  const char *tz = getenv ("TZ");
Packit 8f70b4
  if (tz != NULL && strchr (tz, '/') != NULL)
Packit 8f70b4
    _putenv ("TZ=");
Packit 8f70b4
Packit 8f70b4
  /* On native Windows, tzset() is deprecated.  Use _tzset() instead.  See
Packit 8f70b4
     https://msdn.microsoft.com/en-us/library/ms235451.aspx
Packit 8f70b4
     https://msdn.microsoft.com/en-us/library/90s5c885.aspx  */
Packit 8f70b4
  _tzset ();
Packit 8f70b4
#elif HAVE_TZSET
Packit 8f70b4
  tzset ();
Packit 8f70b4
#else
Packit 8f70b4
  /* Do nothing.  Avoid infinite recursion.  */
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if TZSET_CLOBBERS_LOCALTIME
Packit 8f70b4
  *localtime_buffer_addr = save;
Packit 8f70b4
#endif
Packit 8f70b4
}