localtime.c revision 1.3.2.3 1 1.3.2.3 jtc /* $NetBSD: localtime.c,v 1.3.2.3 1995/05/02 19:36:51 jtc Exp $ */
2 1.3.2.2 jtc
3 1.3.2.2 jtc #ifndef lint
4 1.3.2.2 jtc #ifndef NOID
5 1.3.2.2 jtc static char elsieid[] = "@(#)localtime.c 7.43";
6 1.3.2.2 jtc #endif /* !defined NOID */
7 1.3.2.2 jtc #endif /* !defined lint */
8 1.3.2.2 jtc
9 1.3.2.2 jtc /*
10 1.3.2.2 jtc ** Leap second handling from Bradley White (bww (at) k.gp.cs.cmu.edu).
11 1.3.2.2 jtc ** POSIX-style TZ environment variable handling from Guy Harris
12 1.3.2.2 jtc ** (guy (at) auspex.com).
13 1.3.2.2 jtc */
14 1.3.2.2 jtc
15 1.3.2.2 jtc /*LINTLIBRARY*/
16 1.3.2.2 jtc
17 1.3.2.3 jtc #include "namespace.h"
18 1.3.2.2 jtc #include "private.h"
19 1.3.2.2 jtc #include "tzfile.h"
20 1.3.2.2 jtc #include "fcntl.h"
21 1.3.2.2 jtc
22 1.3.2.2 jtc /*
23 1.3.2.2 jtc ** SunOS 4.1.1 headers lack O_BINARY.
24 1.3.2.2 jtc */
25 1.3.2.2 jtc
26 1.3.2.2 jtc #ifdef O_BINARY
27 1.3.2.2 jtc #define OPEN_MODE (O_RDONLY | O_BINARY)
28 1.3.2.2 jtc #endif /* defined O_BINARY */
29 1.3.2.2 jtc #ifndef O_BINARY
30 1.3.2.2 jtc #define OPEN_MODE O_RDONLY
31 1.3.2.2 jtc #endif /* !defined O_BINARY */
32 1.3.2.2 jtc
33 1.3.2.2 jtc #ifndef WILDABBR
34 1.3.2.2 jtc /*
35 1.3.2.2 jtc ** Someone might make incorrect use of a time zone abbreviation:
36 1.3.2.2 jtc ** 1. They might reference tzname[0] before calling tzset (explicitly
37 1.3.2.2 jtc ** or implicitly).
38 1.3.2.2 jtc ** 2. They might reference tzname[1] before calling tzset (explicitly
39 1.3.2.2 jtc ** or implicitly).
40 1.3.2.2 jtc ** 3. They might reference tzname[1] after setting to a time zone
41 1.3.2.2 jtc ** in which Daylight Saving Time is never observed.
42 1.3.2.2 jtc ** 4. They might reference tzname[0] after setting to a time zone
43 1.3.2.2 jtc ** in which Standard Time is never observed.
44 1.3.2.2 jtc ** 5. They might reference tm.TM_ZONE after calling offtime.
45 1.3.2.2 jtc ** What's best to do in the above cases is open to debate;
46 1.3.2.2 jtc ** for now, we just set things up so that in any of the five cases
47 1.3.2.2 jtc ** WILDABBR is used. Another possibility: initialize tzname[0] to the
48 1.3.2.2 jtc ** string "tzname[0] used before set", and similarly for the other cases.
49 1.3.2.2 jtc ** And another: initialize tzname[0] to "ERA", with an explanation in the
50 1.3.2.2 jtc ** manual page of what this "time zone abbreviation" means (doing this so
51 1.3.2.2 jtc ** that tzname[0] has the "normal" length of three characters).
52 1.3.2.2 jtc */
53 1.3.2.2 jtc #define WILDABBR " "
54 1.3.2.2 jtc #endif /* !defined WILDABBR */
55 1.3.2.2 jtc
56 1.3.2.2 jtc static char wildabbr[] = "WILDABBR";
57 1.3.2.2 jtc
58 1.3.2.2 jtc static const char gmt[] = "GMT";
59 1.3.2.2 jtc
60 1.3.2.2 jtc struct ttinfo { /* time type information */
61 1.3.2.2 jtc long tt_gmtoff; /* GMT offset in seconds */
62 1.3.2.2 jtc int tt_isdst; /* used to set tm_isdst */
63 1.3.2.2 jtc int tt_abbrind; /* abbreviation list index */
64 1.3.2.2 jtc int tt_ttisstd; /* TRUE if transition is std time */
65 1.3.2.2 jtc int tt_ttisgmt; /* TRUE if transition is GMT */
66 1.3.2.2 jtc };
67 1.3.2.2 jtc
68 1.3.2.2 jtc struct lsinfo { /* leap second information */
69 1.3.2.2 jtc time_t ls_trans; /* transition time */
70 1.3.2.2 jtc long ls_corr; /* correction to apply */
71 1.3.2.2 jtc };
72 1.3.2.2 jtc
73 1.3.2.2 jtc #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
74 1.3.2.2 jtc
75 1.3.2.2 jtc #ifdef TZNAME_MAX
76 1.3.2.2 jtc #define MY_TZNAME_MAX TZNAME_MAX
77 1.3.2.2 jtc #endif /* defined TZNAME_MAX */
78 1.3.2.2 jtc #ifndef TZNAME_MAX
79 1.3.2.2 jtc #define MY_TZNAME_MAX 255
80 1.3.2.2 jtc #endif /* !defined TZNAME_MAX */
81 1.3.2.2 jtc
82 1.3.2.2 jtc struct state {
83 1.3.2.2 jtc int leapcnt;
84 1.3.2.2 jtc int timecnt;
85 1.3.2.2 jtc int typecnt;
86 1.3.2.2 jtc int charcnt;
87 1.3.2.2 jtc time_t ats[TZ_MAX_TIMES];
88 1.3.2.2 jtc unsigned char types[TZ_MAX_TIMES];
89 1.3.2.2 jtc struct ttinfo ttis[TZ_MAX_TYPES];
90 1.3.2.2 jtc char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
91 1.3.2.2 jtc (2 * (MY_TZNAME_MAX + 1)))];
92 1.3.2.2 jtc struct lsinfo lsis[TZ_MAX_LEAPS];
93 1.3.2.2 jtc };
94 1.3.2.2 jtc
95 1.3.2.2 jtc struct rule {
96 1.3.2.2 jtc int r_type; /* type of rule--see below */
97 1.3.2.2 jtc int r_day; /* day number of rule */
98 1.3.2.2 jtc int r_week; /* week number of rule */
99 1.3.2.2 jtc int r_mon; /* month number of rule */
100 1.3.2.2 jtc long r_time; /* transition time of rule */
101 1.3.2.2 jtc };
102 1.3.2.2 jtc
103 1.3.2.2 jtc #define JULIAN_DAY 0 /* Jn - Julian day */
104 1.3.2.2 jtc #define DAY_OF_YEAR 1 /* n - day of year */
105 1.3.2.2 jtc #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
106 1.3.2.2 jtc
107 1.3.2.2 jtc /*
108 1.3.2.2 jtc ** Prototypes for static functions.
109 1.3.2.2 jtc */
110 1.3.2.2 jtc
111 1.3.2.2 jtc static long detzcode P((const char * codep));
112 1.3.2.2 jtc static const char * getzname P((const char * strp));
113 1.3.2.2 jtc static const char * getnum P((const char * strp, int * nump, int min,
114 1.3.2.2 jtc int max));
115 1.3.2.2 jtc static const char * getsecs P((const char * strp, long * secsp));
116 1.3.2.2 jtc static const char * getoffset P((const char * strp, long * offsetp));
117 1.3.2.2 jtc static const char * getrule P((const char * strp, struct rule * rulep));
118 1.3.2.2 jtc static void gmtload P((struct state * sp));
119 1.3.2.2 jtc static void gmtsub P((const time_t * timep, long offset,
120 1.3.2.2 jtc struct tm * tmp));
121 1.3.2.2 jtc static void localsub P((const time_t * timep, long offset,
122 1.3.2.2 jtc struct tm * tmp));
123 1.3.2.2 jtc static int increment_overflow P((int * number, int delta));
124 1.3.2.2 jtc static int normalize_overflow P((int * tensptr, int * unitsptr,
125 1.3.2.2 jtc int base));
126 1.3.2.2 jtc static void settzname P((void));
127 1.3.2.2 jtc static time_t time1 P((struct tm * tmp,
128 1.3.2.2 jtc void(*funcp) P((const time_t *,
129 1.3.2.2 jtc long, struct tm *)),
130 1.3.2.2 jtc long offset));
131 1.3.2.2 jtc static time_t time2 P((struct tm *tmp,
132 1.3.2.2 jtc void(*funcp) P((const time_t *,
133 1.3.2.2 jtc long, struct tm*)),
134 1.3.2.2 jtc long offset, int * okayp));
135 1.3.2.2 jtc static void timesub P((const time_t * timep, long offset,
136 1.3.2.2 jtc const struct state * sp, struct tm * tmp));
137 1.3.2.2 jtc static int tmcomp P((const struct tm * atmp,
138 1.3.2.2 jtc const struct tm * btmp));
139 1.3.2.2 jtc static time_t transtime P((time_t janfirst, int year,
140 1.3.2.2 jtc const struct rule * rulep, long offset));
141 1.3.2.2 jtc static int tzload P((const char * name, struct state * sp));
142 1.3.2.2 jtc static int tzparse P((const char * name, struct state * sp,
143 1.3.2.2 jtc int lastditch));
144 1.3.2.2 jtc
145 1.3.2.2 jtc #ifdef ALL_STATE
146 1.3.2.2 jtc static struct state * lclptr;
147 1.3.2.2 jtc static struct state * gmtptr;
148 1.3.2.2 jtc #endif /* defined ALL_STATE */
149 1.3.2.2 jtc
150 1.3.2.2 jtc #ifndef ALL_STATE
151 1.3.2.2 jtc static struct state lclmem;
152 1.3.2.2 jtc static struct state gmtmem;
153 1.3.2.2 jtc #define lclptr (&lclmem)
154 1.3.2.2 jtc #define gmtptr (&gmtmem)
155 1.3.2.2 jtc #endif /* State Farm */
156 1.3.2.2 jtc
157 1.3.2.2 jtc #ifndef TZ_STRLEN_MAX
158 1.3.2.2 jtc #define TZ_STRLEN_MAX 255
159 1.3.2.2 jtc #endif /* !defined TZ_STRLEN_MAX */
160 1.3.2.2 jtc
161 1.3.2.2 jtc static char lcl_TZname[TZ_STRLEN_MAX + 1];
162 1.3.2.2 jtc static int lcl_is_set;
163 1.3.2.2 jtc static int gmt_is_set;
164 1.3.2.2 jtc
165 1.3.2.2 jtc char * tzname[2] = {
166 1.3.2.2 jtc wildabbr,
167 1.3.2.2 jtc wildabbr
168 1.3.2.2 jtc };
169 1.3.2.3 jtc #pragma weak tzname=_tzname
170 1.3.2.2 jtc
171 1.3.2.2 jtc /*
172 1.3.2.2 jtc ** Section 4.12.3 of X3.159-1989 requires that
173 1.3.2.2 jtc ** Except for the strftime function, these functions [asctime,
174 1.3.2.2 jtc ** ctime, gmtime, localtime] return values in one of two static
175 1.3.2.2 jtc ** objects: a broken-down time structure and an array of char.
176 1.3.2.2 jtc ** Thanks to Paul Eggert (eggert (at) twinsun.com) for noting this.
177 1.3.2.2 jtc */
178 1.3.2.2 jtc
179 1.3.2.2 jtc static struct tm tm;
180 1.3.2.2 jtc
181 1.3.2.2 jtc #ifdef USG_COMPAT
182 1.3.2.2 jtc time_t timezone = 0;
183 1.3.2.2 jtc int daylight = 0;
184 1.3.2.3 jtc #pragma weak timezone=_timezone
185 1.3.2.3 jtc #pragma weak daylight=_daylight
186 1.3.2.2 jtc #endif /* defined USG_COMPAT */
187 1.3.2.2 jtc
188 1.3.2.2 jtc #ifdef ALTZONE
189 1.3.2.2 jtc time_t altzone = 0;
190 1.3.2.3 jtc #pragma weak altzone=_altzone
191 1.3.2.2 jtc #endif /* defined ALTZONE */
192 1.3.2.2 jtc
193 1.3.2.2 jtc static long
194 1.3.2.2 jtc detzcode(codep)
195 1.3.2.2 jtc const char * const codep;
196 1.3.2.2 jtc {
197 1.3.2.2 jtc register long result;
198 1.3.2.2 jtc
199 1.3.2.2 jtc result = (codep[0] << 24) \
200 1.3.2.2 jtc | (codep[1] & 0xff) << 16 \
201 1.3.2.2 jtc | (codep[2] & 0xff) << 8
202 1.3.2.2 jtc | (codep[3] & 0xff);
203 1.3.2.2 jtc return result;
204 1.3.2.2 jtc }
205 1.3.2.2 jtc
206 1.3.2.2 jtc static void
207 1.3.2.2 jtc settzname P((void))
208 1.3.2.2 jtc {
209 1.3.2.2 jtc register struct state * const sp = lclptr;
210 1.3.2.2 jtc register int i;
211 1.3.2.2 jtc
212 1.3.2.2 jtc tzname[0] = wildabbr;
213 1.3.2.2 jtc tzname[1] = wildabbr;
214 1.3.2.2 jtc #ifdef USG_COMPAT
215 1.3.2.2 jtc daylight = 0;
216 1.3.2.2 jtc timezone = 0;
217 1.3.2.2 jtc #endif /* defined USG_COMPAT */
218 1.3.2.2 jtc #ifdef ALTZONE
219 1.3.2.2 jtc altzone = 0;
220 1.3.2.2 jtc #endif /* defined ALTZONE */
221 1.3.2.2 jtc #ifdef ALL_STATE
222 1.3.2.2 jtc if (sp == NULL) {
223 1.3.2.2 jtc tzname[0] = tzname[1] = gmt;
224 1.3.2.2 jtc return;
225 1.3.2.2 jtc }
226 1.3.2.2 jtc #endif /* defined ALL_STATE */
227 1.3.2.2 jtc for (i = 0; i < sp->typecnt; ++i) {
228 1.3.2.2 jtc register const struct ttinfo * const ttisp = &sp->ttis[i];
229 1.3.2.2 jtc
230 1.3.2.2 jtc tzname[ttisp->tt_isdst] =
231 1.3.2.2 jtc &sp->chars[ttisp->tt_abbrind];
232 1.3.2.2 jtc #ifdef USG_COMPAT
233 1.3.2.2 jtc if (ttisp->tt_isdst)
234 1.3.2.2 jtc daylight = 1;
235 1.3.2.2 jtc if (i == 0 || !ttisp->tt_isdst)
236 1.3.2.2 jtc timezone = -(ttisp->tt_gmtoff);
237 1.3.2.2 jtc #endif /* defined USG_COMPAT */
238 1.3.2.2 jtc #ifdef ALTZONE
239 1.3.2.2 jtc if (i == 0 || ttisp->tt_isdst)
240 1.3.2.2 jtc altzone = -(ttisp->tt_gmtoff);
241 1.3.2.2 jtc #endif /* defined ALTZONE */
242 1.3.2.2 jtc }
243 1.3.2.2 jtc /*
244 1.3.2.2 jtc ** And to get the latest zone names into tzname. . .
245 1.3.2.2 jtc */
246 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
247 1.3.2.2 jtc register const struct ttinfo * const ttisp =
248 1.3.2.2 jtc &sp->ttis[
249 1.3.2.2 jtc sp->types[i]];
250 1.3.2.2 jtc
251 1.3.2.2 jtc tzname[ttisp->tt_isdst] =
252 1.3.2.2 jtc &sp->chars[ttisp->tt_abbrind];
253 1.3.2.2 jtc }
254 1.3.2.2 jtc }
255 1.3.2.2 jtc
256 1.3.2.2 jtc static int
257 1.3.2.2 jtc tzload(name, sp)
258 1.3.2.2 jtc register const char * name;
259 1.3.2.2 jtc register struct state * const sp;
260 1.3.2.2 jtc {
261 1.3.2.2 jtc register const char * p;
262 1.3.2.2 jtc register int i;
263 1.3.2.2 jtc register int fid;
264 1.3.2.2 jtc
265 1.3.2.2 jtc if (name == NULL && (name = TZDEFAULT) == NULL)
266 1.3.2.2 jtc return -1;
267 1.3.2.2 jtc {
268 1.3.2.2 jtc register int doaccess;
269 1.3.2.2 jtc /*
270 1.3.2.2 jtc ** Section 4.9.1 of the C standard says that
271 1.3.2.2 jtc ** "FILENAME_MAX expands to an integral constant expression
272 1.3.2.2 jtc ** that is the sie needed for an array of char large enough
273 1.3.2.2 jtc ** to hold the longest file name string that the implementation
274 1.3.2.2 jtc ** guarantees can be opened."
275 1.3.2.2 jtc */
276 1.3.2.2 jtc char fullname[FILENAME_MAX + 1];
277 1.3.2.2 jtc
278 1.3.2.2 jtc if (name[0] == ':')
279 1.3.2.2 jtc ++name;
280 1.3.2.2 jtc doaccess = name[0] == '/';
281 1.3.2.2 jtc if (!doaccess) {
282 1.3.2.2 jtc if ((p = TZDIR) == NULL)
283 1.3.2.2 jtc return -1;
284 1.3.2.2 jtc if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
285 1.3.2.2 jtc return -1;
286 1.3.2.2 jtc (void) strcpy(fullname, p);
287 1.3.2.2 jtc (void) strcat(fullname, "/");
288 1.3.2.2 jtc (void) strcat(fullname, name);
289 1.3.2.2 jtc /*
290 1.3.2.2 jtc ** Set doaccess if '.' (as in "../") shows up in name.
291 1.3.2.2 jtc */
292 1.3.2.2 jtc if (strchr(name, '.') != NULL)
293 1.3.2.2 jtc doaccess = TRUE;
294 1.3.2.2 jtc name = fullname;
295 1.3.2.2 jtc }
296 1.3.2.2 jtc if (doaccess && access(name, R_OK) != 0)
297 1.3.2.2 jtc return -1;
298 1.3.2.2 jtc if ((fid = open(name, OPEN_MODE)) == -1)
299 1.3.2.2 jtc return -1;
300 1.3.2.2 jtc }
301 1.3.2.2 jtc {
302 1.3.2.2 jtc struct tzhead * tzhp;
303 1.3.2.2 jtc char buf[sizeof *sp + sizeof *tzhp];
304 1.3.2.2 jtc int ttisstdcnt;
305 1.3.2.2 jtc int ttisgmtcnt;
306 1.3.2.2 jtc
307 1.3.2.2 jtc i = read(fid, buf, sizeof buf);
308 1.3.2.2 jtc if (close(fid) != 0)
309 1.3.2.2 jtc return -1;
310 1.3.2.2 jtc p = buf;
311 1.3.2.2 jtc p += sizeof tzhp->tzh_reserved;
312 1.3.2.2 jtc ttisstdcnt = (int) detzcode(p);
313 1.3.2.2 jtc p += 4;
314 1.3.2.2 jtc ttisgmtcnt = (int) detzcode(p);
315 1.3.2.2 jtc p += 4;
316 1.3.2.2 jtc sp->leapcnt = (int) detzcode(p);
317 1.3.2.2 jtc p += 4;
318 1.3.2.2 jtc sp->timecnt = (int) detzcode(p);
319 1.3.2.2 jtc p += 4;
320 1.3.2.2 jtc sp->typecnt = (int) detzcode(p);
321 1.3.2.2 jtc p += 4;
322 1.3.2.2 jtc sp->charcnt = (int) detzcode(p);
323 1.3.2.2 jtc p += 4;
324 1.3.2.2 jtc if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
325 1.3.2.2 jtc sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
326 1.3.2.2 jtc sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
327 1.3.2.2 jtc sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
328 1.3.2.2 jtc (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
329 1.3.2.2 jtc (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
330 1.3.2.2 jtc return -1;
331 1.3.2.2 jtc if (i - (p - buf) < sp->timecnt * 4 + /* ats */
332 1.3.2.2 jtc sp->timecnt + /* types */
333 1.3.2.2 jtc sp->typecnt * (4 + 2) + /* ttinfos */
334 1.3.2.2 jtc sp->charcnt + /* chars */
335 1.3.2.2 jtc sp->leapcnt * (4 + 4) + /* lsinfos */
336 1.3.2.2 jtc ttisstdcnt + /* ttisstds */
337 1.3.2.2 jtc ttisgmtcnt) /* ttisgmts */
338 1.3.2.2 jtc return -1;
339 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
340 1.3.2.2 jtc sp->ats[i] = detzcode(p);
341 1.3.2.2 jtc p += 4;
342 1.3.2.2 jtc }
343 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
344 1.3.2.2 jtc sp->types[i] = (unsigned char) *p++;
345 1.3.2.2 jtc if (sp->types[i] >= sp->typecnt)
346 1.3.2.2 jtc return -1;
347 1.3.2.2 jtc }
348 1.3.2.2 jtc for (i = 0; i < sp->typecnt; ++i) {
349 1.3.2.2 jtc register struct ttinfo * ttisp;
350 1.3.2.2 jtc
351 1.3.2.2 jtc ttisp = &sp->ttis[i];
352 1.3.2.2 jtc ttisp->tt_gmtoff = detzcode(p);
353 1.3.2.2 jtc p += 4;
354 1.3.2.2 jtc ttisp->tt_isdst = (unsigned char) *p++;
355 1.3.2.2 jtc if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
356 1.3.2.2 jtc return -1;
357 1.3.2.2 jtc ttisp->tt_abbrind = (unsigned char) *p++;
358 1.3.2.2 jtc if (ttisp->tt_abbrind < 0 ||
359 1.3.2.2 jtc ttisp->tt_abbrind > sp->charcnt)
360 1.3.2.2 jtc return -1;
361 1.3.2.2 jtc }
362 1.3.2.2 jtc for (i = 0; i < sp->charcnt; ++i)
363 1.3.2.2 jtc sp->chars[i] = *p++;
364 1.3.2.2 jtc sp->chars[i] = '\0'; /* ensure '\0' at end */
365 1.3.2.2 jtc for (i = 0; i < sp->leapcnt; ++i) {
366 1.3.2.2 jtc register struct lsinfo * lsisp;
367 1.3.2.2 jtc
368 1.3.2.2 jtc lsisp = &sp->lsis[i];
369 1.3.2.2 jtc lsisp->ls_trans = detzcode(p);
370 1.3.2.2 jtc p += 4;
371 1.3.2.2 jtc lsisp->ls_corr = detzcode(p);
372 1.3.2.2 jtc p += 4;
373 1.3.2.2 jtc }
374 1.3.2.2 jtc for (i = 0; i < sp->typecnt; ++i) {
375 1.3.2.2 jtc register struct ttinfo * ttisp;
376 1.3.2.2 jtc
377 1.3.2.2 jtc ttisp = &sp->ttis[i];
378 1.3.2.2 jtc if (ttisstdcnt == 0)
379 1.3.2.2 jtc ttisp->tt_ttisstd = FALSE;
380 1.3.2.2 jtc else {
381 1.3.2.2 jtc ttisp->tt_ttisstd = *p++;
382 1.3.2.2 jtc if (ttisp->tt_ttisstd != TRUE &&
383 1.3.2.2 jtc ttisp->tt_ttisstd != FALSE)
384 1.3.2.2 jtc return -1;
385 1.3.2.2 jtc }
386 1.3.2.2 jtc }
387 1.3.2.2 jtc for (i = 0; i < sp->typecnt; ++i) {
388 1.3.2.2 jtc register struct ttinfo * ttisp;
389 1.3.2.2 jtc
390 1.3.2.2 jtc ttisp = &sp->ttis[i];
391 1.3.2.2 jtc if (ttisgmtcnt == 0)
392 1.3.2.2 jtc ttisp->tt_ttisgmt = FALSE;
393 1.3.2.2 jtc else {
394 1.3.2.2 jtc ttisp->tt_ttisgmt = *p++;
395 1.3.2.2 jtc if (ttisp->tt_ttisgmt != TRUE &&
396 1.3.2.2 jtc ttisp->tt_ttisgmt != FALSE)
397 1.3.2.2 jtc return -1;
398 1.3.2.2 jtc }
399 1.3.2.2 jtc }
400 1.3.2.2 jtc }
401 1.3.2.2 jtc return 0;
402 1.3.2.2 jtc }
403 1.3.2.2 jtc
404 1.3.2.2 jtc static const int mon_lengths[2][MONSPERYEAR] = {
405 1.3.2.2 jtc { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
406 1.3.2.2 jtc { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
407 1.3.2.2 jtc };
408 1.3.2.2 jtc
409 1.3.2.2 jtc static const int year_lengths[2] = {
410 1.3.2.2 jtc DAYSPERNYEAR, DAYSPERLYEAR
411 1.3.2.2 jtc };
412 1.3.2.2 jtc
413 1.3.2.2 jtc /*
414 1.3.2.2 jtc ** Given a pointer into a time zone string, scan until a character that is not
415 1.3.2.2 jtc ** a valid character in a zone name is found. Return a pointer to that
416 1.3.2.2 jtc ** character.
417 1.3.2.2 jtc */
418 1.3.2.2 jtc
419 1.3.2.2 jtc static const char *
420 1.3.2.2 jtc getzname(strp)
421 1.3.2.2 jtc register const char * strp;
422 1.3.2.2 jtc {
423 1.3.2.2 jtc register char c;
424 1.3.2.2 jtc
425 1.3.2.2 jtc while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
426 1.3.2.2 jtc c != '+')
427 1.3.2.2 jtc ++strp;
428 1.3.2.2 jtc return strp;
429 1.3.2.2 jtc }
430 1.3.2.2 jtc
431 1.3.2.2 jtc /*
432 1.3.2.2 jtc ** Given a pointer into a time zone string, extract a number from that string.
433 1.3.2.2 jtc ** Check that the number is within a specified range; if it is not, return
434 1.3.2.2 jtc ** NULL.
435 1.3.2.2 jtc ** Otherwise, return a pointer to the first character not part of the number.
436 1.3.2.2 jtc */
437 1.3.2.2 jtc
438 1.3.2.2 jtc static const char *
439 1.3.2.2 jtc getnum(strp, nump, min, max)
440 1.3.2.2 jtc register const char * strp;
441 1.3.2.2 jtc int * const nump;
442 1.3.2.2 jtc const int min;
443 1.3.2.2 jtc const int max;
444 1.3.2.2 jtc {
445 1.3.2.2 jtc register char c;
446 1.3.2.2 jtc register int num;
447 1.3.2.2 jtc
448 1.3.2.2 jtc if (strp == NULL || !isdigit(*strp))
449 1.3.2.2 jtc return NULL;
450 1.3.2.2 jtc num = 0;
451 1.3.2.2 jtc while ((c = *strp) != '\0' && isdigit(c)) {
452 1.3.2.2 jtc num = num * 10 + (c - '0');
453 1.3.2.2 jtc if (num > max)
454 1.3.2.2 jtc return NULL; /* illegal value */
455 1.3.2.2 jtc ++strp;
456 1.3.2.2 jtc }
457 1.3.2.2 jtc if (num < min)
458 1.3.2.2 jtc return NULL; /* illegal value */
459 1.3.2.2 jtc *nump = num;
460 1.3.2.2 jtc return strp;
461 1.3.2.2 jtc }
462 1.3.2.2 jtc
463 1.3.2.2 jtc /*
464 1.3.2.2 jtc ** Given a pointer into a time zone string, extract a number of seconds,
465 1.3.2.2 jtc ** in hh[:mm[:ss]] form, from the string.
466 1.3.2.2 jtc ** If any error occurs, return NULL.
467 1.3.2.2 jtc ** Otherwise, return a pointer to the first character not part of the number
468 1.3.2.2 jtc ** of seconds.
469 1.3.2.2 jtc */
470 1.3.2.2 jtc
471 1.3.2.2 jtc static const char *
472 1.3.2.2 jtc getsecs(strp, secsp)
473 1.3.2.2 jtc register const char * strp;
474 1.3.2.2 jtc long * const secsp;
475 1.3.2.2 jtc {
476 1.3.2.2 jtc int num;
477 1.3.2.2 jtc
478 1.3.2.2 jtc /*
479 1.3.2.2 jtc ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
480 1.3.2.2 jtc ** "M10.4.6/26", which does not conform to Posix,
481 1.3.2.2 jtc ** but which specifies the equivalent of
482 1.3.2.2 jtc ** ``02:00 on the first Sunday on or after 23 Oct''.
483 1.3.2.2 jtc */
484 1.3.2.2 jtc strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
485 1.3.2.2 jtc if (strp == NULL)
486 1.3.2.2 jtc return NULL;
487 1.3.2.2 jtc *secsp = num * (long) SECSPERHOUR;
488 1.3.2.2 jtc if (*strp == ':') {
489 1.3.2.2 jtc ++strp;
490 1.3.2.2 jtc strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
491 1.3.2.2 jtc if (strp == NULL)
492 1.3.2.2 jtc return NULL;
493 1.3.2.2 jtc *secsp += num * SECSPERMIN;
494 1.3.2.2 jtc if (*strp == ':') {
495 1.3.2.2 jtc ++strp;
496 1.3.2.2 jtc /* `SECSPERMIN' allows for leap seconds. */
497 1.3.2.2 jtc strp = getnum(strp, &num, 0, SECSPERMIN);
498 1.3.2.2 jtc if (strp == NULL)
499 1.3.2.2 jtc return NULL;
500 1.3.2.2 jtc *secsp += num;
501 1.3.2.2 jtc }
502 1.3.2.2 jtc }
503 1.3.2.2 jtc return strp;
504 1.3.2.2 jtc }
505 1.3.2.2 jtc
506 1.3.2.2 jtc /*
507 1.3.2.2 jtc ** Given a pointer into a time zone string, extract an offset, in
508 1.3.2.2 jtc ** [+-]hh[:mm[:ss]] form, from the string.
509 1.3.2.2 jtc ** If any error occurs, return NULL.
510 1.3.2.2 jtc ** Otherwise, return a pointer to the first character not part of the time.
511 1.3.2.2 jtc */
512 1.3.2.2 jtc
513 1.3.2.2 jtc static const char *
514 1.3.2.2 jtc getoffset(strp, offsetp)
515 1.3.2.2 jtc register const char * strp;
516 1.3.2.2 jtc long * const offsetp;
517 1.3.2.2 jtc {
518 1.3.2.2 jtc register int neg;
519 1.3.2.2 jtc
520 1.3.2.2 jtc if (*strp == '-') {
521 1.3.2.2 jtc neg = 1;
522 1.3.2.2 jtc ++strp;
523 1.3.2.2 jtc } else if (isdigit(*strp) || *strp++ == '+')
524 1.3.2.2 jtc neg = 0;
525 1.3.2.2 jtc else return NULL; /* illegal offset */
526 1.3.2.2 jtc strp = getsecs(strp, offsetp);
527 1.3.2.2 jtc if (strp == NULL)
528 1.3.2.2 jtc return NULL; /* illegal time */
529 1.3.2.2 jtc if (neg)
530 1.3.2.2 jtc *offsetp = -*offsetp;
531 1.3.2.2 jtc return strp;
532 1.3.2.2 jtc }
533 1.3.2.2 jtc
534 1.3.2.2 jtc /*
535 1.3.2.2 jtc ** Given a pointer into a time zone string, extract a rule in the form
536 1.3.2.2 jtc ** date[/time]. See POSIX section 8 for the format of "date" and "time".
537 1.3.2.2 jtc ** If a valid rule is not found, return NULL.
538 1.3.2.2 jtc ** Otherwise, return a pointer to the first character not part of the rule.
539 1.3.2.2 jtc */
540 1.3.2.2 jtc
541 1.3.2.2 jtc static const char *
542 1.3.2.2 jtc getrule(strp, rulep)
543 1.3.2.2 jtc const char * strp;
544 1.3.2.2 jtc register struct rule * const rulep;
545 1.3.2.2 jtc {
546 1.3.2.2 jtc if (*strp == 'J') {
547 1.3.2.2 jtc /*
548 1.3.2.2 jtc ** Julian day.
549 1.3.2.2 jtc */
550 1.3.2.2 jtc rulep->r_type = JULIAN_DAY;
551 1.3.2.2 jtc ++strp;
552 1.3.2.2 jtc strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
553 1.3.2.2 jtc } else if (*strp == 'M') {
554 1.3.2.2 jtc /*
555 1.3.2.2 jtc ** Month, week, day.
556 1.3.2.2 jtc */
557 1.3.2.2 jtc rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
558 1.3.2.2 jtc ++strp;
559 1.3.2.2 jtc strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
560 1.3.2.2 jtc if (strp == NULL)
561 1.3.2.2 jtc return NULL;
562 1.3.2.2 jtc if (*strp++ != '.')
563 1.3.2.2 jtc return NULL;
564 1.3.2.2 jtc strp = getnum(strp, &rulep->r_week, 1, 5);
565 1.3.2.2 jtc if (strp == NULL)
566 1.3.2.2 jtc return NULL;
567 1.3.2.2 jtc if (*strp++ != '.')
568 1.3.2.2 jtc return NULL;
569 1.3.2.2 jtc strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
570 1.3.2.2 jtc } else if (isdigit(*strp)) {
571 1.3.2.2 jtc /*
572 1.3.2.2 jtc ** Day of year.
573 1.3.2.2 jtc */
574 1.3.2.2 jtc rulep->r_type = DAY_OF_YEAR;
575 1.3.2.2 jtc strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
576 1.3.2.2 jtc } else return NULL; /* invalid format */
577 1.3.2.2 jtc if (strp == NULL)
578 1.3.2.2 jtc return NULL;
579 1.3.2.2 jtc if (*strp == '/') {
580 1.3.2.2 jtc /*
581 1.3.2.2 jtc ** Time specified.
582 1.3.2.2 jtc */
583 1.3.2.2 jtc ++strp;
584 1.3.2.2 jtc strp = getsecs(strp, &rulep->r_time);
585 1.3.2.2 jtc } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
586 1.3.2.2 jtc return strp;
587 1.3.2.2 jtc }
588 1.3.2.2 jtc
589 1.3.2.2 jtc /*
590 1.3.2.2 jtc ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
591 1.3.2.2 jtc ** year, a rule, and the offset from GMT at the time that rule takes effect,
592 1.3.2.2 jtc ** calculate the Epoch-relative time that rule takes effect.
593 1.3.2.2 jtc */
594 1.3.2.2 jtc
595 1.3.2.2 jtc static time_t
596 1.3.2.2 jtc transtime(janfirst, year, rulep, offset)
597 1.3.2.2 jtc const time_t janfirst;
598 1.3.2.2 jtc const int year;
599 1.3.2.2 jtc register const struct rule * const rulep;
600 1.3.2.2 jtc const long offset;
601 1.3.2.2 jtc {
602 1.3.2.2 jtc register int leapyear;
603 1.3.2.2 jtc register time_t value;
604 1.3.2.2 jtc register int i;
605 1.3.2.2 jtc int d, m1, yy0, yy1, yy2, dow;
606 1.3.2.2 jtc
607 1.3.2.2 jtc INITIALIZE(value);
608 1.3.2.2 jtc leapyear = isleap(year);
609 1.3.2.2 jtc switch (rulep->r_type) {
610 1.3.2.2 jtc
611 1.3.2.2 jtc case JULIAN_DAY:
612 1.3.2.2 jtc /*
613 1.3.2.2 jtc ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
614 1.3.2.2 jtc ** years.
615 1.3.2.2 jtc ** In non-leap years, or if the day number is 59 or less, just
616 1.3.2.2 jtc ** add SECSPERDAY times the day number-1 to the time of
617 1.3.2.2 jtc ** January 1, midnight, to get the day.
618 1.3.2.2 jtc */
619 1.3.2.2 jtc value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
620 1.3.2.2 jtc if (leapyear && rulep->r_day >= 60)
621 1.3.2.2 jtc value += SECSPERDAY;
622 1.3.2.2 jtc break;
623 1.3.2.2 jtc
624 1.3.2.2 jtc case DAY_OF_YEAR:
625 1.3.2.2 jtc /*
626 1.3.2.2 jtc ** n - day of year.
627 1.3.2.2 jtc ** Just add SECSPERDAY times the day number to the time of
628 1.3.2.2 jtc ** January 1, midnight, to get the day.
629 1.3.2.2 jtc */
630 1.3.2.2 jtc value = janfirst + rulep->r_day * SECSPERDAY;
631 1.3.2.2 jtc break;
632 1.3.2.2 jtc
633 1.3.2.2 jtc case MONTH_NTH_DAY_OF_WEEK:
634 1.3.2.2 jtc /*
635 1.3.2.2 jtc ** Mm.n.d - nth "dth day" of month m.
636 1.3.2.2 jtc */
637 1.3.2.2 jtc value = janfirst;
638 1.3.2.2 jtc for (i = 0; i < rulep->r_mon - 1; ++i)
639 1.3.2.2 jtc value += mon_lengths[leapyear][i] * SECSPERDAY;
640 1.3.2.2 jtc
641 1.3.2.2 jtc /*
642 1.3.2.2 jtc ** Use Zeller's Congruence to get day-of-week of first day of
643 1.3.2.2 jtc ** month.
644 1.3.2.2 jtc */
645 1.3.2.2 jtc m1 = (rulep->r_mon + 9) % 12 + 1;
646 1.3.2.2 jtc yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
647 1.3.2.2 jtc yy1 = yy0 / 100;
648 1.3.2.2 jtc yy2 = yy0 % 100;
649 1.3.2.2 jtc dow = ((26 * m1 - 2) / 10 +
650 1.3.2.2 jtc 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
651 1.3.2.2 jtc if (dow < 0)
652 1.3.2.2 jtc dow += DAYSPERWEEK;
653 1.3.2.2 jtc
654 1.3.2.2 jtc /*
655 1.3.2.2 jtc ** "dow" is the day-of-week of the first day of the month. Get
656 1.3.2.2 jtc ** the day-of-month (zero-origin) of the first "dow" day of the
657 1.3.2.2 jtc ** month.
658 1.3.2.2 jtc */
659 1.3.2.2 jtc d = rulep->r_day - dow;
660 1.3.2.2 jtc if (d < 0)
661 1.3.2.2 jtc d += DAYSPERWEEK;
662 1.3.2.2 jtc for (i = 1; i < rulep->r_week; ++i) {
663 1.3.2.2 jtc if (d + DAYSPERWEEK >=
664 1.3.2.2 jtc mon_lengths[leapyear][rulep->r_mon - 1])
665 1.3.2.2 jtc break;
666 1.3.2.2 jtc d += DAYSPERWEEK;
667 1.3.2.2 jtc }
668 1.3.2.2 jtc
669 1.3.2.2 jtc /*
670 1.3.2.2 jtc ** "d" is the day-of-month (zero-origin) of the day we want.
671 1.3.2.2 jtc */
672 1.3.2.2 jtc value += d * SECSPERDAY;
673 1.3.2.2 jtc break;
674 1.3.2.2 jtc }
675 1.3.2.2 jtc
676 1.3.2.2 jtc /*
677 1.3.2.2 jtc ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
678 1.3.2.2 jtc ** question. To get the Epoch-relative time of the specified local
679 1.3.2.2 jtc ** time on that day, add the transition time and the current offset
680 1.3.2.2 jtc ** from GMT.
681 1.3.2.2 jtc */
682 1.3.2.2 jtc return value + rulep->r_time + offset;
683 1.3.2.2 jtc }
684 1.3.2.2 jtc
685 1.3.2.2 jtc /*
686 1.3.2.2 jtc ** Given a POSIX section 8-style TZ string, fill in the rule tables as
687 1.3.2.2 jtc ** appropriate.
688 1.3.2.2 jtc */
689 1.3.2.2 jtc
690 1.3.2.2 jtc static int
691 1.3.2.2 jtc tzparse(name, sp, lastditch)
692 1.3.2.2 jtc const char * name;
693 1.3.2.2 jtc register struct state * const sp;
694 1.3.2.2 jtc const int lastditch;
695 1.3.2.2 jtc {
696 1.3.2.2 jtc const char * stdname;
697 1.3.2.2 jtc const char * dstname;
698 1.3.2.2 jtc size_t stdlen;
699 1.3.2.2 jtc size_t dstlen;
700 1.3.2.2 jtc long stdoffset;
701 1.3.2.2 jtc long dstoffset;
702 1.3.2.2 jtc register time_t * atp;
703 1.3.2.2 jtc register unsigned char * typep;
704 1.3.2.2 jtc register char * cp;
705 1.3.2.2 jtc register int load_result;
706 1.3.2.2 jtc
707 1.3.2.2 jtc INITIALIZE(dstname);
708 1.3.2.2 jtc stdname = name;
709 1.3.2.2 jtc if (lastditch) {
710 1.3.2.2 jtc stdlen = strlen(name); /* length of standard zone name */
711 1.3.2.2 jtc name += stdlen;
712 1.3.2.2 jtc if (stdlen >= sizeof sp->chars)
713 1.3.2.2 jtc stdlen = (sizeof sp->chars) - 1;
714 1.3.2.2 jtc } else {
715 1.3.2.2 jtc name = getzname(name);
716 1.3.2.2 jtc stdlen = name - stdname;
717 1.3.2.2 jtc if (stdlen < 3)
718 1.3.2.2 jtc return -1;
719 1.3.2.2 jtc }
720 1.3.2.2 jtc if (*name == '\0')
721 1.3.2.2 jtc return -1; /* was "stdoffset = 0;" */
722 1.3.2.2 jtc else {
723 1.3.2.2 jtc name = getoffset(name, &stdoffset);
724 1.3.2.2 jtc if (name == NULL)
725 1.3.2.2 jtc return -1;
726 1.3.2.2 jtc }
727 1.3.2.2 jtc load_result = tzload(TZDEFRULES, sp);
728 1.3.2.2 jtc if (load_result != 0)
729 1.3.2.2 jtc sp->leapcnt = 0; /* so, we're off a little */
730 1.3.2.2 jtc if (*name != '\0') {
731 1.3.2.2 jtc dstname = name;
732 1.3.2.2 jtc name = getzname(name);
733 1.3.2.2 jtc dstlen = name - dstname; /* length of DST zone name */
734 1.3.2.2 jtc if (dstlen < 3)
735 1.3.2.2 jtc return -1;
736 1.3.2.2 jtc if (*name != '\0' && *name != ',' && *name != ';') {
737 1.3.2.2 jtc name = getoffset(name, &dstoffset);
738 1.3.2.2 jtc if (name == NULL)
739 1.3.2.2 jtc return -1;
740 1.3.2.2 jtc } else dstoffset = stdoffset - SECSPERHOUR;
741 1.3.2.2 jtc if (*name == ',' || *name == ';') {
742 1.3.2.2 jtc struct rule start;
743 1.3.2.2 jtc struct rule end;
744 1.3.2.2 jtc register int year;
745 1.3.2.2 jtc register time_t janfirst;
746 1.3.2.2 jtc time_t starttime;
747 1.3.2.2 jtc time_t endtime;
748 1.3.2.2 jtc
749 1.3.2.2 jtc ++name;
750 1.3.2.2 jtc if ((name = getrule(name, &start)) == NULL)
751 1.3.2.2 jtc return -1;
752 1.3.2.2 jtc if (*name++ != ',')
753 1.3.2.2 jtc return -1;
754 1.3.2.2 jtc if ((name = getrule(name, &end)) == NULL)
755 1.3.2.2 jtc return -1;
756 1.3.2.2 jtc if (*name != '\0')
757 1.3.2.2 jtc return -1;
758 1.3.2.2 jtc sp->typecnt = 2; /* standard time and DST */
759 1.3.2.2 jtc /*
760 1.3.2.2 jtc ** Two transitions per year, from EPOCH_YEAR to 2037.
761 1.3.2.2 jtc */
762 1.3.2.2 jtc sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
763 1.3.2.2 jtc if (sp->timecnt > TZ_MAX_TIMES)
764 1.3.2.2 jtc return -1;
765 1.3.2.2 jtc sp->ttis[0].tt_gmtoff = -dstoffset;
766 1.3.2.2 jtc sp->ttis[0].tt_isdst = 1;
767 1.3.2.2 jtc sp->ttis[0].tt_abbrind = stdlen + 1;
768 1.3.2.2 jtc sp->ttis[1].tt_gmtoff = -stdoffset;
769 1.3.2.2 jtc sp->ttis[1].tt_isdst = 0;
770 1.3.2.2 jtc sp->ttis[1].tt_abbrind = 0;
771 1.3.2.2 jtc atp = sp->ats;
772 1.3.2.2 jtc typep = sp->types;
773 1.3.2.2 jtc janfirst = 0;
774 1.3.2.2 jtc for (year = EPOCH_YEAR; year <= 2037; ++year) {
775 1.3.2.2 jtc starttime = transtime(janfirst, year, &start,
776 1.3.2.2 jtc stdoffset);
777 1.3.2.2 jtc endtime = transtime(janfirst, year, &end,
778 1.3.2.2 jtc dstoffset);
779 1.3.2.2 jtc if (starttime > endtime) {
780 1.3.2.2 jtc *atp++ = endtime;
781 1.3.2.2 jtc *typep++ = 1; /* DST ends */
782 1.3.2.2 jtc *atp++ = starttime;
783 1.3.2.2 jtc *typep++ = 0; /* DST begins */
784 1.3.2.2 jtc } else {
785 1.3.2.2 jtc *atp++ = starttime;
786 1.3.2.2 jtc *typep++ = 0; /* DST begins */
787 1.3.2.2 jtc *atp++ = endtime;
788 1.3.2.2 jtc *typep++ = 1; /* DST ends */
789 1.3.2.2 jtc }
790 1.3.2.2 jtc janfirst += year_lengths[isleap(year)] *
791 1.3.2.2 jtc SECSPERDAY;
792 1.3.2.2 jtc }
793 1.3.2.2 jtc } else {
794 1.3.2.2 jtc register long theirstdoffset;
795 1.3.2.2 jtc register long theirdstoffset;
796 1.3.2.2 jtc register long theiroffset;
797 1.3.2.2 jtc register int isdst;
798 1.3.2.2 jtc register int i;
799 1.3.2.2 jtc register int j;
800 1.3.2.2 jtc
801 1.3.2.2 jtc if (*name != '\0')
802 1.3.2.2 jtc return -1;
803 1.3.2.2 jtc if (load_result != 0)
804 1.3.2.2 jtc return -1;
805 1.3.2.2 jtc /*
806 1.3.2.2 jtc ** Initial values of theirstdoffset and theirdstoffset.
807 1.3.2.2 jtc */
808 1.3.2.2 jtc theirstdoffset = 0;
809 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
810 1.3.2.2 jtc j = sp->types[i];
811 1.3.2.2 jtc if (!sp->ttis[j].tt_isdst) {
812 1.3.2.2 jtc theirstdoffset = -sp->ttis[j].tt_gmtoff;
813 1.3.2.2 jtc break;
814 1.3.2.2 jtc }
815 1.3.2.2 jtc }
816 1.3.2.2 jtc theirdstoffset = 0;
817 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
818 1.3.2.2 jtc j = sp->types[i];
819 1.3.2.2 jtc if (sp->ttis[j].tt_isdst) {
820 1.3.2.2 jtc theirdstoffset = -sp->ttis[j].tt_gmtoff;
821 1.3.2.2 jtc break;
822 1.3.2.2 jtc }
823 1.3.2.2 jtc }
824 1.3.2.2 jtc /*
825 1.3.2.2 jtc ** Initially we're assumed to be in standard time.
826 1.3.2.2 jtc */
827 1.3.2.2 jtc isdst = FALSE;
828 1.3.2.2 jtc theiroffset = theirstdoffset;
829 1.3.2.2 jtc /*
830 1.3.2.2 jtc ** Now juggle transition times and types
831 1.3.2.2 jtc ** tracking offsets as you do.
832 1.3.2.2 jtc */
833 1.3.2.2 jtc for (i = 0; i < sp->timecnt; ++i) {
834 1.3.2.2 jtc j = sp->types[i];
835 1.3.2.2 jtc sp->types[i] = sp->ttis[j].tt_isdst;
836 1.3.2.2 jtc if (sp->ttis[j].tt_ttisgmt) {
837 1.3.2.2 jtc /* No adjustment to transition time */
838 1.3.2.2 jtc } else {
839 1.3.2.2 jtc /*
840 1.3.2.2 jtc ** If summer time is in effect, and the
841 1.3.2.2 jtc ** transition time was not specified as
842 1.3.2.2 jtc ** standard time, add the summer time
843 1.3.2.2 jtc ** offset to the transition time;
844 1.3.2.2 jtc ** otherwise, add the standard time
845 1.3.2.2 jtc ** offset to the transition time.
846 1.3.2.2 jtc */
847 1.3.2.2 jtc /*
848 1.3.2.2 jtc ** Transitions from DST to DDST
849 1.3.2.2 jtc ** will effectively disappear since
850 1.3.2.2 jtc ** POSIX provides for only one DST
851 1.3.2.2 jtc ** offset.
852 1.3.2.2 jtc */
853 1.3.2.2 jtc if (isdst && !sp->ttis[j].tt_ttisstd) {
854 1.3.2.2 jtc sp->ats[i] += dstoffset -
855 1.3.2.2 jtc theirdstoffset;
856 1.3.2.2 jtc } else {
857 1.3.2.2 jtc sp->ats[i] += stdoffset -
858 1.3.2.2 jtc theirstdoffset;
859 1.3.2.2 jtc }
860 1.3.2.2 jtc }
861 1.3.2.2 jtc theiroffset = -sp->ttis[j].tt_gmtoff;
862 1.3.2.2 jtc if (sp->ttis[j].tt_isdst)
863 1.3.2.2 jtc theirdstoffset = theiroffset;
864 1.3.2.2 jtc else theirstdoffset = theiroffset;
865 1.3.2.2 jtc }
866 1.3.2.2 jtc /*
867 1.3.2.2 jtc ** Finally, fill in ttis.
868 1.3.2.2 jtc ** ttisstd and ttisgmt need not be handled.
869 1.3.2.2 jtc */
870 1.3.2.2 jtc sp->ttis[0].tt_gmtoff = -stdoffset;
871 1.3.2.2 jtc sp->ttis[0].tt_isdst = FALSE;
872 1.3.2.2 jtc sp->ttis[0].tt_abbrind = 0;
873 1.3.2.2 jtc sp->ttis[1].tt_gmtoff = -dstoffset;
874 1.3.2.2 jtc sp->ttis[1].tt_isdst = TRUE;
875 1.3.2.2 jtc sp->ttis[1].tt_abbrind = stdlen + 1;
876 1.3.2.2 jtc }
877 1.3.2.2 jtc } else {
878 1.3.2.2 jtc dstlen = 0;
879 1.3.2.2 jtc sp->typecnt = 1; /* only standard time */
880 1.3.2.2 jtc sp->timecnt = 0;
881 1.3.2.2 jtc sp->ttis[0].tt_gmtoff = -stdoffset;
882 1.3.2.2 jtc sp->ttis[0].tt_isdst = 0;
883 1.3.2.2 jtc sp->ttis[0].tt_abbrind = 0;
884 1.3.2.2 jtc }
885 1.3.2.2 jtc sp->charcnt = stdlen + 1;
886 1.3.2.2 jtc if (dstlen != 0)
887 1.3.2.2 jtc sp->charcnt += dstlen + 1;
888 1.3.2.2 jtc if (sp->charcnt > sizeof sp->chars)
889 1.3.2.2 jtc return -1;
890 1.3.2.2 jtc cp = sp->chars;
891 1.3.2.2 jtc (void) strncpy(cp, stdname, stdlen);
892 1.3.2.2 jtc cp += stdlen;
893 1.3.2.2 jtc *cp++ = '\0';
894 1.3.2.2 jtc if (dstlen != 0) {
895 1.3.2.2 jtc (void) strncpy(cp, dstname, dstlen);
896 1.3.2.2 jtc *(cp + dstlen) = '\0';
897 1.3.2.2 jtc }
898 1.3.2.2 jtc return 0;
899 1.3.2.2 jtc }
900 1.3.2.2 jtc
901 1.3.2.2 jtc static void
902 1.3.2.2 jtc gmtload(sp)
903 1.3.2.2 jtc struct state * const sp;
904 1.3.2.2 jtc {
905 1.3.2.2 jtc if (tzload(gmt, sp) != 0)
906 1.3.2.2 jtc (void) tzparse(gmt, sp, TRUE);
907 1.3.2.2 jtc }
908 1.3.2.2 jtc
909 1.3.2.2 jtc #ifndef STD_INSPIRED
910 1.3.2.2 jtc /*
911 1.3.2.2 jtc ** A non-static declaration of tzsetwall in a system header file
912 1.3.2.2 jtc ** may cause a warning about this upcoming static declaration...
913 1.3.2.2 jtc */
914 1.3.2.2 jtc static
915 1.3.2.2 jtc #endif /* !defined STD_INSPIRED */
916 1.3.2.2 jtc void
917 1.3.2.2 jtc tzsetwall P((void))
918 1.3.2.2 jtc {
919 1.3.2.2 jtc if (lcl_is_set < 0)
920 1.3.2.2 jtc return;
921 1.3.2.2 jtc lcl_is_set = -1;
922 1.3.2.2 jtc
923 1.3.2.2 jtc #ifdef ALL_STATE
924 1.3.2.2 jtc if (lclptr == NULL) {
925 1.3.2.2 jtc lclptr = (struct state *) malloc(sizeof *lclptr);
926 1.3.2.2 jtc if (lclptr == NULL) {
927 1.3.2.2 jtc settzname(); /* all we can do */
928 1.3.2.2 jtc return;
929 1.3.2.2 jtc }
930 1.3.2.2 jtc }
931 1.3.2.2 jtc #endif /* defined ALL_STATE */
932 1.3.2.2 jtc if (tzload((char *) NULL, lclptr) != 0)
933 1.3.2.2 jtc gmtload(lclptr);
934 1.3.2.2 jtc settzname();
935 1.3.2.2 jtc }
936 1.3.2.3 jtc #pragma weak tzsetwall=_tzsetwall
937 1.3.2.2 jtc
938 1.3.2.2 jtc void
939 1.3.2.2 jtc tzset P((void))
940 1.3.2.2 jtc {
941 1.3.2.2 jtc register const char * name;
942 1.3.2.2 jtc
943 1.3.2.2 jtc name = getenv("TZ");
944 1.3.2.2 jtc if (name == NULL) {
945 1.3.2.2 jtc tzsetwall();
946 1.3.2.2 jtc return;
947 1.3.2.2 jtc }
948 1.3.2.2 jtc
949 1.3.2.2 jtc if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
950 1.3.2.2 jtc return;
951 1.3.2.2 jtc lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
952 1.3.2.2 jtc if (lcl_is_set)
953 1.3.2.2 jtc (void) strcpy(lcl_TZname, name);
954 1.3.2.2 jtc
955 1.3.2.2 jtc #ifdef ALL_STATE
956 1.3.2.2 jtc if (lclptr == NULL) {
957 1.3.2.2 jtc lclptr = (struct state *) malloc(sizeof *lclptr);
958 1.3.2.2 jtc if (lclptr == NULL) {
959 1.3.2.2 jtc settzname(); /* all we can do */
960 1.3.2.2 jtc return;
961 1.3.2.2 jtc }
962 1.3.2.2 jtc }
963 1.3.2.2 jtc #endif /* defined ALL_STATE */
964 1.3.2.2 jtc if (*name == '\0') {
965 1.3.2.2 jtc /*
966 1.3.2.2 jtc ** User wants it fast rather than right.
967 1.3.2.2 jtc */
968 1.3.2.2 jtc lclptr->leapcnt = 0; /* so, we're off a little */
969 1.3.2.2 jtc lclptr->timecnt = 0;
970 1.3.2.2 jtc lclptr->ttis[0].tt_gmtoff = 0;
971 1.3.2.2 jtc lclptr->ttis[0].tt_abbrind = 0;
972 1.3.2.2 jtc (void) strcpy(lclptr->chars, gmt);
973 1.3.2.2 jtc } else if (tzload(name, lclptr) != 0)
974 1.3.2.2 jtc if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
975 1.3.2.2 jtc (void) gmtload(lclptr);
976 1.3.2.2 jtc settzname();
977 1.3.2.2 jtc }
978 1.3.2.2 jtc
979 1.3.2.2 jtc /*
980 1.3.2.2 jtc ** The easy way to behave "as if no library function calls" localtime
981 1.3.2.2 jtc ** is to not call it--so we drop its guts into "localsub", which can be
982 1.3.2.2 jtc ** freely called. (And no, the PANS doesn't require the above behavior--
983 1.3.2.2 jtc ** but it *is* desirable.)
984 1.3.2.2 jtc **
985 1.3.2.2 jtc ** The unused offset argument is for the benefit of mktime variants.
986 1.3.2.2 jtc */
987 1.3.2.2 jtc
988 1.3.2.2 jtc /*ARGSUSED*/
989 1.3.2.2 jtc static void
990 1.3.2.2 jtc localsub(timep, offset, tmp)
991 1.3.2.2 jtc const time_t * const timep;
992 1.3.2.2 jtc const long offset;
993 1.3.2.2 jtc struct tm * const tmp;
994 1.3.2.2 jtc {
995 1.3.2.2 jtc register struct state * sp;
996 1.3.2.2 jtc register const struct ttinfo * ttisp;
997 1.3.2.2 jtc register int i;
998 1.3.2.2 jtc const time_t t = *timep;
999 1.3.2.2 jtc
1000 1.3.2.2 jtc sp = lclptr;
1001 1.3.2.2 jtc #ifdef ALL_STATE
1002 1.3.2.2 jtc if (sp == NULL) {
1003 1.3.2.2 jtc gmtsub(timep, offset, tmp);
1004 1.3.2.2 jtc return;
1005 1.3.2.2 jtc }
1006 1.3.2.2 jtc #endif /* defined ALL_STATE */
1007 1.3.2.2 jtc if (sp->timecnt == 0 || t < sp->ats[0]) {
1008 1.3.2.2 jtc i = 0;
1009 1.3.2.2 jtc while (sp->ttis[i].tt_isdst)
1010 1.3.2.2 jtc if (++i >= sp->typecnt) {
1011 1.3.2.2 jtc i = 0;
1012 1.3.2.2 jtc break;
1013 1.3.2.2 jtc }
1014 1.3.2.2 jtc } else {
1015 1.3.2.2 jtc for (i = 1; i < sp->timecnt; ++i)
1016 1.3.2.2 jtc if (t < sp->ats[i])
1017 1.3.2.2 jtc break;
1018 1.3.2.2 jtc i = sp->types[i - 1];
1019 1.3.2.2 jtc }
1020 1.3.2.2 jtc ttisp = &sp->ttis[i];
1021 1.3.2.2 jtc /*
1022 1.3.2.2 jtc ** To get (wrong) behavior that's compatible with System V Release 2.0
1023 1.3.2.2 jtc ** you'd replace the statement below with
1024 1.3.2.2 jtc ** t += ttisp->tt_gmtoff;
1025 1.3.2.2 jtc ** timesub(&t, 0L, sp, tmp);
1026 1.3.2.2 jtc */
1027 1.3.2.2 jtc timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1028 1.3.2.2 jtc tmp->tm_isdst = ttisp->tt_isdst;
1029 1.3.2.2 jtc tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1030 1.3.2.2 jtc #ifdef TM_ZONE
1031 1.3.2.2 jtc tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1032 1.3.2.2 jtc #endif /* defined TM_ZONE */
1033 1.3.2.2 jtc }
1034 1.3.2.2 jtc
1035 1.3.2.2 jtc struct tm *
1036 1.3.2.2 jtc localtime(timep)
1037 1.3.2.2 jtc const time_t * const timep;
1038 1.3.2.2 jtc {
1039 1.3.2.2 jtc tzset();
1040 1.3.2.2 jtc localsub(timep, 0L, &tm);
1041 1.3.2.2 jtc return &tm;
1042 1.3.2.2 jtc }
1043 1.3.2.2 jtc
1044 1.3.2.2 jtc /*
1045 1.3.2.2 jtc ** gmtsub is to gmtime as localsub is to localtime.
1046 1.3.2.2 jtc */
1047 1.3.2.2 jtc
1048 1.3.2.2 jtc static void
1049 1.3.2.2 jtc gmtsub(timep, offset, tmp)
1050 1.3.2.2 jtc const time_t * const timep;
1051 1.3.2.2 jtc const long offset;
1052 1.3.2.2 jtc struct tm * const tmp;
1053 1.3.2.2 jtc {
1054 1.3.2.2 jtc if (!gmt_is_set) {
1055 1.3.2.2 jtc gmt_is_set = TRUE;
1056 1.3.2.2 jtc #ifdef ALL_STATE
1057 1.3.2.2 jtc gmtptr = (struct state *) malloc(sizeof *gmtptr);
1058 1.3.2.2 jtc if (gmtptr != NULL)
1059 1.3.2.2 jtc #endif /* defined ALL_STATE */
1060 1.3.2.2 jtc gmtload(gmtptr);
1061 1.3.2.2 jtc }
1062 1.3.2.2 jtc timesub(timep, offset, gmtptr, tmp);
1063 1.3.2.2 jtc #ifdef TM_ZONE
1064 1.3.2.2 jtc /*
1065 1.3.2.2 jtc ** Could get fancy here and deliver something such as
1066 1.3.2.2 jtc ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1067 1.3.2.2 jtc ** but this is no time for a treasure hunt.
1068 1.3.2.2 jtc */
1069 1.3.2.2 jtc if (offset != 0)
1070 1.3.2.2 jtc tmp->TM_ZONE = wildabbr;
1071 1.3.2.2 jtc else {
1072 1.3.2.2 jtc #ifdef ALL_STATE
1073 1.3.2.2 jtc if (gmtptr == NULL)
1074 1.3.2.2 jtc tmp->TM_ZONE = gmt;
1075 1.3.2.2 jtc else tmp->TM_ZONE = gmtptr->chars;
1076 1.3.2.2 jtc #endif /* defined ALL_STATE */
1077 1.3.2.2 jtc #ifndef ALL_STATE
1078 1.3.2.2 jtc tmp->TM_ZONE = gmtptr->chars;
1079 1.3.2.2 jtc #endif /* State Farm */
1080 1.3.2.2 jtc }
1081 1.3.2.2 jtc #endif /* defined TM_ZONE */
1082 1.3.2.2 jtc }
1083 1.3.2.2 jtc
1084 1.3.2.2 jtc struct tm *
1085 1.3.2.2 jtc gmtime(timep)
1086 1.3.2.2 jtc const time_t * const timep;
1087 1.3.2.2 jtc {
1088 1.3.2.2 jtc gmtsub(timep, 0L, &tm);
1089 1.3.2.2 jtc return &tm;
1090 1.3.2.2 jtc }
1091 1.3.2.2 jtc
1092 1.3.2.2 jtc #ifdef STD_INSPIRED
1093 1.3.2.2 jtc
1094 1.3.2.2 jtc struct tm *
1095 1.3.2.2 jtc offtime(timep, offset)
1096 1.3.2.2 jtc const time_t * const timep;
1097 1.3.2.2 jtc const long offset;
1098 1.3.2.2 jtc {
1099 1.3.2.2 jtc gmtsub(timep, offset, &tm);
1100 1.3.2.2 jtc return &tm;
1101 1.3.2.2 jtc }
1102 1.3.2.3 jtc #pragma weak offtime=_offtime
1103 1.3.2.2 jtc
1104 1.3.2.2 jtc #endif /* defined STD_INSPIRED */
1105 1.3.2.2 jtc
1106 1.3.2.2 jtc static void
1107 1.3.2.2 jtc timesub(timep, offset, sp, tmp)
1108 1.3.2.2 jtc const time_t * const timep;
1109 1.3.2.2 jtc const long offset;
1110 1.3.2.2 jtc register const struct state * const sp;
1111 1.3.2.2 jtc register struct tm * const tmp;
1112 1.3.2.2 jtc {
1113 1.3.2.2 jtc register const struct lsinfo * lp;
1114 1.3.2.2 jtc register long days;
1115 1.3.2.2 jtc register long rem;
1116 1.3.2.2 jtc register int y;
1117 1.3.2.2 jtc register int yleap;
1118 1.3.2.2 jtc register const int * ip;
1119 1.3.2.2 jtc register long corr;
1120 1.3.2.2 jtc register int hit;
1121 1.3.2.2 jtc register int i;
1122 1.3.2.2 jtc
1123 1.3.2.2 jtc corr = 0;
1124 1.3.2.2 jtc hit = 0;
1125 1.3.2.2 jtc #ifdef ALL_STATE
1126 1.3.2.2 jtc i = (sp == NULL) ? 0 : sp->leapcnt;
1127 1.3.2.2 jtc #endif /* defined ALL_STATE */
1128 1.3.2.2 jtc #ifndef ALL_STATE
1129 1.3.2.2 jtc i = sp->leapcnt;
1130 1.3.2.2 jtc #endif /* State Farm */
1131 1.3.2.2 jtc while (--i >= 0) {
1132 1.3.2.2 jtc lp = &sp->lsis[i];
1133 1.3.2.2 jtc if (*timep >= lp->ls_trans) {
1134 1.3.2.2 jtc if (*timep == lp->ls_trans) {
1135 1.3.2.2 jtc hit = ((i == 0 && lp->ls_corr > 0) ||
1136 1.3.2.2 jtc lp->ls_corr > sp->lsis[i - 1].ls_corr);
1137 1.3.2.2 jtc if (hit)
1138 1.3.2.2 jtc while (i > 0 &&
1139 1.3.2.2 jtc sp->lsis[i].ls_trans ==
1140 1.3.2.2 jtc sp->lsis[i - 1].ls_trans + 1 &&
1141 1.3.2.2 jtc sp->lsis[i].ls_corr ==
1142 1.3.2.2 jtc sp->lsis[i - 1].ls_corr + 1) {
1143 1.3.2.2 jtc ++hit;
1144 1.3.2.2 jtc --i;
1145 1.3.2.2 jtc }
1146 1.3.2.2 jtc }
1147 1.3.2.2 jtc corr = lp->ls_corr;
1148 1.3.2.2 jtc break;
1149 1.3.2.2 jtc }
1150 1.3.2.2 jtc }
1151 1.3.2.2 jtc days = *timep / SECSPERDAY;
1152 1.3.2.2 jtc rem = *timep % SECSPERDAY;
1153 1.3.2.2 jtc #ifdef mc68k
1154 1.3.2.2 jtc if (*timep == 0x80000000) {
1155 1.3.2.2 jtc /*
1156 1.3.2.2 jtc ** A 3B1 muffs the division on the most negative number.
1157 1.3.2.2 jtc */
1158 1.3.2.2 jtc days = -24855;
1159 1.3.2.2 jtc rem = -11648;
1160 1.3.2.2 jtc }
1161 1.3.2.2 jtc #endif /* defined mc68k */
1162 1.3.2.2 jtc rem += (offset - corr);
1163 1.3.2.2 jtc while (rem < 0) {
1164 1.3.2.2 jtc rem += SECSPERDAY;
1165 1.3.2.2 jtc --days;
1166 1.3.2.2 jtc }
1167 1.3.2.2 jtc while (rem >= SECSPERDAY) {
1168 1.3.2.2 jtc rem -= SECSPERDAY;
1169 1.3.2.2 jtc ++days;
1170 1.3.2.2 jtc }
1171 1.3.2.2 jtc tmp->tm_hour = (int) (rem / SECSPERHOUR);
1172 1.3.2.2 jtc rem = rem % SECSPERHOUR;
1173 1.3.2.2 jtc tmp->tm_min = (int) (rem / SECSPERMIN);
1174 1.3.2.2 jtc tmp->tm_sec = (int) (rem % SECSPERMIN);
1175 1.3.2.2 jtc if (hit)
1176 1.3.2.2 jtc /*
1177 1.3.2.2 jtc ** A positive leap second requires a special
1178 1.3.2.2 jtc ** representation. This uses "... ??:59:60" et seq.
1179 1.3.2.2 jtc */
1180 1.3.2.2 jtc tmp->tm_sec += hit;
1181 1.3.2.2 jtc tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1182 1.3.2.2 jtc if (tmp->tm_wday < 0)
1183 1.3.2.2 jtc tmp->tm_wday += DAYSPERWEEK;
1184 1.3.2.2 jtc y = EPOCH_YEAR;
1185 1.3.2.2 jtc if (days >= 0)
1186 1.3.2.2 jtc for ( ; ; ) {
1187 1.3.2.2 jtc yleap = isleap(y);
1188 1.3.2.2 jtc if (days < (long) year_lengths[yleap])
1189 1.3.2.2 jtc break;
1190 1.3.2.2 jtc ++y;
1191 1.3.2.2 jtc days = days - (long) year_lengths[yleap];
1192 1.3.2.2 jtc }
1193 1.3.2.2 jtc else do {
1194 1.3.2.2 jtc --y;
1195 1.3.2.2 jtc yleap = isleap(y);
1196 1.3.2.2 jtc days = days + (long) year_lengths[yleap];
1197 1.3.2.2 jtc } while (days < 0);
1198 1.3.2.2 jtc tmp->tm_year = y - TM_YEAR_BASE;
1199 1.3.2.2 jtc tmp->tm_yday = (int) days;
1200 1.3.2.2 jtc ip = mon_lengths[yleap];
1201 1.3.2.2 jtc for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1202 1.3.2.2 jtc days = days - (long) ip[tmp->tm_mon];
1203 1.3.2.2 jtc tmp->tm_mday = (int) (days + 1);
1204 1.3.2.2 jtc tmp->tm_isdst = 0;
1205 1.3.2.2 jtc #ifdef TM_GMTOFF
1206 1.3.2.2 jtc tmp->TM_GMTOFF = offset;
1207 1.3.2.2 jtc #endif /* defined TM_GMTOFF */
1208 1.3.2.2 jtc }
1209 1.3.2.2 jtc
1210 1.3.2.2 jtc char *
1211 1.3.2.2 jtc ctime(timep)
1212 1.3.2.2 jtc const time_t * const timep;
1213 1.3.2.2 jtc {
1214 1.3.2.2 jtc /*
1215 1.3.2.2 jtc ** Section 4.12.3.2 of X3.159-1989 requires that
1216 1.3.2.2 jtc ** The ctime funciton converts the calendar time pointed to by timer
1217 1.3.2.2 jtc ** to local time in the form of a string. It is equivalent to
1218 1.3.2.2 jtc ** asctime(localtime(timer))
1219 1.3.2.2 jtc */
1220 1.3.2.2 jtc return asctime(localtime(timep));
1221 1.3.2.2 jtc }
1222 1.3.2.2 jtc
1223 1.3.2.2 jtc /*
1224 1.3.2.2 jtc ** Adapted from code provided by Robert Elz, who writes:
1225 1.3.2.2 jtc ** The "best" way to do mktime I think is based on an idea of Bob
1226 1.3.2.2 jtc ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
1227 1.3.2.2 jtc ** It does a binary search of the time_t space. Since time_t's are
1228 1.3.2.2 jtc ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1229 1.3.2.2 jtc ** would still be very reasonable).
1230 1.3.2.2 jtc */
1231 1.3.2.2 jtc
1232 1.3.2.2 jtc #ifndef WRONG
1233 1.3.2.2 jtc #define WRONG (-1)
1234 1.3.2.2 jtc #endif /* !defined WRONG */
1235 1.3.2.2 jtc
1236 1.3.2.2 jtc /*
1237 1.3.2.2 jtc ** Simplified normalize logic courtesy Paul Eggert (eggert (at) twinsun.com).
1238 1.3.2.2 jtc */
1239 1.3.2.2 jtc
1240 1.3.2.2 jtc static int
1241 1.3.2.2 jtc increment_overflow(number, delta)
1242 1.3.2.2 jtc int * number;
1243 1.3.2.2 jtc int delta;
1244 1.3.2.2 jtc {
1245 1.3.2.2 jtc int number0;
1246 1.3.2.2 jtc
1247 1.3.2.2 jtc number0 = *number;
1248 1.3.2.2 jtc *number += delta;
1249 1.3.2.2 jtc return (*number < number0) != (delta < 0);
1250 1.3.2.2 jtc }
1251 1.3.2.2 jtc
1252 1.3.2.2 jtc static int
1253 1.3.2.2 jtc normalize_overflow(tensptr, unitsptr, base)
1254 1.3.2.2 jtc int * const tensptr;
1255 1.3.2.2 jtc int * const unitsptr;
1256 1.3.2.2 jtc const int base;
1257 1.3.2.2 jtc {
1258 1.3.2.2 jtc register int tensdelta;
1259 1.3.2.2 jtc
1260 1.3.2.2 jtc tensdelta = (*unitsptr >= 0) ?
1261 1.3.2.2 jtc (*unitsptr / base) :
1262 1.3.2.2 jtc (-1 - (-1 - *unitsptr) / base);
1263 1.3.2.2 jtc *unitsptr -= tensdelta * base;
1264 1.3.2.2 jtc return increment_overflow(tensptr, tensdelta);
1265 1.3.2.2 jtc }
1266 1.3.2.2 jtc
1267 1.3.2.2 jtc static int
1268 1.3.2.2 jtc tmcomp(atmp, btmp)
1269 1.3.2.2 jtc register const struct tm * const atmp;
1270 1.3.2.2 jtc register const struct tm * const btmp;
1271 1.3.2.2 jtc {
1272 1.3.2.2 jtc register int result;
1273 1.3.2.2 jtc
1274 1.3.2.2 jtc if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1275 1.3.2.2 jtc (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1276 1.3.2.2 jtc (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1277 1.3.2.2 jtc (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1278 1.3.2.2 jtc (result = (atmp->tm_min - btmp->tm_min)) == 0)
1279 1.3.2.2 jtc result = atmp->tm_sec - btmp->tm_sec;
1280 1.3.2.2 jtc return result;
1281 1.3.2.2 jtc }
1282 1.3.2.2 jtc
1283 1.3.2.2 jtc static time_t
1284 1.3.2.2 jtc time2(tmp, funcp, offset, okayp)
1285 1.3.2.2 jtc struct tm * const tmp;
1286 1.3.2.2 jtc void (* const funcp) P((const time_t*, long, struct tm*));
1287 1.3.2.2 jtc const long offset;
1288 1.3.2.2 jtc int * const okayp;
1289 1.3.2.2 jtc {
1290 1.3.2.2 jtc register const struct state * sp;
1291 1.3.2.2 jtc register int dir;
1292 1.3.2.2 jtc register int bits;
1293 1.3.2.2 jtc register int i, j ;
1294 1.3.2.2 jtc register int saved_seconds;
1295 1.3.2.2 jtc time_t newt;
1296 1.3.2.2 jtc time_t t;
1297 1.3.2.2 jtc struct tm yourtm, mytm;
1298 1.3.2.2 jtc
1299 1.3.2.2 jtc *okayp = FALSE;
1300 1.3.2.2 jtc yourtm = *tmp;
1301 1.3.2.2 jtc if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1302 1.3.2.2 jtc return WRONG;
1303 1.3.2.2 jtc if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1304 1.3.2.2 jtc return WRONG;
1305 1.3.2.2 jtc if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1306 1.3.2.2 jtc return WRONG;
1307 1.3.2.2 jtc /*
1308 1.3.2.2 jtc ** Turn yourtm.tm_year into an actual year number for now.
1309 1.3.2.2 jtc ** It is converted back to an offset from TM_YEAR_BASE later.
1310 1.3.2.2 jtc */
1311 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1312 1.3.2.2 jtc return WRONG;
1313 1.3.2.2 jtc while (yourtm.tm_mday <= 0) {
1314 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_year, -1))
1315 1.3.2.2 jtc return WRONG;
1316 1.3.2.2 jtc yourtm.tm_mday += year_lengths[isleap(yourtm.tm_year)];
1317 1.3.2.2 jtc }
1318 1.3.2.2 jtc while (yourtm.tm_mday > DAYSPERLYEAR) {
1319 1.3.2.2 jtc yourtm.tm_mday -= year_lengths[isleap(yourtm.tm_year)];
1320 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_year, 1))
1321 1.3.2.2 jtc return WRONG;
1322 1.3.2.2 jtc }
1323 1.3.2.2 jtc for ( ; ; ) {
1324 1.3.2.2 jtc i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1325 1.3.2.2 jtc if (yourtm.tm_mday <= i)
1326 1.3.2.2 jtc break;
1327 1.3.2.2 jtc yourtm.tm_mday -= i;
1328 1.3.2.2 jtc if (++yourtm.tm_mon >= MONSPERYEAR) {
1329 1.3.2.2 jtc yourtm.tm_mon = 0;
1330 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_year, 1))
1331 1.3.2.2 jtc return WRONG;
1332 1.3.2.2 jtc }
1333 1.3.2.2 jtc }
1334 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1335 1.3.2.2 jtc return WRONG;
1336 1.3.2.2 jtc if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1337 1.3.2.2 jtc /*
1338 1.3.2.2 jtc ** We can't set tm_sec to 0, because that might push the
1339 1.3.2.2 jtc ** time below the minimum representable time.
1340 1.3.2.2 jtc ** Set tm_sec to 59 instead.
1341 1.3.2.2 jtc ** This assumes that the minimum representable time is
1342 1.3.2.2 jtc ** not in the same minute that a leap second was deleted from,
1343 1.3.2.2 jtc ** which is a safer assumption than using 58 would be.
1344 1.3.2.2 jtc */
1345 1.3.2.2 jtc if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1346 1.3.2.2 jtc return WRONG;
1347 1.3.2.2 jtc saved_seconds = yourtm.tm_sec;
1348 1.3.2.2 jtc yourtm.tm_sec = SECSPERMIN - 1;
1349 1.3.2.2 jtc } else {
1350 1.3.2.2 jtc saved_seconds = yourtm.tm_sec;
1351 1.3.2.2 jtc yourtm.tm_sec = 0;
1352 1.3.2.2 jtc }
1353 1.3.2.2 jtc /*
1354 1.3.2.2 jtc ** Calculate the number of magnitude bits in a time_t
1355 1.3.2.2 jtc ** (this works regardless of whether time_t is
1356 1.3.2.2 jtc ** signed or unsigned, though lint complains if unsigned).
1357 1.3.2.2 jtc */
1358 1.3.2.2 jtc for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
1359 1.3.2.2 jtc continue;
1360 1.3.2.2 jtc /*
1361 1.3.2.2 jtc ** If time_t is signed, then 0 is the median value,
1362 1.3.2.2 jtc ** if time_t is unsigned, then 1 << bits is median.
1363 1.3.2.2 jtc */
1364 1.3.2.2 jtc t = (t < 0) ? 0 : ((time_t) 1 << bits);
1365 1.3.2.2 jtc for ( ; ; ) {
1366 1.3.2.2 jtc (*funcp)(&t, offset, &mytm);
1367 1.3.2.2 jtc dir = tmcomp(&mytm, &yourtm);
1368 1.3.2.2 jtc if (dir != 0) {
1369 1.3.2.2 jtc if (bits-- < 0)
1370 1.3.2.2 jtc return WRONG;
1371 1.3.2.2 jtc if (bits < 0)
1372 1.3.2.2 jtc --t;
1373 1.3.2.2 jtc else if (dir > 0)
1374 1.3.2.2 jtc t -= (time_t) 1 << bits;
1375 1.3.2.2 jtc else t += (time_t) 1 << bits;
1376 1.3.2.2 jtc continue;
1377 1.3.2.2 jtc }
1378 1.3.2.2 jtc if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1379 1.3.2.2 jtc break;
1380 1.3.2.2 jtc /*
1381 1.3.2.2 jtc ** Right time, wrong type.
1382 1.3.2.2 jtc ** Hunt for right time, right type.
1383 1.3.2.2 jtc ** It's okay to guess wrong since the guess
1384 1.3.2.2 jtc ** gets checked.
1385 1.3.2.2 jtc */
1386 1.3.2.2 jtc /*
1387 1.3.2.2 jtc ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1388 1.3.2.2 jtc */
1389 1.3.2.2 jtc sp = (const struct state *)
1390 1.3.2.2 jtc (((void *) funcp == (void *) localsub) ?
1391 1.3.2.2 jtc lclptr : gmtptr);
1392 1.3.2.2 jtc #ifdef ALL_STATE
1393 1.3.2.2 jtc if (sp == NULL)
1394 1.3.2.2 jtc return WRONG;
1395 1.3.2.2 jtc #endif /* defined ALL_STATE */
1396 1.3.2.2 jtc for (i = 0; i < sp->typecnt; ++i) {
1397 1.3.2.2 jtc if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1398 1.3.2.2 jtc continue;
1399 1.3.2.2 jtc for (j = 0; j < sp->typecnt; ++j) {
1400 1.3.2.2 jtc if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1401 1.3.2.2 jtc continue;
1402 1.3.2.2 jtc newt = t + sp->ttis[j].tt_gmtoff -
1403 1.3.2.2 jtc sp->ttis[i].tt_gmtoff;
1404 1.3.2.2 jtc (*funcp)(&newt, offset, &mytm);
1405 1.3.2.2 jtc if (tmcomp(&mytm, &yourtm) != 0)
1406 1.3.2.2 jtc continue;
1407 1.3.2.2 jtc if (mytm.tm_isdst != yourtm.tm_isdst)
1408 1.3.2.2 jtc continue;
1409 1.3.2.2 jtc /*
1410 1.3.2.2 jtc ** We have a match.
1411 1.3.2.2 jtc */
1412 1.3.2.2 jtc t = newt;
1413 1.3.2.2 jtc goto label;
1414 1.3.2.2 jtc }
1415 1.3.2.2 jtc }
1416 1.3.2.2 jtc return WRONG;
1417 1.3.2.2 jtc }
1418 1.3.2.2 jtc label:
1419 1.3.2.2 jtc newt = t + saved_seconds;
1420 1.3.2.2 jtc if ((newt < t) != (saved_seconds < 0))
1421 1.3.2.2 jtc return WRONG;
1422 1.3.2.2 jtc t = newt;
1423 1.3.2.2 jtc (*funcp)(&t, offset, tmp);
1424 1.3.2.2 jtc *okayp = TRUE;
1425 1.3.2.2 jtc return t;
1426 1.3.2.2 jtc }
1427 1.3.2.2 jtc
1428 1.3.2.2 jtc static time_t
1429 1.3.2.2 jtc time1(tmp, funcp, offset)
1430 1.3.2.2 jtc struct tm * const tmp;
1431 1.3.2.2 jtc void (* const funcp) P((const time_t*, long, struct tm*));
1432 1.3.2.2 jtc const long offset;
1433 1.3.2.2 jtc {
1434 1.3.2.2 jtc register time_t t;
1435 1.3.2.2 jtc register const struct state * sp;
1436 1.3.2.2 jtc register int samei, otheri;
1437 1.3.2.2 jtc int okay;
1438 1.3.2.2 jtc
1439 1.3.2.2 jtc if (tmp->tm_isdst > 1)
1440 1.3.2.2 jtc tmp->tm_isdst = 1;
1441 1.3.2.2 jtc t = time2(tmp, funcp, offset, &okay);
1442 1.3.2.2 jtc #ifdef PCTS
1443 1.3.2.2 jtc /*
1444 1.3.2.2 jtc ** PCTS code courtesy Grant Sullivan (grant (at) osf.org).
1445 1.3.2.2 jtc */
1446 1.3.2.2 jtc if (okay)
1447 1.3.2.2 jtc return t;
1448 1.3.2.2 jtc if (tmp->tm_isdst < 0)
1449 1.3.2.2 jtc tmp->tm_isdst = 0; /* reset to std and try again */
1450 1.3.2.2 jtc #endif /* defined PCTS */
1451 1.3.2.2 jtc #ifndef PCTS
1452 1.3.2.2 jtc if (okay || tmp->tm_isdst < 0)
1453 1.3.2.2 jtc return t;
1454 1.3.2.2 jtc #endif /* !defined PCTS */
1455 1.3.2.2 jtc /*
1456 1.3.2.2 jtc ** We're supposed to assume that somebody took a time of one type
1457 1.3.2.2 jtc ** and did some math on it that yielded a "struct tm" that's bad.
1458 1.3.2.2 jtc ** We try to divine the type they started from and adjust to the
1459 1.3.2.2 jtc ** type they need.
1460 1.3.2.2 jtc */
1461 1.3.2.2 jtc /*
1462 1.3.2.2 jtc ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1463 1.3.2.2 jtc */
1464 1.3.2.2 jtc sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1465 1.3.2.2 jtc lclptr : gmtptr);
1466 1.3.2.2 jtc #ifdef ALL_STATE
1467 1.3.2.2 jtc if (sp == NULL)
1468 1.3.2.2 jtc return WRONG;
1469 1.3.2.2 jtc #endif /* defined ALL_STATE */
1470 1.3.2.2 jtc for (samei = 0; samei < sp->typecnt; ++samei) {
1471 1.3.2.2 jtc if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1472 1.3.2.2 jtc continue;
1473 1.3.2.2 jtc for (otheri = 0; otheri < sp->typecnt; ++otheri) {
1474 1.3.2.2 jtc if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1475 1.3.2.2 jtc continue;
1476 1.3.2.2 jtc tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1477 1.3.2.2 jtc sp->ttis[samei].tt_gmtoff;
1478 1.3.2.2 jtc tmp->tm_isdst = !tmp->tm_isdst;
1479 1.3.2.2 jtc t = time2(tmp, funcp, offset, &okay);
1480 1.3.2.2 jtc if (okay)
1481 1.3.2.2 jtc return t;
1482 1.3.2.2 jtc tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1483 1.3.2.2 jtc sp->ttis[samei].tt_gmtoff;
1484 1.3.2.2 jtc tmp->tm_isdst = !tmp->tm_isdst;
1485 1.3.2.2 jtc }
1486 1.3.2.2 jtc }
1487 1.3.2.2 jtc return WRONG;
1488 1.3.2.2 jtc }
1489 1.3.2.2 jtc
1490 1.3.2.2 jtc time_t
1491 1.3.2.2 jtc mktime(tmp)
1492 1.3.2.2 jtc struct tm * const tmp;
1493 1.3.2.2 jtc {
1494 1.3.2.2 jtc tzset();
1495 1.3.2.2 jtc return time1(tmp, localsub, 0L);
1496 1.3.2.2 jtc }
1497 1.3.2.2 jtc
1498 1.3.2.2 jtc #ifdef STD_INSPIRED
1499 1.3.2.2 jtc
1500 1.3.2.2 jtc time_t
1501 1.3.2.2 jtc timelocal(tmp)
1502 1.3.2.2 jtc struct tm * const tmp;
1503 1.3.2.2 jtc {
1504 1.3.2.2 jtc tmp->tm_isdst = -1; /* in case it wasn't initialized */
1505 1.3.2.2 jtc return mktime(tmp);
1506 1.3.2.2 jtc }
1507 1.3.2.3 jtc #pragma weak timelocal=_timelocal
1508 1.3.2.2 jtc
1509 1.3.2.2 jtc time_t
1510 1.3.2.2 jtc timegm(tmp)
1511 1.3.2.2 jtc struct tm * const tmp;
1512 1.3.2.2 jtc {
1513 1.3.2.2 jtc tmp->tm_isdst = 0;
1514 1.3.2.2 jtc return time1(tmp, gmtsub, 0L);
1515 1.3.2.2 jtc }
1516 1.3.2.3 jtc #pragma weak timegm=_timegm
1517 1.3.2.2 jtc
1518 1.3.2.2 jtc time_t
1519 1.3.2.2 jtc timeoff(tmp, offset)
1520 1.3.2.2 jtc struct tm * const tmp;
1521 1.3.2.2 jtc const long offset;
1522 1.3.2.2 jtc {
1523 1.3.2.2 jtc tmp->tm_isdst = 0;
1524 1.3.2.2 jtc return time1(tmp, gmtsub, offset);
1525 1.3.2.2 jtc }
1526 1.3.2.3 jtc #pragma weak timeoff=_timeoff
1527 1.3.2.2 jtc
1528 1.3.2.2 jtc #endif /* defined STD_INSPIRED */
1529 1.3.2.2 jtc
1530 1.3.2.2 jtc #ifdef CMUCS
1531 1.3.2.2 jtc
1532 1.3.2.2 jtc /*
1533 1.3.2.2 jtc ** The following is supplied for compatibility with
1534 1.3.2.2 jtc ** previous versions of the CMUCS runtime library.
1535 1.3.2.2 jtc */
1536 1.3.2.2 jtc
1537 1.3.2.2 jtc long
1538 1.3.2.2 jtc gtime(tmp)
1539 1.3.2.2 jtc struct tm * const tmp;
1540 1.3.2.2 jtc {
1541 1.3.2.2 jtc const time_t t = mktime(tmp);
1542 1.3.2.2 jtc
1543 1.3.2.2 jtc if (t == WRONG)
1544 1.3.2.2 jtc return -1;
1545 1.3.2.2 jtc return t;
1546 1.3.2.2 jtc }
1547 1.3.2.2 jtc
1548 1.3.2.2 jtc #endif /* defined CMUCS */
1549 1.3.2.2 jtc
1550 1.3.2.2 jtc /*
1551 1.3.2.2 jtc ** XXX--is the below the right way to conditionalize??
1552 1.3.2.2 jtc */
1553 1.3.2.2 jtc
1554 1.3.2.2 jtc #ifdef STD_INSPIRED
1555 1.3.2.2 jtc
1556 1.3.2.2 jtc /*
1557 1.3.2.2 jtc ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1558 1.3.2.2 jtc ** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1559 1.3.2.2 jtc ** is not the case if we are accounting for leap seconds.
1560 1.3.2.2 jtc ** So, we provide the following conversion routines for use
1561 1.3.2.2 jtc ** when exchanging timestamps with POSIX conforming systems.
1562 1.3.2.2 jtc */
1563 1.3.2.2 jtc
1564 1.3.2.2 jtc static long
1565 1.3.2.2 jtc leapcorr(timep)
1566 1.3.2.2 jtc time_t * timep;
1567 1.3.2.2 jtc {
1568 1.3.2.2 jtc register struct state * sp;
1569 1.3.2.2 jtc register struct lsinfo * lp;
1570 1.3.2.2 jtc register int i;
1571 1.3.2.2 jtc
1572 1.3.2.2 jtc sp = lclptr;
1573 1.3.2.2 jtc i = sp->leapcnt;
1574 1.3.2.2 jtc while (--i >= 0) {
1575 1.3.2.2 jtc lp = &sp->lsis[i];
1576 1.3.2.2 jtc if (*timep >= lp->ls_trans)
1577 1.3.2.2 jtc return lp->ls_corr;
1578 1.3.2.2 jtc }
1579 1.3.2.2 jtc return 0;
1580 1.3.2.2 jtc }
1581 1.3.2.2 jtc
1582 1.3.2.2 jtc time_t
1583 1.3.2.2 jtc time2posix(t)
1584 1.3.2.2 jtc time_t t;
1585 1.3.2.2 jtc {
1586 1.3.2.2 jtc tzset();
1587 1.3.2.2 jtc return t - leapcorr(&t);
1588 1.3.2.2 jtc }
1589 1.3.2.3 jtc #pragma weak time2posix=_time2posix
1590 1.3.2.2 jtc
1591 1.3.2.2 jtc time_t
1592 1.3.2.2 jtc posix2time(t)
1593 1.3.2.2 jtc time_t t;
1594 1.3.2.2 jtc {
1595 1.3.2.2 jtc time_t x;
1596 1.3.2.2 jtc time_t y;
1597 1.3.2.2 jtc
1598 1.3.2.2 jtc tzset();
1599 1.3.2.2 jtc /*
1600 1.3.2.2 jtc ** For a positive leap second hit, the result
1601 1.3.2.2 jtc ** is not unique. For a negative leap second
1602 1.3.2.2 jtc ** hit, the corresponding time doesn't exist,
1603 1.3.2.2 jtc ** so we return an adjacent second.
1604 1.3.2.2 jtc */
1605 1.3.2.2 jtc x = t + leapcorr(&t);
1606 1.3.2.2 jtc y = x - leapcorr(&x);
1607 1.3.2.2 jtc if (y < t) {
1608 1.3.2.2 jtc do {
1609 1.3.2.2 jtc x++;
1610 1.3.2.2 jtc y = x - leapcorr(&x);
1611 1.3.2.2 jtc } while (y < t);
1612 1.3.2.2 jtc if (t != y)
1613 1.3.2.2 jtc return x - 1;
1614 1.3.2.2 jtc } else if (y > t) {
1615 1.3.2.2 jtc do {
1616 1.3.2.2 jtc --x;
1617 1.3.2.2 jtc y = x - leapcorr(&x);
1618 1.3.2.2 jtc } while (y > t);
1619 1.3.2.2 jtc if (t != y)
1620 1.3.2.2 jtc return x + 1;
1621 1.3.2.2 jtc }
1622 1.3.2.2 jtc return x;
1623 1.3.2.2 jtc }
1624 1.3.2.3 jtc #pragma weak posix2time=_posix2time
1625 1.3.2.2 jtc
1626 1.3.2.2 jtc #endif /* defined STD_INSPIRED */
1627