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