source: trunk/third/cns/src/admin/maketime.c @ 8789

Revision 8789, 1.9 KB checked in by ghudson, 28 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r8788, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright 1990 by the Massachusetts Institute of Technology.
3 *
4 * For copying and distribution information, please see the file
5 * <mit-copyright.h>.
6 *
7 * Convert a struct tm * to a UNIX time.
8 */
9
10#include <mit-copyright.h>
11#include <osconf.h>
12#ifdef NEED_TIME_H
13#include <time.h>
14#endif
15#include <sys/time.h>
16
17#define daysinyear(y) (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
18
19#define SECSPERDAY 24*60*60
20#define SECSPERHOUR 60*60
21#define SECSPERMIN 60
22
23static int cumdays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
24                             365};
25
26static int leapyear[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
27static int nonleapyear[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
28
29long
30maketime(tp, local)
31register struct tm *tp;
32int local;
33{
34    register long retval;
35    int foo;
36    int *marray;
37
38    if (tp->tm_mon < 0 || tp->tm_mon > 11 ||
39        tp->tm_hour < 0 || tp->tm_hour > 23 ||
40        tp->tm_min < 0 || tp->tm_min > 59 ||
41        tp->tm_sec < 0 || tp->tm_sec > 59) /* out of range */
42        return 0;
43
44    retval = 0;
45    if (tp->tm_year < 1900)
46        foo = tp->tm_year + 1900;
47    else
48        foo = tp->tm_year;
49
50    if (foo < 1901 || foo > 2038)       /* year is too small/large */
51        return 0;
52
53    if (daysinyear(foo) == 366) {
54        if (tp->tm_mon > 1)
55            retval+= SECSPERDAY;        /* add leap day */
56        marray = leapyear;
57    } else
58        marray = nonleapyear;
59
60    if (tp->tm_mday < 0 || tp->tm_mday > marray[tp->tm_mon])
61        return 0;                       /* out of range */
62
63    while (--foo >= 1970)
64        retval += daysinyear(foo) * SECSPERDAY;
65
66    retval += cumdays[tp->tm_mon] * SECSPERDAY;
67    retval += (tp->tm_mday-1) * SECSPERDAY;
68    retval += tp->tm_hour * SECSPERHOUR + tp->tm_min * SECSPERMIN + tp->tm_sec;
69
70    if (local) {
71        /* need to use local time, so we retrieve timezone info */
72        struct timezone tz;
73        struct timeval tv;
74        if (gettimeofday(&tv, &tz) < 0) {
75            /* some error--give up? */
76            return(retval);
77        }
78        retval += tz.tz_minuteswest * SECSPERMIN;
79    }
80    return(retval);
81}
Note: See TracBrowser for help on using the repository browser.