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