78137a5bfdae08411222a4d9d420c1ed85364b65
[reactos.git] / reactos / lib / crt / time / ctime.c
1
2 // fix djdir
3
4 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
5 /* This file has been modified by DJ Delorie. These modifications are
6 ** Copyright (C) 1995 DJ Delorie, 24 Kirsten Ave, Rochester NH,
7 ** 03867-2954, USA.
8 */
9
10 /*
11 * Copyright (c) 1987, 1989 Regents of the University of California.
12 * All rights reserved.
13 *
14 * This code is derived from software contributed to Berkeley by
15 * Arthur David Olson of the National Cancer Institute.
16 *
17 * Redistribution and use in source and binary forms are permitted provided
18 * that: (1) source distributions retain this entire copyright notice and
19 * comment, and (2) distributions including binaries display the following
20 * acknowledgement: ``This product includes software developed by the
21 * University of California, Berkeley and its contributors'' in the
22 * documentation or other materials provided with the distribution and in
23 * all advertising materials mentioning features or use of this software.
24 * Neither the name of the University nor the names of its contributors may
25 * be used to endorse or promote products derived from this software without
26 * specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 */
31
32 /*
33 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
34 ** POSIX-style TZ environment variable handling from Guy Harris
35 ** (guy@auspex.com).
36 */
37
38 #include "precomp.h"
39 #include <fcntl.h>
40 #include <time.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include "tzfile.h"
47
48 #include <io.h>
49
50 #include "posixrul.h"
51
52
53 #ifdef __cplusplus
54 #define CPP_CONST const
55 #else
56 #define CPP_CONST
57 #endif
58
59 #define P(s) s
60 #define alloc_size_t size_t
61 #define qsort_size_t size_t
62 #define fread_size_t size_t
63 #define fwrite_size_t size_t
64
65 #define ACCESS_MODE O_RDONLY|O_BINARY
66 #define OPEN_MODE O_RDONLY|O_BINARY
67
68 /*
69 ** Someone might make incorrect use of a time zone abbreviation:
70 ** 1. They might reference tzname[0] before calling tzset (explicitly
71 ** or implicitly).
72 ** 2. They might reference tzname[1] before calling tzset (explicitly
73 ** or implicitly).
74 ** 3. They might reference tzname[1] after setting to a time zone
75 ** in which Daylight Saving Time is never observed.
76 ** 4. They might reference tzname[0] after setting to a time zone
77 ** in which Standard Time is never observed.
78 ** 5. They might reference tm.TM_ZONE after calling offtime.
79 ** What's best to do in the above cases is open to debate;
80 ** for now, we just set things up so that in any of the five cases
81 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
82 ** string "tzname[0] used before set", and similarly for the other cases.
83 ** And another: initialize tzname[0] to "ERA", with an explanation in the
84 ** manual page of what this "time zone abbreviation" means (doing this so
85 ** that tzname[0] has the "normal" length of three characters).
86 */
87
88 void _set_daylight_export(int);
89 void _set_timezone_export(int);
90
91
92 /* buffers must hold 64 characters! */
93 static char TZ_NAME[64] = "PST";
94 static char TZ_DST_NAME[64] = "PDT";
95
96 #ifndef TRUE
97 #define TRUE 1
98 #define FALSE 0
99 #endif /* !defined TRUE */
100
101 static const char GMT[] = "GMT";
102
103 struct ttinfo { /* time type information */
104 long tt_gmtoff; /* GMT offset in seconds */
105 int tt_isdst; /* used to set tm_isdst */
106 int tt_abbrind; /* abbreviation list index */
107 int tt_ttisstd; /* TRUE if transition is std time */
108 };
109
110 struct lsinfo { /* leap second information */
111 time_t ls_trans; /* transition time */
112 long ls_corr; /* correction to apply */
113 };
114
115 struct state {
116 int leapcnt;
117 int timecnt;
118 int typecnt;
119 int charcnt;
120 time_t ats[TZ_MAX_TIMES];
121 unsigned char types[TZ_MAX_TIMES];
122 struct ttinfo ttis[TZ_MAX_TYPES];
123 char chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ? TZ_MAX_CHARS + 1 : sizeof GMT];
124 struct lsinfo lsis[TZ_MAX_LEAPS];
125 };
126
127 struct rule {
128 int r_type; /* type of rule--see below */
129 int r_day; /* day number of rule */
130 int r_week; /* week number of rule */
131 int r_mon; /* month number of rule */
132 long r_time; /* transition time of rule */
133 };
134
135 #define JULIAN_DAY 0 /* Jn - Julian day */
136 #define DAY_OF_YEAR 1 /* n - day of year */
137 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
138
139 /*
140 ** Prototypes for static functions.
141 */
142 #if 0
143 static long detzcode P((const char * codep));
144 static const char * getzname P((const char * strp));
145 static const char * getnum P((const char * strp, int * nump, int min, int max));
146 static const char * getsecs P((const char * strp, long * secsp));
147 static const char * getoffset P((const char * strp, long * offsetp));
148 static const char * getrule P((const char * strp, struct rule * rulep));
149 static void gmtload P((struct state * sp));
150 static void gmtsub P((const time_t * timep, long offset, struct tm * tmp));
151 static void localsub P((const time_t * timep, long offset, struct tm * tmp));
152 static void normalize P((int * tensptr, int * unitsptr, int base));
153 static void settzname P((void));
154 static time_t time1 P((struct tm * tmp, void (* funcp)(const time_t * CPP_CONST, const long, struct tm * CPP_CONST), long offset));
155 static time_t time2 P((struct tm *tmp, void (* funcp)(const time_t * CPP_CONST, const long, struct tm * CPP_CONST), long offset, int * okayp));
156 static void timesub P((const time_t * timep, long offset, const struct state * sp, struct tm * tmp));
157 static int tmcomp P((const struct tm * atmp, const struct tm * btmp));
158 static time_t transtime P((time_t janfirst, int year, const struct rule * rulep, long offset));
159 static int tzload P((const char * name, struct state * sp));
160 static int tzparse P((const char * name, struct state * sp, int lastditch));
161 static void tzsetwall(void);
162
163 #else
164
165 static const char * getnum(const char * strp, int * CPP_CONST nump, const int min, const int max);
166 static void timesub(const time_t * CPP_CONST timep, const long offset, const struct state * CPP_CONST sp, struct tm * CPP_CONST tmp);
167 static time_t transtime(const time_t janfirst, const int year, const struct rule * CPP_CONST rulep, const long offset);
168 static void tzsetwall(void);
169
170 #endif
171
172 #ifdef ALL_STATE
173 static struct state *lclptr;
174 static struct state *gmtptr;
175 #endif /* defined ALL_STATE */
176
177 #ifndef ALL_STATE
178 static struct state lclmem;
179 static struct state gmtmem;
180 #define lclptr (&lclmem)
181 #define gmtptr (&gmtmem)
182 #endif /* State Farm */
183
184 static int lcl_is_set;
185 static int gmt_is_set;
186
187 char * _tzname[2] = {
188 TZ_NAME,
189 TZ_DST_NAME,
190 };
191
192 static long
193 detzcode(const char * CPP_CONST codep)
194 {
195 long result;
196 int i;
197
198 result = 0;
199 for (i = 0; i < 4; ++i)
200 result = (result << 8) | (codep[i] & 0xff);
201 return result;
202 }
203
204 static void
205 settzname(void)
206 {
207 const struct state * CPP_CONST sp = lclptr;
208 int i;
209
210 _tzname[0] = TZ_NAME;
211 _tzname[1] = TZ_DST_NAME;
212 #ifdef ALL_STATE
213 if (sp == NULL)
214 {
215 _tzname[0] = _tzname[1] = GMT;
216 return;
217 }
218 #endif /* defined ALL_STATE */
219 for (i = 0; i < sp->typecnt; ++i)
220 {
221 register const struct ttinfo * CPP_CONST ttisp = &sp->ttis[i];
222
223 _tzname[ttisp->tt_isdst] =
224 (char *)&sp->chars[ttisp->tt_abbrind];
225 #if 0
226 if (ttisp->tt_isdst) {
227 //_daylight = 1;
228 _set_daylight_export(1);
229 }
230 if (i == 0 || !ttisp->tt_isdst) {
231 //_timezone_dll = -(ttisp->tt_gmtoff);
232 _set_timezone_export(-(ttisp->tt_gmtoff));
233 }
234 if (i == 0 || ttisp->tt_isdst) {
235 _altzone = -(ttisp->tt_gmtoff);
236 }
237 #endif
238 }
239 /*
240 ** And to get the latest zone names into tzname. . .
241 */
242 for (i = 0; i < sp->timecnt; ++i)
243 {
244 const struct ttinfo * CPP_CONST ttisp = &sp->ttis[sp->types[i]];
245
246 _tzname[ttisp->tt_isdst] = (char *)&sp->chars[ttisp->tt_abbrind];
247 }
248 }
249
250 static char* tzdir(void)
251 {
252 static char dir[80]={0}, *cp;
253 if (dir[0] == 0)
254 {
255 if ((cp = getenv("TZDIR")))
256 {
257 strcpy(dir, cp);
258 }
259 else if ((cp = getenv("DJDIR")))
260 {
261 strcpy(dir, cp);
262 strcat(dir, "/zoneinfo");
263 }
264 else
265 strcpy(dir, "./");
266 }
267 return dir;
268 }
269
270 static int tzload(const char* name, struct state* CPP_CONST sp)
271 {
272 const char * p;
273 int i;
274 int fid;
275 char fullname[FILENAME_MAX + 1];
276 const struct tzhead * tzhp;
277 char buf[sizeof *sp + sizeof *tzhp];
278 int ttisstdcnt;
279
280 if (name == NULL && (name = TZDEFAULT) == NULL)
281 return -1;
282
283 if (name[0] == ':')
284 ++name;
285 if (name[0] != '/')
286 {
287 if ((p = tzdir()) == NULL)
288 return -1;
289 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
290 return -1;
291 strcpy(fullname, p);
292 strcat(fullname, "/");
293 strcat(fullname, name);
294 name = fullname;
295 }
296
297 if ((fid = _open(name, OPEN_MODE)) == -1)
298 {
299 const char *base = strrchr(name, '/');
300 if (base)
301 base++;
302 else
303 base = name;
304 if (strcmp(base, "posixrules"))
305 return -1;
306
307 /* We've got a built-in copy of posixrules just in case */
308 memcpy(buf, _posixrules_data, sizeof(_posixrules_data));
309 i = sizeof(_posixrules_data);
310 }
311 else
312 {
313 i = _read(fid, buf, sizeof buf);
314 if (_close(fid) != 0 || i < sizeof *tzhp)
315 return -1;
316 }
317
318 tzhp = (struct tzhead *) buf;
319 ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
320 sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
321 sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
322 sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
323 sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
324 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
325 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
326 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
327 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
328 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
329 return -1;
330 if (i < sizeof *tzhp +
331 sp->timecnt * (4 + sizeof (char)) +
332 sp->typecnt * (4 + 2 * sizeof (char)) +
333 sp->charcnt * sizeof (char) +
334 sp->leapcnt * 2 * 4 +
335 ttisstdcnt * sizeof (char))
336 return -1;
337 p = buf + sizeof *tzhp;
338 for (i = 0; i < sp->timecnt; ++i)
339 {
340 sp->ats[i] = detzcode(p);
341 p += 4;
342 }
343 for (i = 0; i < sp->timecnt; ++i)
344 {
345 sp->types[i] = (unsigned char) *p++;
346 if (sp->types[i] >= sp->typecnt)
347 return -1;
348 }
349 for (i = 0; i < sp->typecnt; ++i)
350 {
351 struct ttinfo * ttisp;
352
353 ttisp = &sp->ttis[i];
354 ttisp->tt_gmtoff = detzcode(p);
355 p += 4;
356 ttisp->tt_isdst = (unsigned char) *p++;
357 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
358 return -1;
359 ttisp->tt_abbrind = (unsigned char) *p++;
360 if (ttisp->tt_abbrind < 0 ||
361 ttisp->tt_abbrind > sp->charcnt)
362 return -1;
363 }
364 for (i = 0; i < sp->charcnt; ++i)
365 sp->chars[i] = *p++;
366 sp->chars[i] = '\0'; /* ensure '\0' at end */
367 for (i = 0; i < sp->leapcnt; ++i)
368 {
369 struct lsinfo * lsisp;
370
371 lsisp = &sp->lsis[i];
372 lsisp->ls_trans = detzcode(p);
373 p += 4;
374 lsisp->ls_corr = detzcode(p);
375 p += 4;
376 }
377 for (i = 0; i < sp->typecnt; ++i)
378 {
379 struct ttinfo * ttisp;
380
381 ttisp = &sp->ttis[i];
382 if (ttisstdcnt == 0)
383 ttisp->tt_ttisstd = FALSE;
384 else
385 {
386 ttisp->tt_ttisstd = *p++;
387 if (ttisp->tt_ttisstd != TRUE &&
388 ttisp->tt_ttisstd != FALSE)
389 return -1;
390 }
391 }
392 return 0;
393 }
394
395 static const int mon_lengths[2][MONSPERYEAR] = {
396 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
397 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
398 };
399
400 static const int year_lengths[2] = {
401 DAYSPERNYEAR, DAYSPERLYEAR
402 };
403
404 /*
405 ** Given a pointer into a time zone string, scan until a character that is not
406 ** a valid character in a zone name is found. Return a pointer to that
407 ** character.
408 */
409
410 static const char*
411 getzname(const char* strp)
412 {
413 char c;
414
415 while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
416 c != '+')
417 ++strp;
418 return strp;
419 }
420
421 /*
422 ** Given a pointer into a time zone string, extract a number from that string.
423 ** Check that the number is within a specified range; if it is not, return
424 ** NULL.
425 ** Otherwise, return a pointer to the first character not part of the number.
426 */
427
428 static const char*
429 getnum(const char* strp, int* CPP_CONST nump, const int min, const int max)
430 {
431 char c;
432 int num;
433
434 if (strp == NULL || !isdigit(*strp))
435 return NULL;
436 num = 0;
437 while ((c = *strp) != '\0' && isdigit(c))
438 {
439 num = num * 10 + (c - '0');
440 if (num > max)
441 return NULL;
442 ++strp;
443 }
444 if (num < min)
445 return NULL;
446 *nump = num;
447 return strp;
448 }
449
450 /*
451 ** Given a pointer into a time zone string, extract a number of seconds,
452 ** in hh[:mm[:ss]] form, from the string.
453 ** If any error occurs, return NULL.
454 ** Otherwise, return a pointer to the first character not part of the number
455 ** of seconds.
456 */
457
458 static const char *
459 getsecs(const char *strp, long * CPP_CONST secsp)
460 {
461 int num;
462
463 strp = getnum(strp, &num, 0, HOURSPERDAY);
464 if (strp == NULL)
465 return NULL;
466 *secsp = num * SECSPERHOUR;
467 if (*strp == ':')
468 {
469 ++strp;
470 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
471 if (strp == NULL)
472 return NULL;
473 *secsp += num * SECSPERMIN;
474 if (*strp == ':')
475 {
476 ++strp;
477 strp = getnum(strp, &num, 0, SECSPERMIN - 1);
478 if (strp == NULL)
479 return NULL;
480 *secsp += num;
481 }
482 }
483 return strp;
484 }
485
486 /*
487 ** Given a pointer into a time zone string, extract an offset, in
488 ** [+-]hh[:mm[:ss]] form, from the string.
489 ** If any error occurs, return NULL.
490 ** Otherwise, return a pointer to the first character not part of the time.
491 */
492
493 static const char *
494 getoffset(const char *strp, long * CPP_CONST offsetp)
495 {
496 int neg;
497
498 if (*strp == '-')
499 {
500 neg = 1;
501 ++strp;
502 }
503 else if (isdigit(*strp) || *strp++ == '+')
504 neg = 0;
505 else
506 return NULL; /* illegal offset */
507 strp = getsecs(strp, offsetp);
508 if (strp == NULL)
509 return NULL; /* illegal time */
510 if (neg)
511 *offsetp = -*offsetp;
512 return strp;
513 }
514
515 /*
516 ** Given a pointer into a time zone string, extract a rule in the form
517 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
518 ** If a valid rule is not found, return NULL.
519 ** Otherwise, return a pointer to the first character not part of the rule.
520 */
521
522 static const char *
523 getrule(const char *strp, struct rule * CPP_CONST rulep)
524 {
525 if (*strp == 'J')
526 {
527 /*
528 ** Julian day.
529 */
530 rulep->r_type = JULIAN_DAY;
531 ++strp;
532 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
533 }
534 else if (*strp == 'M')
535 {
536 /*
537 ** Month, week, day.
538 */
539 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
540 ++strp;
541 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
542 if (strp == NULL)
543 return NULL;
544 if (*strp++ != '.')
545 return NULL;
546 strp = getnum(strp, &rulep->r_week, 1, 5);
547 if (strp == NULL)
548 return NULL;
549 if (*strp++ != '.')
550 return NULL;
551 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
552 }
553 else if (isdigit(*strp))
554 {
555 /*
556 ** Day of year.
557 */
558 rulep->r_type = DAY_OF_YEAR;
559 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
560 }
561 else
562 return NULL; /* invalid format */
563 if (strp == NULL)
564 return NULL;
565 if (*strp == '/')
566 {
567 /*
568 ** Time specified.
569 */
570 ++strp;
571 strp = getsecs(strp, &rulep->r_time);
572 }
573 else
574 rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
575 return strp;
576 }
577
578 /*
579 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
580 ** year, a rule, and the offset from GMT at the time that rule takes effect,
581 ** calculate the Epoch-relative time that rule takes effect.
582 */
583
584 static time_t
585 transtime(const time_t janfirst, const int year, const struct rule * CPP_CONST rulep, const long offset)
586 {
587 int leapyear;
588 time_t value=0;
589 int i;
590 int d, m1, yy0, yy1, yy2, dow;
591
592 leapyear = isleap(year);
593 switch (rulep->r_type)
594 {
595
596 case JULIAN_DAY:
597 /*
598 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
599 ** years.
600 ** In non-leap years, or if the day number is 59 or less, just
601 ** add SECSPERDAY times the day number-1 to the time of
602 ** January 1, midnight, to get the day.
603 */
604 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
605 if (leapyear && rulep->r_day >= 60)
606 value += SECSPERDAY;
607 break;
608
609 case DAY_OF_YEAR:
610 /*
611 ** n - day of year.
612 ** Just add SECSPERDAY times the day number to the time of
613 ** January 1, midnight, to get the day.
614 */
615 value = janfirst + rulep->r_day * SECSPERDAY;
616 break;
617
618 case MONTH_NTH_DAY_OF_WEEK:
619 /*
620 ** Mm.n.d - nth "dth day" of month m.
621 */
622 value = janfirst;
623 for (i = 0; i < rulep->r_mon - 1; ++i)
624 value += mon_lengths[leapyear][i] * SECSPERDAY;
625
626 /*
627 ** Use Zeller's Congruence to get day-of-week of first day of
628 ** month.
629 */
630 m1 = (rulep->r_mon + 9) % 12 + 1;
631 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
632 yy1 = yy0 / 100;
633 yy2 = yy0 % 100;
634 dow = ((26 * m1 - 2) / 10 +
635 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
636 if (dow < 0)
637 dow += DAYSPERWEEK;
638
639 /*
640 ** "dow" is the day-of-week of the first day of the month. Get
641 ** the day-of-month (zero-origin) of the first "dow" day of the
642 ** month.
643 */
644 d = rulep->r_day - dow;
645 if (d < 0)
646 d += DAYSPERWEEK;
647 for (i = 1; i < rulep->r_week; ++i)
648 {
649 if (d + DAYSPERWEEK >=
650 mon_lengths[leapyear][rulep->r_mon - 1])
651 break;
652 d += DAYSPERWEEK;
653 }
654
655 /*
656 ** "d" is the day-of-month (zero-origin) of the day we want.
657 */
658 value += d * SECSPERDAY;
659 break;
660 }
661
662 /*
663 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
664 ** question. To get the Epoch-relative time of the specified local
665 ** time on that day, add the transition time and the current offset
666 ** from GMT.
667 */
668 return value + rulep->r_time + offset;
669 }
670
671 /*
672 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
673 ** appropriate.
674 */
675
676 static int
677 tzparse(const char *name, struct state * CPP_CONST sp, const int lastditch)
678 {
679 const char * stdname;
680 const char * dstname=0;
681 int stdlen;
682 int dstlen;
683 long stdoffset;
684 long dstoffset;
685 time_t * atp;
686 unsigned char * typep;
687 char * cp;
688 int load_result;
689
690 stdname = name;
691 if (lastditch)
692 {
693 stdlen = strlen(name); /* length of standard zone name */
694 name += stdlen;
695 if (stdlen >= sizeof sp->chars)
696 stdlen = (sizeof sp->chars) - 1;
697 }
698 else
699 {
700 name = getzname(name);
701 stdlen = name - stdname;
702 if (stdlen < 3)
703 return -1;
704 }
705 if (*name == '\0')
706 return -1;
707 else
708 {
709 name = getoffset(name, &stdoffset);
710 if (name == NULL)
711 return -1;
712 }
713 load_result = tzload(TZDEFRULES, sp);
714 if (load_result != 0)
715 sp->leapcnt = 0; /* so, we're off a little */
716 if (*name != '\0')
717 {
718 dstname = name;
719 name = getzname(name);
720 dstlen = name - dstname; /* length of DST zone name */
721 if (dstlen < 3)
722 return -1;
723 if (*name != '\0' && *name != ',' && *name != ';')
724 {
725 name = getoffset(name, &dstoffset);
726 if (name == NULL)
727 return -1;
728 }
729 else
730 dstoffset = stdoffset - SECSPERHOUR;
731 if (*name == ',' || *name == ';')
732 {
733 struct rule start;
734 struct rule end;
735 int year;
736 time_t janfirst;
737 time_t starttime;
738 time_t endtime;
739
740 ++name;
741 if ((name = getrule(name, &start)) == NULL)
742 return -1;
743 if (*name++ != ',')
744 return -1;
745 if ((name = getrule(name, &end)) == NULL)
746 return -1;
747 if (*name != '\0')
748 return -1;
749 sp->typecnt = 2; /* standard time and DST */
750 /*
751 ** Two transitions per year, from EPOCH_YEAR to 2037.
752 */
753 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
754 if (sp->timecnt > TZ_MAX_TIMES)
755 return -1;
756 sp->ttis[0].tt_gmtoff = -dstoffset;
757 sp->ttis[0].tt_isdst = 1;
758 sp->ttis[0].tt_abbrind = stdlen + 1;
759 sp->ttis[1].tt_gmtoff = -stdoffset;
760 sp->ttis[1].tt_isdst = 0;
761 sp->ttis[1].tt_abbrind = 0;
762 atp = sp->ats;
763 typep = sp->types;
764 janfirst = 0;
765 for (year = EPOCH_YEAR; year <= 2037; ++year)
766 {
767 starttime = transtime(janfirst, year, &start,
768 stdoffset);
769 endtime = transtime(janfirst, year, &end,
770 dstoffset);
771 if (starttime > endtime)
772 {
773 *atp++ = endtime;
774 *typep++ = 1; /* DST ends */
775 *atp++ = starttime;
776 *typep++ = 0; /* DST begins */
777 }
778 else
779 {
780 *atp++ = starttime;
781 *typep++ = 0; /* DST begins */
782 *atp++ = endtime;
783 *typep++ = 1; /* DST ends */
784 }
785 janfirst +=
786 year_lengths[isleap(year)] * SECSPERDAY;
787 }
788 }
789 else
790 {
791 int sawstd;
792 int sawdst;
793 long stdfix;
794 long dstfix;
795 long oldfix;
796 int isdst;
797 int i;
798
799 if (*name != '\0')
800 return -1;
801 if (load_result != 0)
802 return -1;
803 /*
804 ** Compute the difference between the real and
805 ** prototype standard and summer time offsets
806 ** from GMT, and put the real standard and summer
807 ** time offsets into the rules in place of the
808 ** prototype offsets.
809 */
810 sawstd = FALSE;
811 sawdst = FALSE;
812 stdfix = 0;
813 dstfix = 0;
814 for (i = 0; i < sp->typecnt; ++i)
815 {
816 if (sp->ttis[i].tt_isdst)
817 {
818 oldfix = dstfix;
819 dstfix =
820 sp->ttis[i].tt_gmtoff + dstoffset;
821 if (sawdst && (oldfix != dstfix))
822 return -1;
823 sp->ttis[i].tt_gmtoff = -dstoffset;
824 sp->ttis[i].tt_abbrind = stdlen + 1;
825 sawdst = TRUE;
826 }
827 else
828 {
829 oldfix = stdfix;
830 stdfix =
831 sp->ttis[i].tt_gmtoff + stdoffset;
832 if (sawstd && (oldfix != stdfix))
833 return -1;
834 sp->ttis[i].tt_gmtoff = -stdoffset;
835 sp->ttis[i].tt_abbrind = 0;
836 sawstd = TRUE;
837 }
838 }
839 /*
840 ** Make sure we have both standard and summer time.
841 */
842 if (!sawdst || !sawstd)
843 return -1;
844 /*
845 ** Now correct the transition times by shifting
846 ** them by the difference between the real and
847 ** prototype offsets. Note that this difference
848 ** can be different in standard and summer time;
849 ** the prototype probably has a 1-hour difference
850 ** between standard and summer time, but a different
851 ** difference can be specified in TZ.
852 */
853 isdst = FALSE; /* we start in standard time */
854 for (i = 0; i < sp->timecnt; ++i)
855 {
856 const struct ttinfo * ttisp;
857
858 /*
859 ** If summer time is in effect, and the
860 ** transition time was not specified as
861 ** standard time, add the summer time
862 ** offset to the transition time;
863 ** otherwise, add the standard time offset
864 ** to the transition time.
865 */
866 ttisp = &sp->ttis[sp->types[i]];
867 sp->ats[i] +=
868 (isdst && !ttisp->tt_ttisstd) ?
869 dstfix : stdfix;
870 isdst = ttisp->tt_isdst;
871 }
872 }
873 }
874 else
875 {
876 dstlen = 0;
877 sp->typecnt = 1; /* only standard time */
878 sp->timecnt = 0;
879 sp->ttis[0].tt_gmtoff = -stdoffset;
880 sp->ttis[0].tt_isdst = 0;
881 sp->ttis[0].tt_abbrind = 0;
882 }
883 sp->charcnt = stdlen + 1;
884 if (dstlen != 0)
885 sp->charcnt += dstlen + 1;
886 if (sp->charcnt > sizeof sp->chars)
887 return -1;
888 cp = sp->chars;
889 (void) strncpy(cp, stdname, stdlen);
890 cp += stdlen;
891 *cp++ = '\0';
892 if (dstlen != 0)
893 {
894 (void) strncpy(cp, dstname, dstlen);
895 *(cp + dstlen) = '\0';
896 }
897 return 0;
898 }
899
900 static void
901 gmtload(struct state * CPP_CONST sp)
902 {
903 if (tzload(GMT, sp) != 0)
904 (void) tzparse(GMT, sp, TRUE);
905 }
906
907 /*
908 * @implemented
909 */
910 void
911 _tzset(void)
912 {
913 const char * name;
914
915 name = getenv("TZ");
916 if (name == NULL)
917 {
918 tzsetwall();
919 return;
920 }
921 lcl_is_set = TRUE;
922 #ifdef ALL_STATE
923 if (lclptr == NULL)
924 {
925 lclptr = (struct state *) malloc(sizeof *lclptr);
926 if (lclptr == NULL)
927 {
928 settzname(); /* all we can do */
929 return;
930 }
931 }
932 #endif /* defined ALL_STATE */
933 if (*name == '\0')
934 {
935 /*
936 ** User wants it fast rather than right.
937 */
938 lclptr->leapcnt = 0; /* so, we're off a little */
939 lclptr->timecnt = 0;
940 lclptr->ttis[0].tt_gmtoff = 0;
941 lclptr->ttis[0].tt_abbrind = 0;
942 (void) strcpy(lclptr->chars, GMT);
943 }
944 else if (tzload(name, lclptr) != 0)
945 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
946 gmtload(lclptr);
947 settzname();
948 }
949
950 void
951 tzsetwall(void)
952 {
953 lcl_is_set = TRUE;
954 #ifdef ALL_STATE
955 if (lclptr == NULL)
956 {
957 lclptr = (struct state *) malloc(sizeof *lclptr);
958 if (lclptr == NULL)
959 {
960 settzname(); /* all we can do */
961 return;
962 }
963 }
964 #endif /* defined ALL_STATE */
965 if (tzload((char *) NULL, lclptr) != 0)
966 gmtload(lclptr);
967 settzname();
968 }
969
970 /*
971 ** The easy way to behave "as if no library function calls" localtime
972 ** is to not call it--so we drop its guts into "localsub", which can be
973 ** freely called. (And no, the PANS doesn't require the above behavior--
974 ** but it *is* desirable.)
975 **
976 ** The unused offset argument is for the benefit of mktime variants.
977 */
978
979 /*ARGSUSED*/
980 static void
981 localsub(const time_t * CPP_CONST timep, const long offset, struct tm * CPP_CONST tmp)
982 {
983 const struct state * sp;
984 const struct ttinfo * ttisp;
985 int i;
986 const time_t t = *timep;
987
988 if (!lcl_is_set)
989 _tzset();
990 sp = lclptr;
991 #ifdef ALL_STATE
992 if (sp == NULL)
993 {
994 gmtsub(timep, offset, tmp);
995 return;
996 }
997 #endif /* defined ALL_STATE */
998 if (sp->timecnt == 0 || t < sp->ats[0])
999 {
1000 i = 0;
1001 while (sp->ttis[i].tt_isdst)
1002 if (++i >= sp->typecnt)
1003 {
1004 i = 0;
1005 break;
1006 }
1007 }
1008 else
1009 {
1010 for (i = 1; i < sp->timecnt; ++i)
1011 if (t < sp->ats[i])
1012 break;
1013 i = sp->types[i - 1];
1014 }
1015 ttisp = &sp->ttis[i];
1016 /*
1017 ** To get (wrong) behavior that's compatible with System V Release 2.0
1018 ** you'd replace the statement below with
1019 ** t += ttisp->tt_gmtoff;
1020 ** timesub(&t, 0L, sp, tmp);
1021 */
1022 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1023 tmp->tm_isdst = ttisp->tt_isdst;
1024 _tzname[tmp->tm_isdst] = (char *)&sp->chars[ttisp->tt_abbrind];
1025 #if 0
1026 /* tm_zone doesnt exist in windows msvcrt -Gunnar */
1027 tmp->tm_zone = (char *)&sp->chars[ttisp->tt_abbrind];
1028 #endif
1029 }
1030
1031 /*
1032 * @implemented
1033 */
1034 struct tm *
1035 localtime(const time_t * CPP_CONST timep)
1036 {
1037 static struct tm tm;
1038
1039 localsub(timep, 0L, &tm);
1040 return &tm;
1041 }
1042
1043 /*
1044 ** gmtsub is to gmtime as localsub is to localtime.
1045 */
1046
1047 static void
1048 gmtsub(const time_t * CPP_CONST timep, const long offset, struct tm * CPP_CONST tmp)
1049 {
1050 if (!gmt_is_set)
1051 {
1052 gmt_is_set = TRUE;
1053 #ifdef ALL_STATE
1054 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1055 if (gmtptr != NULL)
1056 #endif /* defined ALL_STATE */
1057 gmtload(gmtptr);
1058 }
1059 timesub(timep, offset, gmtptr, tmp);
1060 /*
1061 ** Could get fancy here and deliver something such as
1062 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1063 ** but this is no time for a treasure hunt.
1064 */
1065 #if 0
1066 /* tm_zone doesnt exist in windows msvcrt -Gunnar */
1067 if (offset != 0)
1068 tmp->tm_zone = TZ_NAME;
1069 else
1070 {
1071 #ifdef ALL_STATE
1072 if (gmtptr == NULL)
1073 tmp->TM_ZONE = GMT;
1074 else
1075 tmp->TM_ZONE = gmtptr->chars;
1076 #endif /* defined ALL_STATE */
1077 #ifndef ALL_STATE
1078 tmp->tm_zone = gmtptr->chars;
1079 #endif /* State Farm */
1080 }
1081 #endif /* if 0 */
1082 }
1083
1084 /*
1085 * @implemented
1086 */
1087 struct tm *
1088 gmtime(const time_t * CPP_CONST timep)
1089 {
1090 static struct tm tm;
1091
1092 gmtsub(timep, 0L, &tm);
1093 return &tm;
1094 }
1095
1096 static void
1097 timesub(const time_t * CPP_CONST timep, const long offset, const struct state * CPP_CONST sp, struct tm * CPP_CONST tmp)
1098 {
1099 const struct lsinfo * lp;
1100 long days;
1101 long rem;
1102 int y;
1103 int yleap;
1104 const int * ip;
1105 long corr;
1106 int hit;
1107 int i;
1108
1109 corr = 0;
1110 hit = FALSE;
1111 #ifdef ALL_STATE
1112 i = (sp == NULL) ? 0 : sp->leapcnt;
1113 #endif /* defined ALL_STATE */
1114 #ifndef ALL_STATE
1115 i = sp->leapcnt;
1116 #endif /* State Farm */
1117 while (--i >= 0)
1118 {
1119 lp = &sp->lsis[i];
1120 if (*timep >= lp->ls_trans)
1121 {
1122 if (*timep == lp->ls_trans)
1123 hit = ((i == 0 && lp->ls_corr > 0) ||
1124 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1125 corr = lp->ls_corr;
1126 break;
1127 }
1128 }
1129 days = *timep / SECSPERDAY;
1130 rem = *timep % SECSPERDAY;
1131 #ifdef mc68k
1132 if (*timep == 0x80000000)
1133 {
1134 /*
1135 ** A 3B1 muffs the division on the most negative number.
1136 */
1137 days = -24855;
1138 rem = -11648;
1139 }
1140 #endif /* mc68k */
1141 rem += (offset - corr);
1142 while (rem < 0)
1143 {
1144 rem += SECSPERDAY;
1145 --days;
1146 }
1147 while (rem >= SECSPERDAY)
1148 {
1149 rem -= SECSPERDAY;
1150 ++days;
1151 }
1152 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1153 rem = rem % SECSPERHOUR;
1154 tmp->tm_min = (int) (rem / SECSPERMIN);
1155 tmp->tm_sec = (int) (rem % SECSPERMIN);
1156 if (hit)
1157 /*
1158 ** A positive leap second requires a special
1159 ** representation. This uses "... ??:59:60".
1160 */
1161 ++(tmp->tm_sec);
1162 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1163 if (tmp->tm_wday < 0)
1164 tmp->tm_wday += DAYSPERWEEK;
1165 y = EPOCH_YEAR;
1166 if (days >= 0)
1167 for ( ; ; )
1168 {
1169 yleap = isleap(y);
1170 if (days < (long) year_lengths[yleap])
1171 break;
1172 ++y;
1173 days = days - (long) year_lengths[yleap];
1174 }
1175 else
1176 do {
1177 --y;
1178 yleap = isleap(y);
1179 days = days + (long) year_lengths[yleap];
1180 } while (days < 0);
1181 tmp->tm_year = y - TM_YEAR_BASE;
1182 tmp->tm_yday = (int) days;
1183 ip = mon_lengths[yleap];
1184 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1185 days = days - (long) ip[tmp->tm_mon];
1186 tmp->tm_mday = (int) (days + 1);
1187 tmp->tm_isdst = 0;
1188 #if 0
1189 /* tm_gmtoff doesnt exist in windows msvcrt -Gunnar */
1190 tmp->tm_gmtoff = offset;
1191 #endif
1192 }
1193
1194 /*
1195 ** A la X3J11
1196 */
1197
1198 /*
1199 * @implemented
1200 */
1201 char *
1202 asctime(const struct tm *timeptr)
1203 {
1204 static const char wday_name[DAYSPERWEEK][3] = {
1205 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1206 };
1207 static const char mon_name[MONSPERYEAR][3] = {
1208 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1209 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1210 };
1211 static char result[26];
1212
1213 (void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
1214 wday_name[timeptr->tm_wday],
1215 mon_name[timeptr->tm_mon],
1216 timeptr->tm_mday, timeptr->tm_hour,
1217 timeptr->tm_min, timeptr->tm_sec,
1218 TM_YEAR_BASE + timeptr->tm_year);
1219 return result;
1220 }
1221
1222 /*
1223 * @implemented
1224 */
1225 char *
1226 ctime(const time_t * CPP_CONST timep)
1227 {
1228 return asctime(localtime(timep));
1229 }
1230
1231 /*
1232 ** Adapted from code provided by Robert Elz, who writes:
1233 ** The "best" way to do mktime I think is based on an idea of Bob
1234 ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
1235 ** It does a binary search of the time_t space. Since time_t's are
1236 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1237 ** would still be very reasonable).
1238 */
1239
1240 #ifndef WRONG
1241 #define WRONG (-1)
1242 #endif /* !defined WRONG */
1243
1244 static void
1245 normalize(int * CPP_CONST tensptr, int * CPP_CONST unitsptr, const int base)
1246 {
1247 if (*unitsptr >= base)
1248 {
1249 *tensptr += *unitsptr / base;
1250 *unitsptr %= base;
1251 }
1252 else if (*unitsptr < 0)
1253 {
1254 --*tensptr;
1255 *unitsptr += base;
1256 if (*unitsptr < 0)
1257 {
1258 *tensptr -= 1 + (-*unitsptr) / base;
1259 *unitsptr = base - (-*unitsptr) % base;
1260 }
1261 }
1262 }
1263
1264 static int
1265 tmcomp(const struct tm * CPP_CONST atmp, const struct tm * CPP_CONST btmp)
1266 {
1267 int result;
1268
1269 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1270 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1271 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1272 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1273 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1274 result = atmp->tm_sec - btmp->tm_sec;
1275 return result;
1276 }
1277
1278 static time_t
1279 time2(struct tm *tmp, void (*const funcp)(const time_t * CPP_CONST, const long, struct tm *), const long offset, int * CPP_CONST okayp)
1280 {
1281 const struct state * sp;
1282 int dir;
1283 int bits;
1284 int i, j ;
1285 int saved_seconds;
1286 time_t newt;
1287 time_t t;
1288 struct tm yourtm, mytm;
1289
1290 *okayp = FALSE;
1291 yourtm = *tmp;
1292 if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
1293 normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
1294 normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
1295 normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
1296 normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
1297 while (yourtm.tm_mday <= 0)
1298 {
1299 --yourtm.tm_year;
1300 yourtm.tm_mday +=
1301 year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
1302 }
1303 for ( ; ; )
1304 {
1305 i = mon_lengths[isleap(yourtm.tm_year +
1306 TM_YEAR_BASE)][yourtm.tm_mon];
1307 if (yourtm.tm_mday <= i)
1308 break;
1309 yourtm.tm_mday -= i;
1310 if (++yourtm.tm_mon >= MONSPERYEAR)
1311 {
1312 yourtm.tm_mon = 0;
1313 ++yourtm.tm_year;
1314 }
1315 }
1316 saved_seconds = yourtm.tm_sec;
1317 yourtm.tm_sec = 0;
1318 /*
1319 ** Calculate the number of magnitude bits in a time_t
1320 ** (this works regardless of whether time_t is
1321 ** signed or unsigned, though lint complains if unsigned).
1322 */
1323 for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
1324 ;
1325 /*
1326 ** If time_t is signed, then 0 is the median value,
1327 ** if time_t is unsigned, then 1 << bits is median.
1328 */
1329 #ifdef _MSVCRT_LIB_
1330 t = (time_t) ((1 << bits) - 1);
1331 #else // TODO: FIXME: review which is correct
1332 t = (time_t) 1 << bits;
1333 #endif /*_MSVCRT_LIB_*/
1334
1335 for ( ; ; )
1336 {
1337 (*funcp)(&t, offset, &mytm);
1338 dir = tmcomp(&mytm, &yourtm);
1339 if (dir != 0)
1340 {
1341 if (bits-- < 0)
1342 return WRONG;
1343 if (bits < 0)
1344 --t;
1345 else if (dir > 0)
1346 t -= (time_t) 1 << bits;
1347 else t += (time_t) 1 << bits;
1348 continue;
1349 }
1350 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1351 break;
1352 /*
1353 ** Right time, wrong type.
1354 ** Hunt for right time, right type.
1355 ** It's okay to guess wrong since the guess
1356 ** gets checked.
1357 */
1358 sp = (const struct state *)
1359 ((funcp == localsub) ? lclptr : gmtptr);
1360 #ifdef ALL_STATE
1361 if (sp == NULL)
1362 return WRONG;
1363 #endif /* defined ALL_STATE */
1364 for (i = 0; i < sp->typecnt; ++i)
1365 {
1366 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1367 continue;
1368 for (j = 0; j < sp->typecnt; ++j)
1369 {
1370 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1371 continue;
1372 newt = t + sp->ttis[j].tt_gmtoff -
1373 sp->ttis[i].tt_gmtoff;
1374 (*funcp)(&newt, offset, &mytm);
1375 if (tmcomp(&mytm, &yourtm) != 0)
1376 continue;
1377 if (mytm.tm_isdst != yourtm.tm_isdst)
1378 continue;
1379 /*
1380 ** We have a match.
1381 */
1382 t = newt;
1383 goto label;
1384 }
1385 }
1386 return WRONG;
1387 }
1388 label:
1389 t += saved_seconds;
1390 (*funcp)(&t, offset, tmp);
1391 *okayp = TRUE;
1392 return t;
1393 }
1394
1395 static time_t
1396 time1(struct tm * CPP_CONST tmp, void (*const funcp)(const time_t * CPP_CONST, const long, struct tm *), const long offset)
1397 {
1398 time_t t;
1399 const struct state * sp;
1400 int samei, otheri;
1401 int okay;
1402
1403 if (tmp->tm_isdst > 1)
1404 tmp->tm_isdst = 1;
1405 t = time2(tmp, funcp, offset, &okay);
1406 if (okay || tmp->tm_isdst < 0)
1407 return t;
1408 /*
1409 ** We're supposed to assume that somebody took a time of one type
1410 ** and did some math on it that yielded a "struct tm" that's bad.
1411 ** We try to divine the type they started from and adjust to the
1412 ** type they need.
1413 */
1414 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
1415 #ifdef ALL_STATE
1416 if (sp == NULL)
1417 return WRONG;
1418 #endif /* defined ALL_STATE */
1419 for (samei = 0; samei < sp->typecnt; ++samei)
1420 {
1421 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1422 continue;
1423 for (otheri = 0; otheri < sp->typecnt; ++otheri)
1424 {
1425 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1426 continue;
1427 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1428 sp->ttis[samei].tt_gmtoff;
1429 tmp->tm_isdst = !tmp->tm_isdst;
1430 t = time2(tmp, funcp, offset, &okay);
1431 if (okay)
1432 return t;
1433 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1434 sp->ttis[samei].tt_gmtoff;
1435 tmp->tm_isdst = !tmp->tm_isdst;
1436 }
1437 }
1438 return WRONG;
1439 }
1440
1441 /*
1442 * @implemented
1443 */
1444 time_t
1445 mktime(struct tm * tmp)
1446 {
1447 return time1(tmp, localsub, 0L);
1448 }