time_rz.c 8.4 KB

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