home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / time / bsdtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-10  |  34.4 KB  |  1,410 lines

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