提交 47a8816d 编写于 作者: R Rich Felker

partially working strptime

it's missing at least:
- derived fields
- week numbers
- short year (without century) support
- locale modifiers
上级 cf8506ad
...@@ -2,177 +2,178 @@ ...@@ -2,177 +2,178 @@
#include <stdlib.h> #include <stdlib.h>
#include <langinfo.h> #include <langinfo.h>
#include <time.h> #include <time.h>
#include <ctype.h>
const char *__langinfo(nl_item); #include <stddef.h>
#include <string.h>
char *strptime(const char *s, const char *f, struct tm *tm) #include <strings.h>
{
return NULL;
}
#if 0
char *strptime(const char *s, const char *f, struct tm *tm) char *strptime(const char *s, const char *f, struct tm *tm)
{ {
nl_item item; int i, w, neg, adj, min, range, *dest;
int *dest; const char *ex;
const char *fmt; size_t len;
for (; *f; f++) { while (*f) {
if (isspace(*f)) goto whitespace; if (*f != '%') {
if (*f == '%') { if (isspace(*f)) for (; *s && isspace(*s); s++);
do_fmt: else if (*s != *f) return 0;
switch (*++f) { else s++;
case '%': f++;
goto literal; }
case 'E': f++;
case 'O': if (*f == '+') f++;
goto do_fmt; if (isdigit(*f)) w=strtoul(f, (void *)&f, 10);
case 'a': else w=-1;
item = ABDAY_1 + tm->tm_wday; adj=0;
goto nl_strcat; switch (*f++) {
case 'A': case 'a': case 'A':
item = DAY_1 + tm->tm_wday; dest = &tm->tm_wday;
goto nl_strcat; min = ABDAY_1;
case 'h': range = 7;
case 'b': goto symbolic_range;
item = ABMON_1 + tm->tm_mon; case 'b': case 'B': case 'h':
goto nl_strcat; dest = &tm->tm_mon;
case 'B': min = ABMON_1;
item = MON_1 + tm->tm_mon; range = 12;
goto nl_strcat; goto symbolic_range;
case 'c': case 'c':
item = D_T_FMT; s = strptime(s, nl_langinfo(D_T_FMT), tm);
goto nl_strftime; if (!s) return 0;
break;
case 'C': case 'C':
val = (1900+tm->tm_year) / 100; case 'd': case 'e':
fmt = "%02d"; dest = &tm->tm_mday;
goto number; min = 1;
case 'd': range = 31;
val = tm->tm_mday; goto numeric_range;
fmt = "%02d";
goto number;
case 'D': case 'D':
fmt = "%m/%d/%y"; s = strptime(s, "%m/%d/%y", tm);
goto recu_strftime; if (!s) return 0;
case 'e': break;
val = tm->tm_mday;
fmt = "%2d";
goto number;
case 'F':
fmt = "%Y-%m-%d";
goto recu_strftime;
case 'g':
// FIXME
val = 0; //week_based_year(tm)%100;
fmt = "%02d";
goto number;
case 'G':
// FIXME
val = 0; //week_based_year(tm);
fmt = "%04d";
goto number;
case 'H': case 'H':
val = tm->tm_hour; dest = &tm->tm_hour;
fmt = "%02d"; min = 0;
goto number; range = 24;
goto numeric_range;
case 'I': case 'I':
val = tm->tm_hour; dest = &tm->tm_hour;
if (!val) val = 12; min = 1;
else if (val > 12) val -= 12; range = 12;
fmt = "%02d"; goto numeric_range;
goto number;
case 'j': case 'j':
val = tm->tm_yday+1; dest = &tm->tm_yday;
fmt = "%03d"; min = 1;
goto number; range = 366;
goto numeric_range;
case 'm': case 'm':
val = tm->tm_mon+1; dest = &tm->tm_mon;
fmt = "%02d"; min = 1;
goto number; range = 12;
adj = 1;
goto numeric_range;
case 'M': case 'M':
val = tm->tm_min; dest = &tm->tm_min;
fmt = "%02d"; min = 0;
goto number; range = 60;
case 'n': goto numeric_range;
case 't': case 'n': case 't':
goto whitespace; for (; *s && isspace(*s); s++);
break;
case 'p': case 'p':
item = tm->tm_hour >= 12 ? PM_STR : AM_STR; ex = nl_langinfo(AM_STR);
goto nl_strcat; len = strlen(ex);
if (!strncasecmp(s, ex, len)) {
tm->tm_hour %= 12;
break;
}
ex = nl_langinfo(PM_STR);
len = strlen(ex);
if (!strncasecmp(s, ex, len)) {
tm->tm_hour %= 12;
tm->tm_hour += 12;
break;
}
return 0;
case 'r': case 'r':
item = T_FMT_AMPM; s = strptime(s, nl_langinfo(T_FMT_AMPM), tm);
goto nl_strftime; if (!s) return 0;
break;
case 'R': case 'R':
fmt = "%H:%M"; s = strptime(s, "%H:%M", tm);
goto recu_strftime; if (!s) return 0;
break;
case 'S': case 'S':
val = tm->tm_sec; dest = &tm->tm_sec;
fmt = "%02d"; min = 0;
goto number; range = 61;
goto numeric_range;
case 'T': case 'T':
fmt = "%H:%M:%S"; s = strptime(s, "%H:%M:%S", tm);
goto recu_strftime; if (!s) return 0;
case 'u': break;
val = tm->tm_wday ? tm->tm_wday : 7;
fmt = "%d";
goto number;
case 'U': case 'U':
case 'V':
case 'W': case 'W':
// FIXME: week number mess.. //FIXME
continue; return 0;
case 'w': case 'w':
val = tm->tm_wday; dest = &tm->tm_wday;
fmt = "%d"; min = 0;
goto number; range = 7;
goto numeric_range;
case 'x': case 'x':
item = D_FMT; s = strptime(s, nl_langinfo(D_FMT), tm);
goto nl_strftime; if (!s) return 0;
break;
case 'X': case 'X':
item = T_FMT; s = strptime(s, nl_langinfo(T_FMT), tm);
goto nl_strftime; if (!s) return 0;
break;
case 'y': case 'y':
val = tm->tm_year % 100; //FIXME
fmt = "%02d"; return 0;
goto number;
case 'Y': case 'Y':
val = tm->tm_year + 1900; dest = &tm->tm_year;
fmt = "%04d"; if (w<0) w=4;
goto number; adj = 1900;
case 'z': goto numeric_digits;
if (tm->tm_isdst < 0) continue; case '%':
val = timezone + (tm->tm_isdst) ? __dst_offset : 0; if (*s++ != '%') return 0;
l += snprintf(s+l, n-l, "%+02d%02d", val/60, abs(val%60)); break;
continue; numeric_range:
case 'Z': if (!isdigit(*s)) return 0;
if (tm->tm_isdst < 0 || !tzname[0] || !tzname[0][0]) *dest = 0;
continue; for (i=1; i<=min+range && isdigit(*s); i*=10)
l += snprintf(s+l, n-l, "%s", tzname[!!tm->tm_isdst]); *dest = *dest * 10 + *s++ - '0';
continue; if (*dest - min >= (unsigned)range) return 0;
} *dest -= adj;
default: switch((char *)dest - (char *)tm) {
return NULL; case offsetof(struct tm, tm_yday):
;
}
goto update;
numeric_digits:
neg = 0;
if (*s == '+') s++;
else if (*s == '-') neg=1, s++;
if (!isdigit(*s)) return 0;
for (i=0; i<w && isdigit(*s); i++)
*dest = *dest * 10 + *s++ - '0';
if (neg) *dest = -*dest;
*dest -= adj;
goto update;
symbolic_range:
for (i=2*range-1; i>=0; i--) {
ex = nl_langinfo(min+i);
len = strlen(ex);
if (strncasecmp(s, ex, len)) continue;
*dest = i % range;
break;
}
if (i<0) return 0;
goto update;
update:
//FIXME
;
} }
literal:
if (*s++ != *f) return NULL;
continue;
whitespace:
while(isspace(*s)) s++;
continue;
number:
l += snprintf(s+l, n-l, fmt, val);
continue;
nl_strcat:
l += snprintf(s+l, n-l, "%s", __langinfo(item));
continue;
nl_strftime:
fmt = __langinfo(item);
recu_strftime:
l += strftime(s+l, n-l, fmt, tm);
} }
if (l >= n) return 0; return (char *)s;
s[l] = 0;
return l;
} }
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册