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