partially working strptime
it's missing at least: - derived fields - week numbers - short year (without century) support - locale modifiers
This commit is contained in:
parent
cf8506ad94
commit
47a8816ded
1 changed files with 149 additions and 148 deletions
|
|
@ -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>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
char *strptime(const char *s, const char *f, struct tm *tm)
|
char *strptime(const char *s, const char *f, struct tm *tm)
|
||||||
{
|
{
|
||||||
return NULL;
|
int i, w, neg, adj, min, range, *dest;
|
||||||
}
|
const char *ex;
|
||||||
|
size_t len;
|
||||||
#if 0
|
while (*f) {
|
||||||
|
if (*f != '%') {
|
||||||
char *strptime(const char *s, const char *f, struct tm *tm)
|
if (isspace(*f)) for (; *s && isspace(*s); s++);
|
||||||
{
|
else if (*s != *f) return 0;
|
||||||
nl_item item;
|
else s++;
|
||||||
int *dest;
|
f++;
|
||||||
const char *fmt;
|
}
|
||||||
for (; *f; f++) {
|
f++;
|
||||||
if (isspace(*f)) goto whitespace;
|
if (*f == '+') f++;
|
||||||
if (*f == '%') {
|
if (isdigit(*f)) w=strtoul(f, (void *)&f, 10);
|
||||||
do_fmt:
|
else w=-1;
|
||||||
switch (*++f) {
|
adj=0;
|
||||||
case '%':
|
switch (*f++) {
|
||||||
goto literal;
|
case 'a': case 'A':
|
||||||
case 'E':
|
dest = &tm->tm_wday;
|
||||||
case 'O':
|
min = ABDAY_1;
|
||||||
goto do_fmt;
|
range = 7;
|
||||||
case 'a':
|
goto symbolic_range;
|
||||||
item = ABDAY_1 + tm->tm_wday;
|
case 'b': case 'B': case 'h':
|
||||||
goto nl_strcat;
|
dest = &tm->tm_mon;
|
||||||
case 'A':
|
min = ABMON_1;
|
||||||
item = DAY_1 + tm->tm_wday;
|
range = 12;
|
||||||
goto nl_strcat;
|
goto symbolic_range;
|
||||||
case 'h':
|
|
||||||
case 'b':
|
|
||||||
item = ABMON_1 + tm->tm_mon;
|
|
||||||
goto nl_strcat;
|
|
||||||
case 'B':
|
|
||||||
item = MON_1 + tm->tm_mon;
|
|
||||||
goto nl_strcat;
|
|
||||||
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;
|
||||||
|
switch((char *)dest - (char *)tm) {
|
||||||
|
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
|
||||||
|
;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
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
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue