00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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
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
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
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 }