]>
code.delx.au - gnu-emacs/blob - lib/time_rz.c
1 /* Time zone functions such as tzalloc and localtime_rz
3 Copyright 2015 Free Software Foundation, Inc.
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)
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.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 /* Although this module is not thread-safe, any races should be fairly
21 rare and reasonably benign. For complete thread-safety, use a C
22 library with a working timezone_t type, so that this module is not
36 static void tzset (void) { }
39 /* A time zone rule. */
42 /* More abbreviations, should they be needed. Their TZ_IS_SET
46 /* If nonzero, the rule represents the TZ environment variable set
47 to the first "abbreviation" (this may be the empty string).
48 Otherwise, it represents an unset TZ. */
51 /* A sequence of null-terminated strings packed next to each other.
52 The strings are followed by an extra null byte. If TZ_IS_SET,
53 there must be at least one string and the first string (which is
54 actually a TZ environment value value) may be empty. Otherwise
55 all strings must be nonempty.
57 Abbreviations are stored here because otherwise the values of
58 tm_zone and/or tzname would be dead after changing TZ and calling
59 tzset. Abbreviations never move once allocated, and are live
60 until tzfree is called. */
61 char abbrs
[FLEXIBLE_ARRAY_MEMBER
];
64 /* The approximate size to use for small allocation requests. This is
65 the largest "small" request for the GNU C library malloc. */
66 enum { DEFAULT_MXFAST
= 64 * sizeof (size_t) / 4 };
68 /* Minimum size of the ABBRS member of struct abbr. ABBRS is larger
69 only in the unlikely case where an abbreviation longer than this is
71 enum { ABBR_SIZE_MIN
= DEFAULT_MXFAST
- offsetof (struct tm_zone
, abbrs
) };
73 static char const TZ
[] = "TZ";
75 /* Magic cookie timezone_t value, for local time. It differs from
76 NULL and from all other timezone_t values. Only the address
77 matters; the pointer is never dereferenced. */
78 static timezone_t
const local_tz
= (timezone_t
) 1;
80 #if HAVE_TM_ZONE || HAVE_TZNAME
82 /* Return true if the values A and B differ according to the rules for
83 tm_isdst: A and B differ if one is zero and the other positive. */
85 isdst_differ (int a
, int b
)
87 return !a
!= !b
&& 0 <= a
&& 0 <= b
;
90 /* Return true if A and B are equal. */
92 equal_tm (const struct tm
*a
, const struct tm
*b
)
94 return ! ((a
->tm_sec
^ b
->tm_sec
)
95 | (a
->tm_min
^ b
->tm_min
)
96 | (a
->tm_hour
^ b
->tm_hour
)
97 | (a
->tm_mday
^ b
->tm_mday
)
98 | (a
->tm_mon
^ b
->tm_mon
)
99 | (a
->tm_year
^ b
->tm_year
)
100 | isdst_differ (a
->tm_isdst
, b
->tm_isdst
));
105 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
106 includes its trailing null byte). Append an extra null byte to
107 mark the end of ABBRS. */
109 extend_abbrs (char *abbrs
, char const *abbr
, size_t abbr_size
)
111 memcpy (abbrs
, abbr
, abbr_size
);
112 abbrs
[abbr_size
] = '\0';
115 /* Return a newly allocated time zone for NAME, or NULL on failure.
116 As a special case, return a nonzero constant for wall clock time, a
117 constant that survives freeing. */
119 tzalloc (char const *name
)
121 size_t name_size
= name
? strlen (name
) + 1 : 0;
122 size_t abbr_size
= name_size
< ABBR_SIZE_MIN
? ABBR_SIZE_MIN
: name_size
+ 1;
123 timezone_t tz
= malloc (offsetof (struct tm_zone
, abbrs
) + abbr_size
);
127 tz
->tz_is_set
= !!name
;
128 extend_abbrs (tz
->abbrs
, name
, name_size
);
134 /* If TZNAME_ADDRESS is nonnull, an assignment of a saved abbreviation.
135 TZNAME_ADDRESS should be either null, or &tzname[0], or &tzname[1].
136 *TZNAME_ADDRESS = TZNAME_VALUE should be done after revert_tz
137 (indirectly) calls tzset, so that revert_tz can overwrite tzset's
138 assignment to tzname. Also, it should be done at the start of
139 the next localtime_tz or mktime_z, to undo the overwrite. */
140 static char **tzname_address
;
141 static char *tzname_value
;
144 /* Save into TZ any nontrivial time zone abbreviation used by TM,
145 and update *TM (or prepare to update tzname) if they use the abbreviation.
146 Return true if successful, false (setting errno) otherwise. */
148 save_abbr (timezone_t tz
, struct tm
*tm
)
150 #if HAVE_TM_ZONE || HAVE_TZNAME
151 char const *zone
= NULL
;
152 char **tzname_zone
= NULL
;
153 char *zone_copy
= (char *) "";
158 if (! (zone
&& *zone
) && 0 <= tm
->tm_isdst
)
159 zone
= *(tzname_zone
= &tzname
[0 < tm
->tm_isdst
]);
162 /* No need to replace null zones, or zones within the struct tm. */
163 if (!zone
|| ((char *) tm
<= zone
&& zone
< (char *) (tm
+ 1)))
168 zone_copy
= tz
->abbrs
;
170 while (strcmp (zone_copy
, zone
) != 0)
172 if (! (*zone_copy
|| (zone_copy
== tz
->abbrs
&& tz
->tz_is_set
)))
174 size_t zone_size
= strlen (zone
) + 1;
175 if (zone_size
< tz
->abbrs
+ ABBR_SIZE_MIN
- zone_copy
)
176 extend_abbrs (zone_copy
, zone
, zone_size
);
179 tz
= tz
->next
= tzalloc (zone
);
183 zone_copy
= tz
->abbrs
;
188 zone_copy
+= strlen (zone_copy
) + 1;
189 if (!*zone_copy
&& tz
->next
)
192 zone_copy
= tz
->abbrs
;
197 /* Replace the zone name so that its lifetime matches that of TZ. */
200 tm
->tm_zone
= zone_copy
;
203 tzname_address
= tzname_zone
;
204 tzname_value
= zone_copy
;
210 /* Free a time zone. */
212 tzfree (timezone_t tz
)
217 timezone_t next
= tz
->next
;
223 /* Get and set the TZ environment variable. These functions can be
224 overridden by programs like Emacs that manage their own environment. */
236 setenv_TZ (char const *tz
)
238 return tz
? setenv (TZ
, tz
, 1) : unsetenv (TZ
);
242 /* Change the environment to match the specified timezone_t value.
243 Return true if successful, false (setting errno) otherwise. */
245 change_env (timezone_t tz
)
247 if (setenv_TZ (tz
->tz_is_set
? tz
->abbrs
: NULL
) != 0)
253 /* Temporarily set the time zone to TZ, which must not be null.
254 Return LOCAL_TZ if the time zone setting is already correct.
255 Otherwise return a newly allocated time zone representing the old
256 setting, or NULL (setting errno) on failure. */
258 set_tz (timezone_t tz
)
260 char *env_tz
= getenv_TZ ();
262 ? tz
->tz_is_set
&& strcmp (tz
->abbrs
, env_tz
) == 0
267 timezone_t old_tz
= tzalloc (env_tz
);
270 if (! change_env (tz
))
272 int saved_errno
= errno
;
281 /* Restore an old setting returned by set_tz. It must not be null.
282 Return true (preserving errno) if successful, false (setting errno)
285 revert_tz (timezone_t tz
)
291 int saved_errno
= errno
;
292 bool ok
= change_env (tz
);
297 tzname_address
= NULL
;
300 char *old_value
= *tzname_address
;
301 *tzname_address
= tzname_value
;
302 tzname_value
= old_value
;
311 /* Restore an old tzname setting that was temporarily munged by revert_tz. */
313 restore_tzname (void)
318 *tzname_address
= tzname_value
;
319 tzname_address
= NULL
;
324 /* Use time zone TZ to compute localtime_r (T, TM). */
326 localtime_rz (timezone_t tz
, time_t const *t
, struct tm
*tm
)
331 return gmtime_r (t
, tm
);
334 timezone_t old_tz
= set_tz (tz
);
337 tm
= localtime_r (t
, tm
);
338 if (tm
&& !save_abbr (tz
, tm
))
340 if (revert_tz (old_tz
))
347 /* Use time zone TZ to compute mktime (TM). */
349 mktime_z (timezone_t tz
, struct tm
*tm
)
357 timezone_t old_tz
= set_tz (tz
);
360 time_t t
= mktime (tm
);
361 #if HAVE_TM_ZONE || HAVE_TZNAME
365 || (localtime_r (&t
, &tm_1
) && equal_tm (tm
, &tm_1
)))
366 && !save_abbr (tz
, tm
))
369 if (revert_tz (old_tz
))