1 /*
2 FUNCTION
3 <<tzset>>---set timezone characteristics from <[TZ]> environment variable
4
5 INDEX
6 tzset
7
8 SYNOPSIS
9 #include <time.h>
10 void tzset(void);
11
12 DESCRIPTION
13 <<tzset>> examines the <[TZ]> environment variable and sets up the three
14 external variables: <<_timezone>>, <<_daylight>>, and <<tzname>>.
15 The value of <<_timezone>> shall be the offset from the current time
16 to Universal Time.
17 The value of <<_daylight>> shall be 0 if there is no daylight savings
18 time for the current time zone, otherwise it will be non-zero.
19 The <<tzname>> array has two entries: the first is the designation of the
20 standard time period, the second is the designation of the alternate time
21 period.
22
23 The <[TZ]> environment variable is expected to be in the following POSIX
24 format (spaces inserted for clarity):
25
26 <[std]> <[offset1]> [<[dst]> [<[offset2]>] [,<[start]> [/<[time1]>], <[end]> [/<[time2]>]]]
27
28 where:
29
30 <[std]> is the designation for the standard time period (minimum 3,
31 maximum <<TZNAME_MAX>> bytes) in one of two forms:
32
33 *- quoted within angle bracket '<' '>' characters: portable numeric
34 sign or alphanumeric characters in the current locale; the
35 quoting characters are not included in the designation
36
37 *- unquoted: portable alphabetic characters in the current locale
38
39 <[offset1]> is the value to add to local standard time to get Universal Time;
40 it has the format:
41
42 [<[S]>]<[hh]>[:<[mm]>[:<[ss]>]]
43
44 where:
45
46 <[S]> is an optional numeric sign character; if negative '-', the
47 time zone is East of the International Reference
48 Meridian; else it is positive and West, and '+' may be used
49
50 <[hh]> is the required numeric hour between 0 and 24
51
52 <[mm]> is the optional numeric minute between 0 and 59
53
54 <[ss]> is the optional numeric second between 0 and 59
55
56 <[dst]> is the designation of the alternate (daylight saving or
57 summer) time period, with the same limits and forms as
58 the standard time period designation
59
60 <[offset2]> is the value to add to local alternate time to get
61 Universal Time; it has the same format as <[offset1]>
62
63 <[start]> is the date in the year that alternate time starts;
64 the form may be one of:
65 (quotes "'" around characters below are used only to distinguish literals)
66
67 <[n]> zero based Julian day (0-365), counting February 29 Leap days
68
69 'J'<[n]> one based Julian day (1-365), not counting February 29 Leap
70 days; in all years day 59 is February 28 and day 60 is March 1
71
72 'M'<[m]>'.'<[w]>'.'<[d]>
73 month <[m]> (1-12) week <[w]> (1-5) day <[d]> (0-6) where week 1 is
74 the first week in month <[m]> with day <[d]>; week 5 is the last
75 week in the month; day 0 is Sunday
76
77 <[time1]> is the optional local time that alternate time starts, in
78 the same format as <[offset1]> without any sign, and defaults
79 to 02:00:00
80
81 <[end]> is the date in the year that alternate time ends, in the same
82 forms as <[start]>
83
84 <[time2]> is the optional local time that alternate time ends, in
85 the same format as <[offset1]> without any sign, and
86 defaults to 02:00:00
87
88 Note that there is no white-space padding between fields. Also note that
89 if <[TZ]> is null, the default is Universal Time which has no daylight saving
90 time. If <[TZ]> is empty, the default EST5EDT is used.
91
92 RETURNS
93 There is no return value.
94
95 PORTABILITY
96 <<tzset>> is part of the POSIX standard.
97
98 Supporting OS subroutine required: None
99 */
100
101 #define _DEFAULT_SOURCE
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <sys/types.h>
106 #include <time.h>
107 #include <limits.h>
108 #include "local.h"
109
110 #define TZNAME_MIN 3 /* POSIX min TZ abbr size local def */
111
112 static char __tzname_std[TZNAME_MAX + 2];
113 static char __tzname_dst[TZNAME_MAX + 2];
114
115 void
_tzset_unlocked(void)116 _tzset_unlocked (void)
117 {
118 char *tzenv;
119 unsigned short hh, mm, ss, m, w, d;
120 int sign, n;
121 int i, ch;
122 long offset0, offset1;
123 __tzinfo_type *tz = __gettzinfo ();
124 static const struct __tzrule_struct default_tzrule = {'J', 0, 0, 0, 0, (time_t)0, 0L };
125
126 if ((tzenv = getenv ("TZ")) == NULL)
127 {
128 _timezone = 0;
129 _daylight = 0;
130 tzname[0] = "GMT";
131 tzname[1] = "GMT";
132 tz->__tzrule[0] = default_tzrule;
133 tz->__tzrule[1] = default_tzrule;
134 return;
135 }
136
137 /* default to unnamed UTC in case of error */
138 _timezone = 0;
139 _daylight = 0;
140 tzname[0] = "";
141 tzname[1] = "";
142 tz->__tzrule[0] = default_tzrule;
143 tz->__tzrule[1] = default_tzrule;
144
145 /* ignore implementation-specific format specifier */
146 if (*tzenv == ':')
147 ++tzenv;
148
149 /* allow POSIX angle bracket < > quoted signed alphanumeric tz abbr e.g. <MESZ+0330> */
150 if (*tzenv == '<')
151 {
152 ++tzenv;
153
154 /* quit if no items, too few or too many chars, or no close quote '>' */
155 if (sscanf (tzenv, "%11[-+0-9A-Za-z]%n", __tzname_std, &n) <= 0
156 || n < TZNAME_MIN || TZNAME_MAX < n || '>' != tzenv[n])
157 return;
158
159 ++tzenv; /* bump for close quote '>' */
160 }
161 else
162 {
163 /* allow POSIX unquoted alphabetic tz abbr e.g. MESZ */
164 if (sscanf (tzenv, "%11[A-Za-z]%n", __tzname_std, &n) <= 0
165 || n < TZNAME_MIN || TZNAME_MAX < n)
166 return;
167 }
168
169 tzenv += n;
170
171 sign = 1;
172 if (*tzenv == '-')
173 {
174 sign = -1;
175 ++tzenv;
176 }
177 else if (*tzenv == '+')
178 ++tzenv;
179
180 mm = 0;
181 ss = 0;
182
183 if (sscanf (tzenv, "%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n) < 1)
184 return;
185
186 offset0 = sign * (ss + SECSPERMIN * mm + SECSPERHOUR * hh);
187 tzenv += n;
188
189 /* allow POSIX angle bracket < > quoted signed alphanumeric tz abbr e.g. <MESZ+0330> */
190 if (*tzenv == '<')
191 {
192 ++tzenv;
193
194 /* quit if no items, too few or too many chars, or no close quote '>' */
195 if (sscanf (tzenv, "%11[-+0-9A-Za-z]%n", __tzname_dst, &n) <= 0 && tzenv[0] == '>')
196 { /* No dst */
197 tzname[0] = __tzname_std;
198 tzname[1] = tzname[0];
199 tz->__tzrule[0].offset = offset0;
200 _timezone = offset0;
201 return;
202 }
203 else if (n < TZNAME_MIN || TZNAME_MAX < n || '>' != tzenv[n])
204 { /* error */
205 return;
206 }
207
208 ++tzenv; /* bump for close quote '>' */
209 }
210 else
211 {
212 /* allow POSIX unquoted alphabetic tz abbr e.g. MESZ */
213 if (sscanf (tzenv, "%11[A-Za-z]%n", __tzname_dst, &n) <= 0)
214 { /* No dst */
215 tzname[0] = __tzname_std;
216 tzname[1] = tzname[0];
217 tz->__tzrule[0].offset = offset0;
218 _timezone = offset0;
219 return;
220 }
221 else if (n < TZNAME_MIN || TZNAME_MAX < n)
222 { /* error */
223 return;
224 }
225 }
226
227 tzenv += n;
228
229 /* otherwise we have a dst name, look for the offset */
230 sign = 1;
231 if (*tzenv == '-')
232 {
233 sign = -1;
234 ++tzenv;
235 }
236 else if (*tzenv == '+')
237 ++tzenv;
238
239 hh = 0;
240 mm = 0;
241 ss = 0;
242
243 n = 0;
244 if (sscanf (tzenv, "%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n) <= 0)
245 offset1 = offset0 - 3600;
246 else
247 offset1 = sign * (ss + SECSPERMIN * mm + SECSPERHOUR * hh);
248
249 tzenv += n;
250
251 for (i = 0; i < 2; ++i)
252 {
253 if (*tzenv == ',')
254 ++tzenv;
255
256 if (*tzenv == 'M')
257 {
258 if (sscanf (tzenv, "M%hu%n.%hu%n.%hu%n", &m, &n, &w, &n, &d, &n) != 3 ||
259 m < 1 || m > 12 || w < 1 || w > 5 || d > 6)
260 return;
261
262 tz->__tzrule[i].ch = 'M';
263 tz->__tzrule[i].m = m;
264 tz->__tzrule[i].n = w;
265 tz->__tzrule[i].d = d;
266
267 tzenv += n;
268 }
269 else
270 {
271 char *end;
272 if (*tzenv == 'J')
273 {
274 ch = 'J';
275 ++tzenv;
276 }
277 else
278 ch = 'D';
279
280 d = strtoul (tzenv, &end, 10);
281
282 /* if unspecified, default to US settings */
283 /* From 1987-2006, US was M4.1.0,M10.5.0, but starting in 2007 is
284 * M3.2.0,M11.1.0 (2nd Sunday March through 1st Sunday November) */
285 if (end == tzenv)
286 {
287 if (i == 0)
288 {
289 tz->__tzrule[0].ch = 'M';
290 tz->__tzrule[0].m = 3;
291 tz->__tzrule[0].n = 2;
292 tz->__tzrule[0].d = 0;
293 }
294 else
295 {
296 tz->__tzrule[1].ch = 'M';
297 tz->__tzrule[1].m = 11;
298 tz->__tzrule[1].n = 1;
299 tz->__tzrule[1].d = 0;
300 }
301 }
302 else
303 {
304 tz->__tzrule[i].ch = ch;
305 tz->__tzrule[i].d = d;
306 }
307
308 tzenv = end;
309 }
310
311 /* default time is 02:00:00 am */
312 hh = 2;
313 mm = 0;
314 ss = 0;
315 n = 0;
316
317 if (*tzenv == '/')
318 if (sscanf (tzenv, "/%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n) <= 0)
319 {
320 /* error in time format, restore tz rules to default and return */
321 tz->__tzrule[0] = default_tzrule;
322 tz->__tzrule[1] = default_tzrule;
323 return;
324 }
325
326 tz->__tzrule[i].s = ss + SECSPERMIN * mm + SECSPERHOUR * hh;
327
328 tzenv += n;
329 }
330
331 tz->__tzrule[0].offset = offset0;
332 tz->__tzrule[1].offset = offset1;
333 tzname[0] = __tzname_std;
334 tzname[1] = __tzname_dst;
335 __tzcalc_limits (tz->__tzyear);
336 _timezone = tz->__tzrule[0].offset;
337 _daylight = tz->__tzrule[0].offset != tz->__tzrule[1].offset;
338 }
339
340 void
tzset(void)341 tzset (void)
342 {
343 TZ_LOCK;
344 _tzset_unlocked ();
345 TZ_UNLOCK;
346 }
347