27 static const char* kWeekFull[] = {
28 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
29 "Thursday",
"Friday",
"Saturday" 32 static const char* kWeekAbbr[] = {
33 "Sun",
"Mon",
"Tue",
"Wed",
37 static const char* kMonthFull[] = {
38 "January",
"February",
"March",
"April",
"May",
"June",
39 "July",
"August",
"September",
"October",
"November",
"December" 42 static const char* kMonthAbbr[] = {
43 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
44 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" 47 static const char* _parse_num(
const char* s,
int low,
int high,
int* value) {
49 for (*value = 0; *p != NULL && isdigit(*p); ++p) {
50 *value = (*value) * 10 + (int)(*p) - (int)(
'0');
53 if (p == s || *value < low || *value > high)
return NULL;
57 static char* _strptime(
const char *s,
const char *format,
struct tm *tm) {
60 while (*format != NULL && *s != NULL) {
62 if (*s != *format)
return NULL;
75 for (i = 0; i < 7; ++
i) {
76 len = (int)(strlen(kWeekAbbr[i]));
77 if (_strnicmp(kWeekAbbr[i], s, len) == 0) {
82 len = (int)(strlen(kWeekFull[i]));
83 if (_strnicmp(kWeekFull[i], s, len) == 0) {
88 if (tm->tm_wday == -1)
return NULL;
99 for (i = 0; i < 12; ++
i) {
100 len = (int)(strlen(kMonthAbbr[i]));
101 if (_strnicmp(kMonthAbbr[i], s, len) == 0) {
106 len = (int)(strlen(kMonthFull[i]));
107 if (_strnicmp(kMonthFull[i], s, len) == 0) {
112 if (tm->tm_mon == -1)
return NULL;
119 s = _parse_num(s, 1, 12, &tm->tm_mon);
120 if (s == NULL)
return NULL;
127 s = _parse_num(s, 1, 31, &tm->tm_mday);
128 if (s == NULL)
return NULL;
133 s = _parse_num(s, 0, 23, &tm->tm_hour);
134 if (s == NULL)
return NULL;
139 s = _parse_num(s, 0, 59, &tm->tm_min);
140 if (s == NULL)
return NULL;
145 s = _parse_num(s, 0, 60, &tm->tm_sec);
146 if (s == NULL)
return NULL;
151 s = _parse_num(s, 1900, 9999, &tm->tm_year);
152 if (s == NULL)
return NULL;
158 s = _parse_num(s, 0, 99, &tm->tm_year);
159 if (s == NULL)
return NULL;
160 if (tm->tm_year <= 68) {
168 while (isspace(*s)) ++s;
173 if (*s !=
'%')
return NULL;
184 if (*format != NULL) {
191 char*
strptime(
const char *buf,
const char *fmt,
struct tm *tm) {
192 return _strptime(buf, fmt, tm);