Back to index

courier  0.68.2
pcpgmtime.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 "pcp.h"
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include <time.h>
00014 #include <fcntl.h>
00015 #include <errno.h>
00016 
00017 #include "pcp.h"
00018 
00019 #define leap(y) ( \
00020                         ((y) % 400) == 0 || \
00021                         (((y) % 4) == 0 && (y) % 100) )
00022 
00023 static unsigned mlength[]={31,28,31,30,31,30,31,31,30,31,30,31};
00024 #define mdays(m,y)      ( (m) != 2 ? mlength[(m)-1] : leap(y) ? 29:28)
00025 
00026 
00027 static int tmcmp(int y1, int m1, int d1,
00028                int y2, int m2, int d2)
00029 {
00030        if (y1 < y2)
00031               return (-1);
00032        if (y1 > y2)
00033               return (1);
00034        if (m1 < m2)
00035               return (-1);
00036        if (m1 > m2)
00037               return (1);
00038        if (d1 < d2)
00039               return (-1);
00040        if (d1 > d2)
00041               return (1);
00042        return (0);
00043 }
00044 
00045 time_t pcp_gmtime(int y, int m, int d, int hh, int mm, int ss)
00046 {
00047        time_t t;
00048        struct tm *tmptr;
00049        struct tm mytm;
00050        int n;
00051 
00052        if (y < 1970 || y > 3000 || m < 1 || m > 12 || d < 1 ||
00053            d > mdays(m, y) || hh < 0 || mm < 0 || ss < 0 || hh > 23 ||
00054            mm > 59 || ss > 59)
00055               return (0);
00056 
00057        time(&t);
00058 
00059        tmptr=localtime(&t);
00060        if (!tmptr)
00061               return (0);
00062 
00063        mytm= *tmptr;
00064        mytm.tm_year = y - 1900;
00065        mytm.tm_mon = m - 1;
00066        mytm.tm_mday = d;
00067        mytm.tm_hour=0;
00068        mytm.tm_min=0;
00069        mytm.tm_sec=0;
00070 
00071        t=mktime(&mytm);
00072        if (t == (time_t)-1)
00073               return (0);
00074 
00075        /* Do it again, due to a potential timezone change */
00076 
00077        tmptr=localtime(&t);
00078        mytm= *tmptr;
00079        mytm.tm_year = y - 1900;
00080        mytm.tm_mon = m - 1;
00081        mytm.tm_mday = d;
00082        mytm.tm_hour=0;
00083        mytm.tm_min=0;
00084        mytm.tm_sec=0;
00085 
00086        t=mktime(&mytm);
00087        if (t == (time_t)-1)
00088               return (0);
00089 
00090        /* Sanity check */
00091 
00092        tmptr=localtime(&t);
00093        if (tmptr->tm_hour || tmptr->tm_min || tmptr->tm_sec)
00094        {
00095               return (0);
00096        }
00097 
00098        tmptr=gmtime(&t);
00099 
00100        n=tmcmp(tmptr->tm_year + 1900,
00101               tmptr->tm_mon + 1,
00102               tmptr->tm_mday,
00103               y, m, d);
00104        if (n < 0)
00105        {
00106               while (n < 0)
00107               {
00108                      t += 24 * 60 * 60;
00109                      tmptr=gmtime(&t);
00110                      n=tmcmp(tmptr->tm_year + 1900,
00111                             tmptr->tm_mon + 1,
00112                             tmptr->tm_mday,
00113                             y, m, d);
00114               }
00115        }
00116        else while (n > 0)
00117        {
00118               t -= 24 * 60 * 60;
00119               tmptr=gmtime(&t);
00120               n=tmcmp(tmptr->tm_year + 1900,
00121                      tmptr->tm_mon + 1,
00122                      tmptr->tm_mday,
00123                      y, m, d);
00124        }
00125 
00126        t -= tmptr->tm_hour * 60 * 60 + tmptr->tm_min * 60 + tmptr->tm_sec;
00127 
00128        return (t + hh * 60 * 60 + mm * 60 + ss);
00129 }
00130 
00131 time_t pcp_gmtime_s(const char *p)
00132 {
00133        int y, m, d, hh, mm, ss;
00134 
00135        if (sscanf(p, "%4d%2d%2d%2d%2d%2d", &y, &m, &d, &hh, &mm, &ss)
00136            != 6)
00137               return (0);
00138        return (pcp_gmtime(y, m, d, hh, mm, ss));
00139 }
00140 
00141 void pcp_gmtimestr(time_t t, char *buf)
00142 {
00143        struct tm *tmptr=gmtime(&t);
00144 
00145        sprintf(buf, "%04d%02d%02d%02d%02d%02d",
00146               tmptr->tm_year + 1900,
00147               tmptr->tm_mon+1,
00148               tmptr->tm_mday,
00149               tmptr->tm_hour,
00150               tmptr->tm_min,
00151               tmptr->tm_sec);
00152 }