Back to index

glibc  2.9
ns_date.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (c) 1999 by Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00010  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00011  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00012  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00013  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00014  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00015  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 #if !defined(_LIBC) && !defined(lint)
00019 static const char rcsid[] = "$Id: ns_date.c,v 1.1 2008/08/01 17:15:09 drepper Exp $";
00020 #endif
00021 
00022 /* Import. */
00023 
00024 #include <arpa/nameser.h>
00025 
00026 #include <ctype.h>
00027 #include <errno.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <time.h>
00031 
00032 #define SPRINTF(x) ((size_t)sprintf x)
00033 
00034 /* Forward. */
00035 
00036 static int    datepart(const char *, int, int, int, int *);
00037 
00038 /* Public. */
00039 
00040 /*%
00041  * Convert a date in ASCII into the number of seconds since
00042  * 1 January 1970 (GMT assumed).  Format is yyyymmddhhmmss, all
00043  * digits required, no spaces allowed.
00044  */
00045 
00046 u_int32_t
00047 ns_datetosecs(const char *cp, int *errp) {
00048        struct tm time;
00049        u_int32_t result;
00050        int mdays, i;
00051        static const int days_per_month[12] =
00052               {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00053 
00054        if (strlen(cp) != 14U) {
00055               *errp = 1;
00056               return (0);
00057        }
00058        *errp = 0;
00059 
00060        memset(&time, 0, sizeof time);
00061        time.tm_year  = datepart(cp +  0, 4, 1990, 9999, errp) - 1900;
00062        time.tm_mon   = datepart(cp +  4, 2,   01,   12, errp) - 1;
00063        time.tm_mday  = datepart(cp +  6, 2,   01,   31, errp);
00064        time.tm_hour  = datepart(cp +  8, 2,   00,   23, errp);
00065        time.tm_min   = datepart(cp + 10, 2,   00,   59, errp);
00066        time.tm_sec   = datepart(cp + 12, 2,   00,   59, errp);
00067        if (*errp)           /*%< Any parse errors? */
00068               return (0);
00069 
00070        /*
00071         * OK, now because timegm() is not available in all environments,
00072         * we will do it by hand.  Roll up sleeves, curse the gods, begin!
00073         */
00074 
00075 #define SECS_PER_DAY    ((u_int32_t)24*60*60)
00076 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
00077 
00078        result  = time.tm_sec;                           /*%< Seconds */
00079        result += time.tm_min * 60;               /*%< Minutes */
00080        result += time.tm_hour * (60*60);         /*%< Hours */
00081        result += (time.tm_mday - 1) * SECS_PER_DAY;     /*%< Days */
00082        /* Months are trickier.  Look without leaping, then leap */
00083        mdays = 0;
00084        for (i = 0; i < time.tm_mon; i++)
00085               mdays += days_per_month[i];
00086        result += mdays * SECS_PER_DAY;                  /*%< Months */
00087        if (time.tm_mon > 1 && isleap(1900+time.tm_year))
00088               result += SECS_PER_DAY;            /*%< Add leapday for this year */
00089        /* First figure years without leapdays, then add them in.  */
00090        /* The loop is slow, FIXME, but simple and accurate.  */
00091        result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */
00092        for (i = 70; i < time.tm_year; i++)
00093               if (isleap(1900+i))
00094                      result += SECS_PER_DAY; /*%< Add leapday for prev year */
00095        return (result);
00096 }
00097 
00098 /* Private. */
00099 
00100 /*%
00101  * Parse part of a date.  Set error flag if any error.
00102  * Don't reset the flag if there is no error.
00103  */
00104 static int
00105 datepart(const char *buf, int size, int min, int max, int *errp) {
00106        int result = 0;
00107        int i;
00108 
00109        for (i = 0; i < size; i++) {
00110               if (!isdigit((unsigned char)(buf[i])))
00111                      *errp = 1;
00112               result = (result * 10) + buf[i] - '0';
00113        }
00114        if (result < min)
00115               *errp = 1;
00116        if (result > max)
00117               *errp = 1;
00118        return (result);
00119 }
00120