time_rz.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* Time zone functions such as tzalloc and localtime_rz
  2. Copyright 2015-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. /* Although this module is not thread-safe, any races should be fairly
  15. rare and reasonably benign. For complete thread-safety, use a C
  16. library with a working timezone_t type, so that this module is not
  17. needed. */
  18. #include <config.h>
  19. #include <time.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "flexmember.h"
  25. #include "idx.h"
  26. #include "time-internal.h"
  27. /* The approximate size to use for small allocation requests. This is
  28. the largest "small" request for the GNU C library malloc. */
  29. enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
  30. /* Minimum size of the ABBRS member of struct tm_zone. ABBRS is larger
  31. only in the unlikely case where an abbreviation longer than this is
  32. used. */
  33. enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
  34. /* Magic cookie timezone_t value, for local time. It differs from
  35. NULL and from all other timezone_t values. Only the address
  36. matters; the pointer is never dereferenced. */
  37. static timezone_t const local_tz = (timezone_t) 1;
  38. /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
  39. includes its trailing null byte). Append an extra null byte to
  40. mark the end of ABBRS. */
  41. static void
  42. extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
  43. {
  44. memcpy (abbrs, abbr, abbr_size);
  45. abbrs[abbr_size] = '\0';
  46. }
  47. /* Return a newly allocated time zone for NAME, or NULL on failure.
  48. A null NAME stands for wall clock time (which is like unset TZ). */
  49. timezone_t
  50. tzalloc (char const *name)
  51. {
  52. size_t name_size = name ? strlen (name) + 1 : 0;
  53. size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
  54. timezone_t tz = malloc (FLEXSIZEOF (struct tm_zone, abbrs, abbr_size));
  55. if (tz)
  56. {
  57. tz->next = NULL;
  58. #if HAVE_TZNAME && !HAVE_STRUCT_TM_TM_ZONE
  59. tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
  60. #endif
  61. tz->tz_is_set = !!name;
  62. tz->abbrs[0] = '\0';
  63. if (name)
  64. extend_abbrs (tz->abbrs, name, name_size);
  65. }
  66. return tz;
  67. }
  68. /* Save into TZ any nontrivial time zone abbreviation used by TM, and
  69. update *TM (if HAVE_STRUCT_TM_TM_ZONE) or *TZ (if
  70. !HAVE_STRUCT_TM_TM_ZONE && HAVE_TZNAME) if they use the abbreviation.
  71. Return true if successful, false (setting errno) otherwise. */
  72. static bool
  73. save_abbr (timezone_t tz, struct tm *tm)
  74. {
  75. #if HAVE_STRUCT_TM_TM_ZONE || HAVE_TZNAME
  76. char const *zone = NULL;
  77. char *zone_copy = (char *) "";
  78. # if HAVE_TZNAME
  79. int tzname_index = -1;
  80. # endif
  81. # if HAVE_STRUCT_TM_TM_ZONE
  82. zone = tm->tm_zone;
  83. # endif
  84. # if HAVE_TZNAME
  85. if (! (zone && *zone) && 0 <= tm->tm_isdst)
  86. {
  87. tzname_index = tm->tm_isdst != 0;
  88. zone = tzname[tzname_index];
  89. }
  90. # endif
  91. /* No need to replace null zones, or zones within the struct tm. */
  92. if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
  93. return true;
  94. if (*zone)
  95. {
  96. zone_copy = tz->abbrs;
  97. while (strcmp (zone_copy, zone) != 0)
  98. {
  99. if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
  100. {
  101. idx_t zone_size = strlen (zone) + 1;
  102. if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
  103. extend_abbrs (zone_copy, zone, zone_size);
  104. else
  105. {
  106. tz = tz->next = tzalloc (zone);
  107. if (!tz)
  108. return false;
  109. tz->tz_is_set = 0;
  110. zone_copy = tz->abbrs;
  111. }
  112. break;
  113. }
  114. zone_copy += strlen (zone_copy) + 1;
  115. if (!*zone_copy && tz->next)
  116. {
  117. tz = tz->next;
  118. zone_copy = tz->abbrs;
  119. }
  120. }
  121. }
  122. /* Replace the zone name so that its lifetime matches that of TZ. */
  123. # if HAVE_STRUCT_TM_TM_ZONE
  124. tm->tm_zone = zone_copy;
  125. # else
  126. if (0 <= tzname_index)
  127. tz->tzname_copy[tzname_index] = zone_copy;
  128. # endif
  129. #endif
  130. return true;
  131. }
  132. /* Free a time zone. */
  133. void
  134. tzfree (timezone_t tz)
  135. {
  136. if (tz != local_tz)
  137. while (tz)
  138. {
  139. timezone_t next = tz->next;
  140. free (tz);
  141. tz = next;
  142. }
  143. }
  144. /* Get and set the TZ environment variable. These functions can be
  145. overridden by programs like Emacs that manage their own environment. */
  146. #ifndef getenv_TZ
  147. static char *
  148. getenv_TZ (void)
  149. {
  150. return getenv ("TZ");
  151. }
  152. #endif
  153. #ifndef setenv_TZ
  154. static int
  155. setenv_TZ (char const *tz)
  156. {
  157. return tz ? setenv ("TZ", tz, 1) : unsetenv ("TZ");
  158. }
  159. #endif
  160. /* Change the environment to match the specified timezone_t value.
  161. Return true if successful, false (setting errno) otherwise. */
  162. static bool
  163. change_env (timezone_t tz)
  164. {
  165. if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
  166. return false;
  167. tzset ();
  168. return true;
  169. }
  170. /* Temporarily set the time zone to TZ, which must not be null.
  171. Return LOCAL_TZ if the time zone setting is already correct.
  172. Otherwise return a newly allocated time zone representing the old
  173. setting, or NULL (setting errno) on failure. */
  174. static timezone_t
  175. set_tz (timezone_t tz)
  176. {
  177. char *env_tz = getenv_TZ ();
  178. if (env_tz
  179. ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
  180. : !tz->tz_is_set)
  181. return local_tz;
  182. else
  183. {
  184. timezone_t old_tz = tzalloc (env_tz);
  185. if (!old_tz)
  186. return old_tz;
  187. if (! change_env (tz))
  188. {
  189. int saved_errno = errno;
  190. tzfree (old_tz);
  191. errno = saved_errno;
  192. return NULL;
  193. }
  194. return old_tz;
  195. }
  196. }
  197. /* Restore an old setting returned by set_tz. It must not be null.
  198. Return true (preserving errno) if successful, false (setting errno)
  199. otherwise. */
  200. static bool
  201. revert_tz (timezone_t tz)
  202. {
  203. if (tz == local_tz)
  204. return true;
  205. else
  206. {
  207. int saved_errno = errno;
  208. bool ok = change_env (tz);
  209. if (!ok)
  210. saved_errno = errno;
  211. tzfree (tz);
  212. errno = saved_errno;
  213. return ok;
  214. }
  215. }
  216. /* Use time zone TZ to compute localtime_r (T, TM). */
  217. struct tm *
  218. localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
  219. {
  220. #ifdef HAVE_LOCALTIME_INFLOOP_BUG
  221. /* The -67768038400665599 comes from:
  222. https://lists.gnu.org/r/bug-gnulib/2017-07/msg00142.html
  223. On affected platforms the greatest POSIX-compatible time_t value
  224. that could return nonnull is 67768036191766798 (when
  225. TZ="XXX24:59:59" it resolves to the year 2**31 - 1 + 1900, on
  226. 12-31 at 23:59:59), so test for that too while we're in the
  227. neighborhood. */
  228. if (! (-67768038400665599 <= *t && *t <= 67768036191766798))
  229. {
  230. errno = EOVERFLOW;
  231. return NULL;
  232. }
  233. #endif
  234. if (!tz)
  235. return gmtime_r (t, tm);
  236. else
  237. {
  238. timezone_t old_tz = set_tz (tz);
  239. if (old_tz)
  240. {
  241. bool abbr_saved = localtime_r (t, tm) && save_abbr (tz, tm);
  242. if (revert_tz (old_tz) && abbr_saved)
  243. return tm;
  244. }
  245. return NULL;
  246. }
  247. }
  248. /* Use time zone TZ to compute mktime (TM). */
  249. time_t
  250. mktime_z (timezone_t tz, struct tm *tm)
  251. {
  252. if (!tz)
  253. return timegm (tm);
  254. else
  255. {
  256. timezone_t old_tz = set_tz (tz);
  257. if (old_tz)
  258. {
  259. struct tm tm_1;
  260. tm_1.tm_sec = tm->tm_sec;
  261. tm_1.tm_min = tm->tm_min;
  262. tm_1.tm_hour = tm->tm_hour;
  263. tm_1.tm_mday = tm->tm_mday;
  264. tm_1.tm_mon = tm->tm_mon;
  265. tm_1.tm_year = tm->tm_year;
  266. tm_1.tm_yday = -1;
  267. tm_1.tm_isdst = tm->tm_isdst;
  268. time_t t = mktime (&tm_1);
  269. bool ok = 0 <= tm_1.tm_yday;
  270. #if HAVE_STRUCT_TM_TM_ZONE || HAVE_TZNAME
  271. ok = ok && save_abbr (tz, &tm_1);
  272. #endif
  273. if (revert_tz (old_tz) && ok)
  274. {
  275. *tm = tm_1;
  276. return t;
  277. }
  278. }
  279. return -1;
  280. }
  281. }