33 {
"$Id: util_mit.cc 24370 2011-03-28 16:21:32Z jimg $"
42 #ifndef TM_IN_SYS_TIME
48 #include <sys/types.h>
63 static const char * months[12] =
65 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
69 static const char * wkdays[7] =
71 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
83 #define TOLOWER(c) tolower((int) (c))
84 #define TOUPPER(c) toupper((int) (c))
88 strncasecomp(
const char *a,
const char *b,
int n)
93 for (p = a, q = b;; p++, q++) {
95 if (p == a + n)
return 0;
96 if (!(*p && *q))
return *p - *q;
98 if (diff)
return diff;
104 make_month(
char * s,
char ** ends)
107 while (!isalpha((
int) *ptr)) ptr++;
111 for (i = 0; i < 12; i++)
112 if (!strncasecomp(months[i], ptr, 3))
return i;
140 if ((s = (
char *)strchr(str,
','))) {
142 while (*s && *s ==
' ') s++;
143 if (strchr(s,
'-')) {
144 DBG(cerr <<
"Format...... Weekday, 00-Mon-00 00:00:00 GMT"
146 if ((
int)strlen(s) < 18) {
147 DBG(cerr <<
"ERROR....... Not a valid time format \""
148 << s <<
"\"" << endl);
151 tm.tm_mday = strtol(s, &s, 10);
152 tm.tm_mon = make_month(s, &s);
154 tm.tm_year = strtol(s, &s, 10);
155 tm.tm_hour = strtol(s, &s, 10);
157 tm.tm_min = strtol(s, &s, 10);
159 tm.tm_sec = strtol(s, &s, 10);
163 DBG(cerr <<
"Format...... Wkd, 00 Mon 0000 00:00:00 GMT" << endl);
164 if ((
int)strlen(s) < 20) {
165 DBG(cerr <<
"ERROR....... Not a valid time format \""
166 << s <<
"\"" << endl);
169 tm.tm_mday = strtol(s, &s, 10);
170 tm.tm_mon = make_month(s, &s);
171 tm.tm_year = strtol(s, &s, 10) - 1900;
172 tm.tm_hour = strtol(s, &s, 10);
174 tm.tm_min = strtol(s, &s, 10);
176 tm.tm_sec = strtol(s, &s, 10);
179 else if (isdigit((
int) *str)) {
181 if (strchr(str,
'T')) {
182 DBG(cerr <<
"Format...... YYYY.MM.DDThh:mmStzWkd" << endl);
184 while (*s && *s ==
' ') s++;
185 if ((
int)strlen(s) < 21) {
186 DBG(cerr <<
"ERROR....... Not a valid time format \""
187 << s <<
"\"" << endl);
190 tm.tm_year = strtol(s, &s, 10) - 1900;
192 tm.tm_mon = strtol(s, &s, 10);
194 tm.tm_mday = strtol(s, &s, 10);
196 tm.tm_hour = strtol(s, &s, 10);
198 tm.tm_min = strtol(s, &s, 10);
200 tm.tm_sec = strtol(s, &s, 10);
204 t = expand ? time(NULL) + atol(str) : atol(str);
211 DBG(cerr <<
"Format...... Wkd Mon 00 00:00:00 0000 GMT" << endl);
213 while (*s && *s ==
' ') s++;
214 DBG(cerr <<
"Trying...... The Wrong time format: " << s << endl);
215 if ((
int)strlen(s) < 24) {
216 DBG(cerr <<
"ERROR....... Not a valid time format \""
217 << s <<
"\"" << endl);
220 tm.tm_mon = make_month(s, &s);
221 tm.tm_mday = strtol(s, &s, 10);
222 tm.tm_hour = strtol(s, &s, 10);
224 tm.tm_min = strtol(s, &s, 10);
226 tm.tm_sec = strtol(s, &s, 10);
227 tm.tm_year = strtol(s, &s, 10) - 1900;
229 if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
230 tm.tm_min < 0 || tm.tm_min > 59 ||
231 tm.tm_hour < 0 || tm.tm_hour > 23 ||
232 tm.tm_mday < 1 || tm.tm_mday > 31 ||
233 tm.tm_mon < 0 || tm.tm_mon > 11 ||
234 tm.tm_year < 70 || tm.tm_year > 120) {
235 DBG(cerr <<
"ERROR....... Parsed illegal time" << endl);
252 time_t now = time(0);
254 struct tm gmt, local;
255 offset = mktime(gmtime_r(&now, &gmt)) - mktime(localtime_r(&now, &local));
257 offset = mktime(gmtime(&now)) - mktime(localtime(&now));
260 t = mktime(&tm) + offset;
264 #error "Neither mktime nor timegm defined"
269 DBG(cerr <<
"Time string. " << str <<
" parsed to " << t
270 <<
" calendar time or \"" << ctime(&t) <<
"\" in local time" << endl);
294 #if defined(_REENTRANT) || defined(SOLARIS)
296 localtime_r(calendar, &loctime);
297 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S", &loctime);
299 struct tm *loctime = localtime(calendar);
300 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S", loctime);
304 #if defined(_REENTRANT) || defined(SOLARIS)
306 gmtime_r(calendar, &gmt);
307 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S GMT", &gmt);
309 struct tm *gmt = gmtime(calendar);
310 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S GMT", gmt);
317 #if defined(_REENTRANT)
319 localtime_r(calendar, &loctime);
320 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d",
321 wkdays[loctime.tm_wday],
323 months[loctime.tm_mon],
324 loctime.tm_year + 1900,
329 struct tm *loctime = localtime(calendar);
330 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d",
331 wkdays[loctime->tm_wday],
333 months[loctime->tm_mon],
334 loctime->tm_year + 1900,
341 #if defined(_REENTRANT) || defined(SOLARIS)
343 gmtime_r(calendar, &gmt);
344 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
353 struct tm *gmt = gmtime(calendar);
354 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
355 wkdays[gmt->tm_wday],
time_t parse_time(const char *str, bool expand)
string date_time_str(time_t *calendar, bool local)