Blame resolv/ns_date.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
Packit 6c4009
 * Copyright (c) 1999 by Internet Software Consortium.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
Packit 6c4009
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit 6c4009
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
Packit 6c4009
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit 6c4009
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit 6c4009
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
Packit 6c4009
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* Import. */
Packit 6c4009
Packit 6c4009
#include <arpa/nameser.h>
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
#define SPRINTF(x) ((size_t)sprintf x)
Packit 6c4009
Packit 6c4009
/* Forward. */
Packit 6c4009
Packit 6c4009
static int	datepart(const char *, int, int, int, int *);
Packit 6c4009
Packit 6c4009
/* Public. */
Packit 6c4009
Packit 6c4009
/*%
Packit 6c4009
 * Convert a date in ASCII into the number of seconds since
Packit 6c4009
 * 1 January 1970 (GMT assumed).  Format is yyyymmddhhmmss, all
Packit 6c4009
 * digits required, no spaces allowed.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
uint32_t
Packit 6c4009
ns_datetosecs(const char *cp, int *errp) {
Packit 6c4009
	struct tm time;
Packit 6c4009
	uint32_t result;
Packit 6c4009
	int mdays, i;
Packit 6c4009
	static const int days_per_month[12] =
Packit 6c4009
		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Packit 6c4009
Packit 6c4009
	if (strlen(cp) != 14U) {
Packit 6c4009
		*errp = 1;
Packit 6c4009
		return (0);
Packit 6c4009
	}
Packit 6c4009
	*errp = 0;
Packit 6c4009
Packit 6c4009
	memset(&time, 0, sizeof time);
Packit 6c4009
	time.tm_year  = datepart(cp +  0, 4, 1990, 9999, errp) - 1900;
Packit 6c4009
	time.tm_mon   = datepart(cp +  4, 2,   01,   12, errp) - 1;
Packit 6c4009
	time.tm_mday  = datepart(cp +  6, 2,   01,   31, errp);
Packit 6c4009
	time.tm_hour  = datepart(cp +  8, 2,   00,   23, errp);
Packit 6c4009
	time.tm_min   = datepart(cp + 10, 2,   00,   59, errp);
Packit 6c4009
	time.tm_sec   = datepart(cp + 12, 2,   00,   59, errp);
Packit 6c4009
	if (*errp)		/*%< Any parse errors? */
Packit 6c4009
		return (0);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * OK, now because timegm() is not available in all environments,
Packit 6c4009
	 * we will do it by hand.  Roll up sleeves, curse the gods, begin!
Packit 6c4009
	 */
Packit 6c4009
Packit 6c4009
#define SECS_PER_DAY    ((uint32_t)24*60*60)
Packit 6c4009
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
Packit 6c4009
Packit 6c4009
	result  = time.tm_sec;				/*%< Seconds */
Packit 6c4009
	result += time.tm_min * 60;			/*%< Minutes */
Packit 6c4009
	result += time.tm_hour * (60*60);		/*%< Hours */
Packit 6c4009
	result += (time.tm_mday - 1) * SECS_PER_DAY;	/*%< Days */
Packit 6c4009
	/* Months are trickier.  Look without leaping, then leap */
Packit 6c4009
	mdays = 0;
Packit 6c4009
	for (i = 0; i < time.tm_mon; i++)
Packit 6c4009
		mdays += days_per_month[i];
Packit 6c4009
	result += mdays * SECS_PER_DAY;			/*%< Months */
Packit 6c4009
	if (time.tm_mon > 1 && isleap(1900+time.tm_year))
Packit 6c4009
		result += SECS_PER_DAY;		/*%< Add leapday for this year */
Packit 6c4009
	/* First figure years without leapdays, then add them in.  */
Packit 6c4009
	/* The loop is slow, FIXME, but simple and accurate.  */
Packit 6c4009
	result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */
Packit 6c4009
	for (i = 70; i < time.tm_year; i++)
Packit 6c4009
		if (isleap(1900+i))
Packit 6c4009
			result += SECS_PER_DAY; /*%< Add leapday for prev year */
Packit 6c4009
	return (result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Private. */
Packit 6c4009
Packit 6c4009
/*%
Packit 6c4009
 * Parse part of a date.  Set error flag if any error.
Packit 6c4009
 * Don't reset the flag if there is no error.
Packit 6c4009
 */
Packit 6c4009
static int
Packit 6c4009
datepart(const char *buf, int size, int min, int max, int *errp) {
Packit 6c4009
	int result = 0;
Packit 6c4009
	int i;
Packit 6c4009
Packit 6c4009
	for (i = 0; i < size; i++) {
Packit 6c4009
		if (!isdigit((unsigned char)(buf[i])))
Packit 6c4009
			*errp = 1;
Packit 6c4009
		result = (result * 10) + buf[i] - '0';
Packit 6c4009
	}
Packit 6c4009
	if (result < min)
Packit 6c4009
		*errp = 1;
Packit 6c4009
	if (result > max)
Packit 6c4009
		*errp = 1;
Packit 6c4009
	return (result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*! \file */