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 #include "port_before.h" 197c478bd9Sstevel@tonic-gate 207c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include <ctype.h> 237c478bd9Sstevel@tonic-gate #include <errno.h> 247c478bd9Sstevel@tonic-gate #include <stdio.h> 257c478bd9Sstevel@tonic-gate #include <string.h> 267c478bd9Sstevel@tonic-gate #include <time.h> 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include "port_after.h" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR 317c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x) 327c478bd9Sstevel@tonic-gate #else 337c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x) 347c478bd9Sstevel@tonic-gate #endif 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* Forward. */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate static int datepart(const char *, int, int, int, int *); 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* Public. */ 417c478bd9Sstevel@tonic-gate 42*9525b14bSRao Shoaib /*% 43*9525b14bSRao Shoaib * Convert a date in ASCII into the number of seconds since 44*9525b14bSRao Shoaib * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all 45*9525b14bSRao Shoaib * digits required, no spaces allowed. 46*9525b14bSRao Shoaib */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate u_int32_t 497c478bd9Sstevel@tonic-gate ns_datetosecs(const char *cp, int *errp) { 507c478bd9Sstevel@tonic-gate struct tm time; 517c478bd9Sstevel@tonic-gate u_int32_t result; 527c478bd9Sstevel@tonic-gate int mdays, i; 537c478bd9Sstevel@tonic-gate static const int days_per_month[12] = 547c478bd9Sstevel@tonic-gate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 557c478bd9Sstevel@tonic-gate 56*9525b14bSRao Shoaib if (strlen(cp) != 14U) { 577c478bd9Sstevel@tonic-gate *errp = 1; 587c478bd9Sstevel@tonic-gate return (0); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate *errp = 0; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate memset(&time, 0, sizeof time); 637c478bd9Sstevel@tonic-gate time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900; 647c478bd9Sstevel@tonic-gate time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1; 657c478bd9Sstevel@tonic-gate time.tm_mday = datepart(cp + 6, 2, 01, 31, errp); 667c478bd9Sstevel@tonic-gate time.tm_hour = datepart(cp + 8, 2, 00, 23, errp); 677c478bd9Sstevel@tonic-gate time.tm_min = datepart(cp + 10, 2, 00, 59, errp); 687c478bd9Sstevel@tonic-gate time.tm_sec = datepart(cp + 12, 2, 00, 59, errp); 69*9525b14bSRao Shoaib if (*errp) /*%< Any parse errors? */ 707c478bd9Sstevel@tonic-gate return (0); 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * OK, now because timegm() is not available in all environments, 747c478bd9Sstevel@tonic-gate * we will do it by hand. Roll up sleeves, curse the gods, begin! 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #define SECS_PER_DAY ((u_int32_t)24*60*60) 787c478bd9Sstevel@tonic-gate #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 797c478bd9Sstevel@tonic-gate 80*9525b14bSRao Shoaib result = time.tm_sec; /*%< Seconds */ 81*9525b14bSRao Shoaib result += time.tm_min * 60; /*%< Minutes */ 82*9525b14bSRao Shoaib result += time.tm_hour * (60*60); /*%< Hours */ 83*9525b14bSRao Shoaib result += (time.tm_mday - 1) * SECS_PER_DAY; /*%< Days */ 847c478bd9Sstevel@tonic-gate /* Months are trickier. Look without leaping, then leap */ 857c478bd9Sstevel@tonic-gate mdays = 0; 867c478bd9Sstevel@tonic-gate for (i = 0; i < time.tm_mon; i++) 877c478bd9Sstevel@tonic-gate mdays += days_per_month[i]; 88*9525b14bSRao Shoaib result += mdays * SECS_PER_DAY; /*%< Months */ 897c478bd9Sstevel@tonic-gate if (time.tm_mon > 1 && isleap(1900+time.tm_year)) 90*9525b14bSRao Shoaib result += SECS_PER_DAY; /*%< Add leapday for this year */ 917c478bd9Sstevel@tonic-gate /* First figure years without leapdays, then add them in. */ 927c478bd9Sstevel@tonic-gate /* The loop is slow, FIXME, but simple and accurate. */ 93*9525b14bSRao Shoaib result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */ 947c478bd9Sstevel@tonic-gate for (i = 70; i < time.tm_year; i++) 957c478bd9Sstevel@tonic-gate if (isleap(1900+i)) 96*9525b14bSRao Shoaib result += SECS_PER_DAY; /*%< Add leapday for prev year */ 977c478bd9Sstevel@tonic-gate return (result); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* Private. */ 1017c478bd9Sstevel@tonic-gate 102*9525b14bSRao Shoaib /*% 1037c478bd9Sstevel@tonic-gate * Parse part of a date. Set error flag if any error. 1047c478bd9Sstevel@tonic-gate * Don't reset the flag if there is no error. 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate static int 1077c478bd9Sstevel@tonic-gate datepart(const char *buf, int size, int min, int max, int *errp) { 1087c478bd9Sstevel@tonic-gate int result = 0; 1097c478bd9Sstevel@tonic-gate int i; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate for (i = 0; i < size; i++) { 1127c478bd9Sstevel@tonic-gate if (!isdigit((unsigned char)(buf[i]))) 1137c478bd9Sstevel@tonic-gate *errp = 1; 1147c478bd9Sstevel@tonic-gate result = (result * 10) + buf[i] - '0'; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate if (result < min) 1177c478bd9Sstevel@tonic-gate *errp = 1; 1187c478bd9Sstevel@tonic-gate if (result > max) 1197c478bd9Sstevel@tonic-gate *errp = 1; 1207c478bd9Sstevel@tonic-gate return (result); 1217c478bd9Sstevel@tonic-gate } 122*9525b14bSRao Shoaib 123*9525b14bSRao Shoaib /*! \file */ 124