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