Back to index

courier  0.68.2
pcpparseymd.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include "config.h"
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <errno.h>
00012 #include <signal.h>
00013 #include <fcntl.h>
00014 #include <unistd.h>
00015 #include <time.h>
00016 #include <sys/stat.h>
00017 #include "pcp.h"
00018 
00019 /*
00020 ** Map yyyymmdd into time_t by reverse-engineering mktime().
00021 */
00022 
00023 static time_t sod(struct tm *tmptr, unsigned y, unsigned m, unsigned d)
00024 {
00025        struct tm mytm;
00026        time_t tt;
00027 
00028        /* Ok, first try */
00029 
00030        mytm= *tmptr;
00031        mytm.tm_year=y-1900;
00032        mytm.tm_mon=m-1;
00033        mytm.tm_mday=d;
00034        mytm.tm_hour=0;
00035        mytm.tm_min=0;
00036        mytm.tm_sec=0;
00037 
00038        tt=mktime(&mytm);
00039 
00040        if (tt == (time_t)-1 ||
00041            (tmptr=localtime(&tt)) == NULL)
00042               return (0);
00043 
00044        /* Do it one more time, due to timezone changes. */
00045 
00046        mytm= *tmptr;
00047        mytm.tm_year=y-1900;
00048        mytm.tm_mon=m-1;
00049        mytm.tm_mday=d;
00050        mytm.tm_hour=0;
00051        mytm.tm_min=0;
00052        mytm.tm_sec=0;
00053 
00054        tt=mktime(&mytm);
00055 
00056        if (tt == (time_t)-1)
00057               return (0);
00058        return (tt);
00059 }
00060 
00061 int pcp_parse_ymd(unsigned y, unsigned m, unsigned d, time_t *s, time_t *e)
00062 {
00063        time_t tt;
00064        struct tm *tmptr;
00065 
00066        time(&tt);
00067 
00068        tmptr=localtime(&tt);
00069 
00070        if (!tmptr)
00071               return (-1);
00072 
00073        if ((tt=sod(tmptr, y, m, d)) == 0)
00074               return (-1);
00075 
00076        *s=tt;
00077 
00078        tt += 36 * 60 * 60;
00079 
00080        tmptr=localtime(&tt);
00081 
00082        if (!tmptr)
00083               return (-1);
00084 
00085        if ((tt=sod(tmptr, tmptr->tm_year + 1900, tmptr->tm_mon+1,
00086                   tmptr->tm_mday)) == 0)
00087               return (-1);
00088 
00089        *e=tt;
00090        return (0);
00091 }
00092