ns_date.c revision 1.1.1.2
1/* $NetBSD: ns_date.c,v 1.1.1.2 2012/09/09 16:08:03 christos 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#ifndef lint 21static const char rcsid[] = "Id: ns_date.c,v 1.6 2005/04/27 04:56:39 sra Exp "; 22#endif 23 24/* Import. */ 25 26#include "port_before.h" 27 28#include <arpa/nameser.h> 29 30#include <ctype.h> 31#include <errno.h> 32#include <stdio.h> 33#include <string.h> 34#include <time.h> 35 36#include "port_after.h" 37 38#ifdef SPRINTF_CHAR 39# define SPRINTF(x) strlen(sprintf/**/x) 40#else 41# define SPRINTF(x) ((size_t)sprintf x) 42#endif 43 44/* Forward. */ 45 46static int datepart(const char *, int, int, int, int *); 47 48/* Public. */ 49 50/*% 51 * Convert a date in ASCII into the number of seconds since 52 * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all 53 * digits required, no spaces allowed. 54 */ 55 56u_int32_t 57ns_datetosecs(const char *cp, int *errp) { 58 struct tm time; 59 u_int32_t result; 60 int mdays, i; 61 static const int days_per_month[12] = 62 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 63 64 if (strlen(cp) != 14U) { 65 *errp = 1; 66 return (0); 67 } 68 *errp = 0; 69 70 memset(&time, 0, sizeof time); 71 time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900; 72 time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1; 73 time.tm_mday = datepart(cp + 6, 2, 01, 31, errp); 74 time.tm_hour = datepart(cp + 8, 2, 00, 23, errp); 75 time.tm_min = datepart(cp + 10, 2, 00, 59, errp); 76 time.tm_sec = datepart(cp + 12, 2, 00, 59, errp); 77 if (*errp) /*%< Any parse errors? */ 78 return (0); 79 80 /* 81 * OK, now because timegm() is not available in all environments, 82 * we will do it by hand. Roll up sleeves, curse the gods, begin! 83 */ 84 85#define SECS_PER_DAY ((u_int32_t)24*60*60) 86#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 87 88 result = time.tm_sec; /*%< Seconds */ 89 result += time.tm_min * 60; /*%< Minutes */ 90 result += time.tm_hour * (60*60); /*%< Hours */ 91 result += (time.tm_mday - 1) * SECS_PER_DAY; /*%< Days */ 92 /* Months are trickier. Look without leaping, then leap */ 93 mdays = 0; 94 for (i = 0; i < time.tm_mon; i++) 95 mdays += days_per_month[i]; 96 result += mdays * SECS_PER_DAY; /*%< Months */ 97 if (time.tm_mon > 1 && isleap(1900+time.tm_year)) 98 result += SECS_PER_DAY; /*%< Add leapday for this year */ 99 /* First figure years without leapdays, then add them in. */ 100 /* The loop is slow, FIXME, but simple and accurate. */ 101 result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */ 102 for (i = 70; i < time.tm_year; i++) 103 if (isleap(1900+i)) 104 result += SECS_PER_DAY; /*%< Add leapday for prev year */ 105 return (result); 106} 107 108/* Private. */ 109 110/*% 111 * Parse part of a date. Set error flag if any error. 112 * Don't reset the flag if there is no error. 113 */ 114static int 115datepart(const char *buf, int size, int min, int max, int *errp) { 116 int result = 0; 117 int i; 118 119 for (i = 0; i < size; i++) { 120 if (!isdigit((unsigned char)(buf[i]))) 121 *errp = 1; 122 result = (result * 10) + buf[i] - '0'; 123 } 124 if (result < min) 125 *errp = 1; 126 if (result > max) 127 *errp = 1; 128 return (result); 129} 130 131/*! \file */ 132