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