10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 30Sstevel@tonic-gate * Copyright (c) 1999 by Internet Software Consortium. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #ifndef lint 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ns_date.c,v 1.6 2005/04/27 04:56:39 sra Exp $"; 200Sstevel@tonic-gate #endif 210Sstevel@tonic-gate 220Sstevel@tonic-gate /* Import. */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate #include "port_before.h" 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <arpa/nameser.h> 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <ctype.h> 290Sstevel@tonic-gate #include <errno.h> 300Sstevel@tonic-gate #include <stdio.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate #include <time.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include "port_after.h" 350Sstevel@tonic-gate 360Sstevel@tonic-gate #ifdef SPRINTF_CHAR 370Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x) 380Sstevel@tonic-gate #else 390Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x) 400Sstevel@tonic-gate #endif 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* Forward. */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate static int datepart(const char *, int, int, int, int *); 450Sstevel@tonic-gate 460Sstevel@tonic-gate /* Public. */ 470Sstevel@tonic-gate 48*11038SRao.Shoaib@Sun.COM /*% 49*11038SRao.Shoaib@Sun.COM * Convert a date in ASCII into the number of seconds since 50*11038SRao.Shoaib@Sun.COM * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all 51*11038SRao.Shoaib@Sun.COM * digits required, no spaces allowed. 52*11038SRao.Shoaib@Sun.COM */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate u_int32_t 550Sstevel@tonic-gate ns_datetosecs(const char *cp, int *errp) { 560Sstevel@tonic-gate struct tm time; 570Sstevel@tonic-gate u_int32_t result; 580Sstevel@tonic-gate int mdays, i; 590Sstevel@tonic-gate static const int days_per_month[12] = 600Sstevel@tonic-gate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 610Sstevel@tonic-gate 62*11038SRao.Shoaib@Sun.COM if (strlen(cp) != 14U) { 630Sstevel@tonic-gate *errp = 1; 640Sstevel@tonic-gate return (0); 650Sstevel@tonic-gate } 660Sstevel@tonic-gate *errp = 0; 670Sstevel@tonic-gate 680Sstevel@tonic-gate memset(&time, 0, sizeof time); 690Sstevel@tonic-gate time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900; 700Sstevel@tonic-gate time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1; 710Sstevel@tonic-gate time.tm_mday = datepart(cp + 6, 2, 01, 31, errp); 720Sstevel@tonic-gate time.tm_hour = datepart(cp + 8, 2, 00, 23, errp); 730Sstevel@tonic-gate time.tm_min = datepart(cp + 10, 2, 00, 59, errp); 740Sstevel@tonic-gate time.tm_sec = datepart(cp + 12, 2, 00, 59, errp); 75*11038SRao.Shoaib@Sun.COM if (*errp) /*%< Any parse errors? */ 760Sstevel@tonic-gate return (0); 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * OK, now because timegm() is not available in all environments, 800Sstevel@tonic-gate * we will do it by hand. Roll up sleeves, curse the gods, begin! 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate 830Sstevel@tonic-gate #define SECS_PER_DAY ((u_int32_t)24*60*60) 840Sstevel@tonic-gate #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 850Sstevel@tonic-gate 86*11038SRao.Shoaib@Sun.COM result = time.tm_sec; /*%< Seconds */ 87*11038SRao.Shoaib@Sun.COM result += time.tm_min * 60; /*%< Minutes */ 88*11038SRao.Shoaib@Sun.COM result += time.tm_hour * (60*60); /*%< Hours */ 89*11038SRao.Shoaib@Sun.COM result += (time.tm_mday - 1) * SECS_PER_DAY; /*%< Days */ 900Sstevel@tonic-gate /* Months are trickier. Look without leaping, then leap */ 910Sstevel@tonic-gate mdays = 0; 920Sstevel@tonic-gate for (i = 0; i < time.tm_mon; i++) 930Sstevel@tonic-gate mdays += days_per_month[i]; 94*11038SRao.Shoaib@Sun.COM result += mdays * SECS_PER_DAY; /*%< Months */ 950Sstevel@tonic-gate if (time.tm_mon > 1 && isleap(1900+time.tm_year)) 96*11038SRao.Shoaib@Sun.COM result += SECS_PER_DAY; /*%< Add leapday for this year */ 970Sstevel@tonic-gate /* First figure years without leapdays, then add them in. */ 980Sstevel@tonic-gate /* The loop is slow, FIXME, but simple and accurate. */ 99*11038SRao.Shoaib@Sun.COM result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */ 1000Sstevel@tonic-gate for (i = 70; i < time.tm_year; i++) 1010Sstevel@tonic-gate if (isleap(1900+i)) 102*11038SRao.Shoaib@Sun.COM result += SECS_PER_DAY; /*%< Add leapday for prev year */ 1030Sstevel@tonic-gate return (result); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* Private. */ 1070Sstevel@tonic-gate 108*11038SRao.Shoaib@Sun.COM /*% 1090Sstevel@tonic-gate * Parse part of a date. Set error flag if any error. 1100Sstevel@tonic-gate * Don't reset the flag if there is no error. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate static int 1130Sstevel@tonic-gate datepart(const char *buf, int size, int min, int max, int *errp) { 1140Sstevel@tonic-gate int result = 0; 1150Sstevel@tonic-gate int i; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate for (i = 0; i < size; i++) { 1180Sstevel@tonic-gate if (!isdigit((unsigned char)(buf[i]))) 1190Sstevel@tonic-gate *errp = 1; 1200Sstevel@tonic-gate result = (result * 10) + buf[i] - '0'; 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate if (result < min) 1230Sstevel@tonic-gate *errp = 1; 1240Sstevel@tonic-gate if (result > max) 1250Sstevel@tonic-gate *errp = 1; 1260Sstevel@tonic-gate return (result); 1270Sstevel@tonic-gate } 128*11038SRao.Shoaib@Sun.COM 129*11038SRao.Shoaib@Sun.COM /*! \file */ 130