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