fix bug in millisec parsing

This commit is contained in:
CK Tan 2021-07-04 19:20:08 -07:00
parent 24e4472b44
commit 511749ebb9

28
toml.c
View file

@ -1900,6 +1900,8 @@ toml_table_t* toml_table_at(const toml_array_t* arr, int idx)
} }
static int parse_millisec(const char* p, const char** endp);
int toml_rtots(toml_raw_t src_, toml_timestamp_t* ret) int toml_rtots(toml_raw_t src_, toml_timestamp_t* ret)
{ {
if (! src_) return -1; if (! src_) return -1;
@ -1941,17 +1943,9 @@ int toml_rtots(toml_raw_t src_, toml_timestamp_t* ret)
/* optionally, parse millisec */ /* optionally, parse millisec */
p += 8; p += 8;
if (*p == '.') { if (*p == '.') {
char* qq; p++; /* skip '.' */
p++; const char* qq;
errno = 0; *millisec = parse_millisec(p, &qq);
*millisec = strtol(p, &qq, 10);
if (errno) {
return -1;
}
while (*millisec > 999) {
*millisec /= 10;
}
ret->millisec = millisec; ret->millisec = millisec;
p = qq; p = qq;
} }
@ -2303,3 +2297,15 @@ toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key)
} }
return ret; return ret;
} }
static int parse_millisec(const char* p, const char** endp)
{
int ret = 0;
int unit = 100; /* unit in millisec */
for ( ; '0' <= *p && *p <= '9'; p++, unit /= 10) {
ret += (*p - '0') * unit;
}
*endp = p;
return ret;
}