17c478bd9Sstevel@tonic-gate /* 2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 37c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Internet Software Consortium. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 87c478bd9Sstevel@tonic-gate * 9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate #ifndef lint 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ns_date.c,v 1.6 2005/04/27 04:56:39 sra Exp $"; 207c478bd9Sstevel@tonic-gate #endif 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* Import. */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include "port_before.h" 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <ctype.h> 297c478bd9Sstevel@tonic-gate #include <errno.h> 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <time.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "port_after.h" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR 377c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x) 387c478bd9Sstevel@tonic-gate #else 397c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x) 407c478bd9Sstevel@tonic-gate #endif 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* Forward. */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static int datepart(const char *, int, int, int, int *); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* Public. */ 477c478bd9Sstevel@tonic-gate 48*9525b14bSRao Shoaib /*% 49*9525b14bSRao Shoaib * Convert a date in ASCII into the number of seconds since 50*9525b14bSRao Shoaib * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all 51*9525b14bSRao Shoaib * digits required, no spaces allowed. 52*9525b14bSRao Shoaib */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate u_int32_t 557c478bd9Sstevel@tonic-gate ns_datetosecs(const char *cp, int *errp) { 567c478bd9Sstevel@tonic-gate struct tm time; 577c478bd9Sstevel@tonic-gate u_int32_t result; 587c478bd9Sstevel@tonic-gate int mdays, i; 597c478bd9Sstevel@tonic-gate static const int days_per_month[12] = 607c478bd9Sstevel@tonic-gate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 617c478bd9Sstevel@tonic-gate 62*9525b14bSRao Shoaib if (strlen(cp) != 14U) { 637c478bd9Sstevel@tonic-gate *errp = 1; 647c478bd9Sstevel@tonic-gate return (0); 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate *errp = 0; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate memset(&time, 0, sizeof time); 697c478bd9Sstevel@tonic-gate time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900; 707c478bd9Sstevel@tonic-gate time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1; 717c478bd9Sstevel@tonic-gate time.tm_mday = datepart(cp + 6, 2, 01, 31, errp); 727c478bd9Sstevel@tonic-gate time.tm_hour = datepart(cp + 8, 2, 00, 23, errp); 737c478bd9Sstevel@tonic-gate time.tm_min = datepart(cp + 10, 2, 00, 59, errp); 747c478bd9Sstevel@tonic-gate time.tm_sec = datepart(cp + 12, 2, 00, 59, errp); 75*9525b14bSRao Shoaib if (*errp) /*%< Any parse errors? */ 767c478bd9Sstevel@tonic-gate return (0); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * OK, now because timegm() is not available in all environments, 807c478bd9Sstevel@tonic-gate * we will do it by hand. Roll up sleeves, curse the gods, begin! 817c478bd9Sstevel@tonic-gate */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define SECS_PER_DAY ((u_int32_t)24*60*60) 847c478bd9Sstevel@tonic-gate #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 857c478bd9Sstevel@tonic-gate 86*9525b14bSRao Shoaib result = time.tm_sec; /*%< Seconds */ 87*9525b14bSRao Shoaib result += time.tm_min * 60; /*%< Minutes */ 88*9525b14bSRao Shoaib result += time.tm_hour * (60*60); /*%< Hours */ 89*9525b14bSRao Shoaib result += (time.tm_mday - 1) * SECS_PER_DAY; /*%< Days */ 907c478bd9Sstevel@tonic-gate /* Months are trickier. Look without leaping, then leap */ 917c478bd9Sstevel@tonic-gate mdays = 0; 927c478bd9Sstevel@tonic-gate for (i = 0; i < time.tm_mon; i++) 937c478bd9Sstevel@tonic-gate mdays += days_per_month[i]; 94*9525b14bSRao Shoaib result += mdays * SECS_PER_DAY; /*%< Months */ 957c478bd9Sstevel@tonic-gate if (time.tm_mon > 1 && isleap(1900+time.tm_year)) 96*9525b14bSRao Shoaib result += SECS_PER_DAY; /*%< Add leapday for this year */ 977c478bd9Sstevel@tonic-gate /* First figure years without leapdays, then add them in. */ 987c478bd9Sstevel@tonic-gate /* The loop is slow, FIXME, but simple and accurate. */ 99*9525b14bSRao Shoaib result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */ 1007c478bd9Sstevel@tonic-gate for (i = 70; i < time.tm_year; i++) 1017c478bd9Sstevel@tonic-gate if (isleap(1900+i)) 102*9525b14bSRao Shoaib result += SECS_PER_DAY; /*%< Add leapday for prev year */ 1037c478bd9Sstevel@tonic-gate return (result); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* Private. */ 1077c478bd9Sstevel@tonic-gate 108*9525b14bSRao Shoaib /*% 1097c478bd9Sstevel@tonic-gate * Parse part of a date. Set error flag if any error. 1107c478bd9Sstevel@tonic-gate * Don't reset the flag if there is no error. 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate static int 1137c478bd9Sstevel@tonic-gate datepart(const char *buf, int size, int min, int max, int *errp) { 1147c478bd9Sstevel@tonic-gate int result = 0; 1157c478bd9Sstevel@tonic-gate int i; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate for (i = 0; i < size; i++) { 1187c478bd9Sstevel@tonic-gate if (!isdigit((unsigned char)(buf[i]))) 1197c478bd9Sstevel@tonic-gate *errp = 1; 1207c478bd9Sstevel@tonic-gate result = (result * 10) + buf[i] - '0'; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate if (result < min) 1237c478bd9Sstevel@tonic-gate *errp = 1; 1247c478bd9Sstevel@tonic-gate if (result > max) 1257c478bd9Sstevel@tonic-gate *errp = 1; 1267c478bd9Sstevel@tonic-gate return (result); 1277c478bd9Sstevel@tonic-gate } 128*9525b14bSRao Shoaib 129*9525b14bSRao Shoaib /*! \file */ 130