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