Annotation of 43BSDTahoe/new/nntp/server/time.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char    *sccsid = "@(#)time.c   1.7     (Berkeley) 6/26/87";
                      3: #endif
                      4: 
                      5: /*
                      6:  * Collection of routines for dealing with ASCII time strings.
                      7:  * These may actually be useful in their own right.
                      8:  */
                      9: 
                     10: #include <stdio.h>
                     11: #include <sys/types.h>
                     12: #include <sys/time.h>
                     13: #include <ctype.h>
                     14: #include <strings.h>
                     15: 
                     16: /*
                     17:  * dtol -- convert date to long integer.  This is not implicitly
                     18:  * local time, or any other kind of time, for that matter.  If you
                     19:  * pass it a date you think is GMT, you wind up with that number of
                     20:  * seconds...
                     21:  *
                     22:  *     Parameters:             "date_ascii" is in the form "yymmddhhmmss".
                     23:  *
                     24:  *     Returns:                Long integer containing number
                     25:  *                             of seconds since 000000 Jan 1, 1970,
                     26:  *                             and "date".  -1 on error.
                     27:  *
                     28:  *     Side effects:           None.
                     29:  */
                     30: 
                     31: #define twodigtoi(x)   (((x[0]) - '0') + (x[1] - '0')*10)
                     32: #define        dysize(y)       ((y % 4 ? 365 : 366))
                     33: 
                     34: static int     dmsize[12] =
                     35:     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                     36: 
                     37: long
                     38: dtol(date_ascii)
                     39:        char    *date_ascii;
                     40: {
                     41:        char    date[32], *date_str;
                     42:        char    *lhs, *rhs;
                     43:        char    temp;
                     44:        long    seconds;
                     45:        int     year, month, day, hour, mins, secs;
                     46:        int     len, i;
                     47: 
                     48:        len = strlen(date_ascii);
                     49:        if (len != sizeof("yymmddhhmmss")-1)
                     50:                return (-1);
                     51: 
                     52:        (void) strcpy(date, date_ascii);
                     53:        date_str = date;
                     54: 
                     55: #ifdef debug
                     56:        printf("date_str = %s\n", date_str);
                     57: #endif
                     58:        rhs = date_str + len - 1;
                     59:        lhs = date_str;
                     60: 
                     61:        for (; lhs < rhs; ++lhs, --rhs) {
                     62:                temp = *lhs;
                     63:                *lhs = *rhs;
                     64:                *rhs = temp;
                     65:                if (!isdigit(temp) || !isdigit(*lhs))
                     66:                        return (-1);
                     67:        }
                     68: 
                     69:        lhs = date_str;
                     70: #ifdef debug
                     71:        printf("date_str = %s\n", date_str);
                     72: #endif
                     73: 
                     74:        secs = twodigtoi(lhs);
                     75:        lhs += 2;
                     76:        mins = twodigtoi(lhs);
                     77:        lhs += 2;
                     78:        hour = twodigtoi(lhs);
                     79:        lhs += 2;
                     80:        day = twodigtoi(lhs);
                     81:        lhs += 2;
                     82:        month = twodigtoi(lhs);
                     83:        lhs += 2;
                     84:        year = twodigtoi(lhs);
                     85: 
                     86:        if (month < 1 || month > 12 ||
                     87:            day < 1 || day > 31 ||
                     88:            mins < 0 || mins > 59 ||
                     89:            secs < 0 || secs > 59)
                     90:                return (-1);
                     91:        if (hour == 24) {
                     92:                hour = 0;
                     93:                day++;
                     94:        }
                     95:        if (hour < 0 || hour > 23)
                     96:                return (-1);
                     97:        seconds = 0;
                     98:        year += 1900;
                     99:        for (i = 1970; i < year; i++)
                    100:                seconds += dysize(i);
                    101:        /* Leap year */
                    102:        if (dysize(year) == 366 && month >= 3)
                    103:                seconds++;
                    104:        while (--month)
                    105:                seconds += dmsize[month-1];
                    106:        seconds += day-1;
                    107:        seconds = 24 * seconds + hour;
                    108:        seconds = 60 * seconds + mins;
                    109:        seconds = 60 * seconds + secs;
                    110: 
                    111:        return (seconds);
                    112: }
                    113: 
                    114: 
                    115: /*
                    116:  * ltod -- convert long integer to date string.
                    117:  *
                    118:  *     Parameters:             "date" is in the number of seconds
                    119:  *                             since the epoch.
                    120:  *
                    121:  *     Returns:                Pointer to static data in the form
                    122:  *                             yymmddhhmmss\0.
                    123:  *
                    124:  *     Side effects:           None.
                    125:  */
                    126: 
                    127: char *
                    128: ltod(date)
                    129:        long            date;
                    130: {
                    131:        static char     timebuf[32];
                    132:        struct tm       *tp;
                    133: 
                    134:        tp = gmtime(&date);
                    135: 
                    136:        (void) sprintf(timebuf, "%02d%02d%02d%02d%02d%02d",
                    137:                tp->tm_year,
                    138:                tp->tm_mon + 1,         /* 0 to 11??? How silly. */
                    139:                tp->tm_mday,
                    140:                tp->tm_hour,
                    141:                tp->tm_min,
                    142:                tp->tm_sec);
                    143: 
                    144:        return (timebuf);
                    145: }
                    146: 
                    147: 
                    148: /*
                    149:  * local_to_gmt -- convert a local time (in form of number of
                    150:  * seconds since you-know-when) to GMT.
                    151:  *
                    152:  *     Parameters:     "date" is date we want converted, in
                    153:  *                     seconds since 000000 1 Jan 1970.
                    154:  *
                    155:  *     Returns:        Number of seconds corrected for local
                    156:  *                     and dst.
                    157:  */
                    158: 
                    159: long
                    160: local_to_gmt(date)
                    161:        long    date;
                    162: {
                    163:        struct  timeval tv;
                    164:        struct  timezone tz;
                    165: 
                    166:        (void) gettimeofday(&tv, &tz);
                    167:        date += (long) tz.tz_minuteswest * 60;
                    168: 
                    169:        /* now fix up local daylight time */
                    170:        if (localtime((time_t *)&date)->tm_isdst)
                    171:                date -= 60*60;
                    172: 
                    173:        return (date);
                    174: }
                    175: 
                    176: /*
                    177:  * gmt_to_local -- take a GMT time expressed in seconds since
                    178:  * the epoch, and convert it to local time.
                    179:  *
                    180:  *     Parameters:     "date" is the number of seconds...
                    181:  *
                    182:  *     Returns:        Number of seconds corrected to reflect
                    183:  *                     local time and dst.
                    184:  */
                    185: 
                    186: long
                    187: gmt_to_local(date)
                    188:        long    date;
                    189: {
                    190:        struct  timeval tv;
                    191:        struct  timezone tz;
                    192: 
                    193:        (void) gettimeofday(&tv, &tz);
                    194:        date -= (long) tz.tz_minuteswest * 60;
                    195: 
                    196:        /* now fix up local daylight time */
                    197:        if (localtime((time_t *)&date)->tm_isdst)
                    198:                date += 60*60;
                    199: 
                    200:        return (date);
                    201: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.