]> git.saurik.com Git - apple/libresolv.git/blob - ns_date.c
libresolv-19.tar.gz
[apple/libresolv.git] / 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 __APPLE__
19 #ifndef lint
20 static const char rcsid[] = "$Id: ns_date.c,v 1.1 2006/03/01 19:01:36 majka Exp $";
21 #endif
22 #endif
23
24 /* Import. */
25
26 #ifndef __APPLE__
27 #include "port_before.h"
28 #endif
29
30 #include <arpa/nameser.h>
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37
38 #ifndef __APPLE__
39 #include "port_after.h"
40 #endif
41
42 #ifdef SPRINTF_CHAR
43 # define SPRINTF(x) strlen(sprintf/**/x)
44 #else
45 # define SPRINTF(x) ((size_t)sprintf x)
46 #endif
47
48 /* Forward. */
49
50 static int datepart(const char *, int, int, int, int *);
51
52 /* Public. */
53
54 /* Convert a date in ASCII into the number of seconds since
55 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all
56 digits required, no spaces allowed. */
57
58 u_int32_t
59 ns_datetosecs(const char *cp, int *errp) {
60 struct tm time;
61 u_int32_t result;
62 int mdays, i;
63 static const int days_per_month[12] =
64 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
65
66 if (strlen(cp) != 14) {
67 *errp = 1;
68 return (0);
69 }
70 *errp = 0;
71
72 memset(&time, 0, sizeof time);
73 time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900;
74 time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1;
75 time.tm_mday = datepart(cp + 6, 2, 01, 31, errp);
76 time.tm_hour = datepart(cp + 8, 2, 00, 23, errp);
77 time.tm_min = datepart(cp + 10, 2, 00, 59, errp);
78 time.tm_sec = datepart(cp + 12, 2, 00, 59, errp);
79 if (*errp) /* Any parse errors? */
80 return (0);
81
82 /*
83 * OK, now because timegm() is not available in all environments,
84 * we will do it by hand. Roll up sleeves, curse the gods, begin!
85 */
86
87 #define SECS_PER_DAY ((u_int32_t)24*60*60)
88 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
89
90 result = time.tm_sec; /* Seconds */
91 result += time.tm_min * 60; /* Minutes */
92 result += time.tm_hour * (60*60); /* Hours */
93 result += (time.tm_mday - 1) * SECS_PER_DAY; /* Days */
94
95 /* Months are trickier. Look without leaping, then leap */
96 mdays = 0;
97 for (i = 0; i < time.tm_mon; i++)
98 mdays += days_per_month[i];
99 result += mdays * SECS_PER_DAY; /* Months */
100 if (time.tm_mon > 1 && isleap(1900+time.tm_year))
101 result += SECS_PER_DAY; /* Add leapday for this year */
102
103 /* First figure years without leapdays, then add them in. */
104 /* The loop is slow, FIXME, but simple and accurate. */
105 result += (time.tm_year - 70) * (SECS_PER_DAY*365); /* Years */
106 for (i = 70; i < time.tm_year; i++)
107 if (isleap(1900+i))
108 result += SECS_PER_DAY; /* Add leapday for prev year */
109
110 return (result);
111 }
112
113 /* Private. */
114
115 /*
116 * Parse part of a date. Set error flag if any error.
117 * Don't reset the flag if there is no error.
118 */
119 static int
120 datepart(const char *buf, int size, int min, int max, int *errp) {
121 int result = 0;
122 int i;
123
124 for (i = 0; i < size; i++) {
125 if (!isdigit((unsigned char)(buf[i])))
126 *errp = 1;
127 result = (result * 10) + buf[i] - '0';
128 }
129 if (result < min)
130 *errp = 1;
131 if (result > max)
132 *errp = 1;
133 return (result);
134 }