summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2020-10-12 18:55:35 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2020-10-12 20:13:46 +0200
commit33c50db302957bca2a850ac8d0b960d05ee0520e (patch)
tree4c83183ad3a379b0837fb264d3ada2bb56102741
parent0ea6495717a3245d1da079b5d9570a6cf776ef1a (diff)
simplify time parsing
-rw-r--r--sfeed.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/sfeed.c b/sfeed.c
index 9accd00..cfec8f7 100644
--- a/sfeed.c
+++ b/sfeed.c
@@ -554,9 +554,15 @@ parsetime(const char *s, time_t *tp)
if (!isdigit((unsigned char)*s) && !isalpha((unsigned char)*s))
return -1;
- if (strspn(s, "0123456789") >= 4) {
+ if (isdigit((unsigned char)s[0]) &&
+ isdigit((unsigned char)s[1]) &&
+ isdigit((unsigned char)s[2]) &&
+ isdigit((unsigned char)s[3])) {
/* formats "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S" or "%Y%m%d%H%M%S" */
- vi = 0;
+ va[0] = ((s[0] - '0') * 1000) + ((s[1] - '0') * 100) +
+ ((s[2] - '0') * 10) + (s[3] - '0');
+ vi = 1;
+ s += 4;
} else {
/* format: "[%a, ]%d %b %Y %H:%M:%S" */
/* parse "[%a, ]%d %b %Y " part, then use time parsing as above */
@@ -602,17 +608,16 @@ parsetime(const char *s, time_t *tp)
}
/* parse time parts (and possibly remaining date parts) */
- for (; *s && vi < 6; vi++) {
- for (i = 0, v = 0; i < ((vi == 0) ? 4 : 2) &&
- isdigit((unsigned char)*s); s++, i++) {
- v = (v * 10) + (*s - '0');
- }
- va[vi] = v;
-
- if ((vi < 2 && *s == '-') ||
- (vi == 2 && (*s == 'T' || isspace((unsigned char)*s))) ||
- (vi > 2 && *s == ':'))
+ for (; *s && vi < 6; ) {
+ if (isdigit((unsigned char)s[0]) && isdigit((unsigned char)s[1])) {
+ va[vi++] = ((s[0] - '0') * 10) + (s[1] - '0');
+ s += 2;
+ } else if (vi > 2 && (*s == '-' || *s == '+' || *s == '.' ||
+ isspace((unsigned char)*s))) {
+ break;
+ } else {
s++;
+ }
}
/* skip milliseconds in for example: "%Y-%m-%dT%H:%M:%S.000Z" */