time_t posixtime (const char *s, unsigned int syntax_bits) { t.tm_isdst = -1; if (posix_time_parse (s, syntax_bits)) return (time_t)-1; else return mktime (&t); }
bool posixtime (time_t *p, const char *s, unsigned int syntax_bits) { struct tm tm0; struct tm tm1; struct tm const *tm; time_t t; if (posix_time_parse (&tm0, s, syntax_bits)) return false; tm1 = tm0; tm1.tm_isdst = -1; t = mktime (&tm1); if (t != (time_t) -1) tm = &tm1; else { /* mktime returns -1 for errors, but -1 is also a valid time_t value. Check whether an error really occurred. */ tm = localtime (&t); if (! tm) return false; } /* Reject dates like "September 31" and times like "25:61". Do not reject times that specify "60" as the number of seconds. */ if ((tm0.tm_year ^ tm->tm_year) | (tm0.tm_mon ^ tm->tm_mon) | (tm0.tm_mday ^ tm->tm_mday) | (tm0.tm_hour ^ tm->tm_hour) | (tm0.tm_min ^ tm->tm_min) | (tm0.tm_sec ^ tm->tm_sec)) { /* Any mismatch without 60 in the tm_sec field is invalid. */ if (tm0.tm_sec != 60) return false; { /* Allow times like 01:35:60 or 23:59:60. */ time_t dummy; char buf[16]; char *b = stpcpy (buf, s); strcpy (b - 2, "59"); if (!posixtime (&dummy, buf, syntax_bits)) return false; } } *p = t; return true; }
bool posixtime (time_t *p, const char *s, unsigned int syntax_bits) { struct tm tm0; struct tm tm1; struct tm const *tm; time_t t; if (posix_time_parse (&tm0, s, syntax_bits)) return false; tm1 = tm0; tm1.tm_isdst = -1; t = mktime (&tm1); if (t != (time_t) -1) tm = &tm1; else { /* mktime returns -1 for errors, but -1 is also a valid time_t value. Check whether an error really occurred. */ tm = localtime (&t); if (! tm) return false; } /* Reject dates like "September 31" and times like "25:61". */ if ((tm0.tm_year ^ tm->tm_year) | (tm0.tm_mon ^ tm->tm_mon) | (tm0.tm_mday ^ tm->tm_mday) | (tm0.tm_hour ^ tm->tm_hour) | (tm0.tm_min ^ tm->tm_min) | (tm0.tm_sec ^ tm->tm_sec)) return false; *p = t; return true; }