56#define MAX_TIME_STR_LEN 40
62static const char * months[12] =
64 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
68static const char * wkdays[7] =
70 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
82#define TOLOWER(c) tolower((int) (c))
83#define TOUPPER(c) toupper((int) (c))
88strncasecomp(
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;
97 diff = TOLOWER(*p) - TOLOWER(*q);
98 if (diff)
return diff;
106make_month(
char * s,
char ** ends)
109 while (!isalpha((
int) *ptr)) ptr++;
113 for (i = 0; i < 12; i++)
114 if (!strncasecmp(months[i], ptr, 3))
return i;
119#if !defined(HAVE_TIMEGM) && defined(HAVE_MKTIME)
125 time_t now = time(0);
127 struct tm gmt, local;
128 offset = mktime(gmtime_r(&now, &gmt)) - mktime(localtime_r(&now, &local));
130 offset = mktime(gmtime(&now)) - mktime(localtime(&now));
161 if ((s = (
char *)strchr(str,
','))) {
163 while (*s && *s ==
' ') s++;
164 if (strchr(s,
'-')) {
165 DBG(cerr <<
"Format...... Weekday, 00-Mon-00 00:00:00 GMT" << endl);
166 if ((
int)strnlen(s, MAX_TIME_STR_LEN) < 18) {
167 DBG(cerr <<
"ERROR....... Not a valid time format \"" << s <<
"\"" << endl);
170 tm.tm_mday = strtol(s, &s, 10);
171 tm.tm_mon = make_month(s, &s);
173 tm.tm_year = strtol(s, &s, 10);
174 tm.tm_hour = strtol(s, &s, 10);
176 tm.tm_min = strtol(s, &s, 10);
178 tm.tm_sec = strtol(s, &s, 10);
181 DBG(cerr <<
"Format...... Wkd, 00 Mon 0000 00:00:00 GMT" << endl);
182 if ((
int)strnlen(s, MAX_TIME_STR_LEN) < 20) {
183 DBG(cerr <<
"ERROR....... Not a valid time format \""
184 << s <<
"\"" << endl);
187 tm.tm_mday = strtol(s, &s, 10);
188 tm.tm_mon = make_month(s, &s);
189 tm.tm_year = strtol(s, &s, 10) - 1900;
190 tm.tm_hour = strtol(s, &s, 10);
192 tm.tm_min = strtol(s, &s, 10);
194 tm.tm_sec = strtol(s, &s, 10);
197 else if (isdigit((
int) *str)) {
198 if (strchr(str,
'T')) {
199 DBG(cerr <<
"Format...... YYYY.MM.DDThh:mmStzWkd" << endl);
201 while (*s && *s ==
' ') s++;
202 if ((
int)strnlen(s, MAX_TIME_STR_LEN) < 21) {
203 DBG(cerr <<
"ERROR....... Not a valid time format \"" << s <<
"\"" << endl);
206 tm.tm_year = strtol(s, &s, 10) - 1900;
208 tm.tm_mon = strtol(s, &s, 10); tm.tm_mon--;
210 tm.tm_mday = strtol(s, &s, 10);
212 tm.tm_hour = strtol(s, &s, 10);
214 tm.tm_min = strtol(s, &s, 10);
216 tm.tm_sec = strtol(s, &s, 10);
219 t = expand ? time(NULL) + atol(str) : atol(str);
224 DBG(cerr <<
"Format...... Wkd Mon 00 00:00:00 0000 GMT" << endl);
226 while (*s && *s ==
' ') s++;
227 DBG(cerr <<
"Trying...... The Wrong time format: " << s << endl);
228 if ((
int)strnlen(s, MAX_TIME_STR_LEN) < 24) {
229 DBG(cerr <<
"ERROR....... Not a valid time format \"" << s <<
"\"" << endl);
232 tm.tm_mon = make_month(s, &s);
233 tm.tm_mday = strtol(s, &s, 10);
234 tm.tm_hour = strtol(s, &s, 10);
236 tm.tm_min = strtol(s, &s, 10);
238 tm.tm_sec = strtol(s, &s, 10);
239 tm.tm_year = strtol(s, &s, 10) - 1900;
242 if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
243 tm.tm_min < 0 || tm.tm_min > 59 ||
244 tm.tm_hour < 0 || tm.tm_hour > 23 ||
245 tm.tm_mday < 1 || tm.tm_mday > 31 ||
246 tm.tm_mon < 0 || tm.tm_mon > 11 ||
247 tm.tm_year < 70 || tm.tm_year > 138) {
248 DBG(cerr <<
"ERROR....... Parsed illegal time" << endl);
255#if defined(HAVE_TIMEGM)
257#elif defined(HAVE_MKTIME)
258 return mktime(&tm) + offset_from_utc();
260#error "Neither mktime nor timegm defined"
275 if (!calendar)
return "";
277 char buf[MAX_TIME_STR_LEN];
281#if defined(_REENTRANT) || defined(SOLARIS)
283 localtime_r(calendar, &loctime);
284 strftime(buf, MAX_TIME_STR_LEN,
"%a, %d %b %Y %H:%M:%S", &loctime);
286 struct tm *loctime = localtime(calendar);
287 strftime(buf, MAX_TIME_STR_LEN,
"%a, %d %b %Y %H:%M:%S", loctime);
291#if defined(_REENTRANT) || defined(SOLARIS)
293 gmtime_r(calendar, &gmt);
294 strftime(buf, MAX_TIME_STR_LEN,
"%a, %d %b %Y %H:%M:%S GMT", &gmt);
296 struct tm *gmt = gmtime(calendar);
297 strftime(buf, MAX_TIME_STR_LEN,
"%a, %d %b %Y %H:%M:%S GMT", gmt);
304#if defined(_REENTRANT)
306 localtime_r(calendar, &loctime);
307 snprintf(buf, MAX_TIME_STR_LEN,
"%s, %02d %s %04d %02d:%02d:%02d",
308 wkdays[loctime.tm_wday],
310 months[loctime.tm_mon],
311 loctime.tm_year + 1900,
316 struct tm *loctime = localtime(calendar);
319 snprintf(buf, MAX_TIME_STR_LEN,
"%s, %02d %s %04d %02d:%02d:%02d",
320 wkdays[loctime->tm_wday],
322 months[loctime->tm_mon],
323 loctime->tm_year + 1900,
330#if defined(_REENTRANT) || defined(SOLARIS)
332 gmtime_r(calendar, &gmt);
333 snprintf(buf, MAX_TIME_STR_LEN,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
342 struct tm *gmt = gmtime(calendar);
345 snprintf(buf, MAX_TIME_STR_LEN,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
346 wkdays[gmt->tm_wday],
top level DAP object to house generic methods
string date_time_str(time_t *calendar, bool local)
time_t parse_time(const char *str, bool expand)