Merge branch 'ph/parse-date-reduced-precision'
Loosen date parsing heuristics. * ph/parse-date-reduced-precision: date.c: allow ISO 8601 reduced precision timesmaint
commit
d2917b9099
37
date.c
37
date.c
|
|
@ -493,6 +493,12 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
|
|||
return 2;
|
||||
}
|
||||
|
||||
/* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
|
||||
if (*date == 'T' && isdigit(date[1]) && tm->tm_hour == -1) {
|
||||
tm->tm_min = tm->tm_sec = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* BAD CRAP */
|
||||
return skip_alpha(date);
|
||||
}
|
||||
|
|
@ -638,6 +644,18 @@ static inline int nodate(struct tm *tm)
|
|||
tm->tm_sec) < 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Have we seen an ISO-8601-alike date, i.e. 20220101T0,
|
||||
* In which, hour is still unset,
|
||||
* and minutes and second has been set to 0.
|
||||
*/
|
||||
static inline int maybeiso8601(struct tm *tm)
|
||||
{
|
||||
return tm->tm_hour == -1 &&
|
||||
tm->tm_min == 0 &&
|
||||
tm->tm_sec == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* We've seen a digit. Time? Year? Date?
|
||||
*/
|
||||
|
|
@ -701,6 +719,25 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
|
|||
return end - date;
|
||||
}
|
||||
|
||||
/* reduced precision of ISO-8601's time: HHMM or HH */
|
||||
if (maybeiso8601(tm)) {
|
||||
unsigned int num1 = num;
|
||||
unsigned int num2 = 0;
|
||||
if (n == 4) {
|
||||
num1 = num / 100;
|
||||
num2 = num % 100;
|
||||
}
|
||||
if ((n == 4 || n == 2) && !nodate(tm) &&
|
||||
set_time(num1, num2, 0, tm) == 0)
|
||||
return n;
|
||||
/*
|
||||
* We thought this is an ISO-8601 time string,
|
||||
* we set minutes and seconds to 0,
|
||||
* turn out it isn't, rollback the change.
|
||||
*/
|
||||
tm->tm_min = tm->tm_sec = -1;
|
||||
}
|
||||
|
||||
/* Four-digit year or a timezone? */
|
||||
if (n == 4) {
|
||||
if (num <= 1400 && *offset == -1) {
|
||||
|
|
|
|||
|
|
@ -88,6 +88,13 @@ check_parse 2008-02-14 bad
|
|||
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
|
||||
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
|
||||
check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
|
||||
check_parse '20080214T20:30:45' '2008-02-14 20:30:45 +0000'
|
||||
check_parse '20080214T20:30' '2008-02-14 20:30:00 +0000'
|
||||
check_parse '20080214T20' '2008-02-14 20:00:00 +0000'
|
||||
check_parse '20080214T203045' '2008-02-14 20:30:45 +0000'
|
||||
check_parse '20080214T2030' '2008-02-14 20:30:00 +0000'
|
||||
check_parse '20080214T000000.20' '2008-02-14 00:00:00 +0000'
|
||||
check_parse '20080214T00:00:00.20' '2008-02-14 00:00:00 +0000'
|
||||
check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400'
|
||||
check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400'
|
||||
check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400'
|
||||
|
|
@ -99,6 +106,7 @@ check_parse '2008-02-14 20:30:45 -05' '2008-02-14 20:30:45 -0500'
|
|||
check_parse '2008-02-14 20:30:45 -:30' '2008-02-14 20:30:45 +0000'
|
||||
check_parse '2008-02-14 20:30:45 -05:00' '2008-02-14 20:30:45 -0500'
|
||||
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
|
||||
check_parse 'Thu, 7 Apr 2005 15:14:13 -0700' '2005-04-07 15:14:13 -0700'
|
||||
|
||||
check_approxidate() {
|
||||
echo "$1 -> $2 +0000" >expect
|
||||
|
|
|
|||
Loading…
Reference in New Issue