This source file includes following definitions.
- extend_abbrs
- tzalloc
- save_abbr
- tzfree
- getenv_TZ
- setenv_TZ
- change_env
- set_tz
- revert_tz
- localtime_rz
- mktime_z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #include <config.h>
26
27 #include <time.h>
28
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "flexmember.h"
36 #include "idx.h"
37 #include "time-internal.h"
38
39
40
41 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
42
43
44
45
46 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
47
48
49
50
51 static timezone_t const local_tz = (timezone_t) 1;
52
53
54
55
56 static void
57 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
58 {
59 memcpy (abbrs, abbr, abbr_size);
60 abbrs[abbr_size] = '\0';
61 }
62
63
64
65 timezone_t
66 tzalloc (char const *name)
67 {
68 size_t name_size = name ? strlen (name) + 1 : 0;
69 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
70 timezone_t tz = malloc (FLEXSIZEOF (struct tm_zone, abbrs, abbr_size));
71 if (tz)
72 {
73 tz->next = NULL;
74 #if HAVE_TZNAME && !HAVE_STRUCT_TM_TM_ZONE
75 tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
76 #endif
77 tz->tz_is_set = !!name;
78 tz->abbrs[0] = '\0';
79 if (name)
80 extend_abbrs (tz->abbrs, name, name_size);
81 }
82 return tz;
83 }
84
85
86
87
88
89 static bool
90 save_abbr (timezone_t tz, struct tm *tm)
91 {
92 #if HAVE_STRUCT_TM_TM_ZONE || HAVE_TZNAME
93 char const *zone = NULL;
94 char *zone_copy = (char *) "";
95
96 # if HAVE_TZNAME
97 int tzname_index = -1;
98 # endif
99
100 # if HAVE_STRUCT_TM_TM_ZONE
101 zone = tm->tm_zone;
102 # endif
103
104 # if HAVE_TZNAME
105 if (! (zone && *zone) && 0 <= tm->tm_isdst)
106 {
107 tzname_index = tm->tm_isdst != 0;
108 zone = tzname[tzname_index];
109 }
110 # endif
111
112
113 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
114 return true;
115
116 if (*zone)
117 {
118 zone_copy = tz->abbrs;
119
120 while (strcmp (zone_copy, zone) != 0)
121 {
122 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
123 {
124 idx_t zone_size = strlen (zone) + 1;
125 if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
126 extend_abbrs (zone_copy, zone, zone_size);
127 else
128 {
129 tz = tz->next = tzalloc (zone);
130 if (!tz)
131 return false;
132 tz->tz_is_set = 0;
133 zone_copy = tz->abbrs;
134 }
135 break;
136 }
137
138 zone_copy += strlen (zone_copy) + 1;
139 if (!*zone_copy && tz->next)
140 {
141 tz = tz->next;
142 zone_copy = tz->abbrs;
143 }
144 }
145 }
146
147
148 # if HAVE_STRUCT_TM_TM_ZONE
149 tm->tm_zone = zone_copy;
150 # else
151 if (0 <= tzname_index)
152 tz->tzname_copy[tzname_index] = zone_copy;
153 # endif
154 #endif
155
156 return true;
157 }
158
159
160 void
161 tzfree (timezone_t tz)
162 {
163 if (tz != local_tz)
164 while (tz)
165 {
166 timezone_t next = tz->next;
167 free (tz);
168 tz = next;
169 }
170 }
171
172
173
174
175 #ifndef getenv_TZ
176 static char *
177 getenv_TZ (void)
178 {
179 return getenv ("TZ");
180 }
181 #endif
182
183 #ifndef setenv_TZ
184 static int
185 setenv_TZ (char const *tz)
186 {
187 return tz ? setenv ("TZ", tz, 1) : unsetenv ("TZ");
188 }
189 #endif
190
191
192
193 static bool
194 change_env (timezone_t tz)
195 {
196 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
197 return false;
198 tzset ();
199 return true;
200 }
201
202
203
204
205
206 static timezone_t
207 set_tz (timezone_t tz)
208 {
209 char *env_tz = getenv_TZ ();
210 if (env_tz
211 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
212 : !tz->tz_is_set)
213 return local_tz;
214 else
215 {
216 timezone_t old_tz = tzalloc (env_tz);
217 if (!old_tz)
218 return old_tz;
219 if (! change_env (tz))
220 {
221 int saved_errno = errno;
222 tzfree (old_tz);
223 errno = saved_errno;
224 return NULL;
225 }
226 return old_tz;
227 }
228 }
229
230
231
232
233 static bool
234 revert_tz (timezone_t tz)
235 {
236 if (tz == local_tz)
237 return true;
238 else
239 {
240 int saved_errno = errno;
241 bool ok = change_env (tz);
242 if (!ok)
243 saved_errno = errno;
244 tzfree (tz);
245 errno = saved_errno;
246 return ok;
247 }
248 }
249
250
251 struct tm *
252 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
253 {
254 #ifdef HAVE_LOCALTIME_INFLOOP_BUG
255
256
257
258
259
260
261
262 if (! (-67768038400665599 <= *t && *t <= 67768036191766798))
263 {
264 errno = EOVERFLOW;
265 return NULL;
266 }
267 #endif
268
269 if (!tz)
270 return gmtime_r (t, tm);
271 else
272 {
273 timezone_t old_tz = set_tz (tz);
274 if (old_tz)
275 {
276 bool abbr_saved = localtime_r (t, tm) && save_abbr (tz, tm);
277 if (revert_tz (old_tz) && abbr_saved)
278 return tm;
279 }
280 return NULL;
281 }
282 }
283
284
285 time_t
286 mktime_z (timezone_t tz, struct tm *tm)
287 {
288 if (!tz)
289 return timegm (tm);
290 else
291 {
292 timezone_t old_tz = set_tz (tz);
293 if (old_tz)
294 {
295 struct tm tm_1;
296 tm_1.tm_sec = tm->tm_sec;
297 tm_1.tm_min = tm->tm_min;
298 tm_1.tm_hour = tm->tm_hour;
299 tm_1.tm_mday = tm->tm_mday;
300 tm_1.tm_mon = tm->tm_mon;
301 tm_1.tm_year = tm->tm_year;
302 tm_1.tm_yday = -1;
303 tm_1.tm_isdst = tm->tm_isdst;
304 time_t t = mktime (&tm_1);
305 bool ok = 0 <= tm_1.tm_yday;
306 #if HAVE_STRUCT_TM_TM_ZONE || HAVE_TZNAME
307 ok = ok && save_abbr (tz, &tm_1);
308 #endif
309 if (revert_tz (old_tz) && ok)
310 {
311 *tm = tm_1;
312 return t;
313 }
314 }
315 return -1;
316 }
317 }