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 #include "config.h"
00031
00032 static char rcsid[] not_used =
00033 {"$Id: util_mit.cc 18315 2008-03-03 20:14:44Z jimg $"
00034 };
00035
00036 #include <cstring>
00037 #include <cstdlib>
00038 #include <cstdio>
00039 #include <string>
00040 #include <ctype.h>
00041
00042 #ifndef TM_IN_SYS_TIME
00043 #include <time.h>
00044 #else
00045 #include <sys/time.h>
00046 #endif
00047
00048 #include <sys/types.h>
00049 #include <sys/stat.h>
00050
00051 #include <iostream>
00052
00053 #include "util_mit.h"
00054
00055 using std::cerr;
00056 using std::endl;
00057 using std::string;
00058 using std::sprintf;
00059
00060 #include "debug.h"
00061
00062 namespace libdap {
00063
00064 static char * months[12] =
00065 {
00066 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00067 };
00068
00069 #ifndef HAVE_STRFTIME
00070 static char * wkdays[7] =
00071 {
00072 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00073 };
00074 #endif
00075
00076
00077
00078
00079
00080
00081
00082
00083 #ifndef TOLOWER
00084 #define TOLOWER(c) tolower((int) (c))
00085 #define TOUPPER(c) toupper((int) (c))
00086 #endif
00087
00088 static int
00089 strncasecomp(const char *a, const char *b, int n)
00090 {
00091 const char *p = a;
00092 const char *q = b;
00093
00094 for (p = a, q = b;; p++, q++) {
00095 int diff;
00096 if (p == a + n) return 0;
00097 if (!(*p && *q)) return *p - *q;
00098 diff = TOLOWER(*p) - TOLOWER(*q);
00099 if (diff) return diff;
00100 }
00101
00102 }
00103
00104 static int
00105 make_month(char * s, char ** ends)
00106 {
00107 char * ptr = s;
00108 while (!isalpha((int) *ptr)) ptr++;
00109 if (*ptr) {
00110 int i;
00111 *ends = ptr + 3;
00112 for (i = 0; i < 12; i++)
00113 if (!strncasecomp(months[i], ptr, 3)) return i;
00114 }
00115 return 0;
00116 }
00117
00132 time_t
00133 parse_time(const char * str, bool expand)
00134 {
00135 char * s;
00136 struct tm tm;
00137 time_t t;
00138
00139 if (!str) return 0;
00140
00141 if ((s = (char *)strchr(str, ','))) {
00142 s++;
00143 while (*s && *s == ' ') s++;
00144 if (strchr(s, '-')) {
00145 DBG(cerr << "Format...... Weekday, 00-Mon-00 00:00:00 GMT"
00146 << endl);
00147 if ((int)strlen(s) < 18) {
00148 DBG(cerr << "ERROR....... Not a valid time format \""
00149 << s << "\"" << endl);
00150 return 0;
00151 }
00152 tm.tm_mday = strtol(s, &s, 10);
00153 tm.tm_mon = make_month(s, &s);
00154 ++s;
00155 tm.tm_year = strtol(s, &s, 10);
00156 tm.tm_hour = strtol(s, &s, 10);
00157 ++s;
00158 tm.tm_min = strtol(s, &s, 10);
00159 ++s;
00160 tm.tm_sec = strtol(s, &s, 10);
00161
00162 }
00163 else {
00164 DBG(cerr << "Format...... Wkd, 00 Mon 0000 00:00:00 GMT" << endl);
00165 if ((int)strlen(s) < 20) {
00166 DBG(cerr << "ERROR....... Not a valid time format \""
00167 << s << "\"" << endl);
00168 return 0;
00169 }
00170 tm.tm_mday = strtol(s, &s, 10);
00171 tm.tm_mon = make_month(s, &s);
00172 tm.tm_year = strtol(s, &s, 10) - 1900;
00173 tm.tm_hour = strtol(s, &s, 10);
00174 ++s;
00175 tm.tm_min = strtol(s, &s, 10);
00176 ++s;
00177 tm.tm_sec = strtol(s, &s, 10);
00178 }
00179 }
00180 else if (isdigit((int) *str)) {
00181
00182 if (strchr(str, 'T')) {
00183 DBG(cerr << "Format...... YYYY.MM.DDThh:mmStzWkd" << endl);
00184 s = (char *) str;
00185 while (*s && *s == ' ') s++;
00186 if ((int)strlen(s) < 21) {
00187 DBG(cerr << "ERROR....... Not a valid time format \""
00188 << s << "\"" << endl);
00189 return 0;
00190 }
00191 tm.tm_year = strtol(s, &s, 10) - 1900;
00192 ++s;
00193 tm.tm_mon = strtol(s, &s, 10);
00194 ++s;
00195 tm.tm_mday = strtol(s, &s, 10);
00196 ++s;
00197 tm.tm_hour = strtol(s, &s, 10);
00198 ++s;
00199 tm.tm_min = strtol(s, &s, 10);
00200 ++s;
00201 tm.tm_sec = strtol(s, &s, 10);
00202
00203 }
00204 else {
00205 t = expand ? time(NULL) + atol(str) : atol(str);
00206
00207 return t;
00208 }
00209
00210 }
00211 else {
00212 DBG(cerr << "Format...... Wkd Mon 00 00:00:00 0000 GMT" << endl);
00213 s = (char *) str;
00214 while (*s && *s == ' ') s++;
00215 DBG(cerr << "Trying...... The Wrong time format: " << s << endl);
00216 if ((int)strlen(s) < 24) {
00217 DBG(cerr << "ERROR....... Not a valid time format \""
00218 << s << "\"" << endl);
00219 return 0;
00220 }
00221 tm.tm_mon = make_month(s, &s);
00222 tm.tm_mday = strtol(s, &s, 10);
00223 tm.tm_hour = strtol(s, &s, 10);
00224 ++s;
00225 tm.tm_min = strtol(s, &s, 10);
00226 ++s;
00227 tm.tm_sec = strtol(s, &s, 10);
00228 tm.tm_year = strtol(s, &s, 10) - 1900;
00229 }
00230 if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
00231 tm.tm_min < 0 || tm.tm_min > 59 ||
00232 tm.tm_hour < 0 || tm.tm_hour > 23 ||
00233 tm.tm_mday < 1 || tm.tm_mday > 31 ||
00234 tm.tm_mon < 0 || tm.tm_mon > 11 ||
00235 tm.tm_year < 70 || tm.tm_year > 120) {
00236 DBG(cerr << "ERROR....... Parsed illegal time" << endl);
00237 return 0;
00238 }
00239
00240
00241 tm.tm_isdst = -1;
00242
00243 #ifdef HAVE_TIMEGM
00244
00245 t = timegm(&tm);
00246
00247 #else
00248
00249 #ifdef HAVE_MKTIME
00250
00251
00252 time_t offset;
00253 time_t now = time(0);
00254 #ifdef _REENTRANT
00255 struct tm gmt, local;
00256 offset = mktime(gmtime_r(&now, &gmt)) - mktime(localtime_r(&now, &local));
00257 #else
00258 offset = mktime(gmtime(&now)) - mktime(localtime(&now));
00259 #endif
00260
00261 t = mktime(&tm) + offset;
00262
00263 #else
00264
00265 #error "Neither mktime nor timegm defined"
00266
00267 #endif
00268 #endif
00269
00270 DBG(cerr << "Time string. " << str << " parsed to " << t
00271 << " calendar time or \"" << ctime(&t) << "\" in local time" << endl);
00272
00273 return t;
00274 }
00275
00285 string date_time_str(time_t *calendar, bool local)
00286 {
00287 char buf[40];
00288
00289 #ifdef HAVE_STRFTIME
00290 if (local) {
00291
00292
00293
00294
00295 #if defined(_REENTRANT) || defined(SOLARIS)
00296 struct tm loctime;
00297 localtime_r(calendar, &loctime);
00298 strftime(buf, 40, "%a, %d %b %Y %H:%M:%S", &loctime);
00299 #else
00300 struct tm *loctime = localtime(calendar);
00301 strftime(buf, 40, "%a, %d %b %Y %H:%M:%S", loctime);
00302 #endif
00303 }
00304 else {
00305 #if defined(_REENTRANT) || defined(SOLARIS)
00306 struct tm gmt;
00307 gmtime_r(calendar, &gmt);
00308 strftime(buf, 40, "%a, %d %b %Y %H:%M:%S GMT", &gmt);
00309 #else
00310 struct tm *gmt = gmtime(calendar);
00311 strftime(buf, 40, "%a, %d %b %Y %H:%M:%S GMT", gmt);
00312 #endif
00313 }
00314
00315 #else
00316
00317 if (local) {
00318 #if defined(_REENTRANT)
00319 struct tm loctime;
00320 localtime_r(calendar, &loctime);
00321 sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d",
00322 wkdays[loctime.tm_wday],
00323 loctime.tm_mday,
00324 months[loctime.tm_mon],
00325 loctime.tm_year + 1900,
00326 loctime.tm_hour,
00327 loctime.tm_min,
00328 loctime.tm_sec);
00329 #else
00330 struct tm *loctime = localtime(calendar);
00331 sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d",
00332 wkdays[loctime->tm_wday],
00333 loctime->tm_mday,
00334 months[loctime->tm_mon],
00335 loctime->tm_year + 1900,
00336 loctime->tm_hour,
00337 loctime->tm_min,
00338 loctime->tm_sec);
00339 #endif
00340 }
00341 else {
00342 #if defined(_REENTRANT) || defined(SOLARIS)
00343 struct tm gmt;
00344 gmtime_r(calendar, &gmt);
00345 sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d GMT",
00346 wkdays[gmt.tm_wday],
00347 gmt.tm_mday,
00348 months[gmt.tm_mon],
00349 gmt.tm_year + 1900,
00350 gmt.tm_hour,
00351 gmt.tm_min,
00352 gmt.tm_sec);
00353 #else
00354 struct tm *gmt = gmtime(calendar);
00355 sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d GMT",
00356 wkdays[gmt->tm_wday],
00357 gmt->tm_mday,
00358 months[gmt->tm_mon],
00359 gmt->tm_year + 1900,
00360 gmt->tm_hour,
00361 gmt->tm_min,
00362 gmt->tm_sec);
00363 #endif
00364 }
00365 #endif
00366 return string(buf);
00367 }
00368
00369 }