38 #ifndef TM_IN_SYS_TIME 44 #include <sys/types.h> 59 static const char * months[12] =
61 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" 65 static const char * wkdays[7] =
67 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" 79 #define TOLOWER(c) tolower((int) (c)) 80 #define TOUPPER(c) toupper((int) (c)) 84 strncasecomp(
const char *a,
const char *b,
int n)
89 for (p = a, q = b;; p++, q++) {
91 if (p == a + n)
return 0;
92 if (!(*p && *q))
return *p - *q;
93 diff = TOLOWER(*p) - TOLOWER(*q);
94 if (diff)
return diff;
101 make_month(
char * s,
char ** ends)
104 while (!isalpha((
int) *ptr)) ptr++;
108 for (i = 0; i < 12; i++)
109 if (!strncasecomp(months[i], ptr, 3))
return i;
137 if ((s = (
char *)strchr(str,
','))) {
139 while (*s && *s ==
' ') s++;
140 if (strchr(s,
'-')) {
141 DBG(cerr <<
"Format...... Weekday, 00-Mon-00 00:00:00 GMT" 143 if ((
int)strlen(s) < 18) {
144 DBG(cerr <<
"ERROR....... Not a valid time format \"" 145 << s <<
"\"" << endl);
148 tm.tm_mday = strtol(s, &s, 10);
149 tm.tm_mon = make_month(s, &s);
151 tm.tm_year = strtol(s, &s, 10);
152 tm.tm_hour = strtol(s, &s, 10);
154 tm.tm_min = strtol(s, &s, 10);
156 tm.tm_sec = strtol(s, &s, 10);
160 DBG(cerr <<
"Format...... Wkd, 00 Mon 0000 00:00:00 GMT" << endl);
161 if ((
int)strlen(s) < 20) {
162 DBG(cerr <<
"ERROR....... Not a valid time format \"" 163 << s <<
"\"" << endl);
166 tm.tm_mday = strtol(s, &s, 10);
167 tm.tm_mon = make_month(s, &s);
168 tm.tm_year = strtol(s, &s, 10) - 1900;
169 tm.tm_hour = strtol(s, &s, 10);
171 tm.tm_min = strtol(s, &s, 10);
173 tm.tm_sec = strtol(s, &s, 10);
176 else if (isdigit((
int) *str)) {
178 if (strchr(str,
'T')) {
179 DBG(cerr <<
"Format...... YYYY.MM.DDThh:mmStzWkd" << endl);
181 while (*s && *s ==
' ') s++;
182 if ((
int)strlen(s) < 21) {
183 DBG(cerr <<
"ERROR....... Not a valid time format \"" 184 << s <<
"\"" << endl);
187 tm.tm_year = strtol(s, &s, 10) - 1900;
189 tm.tm_mon = strtol(s, &s, 10);
191 tm.tm_mday = strtol(s, &s, 10);
193 tm.tm_hour = strtol(s, &s, 10);
195 tm.tm_min = strtol(s, &s, 10);
197 tm.tm_sec = strtol(s, &s, 10);
201 t = expand ? time(NULL) + atol(str) : atol(str);
208 DBG(cerr <<
"Format...... Wkd Mon 00 00:00:00 0000 GMT" << endl);
210 while (*s && *s ==
' ') s++;
211 DBG(cerr <<
"Trying...... The Wrong time format: " << s << endl);
212 if ((
int)strlen(s) < 24) {
213 DBG(cerr <<
"ERROR....... Not a valid time format \"" 214 << s <<
"\"" << endl);
217 tm.tm_mon = make_month(s, &s);
218 tm.tm_mday = strtol(s, &s, 10);
219 tm.tm_hour = strtol(s, &s, 10);
221 tm.tm_min = strtol(s, &s, 10);
223 tm.tm_sec = strtol(s, &s, 10);
224 tm.tm_year = strtol(s, &s, 10) - 1900;
226 if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
227 tm.tm_min < 0 || tm.tm_min > 59 ||
228 tm.tm_hour < 0 || tm.tm_hour > 23 ||
229 tm.tm_mday < 1 || tm.tm_mday > 31 ||
230 tm.tm_mon < 0 || tm.tm_mon > 11 ||
231 tm.tm_year < 70 || tm.tm_year > 120) {
232 DBG(cerr <<
"ERROR....... Parsed illegal time" << endl);
249 time_t now = time(0);
251 struct tm gmt, local;
252 offset = mktime(gmtime_r(&now, &gmt)) - mktime(localtime_r(&now, &local));
254 offset = mktime(gmtime(&now)) - mktime(localtime(&now));
257 t = mktime(&tm) + offset;
261 #error "Neither mktime nor timegm defined" 266 DBG(cerr <<
"Time string. " << str <<
" parsed to " << t
267 <<
" calendar time or \"" << ctime(&t) <<
"\" in local time" << endl);
291 #if defined(_REENTRANT) || defined(SOLARIS) 293 localtime_r(calendar, &loctime);
294 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S", &loctime);
296 struct tm *loctime = localtime(calendar);
297 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S", loctime);
301 #if defined(_REENTRANT) || defined(SOLARIS) 303 gmtime_r(calendar, &gmt);
304 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S GMT", &gmt);
306 struct tm *gmt = gmtime(calendar);
307 strftime(buf, 40,
"%a, %d %b %Y %H:%M:%S GMT", gmt);
314 #if defined(_REENTRANT) 316 localtime_r(calendar, &loctime);
317 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d",
318 wkdays[loctime.tm_wday],
320 months[loctime.tm_mon],
321 loctime.tm_year + 1900,
326 struct tm *loctime = localtime(calendar);
329 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d",
330 wkdays[loctime->tm_wday],
332 months[loctime->tm_mon],
333 loctime->tm_year + 1900,
340 #if defined(_REENTRANT) || defined(SOLARIS) 342 gmtime_r(calendar, &gmt);
343 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
352 struct tm *gmt = gmtime(calendar);
355 snprintf(buf, 40,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
356 wkdays[gmt->tm_wday],
time_t parse_time(const char *str, bool expand)
top level DAP object to house generic methods
string date_time_str(time_t *calendar, bool local)