abyss_date.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 **
00003 ** This file is part of the ABYSS Web server project.
00004 **
00005 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
00006 ** All rights reserved.
00007 **
00008 ** Redistribution and use in source and binary forms, with or without
00009 ** modification, are permitted provided that the following conditions
00010 ** are met:
00011 ** 1. Redistributions of source code must retain the above copyright
00012 **    notice, this list of conditions and the following disclaimer.
00013 ** 2. Redistributions in binary form must reproduce the above copyright
00014 **    notice, this list of conditions and the following disclaimer in the
00015 **    documentation and/or other materials provided with the distribution.
00016 ** 3. The name of the author may not be used to endorse or promote products
00017 **    derived from this software without specific prior written permission.
00018 ** 
00019 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00020 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00023 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029 ** SUCH DAMAGE.
00030 **
00031 *******************************************************************************/
00032 
00033 #include <ctype.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <time.h>
00037 #include <stdlib.h>
00038 
00039 
00040 #include <inttypes.h>
00041 #include "abyss_date.h"
00042 
00043 /*********************************************************************
00044 ** Date
00045 *********************************************************************/
00046 
00047 static char *_DateDay[7]=
00048 {
00049     "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
00050 };
00051 
00052 static char *_DateMonth[12]=
00053 {
00054     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
00055 };
00056 
00057 static int32_t _DateTimeBias=0;
00058 static char _DateTimeBiasStr[6]="";
00059 
00060 abyss_bool DateToString(TDate *tm,char *s)
00061 {
00062     if (mktime(tm)==(time_t)(-1))
00063     {
00064         *s='\0';
00065         return FALSE;
00066     };
00067 
00068     sprintf(s,"%s, %02d %s %04d %02d:%02d:%02d GMT",_DateDay[tm->tm_wday],tm->tm_mday,
00069                 _DateMonth[tm->tm_mon],tm->tm_year+1900,tm->tm_hour,tm->tm_min,tm->tm_sec);
00070 
00071     return TRUE;
00072 }
00073 
00074 
00075 
00076 abyss_bool
00077 DateToLogString(TDate * const tmP,
00078                 char *  const s) {
00079     time_t t;
00080 
00081     t = mktime(tmP);
00082     if (t != (time_t)(-1)) {
00083         TDate d;
00084         abyss_bool success;
00085         success = DateFromLocal(&d, t);
00086         if (success) {
00087             sprintf(s, "%02d/%s/%04d:%02d:%02d:%02d %s",
00088                     d.tm_mday, _DateMonth[d.tm_mon],
00089                     d.tm_year+1900, d.tm_hour, d.tm_min, d.tm_sec,
00090                     _DateTimeBiasStr);
00091             return TRUE;
00092         }
00093     }
00094     *s = '\0';
00095     return FALSE;
00096 }
00097 
00098 
00099 
00100 abyss_bool
00101 DateDecode(const char *  const dateString,
00102            TDate *       const tmP) {
00103 
00104     int rc;
00105     const char * s;
00106     uint32_t n;
00107 
00108     s = &dateString[0];
00109 
00110     /* Ignore spaces, day name and spaces */
00111     while ((*s==' ') || (*s=='\t'))
00112         ++s;
00113 
00114     while ((*s!=' ') && (*s!='\t'))
00115         ++s;
00116 
00117     while ((*s==' ') || (*s=='\t'))
00118         ++s;
00119 
00120     /* try to recognize the date format */
00121     rc = sscanf(s, "%*s %d %d:%d:%d %d%*s", &tmP->tm_mday, &tmP->tm_hour,
00122                 &tmP->tm_min, &tmP->tm_sec, &tmP->tm_year);
00123     if (rc != 5) {
00124         int rc;
00125         rc = sscanf(s, "%d %n%*s %d %d:%d:%d GMT%*s",
00126                     &tmP->tm_mday,&n,&tmP->tm_year,
00127                     &tmP->tm_hour, &tmP->tm_min, &tmP->tm_sec);
00128         if (rc != 5) {
00129             int rc;
00130             rc = sscanf(s, "%d-%n%*[A-Za-z]-%d %d:%d:%d GMT%*s",
00131                         &tmP->tm_mday, &n, &tmP->tm_year,
00132                         &tmP->tm_hour, &tmP->tm_min, &tmP->tm_sec);
00133             if (rc != 5)
00134                 return FALSE;
00135         }
00136     }    
00137     /* s points now to the month string */
00138     s += n;
00139     for (n = 0; n < 12; ++n) {
00140         char * p;
00141 
00142         p =_DateMonth[n];
00143 
00144         if (tolower(*p++) == tolower(*s))
00145             if (*p++ == tolower(s[1]))
00146                 if (*p == tolower(s[2]))
00147                     break;
00148     }
00149 
00150     if (n == 12)
00151         return FALSE;
00152 
00153     tmP->tm_mon = n;
00154 
00155     /* finish the work */
00156     if (tmP->tm_year > 1900)
00157         tmP->tm_year -= 1900;
00158     else {
00159         if (tmP->tm_year < 70)
00160             tmP->tm_year += 100;
00161     }
00162     tmP->tm_isdst = 0;
00163 
00164     return (mktime(tmP) != (time_t)(-1));
00165 }
00166 
00167 
00168 
00169 int32_t DateCompare(TDate *d1,TDate *d2)
00170 {
00171     int32_t x;
00172 
00173     if ((x=d1->tm_year-d2->tm_year)==0)
00174         if ((x=d1->tm_mon-d2->tm_mon)==0)
00175             if ((x=d1->tm_mday-d2->tm_mday)==0)
00176                 if ((x=d1->tm_hour-d2->tm_hour)==0)
00177                     if ((x=d1->tm_min-d2->tm_min)==0)
00178                         x=d1->tm_sec-d2->tm_sec;
00179 
00180     return x;
00181 }
00182 
00183 
00184 
00185 abyss_bool
00186 DateFromGMT(TDate *d,time_t t) {
00187     TDate *dx;
00188 
00189     dx=gmtime(&t);
00190     if (dx) {
00191         *d=*dx;
00192         return TRUE;
00193     };
00194 
00195     return FALSE;
00196 }
00197 
00198 abyss_bool DateFromLocal(TDate *d,time_t t)
00199 {
00200     return DateFromGMT(d,t+_DateTimeBias*2);
00201 }
00202 
00203 
00204 
00205 abyss_bool
00206 DateInit(void) {
00207     time_t t;
00208     TDate gmt,local,*d;
00209 
00210     time(&t);
00211     if (DateFromGMT(&gmt,t)) {
00212         d=localtime(&t);
00213         if (d) {
00214             local=*d;
00215             _DateTimeBias =
00216                 (local.tm_sec-gmt.tm_sec)+(local.tm_min-gmt.tm_min)*60
00217                 +(local.tm_hour-gmt.tm_hour)*3600;
00218             sprintf(_DateTimeBiasStr, "%+03d%02d",
00219                     _DateTimeBias/3600,(abs(_DateTimeBias) % 3600)/60);
00220             return TRUE;
00221         };
00222     }
00223     return FALSE;
00224 }

Generated on Thu May 17 10:00:23 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6