Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / minires / ns_date.c
1 /*
2  * Copyright (c) 1999 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_date.c,v 1.2 2000/04/11 01:58:39 mellon Exp $";
20 #endif
21
22 /* Import. */
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33
34 #include "minires/minires.h"
35 #include "arpa/nameser.h"
36
37 #ifdef SPRINTF_CHAR
38 # define SPRINTF(x) strlen(sprintf/**/x)
39 #else
40 # define SPRINTF(x) ((size_t)sprintf x)
41 #endif
42
43 /* Forward. */
44
45 static int      datepart(const char *, int, int, int, int *);
46
47 /* Public. */
48
49 /* Convert a date in ASCII into the number of seconds since
50    1 January 1970 (GMT assumed).  Format is yyyymmddhhmmss, all
51    digits required, no spaces allowed.  */
52
53 u_int32_t
54 ns_datetosecs(const char *cp, int *errp) {
55         struct tm time;
56         u_int32_t result;
57         int mdays, i;
58         static const int days_per_month[12] =
59                 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
60
61         if (strlen(cp) != 14) {
62                 *errp = 1;
63                 return (0);
64         }
65         *errp = 0;
66
67         memset(&time, 0, sizeof time);
68         time.tm_year  = datepart(cp +  0, 4, 1990, 9999, errp) - 1900;
69         time.tm_mon   = datepart(cp +  4, 2,   01,   12, errp) - 1;
70         time.tm_mday  = datepart(cp +  6, 2,   01,   31, errp);
71         time.tm_hour  = datepart(cp +  8, 2,   00,   23, errp);
72         time.tm_min   = datepart(cp + 10, 2,   00,   59, errp);
73         time.tm_sec   = datepart(cp + 12, 2,   00,   59, errp);
74         if (*errp)              /* Any parse errors? */
75                 return (0);
76
77         /* 
78          * OK, now because timegm() is not available in all environments,
79          * we will do it by hand.  Roll up sleeves, curse the gods, begin!
80          */
81
82 #define SECS_PER_DAY    ((u_int32_t)24*60*60)
83 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
84
85         result  = time.tm_sec;                          /* Seconds */
86         result += time.tm_min * 60;                     /* Minutes */
87         result += time.tm_hour * (60*60);               /* Hours */
88         result += (time.tm_mday - 1) * SECS_PER_DAY;    /* Days */
89
90         /* Months are trickier.  Look without leaping, then leap */
91         mdays = 0;
92         for (i = 0; i < time.tm_mon; i++)
93                 mdays += days_per_month[i];
94         result += mdays * SECS_PER_DAY;                 /* Months */
95         if (time.tm_mon > 1 && isleap(1900+time.tm_year))
96                 result += SECS_PER_DAY;         /* Add leapday for this year */
97
98         /* First figure years without leapdays, then add them in.  */
99         /* The loop is slow, FIXME, but simple and accurate.  */
100         result += (time.tm_year - 70) * (SECS_PER_DAY*365); /* Years */
101         for (i = 70; i < time.tm_year; i++)
102                 if (isleap(1900+i))
103                         result += SECS_PER_DAY; /* Add leapday for prev year */
104
105         return (result);
106 }
107
108 /* Private. */
109
110 /*
111  * Parse part of a date.  Set error flag if any error.
112  * Don't reset the flag if there is no error.
113  */
114 static int
115 datepart(const char *buf, int size, int min, int max, int *errp) {
116         int result = 0;
117         int i;
118
119         for (i = 0; i < size; i++) {
120                 if (!isdigit(buf[i]))
121                         *errp = 1;
122                 result = (result * 10) + buf[i] - '0';
123         }
124         if (result < min)
125                 *errp = 1;
126         if (result > max)
127                 *errp = 1;
128         return (result);
129 }