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