1 /* Provide tzset for systems that don't have it or for which it's broken.
2 
3    Copyright (C) 2001-2003, 2005-2007, 2009-2020 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
17 
18 /* written by Jim Meyering */
19 
20 #include <config.h>
21 
22 /* Specification.  */
23 #include <time.h>
24 
25 #include "localtime-buffer.h"
26 
27 /* This is a wrapper for tzset, for systems on which tzset may clobber
28    the static buffer used for localtime's result.
29    Work around the bug in some systems whereby tzset clobbers the
30    static buffer that localtime uses for its return value.  The
31    tzset function from Solaris 2.5, 2.5.1, and 2.6 has this problem.  */
32 
33 void
tzset(void)34 tzset (void)
35 #undef tzset
36 {
37 #if TZSET_CLOBBERS_LOCALTIME
38   /* Save and restore the contents of the buffer used for localtime's
39      result around the call to tzset.  */
40   struct tm save = *localtime_buffer_addr;
41 #endif
42 
43 #if defined _WIN32 && ! defined __CYGWIN__
44   /* Rectify the value of the environment variable TZ.
45      There are four possible kinds of such values:
46        - Traditional US time zone names, e.g. "PST8PDT".  Syntax: see
47          <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>
48        - Time zone names based on geography, that contain one or more
49          slashes, e.g. "Europe/Moscow".
50        - Time zone names based on geography, without slashes, e.g.
51          "Singapore".
52        - Time zone names that contain explicit DST rules.  Syntax: see
53          <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>
54      The Microsoft CRT understands only the first kind.  It produces incorrect
55      results if the value of TZ is of the other kinds.
56      But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value
57      of the second kind for most geographies, or of the first kind in a few
58      other geographies.  If it is of the second kind, neutralize it.  For the
59      Microsoft CRT, an absent or empty TZ means the time zone that the user
60      has set in the Windows Control Panel.
61      If the value of TZ is of the third or fourth kind -- Cygwin programs
62      understand these syntaxes as well --, it does not matter whether we
63      neutralize it or not, since these values occur only when a Cygwin user
64      has set TZ explicitly; this case is 1. rare and 2. under the user's
65      responsibility.  */
66   const char *tz = getenv ("TZ");
67   if (tz != NULL && strchr (tz, '/') != NULL)
68     _putenv ("TZ=");
69 
70   /* On native Windows, tzset() is deprecated.  Use _tzset() instead.  See
71      <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-tzset>
72      <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>  */
73   _tzset ();
74 #elif HAVE_TZSET
75   tzset ();
76 #else
77   /* Do nothing.  Avoid infinite recursion.  */
78 #endif
79 
80 #if TZSET_CLOBBERS_LOCALTIME
81   *localtime_buffer_addr = save;
82 #endif
83 }
84