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