Merge branch 'dd/iso-8601-updates'

The approxidate parser learns to parse seconds with fraction.

* dd/iso-8601-updates:
  date.c: allow compact version of ISO-8601 datetime
  date.c: skip fractional second part of ISO-8601
  date.c: validate and set time in a helper function
  date.c: s/is_date/set_date/
maint
Junio C Hamano 2020-05-05 14:54:26 -07:00
commit cdfa156a93
3 changed files with 62 additions and 16 deletions

View File

@ -20,7 +20,10 @@ RFC 2822::
ISO 8601:: ISO 8601::
Time and date specified by the ISO 8601 standard, for example Time and date specified by the ISO 8601 standard, for example
`2005-04-07T22:13:13`. The parser accepts a space instead of the `2005-04-07T22:13:13`. The parser accepts a space instead of the
`T` character as well. `T` character as well. Fractional parts of a second will be ignored,
for example `2005-04-07T22:13:13.019` will be treated as
`2005-04-07T22:13:13`

+ +
NOTE: In addition, the date part is accepted in the following formats: NOTE: In addition, the date part is accepted in the following formats:
`YYYY.MM.DD`, `MM/DD/YYYY` and `DD.MM.YYYY`. `YYYY.MM.DD`, `MM/DD/YYYY` and `DD.MM.YYYY`.

67
date.c
View File

@ -497,7 +497,7 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
return skip_alpha(date); return skip_alpha(date);
} }


static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
{ {
if (month > 0 && month < 13 && day > 0 && day < 32) { if (month > 0 && month < 13 && day > 0 && day < 32) {
struct tm check = *tm; struct tm check = *tm;
@ -518,9 +518,9 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
else if (year < 38) else if (year < 38)
r->tm_year = year + 100; r->tm_year = year + 100;
else else
return 0; return -1;
if (!now_tm) if (!now_tm)
return 1; return 0;


specified = tm_to_time_t(r); specified = tm_to_time_t(r);


@ -529,14 +529,33 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
* sure it is not later than ten days from now... * sure it is not later than ten days from now...
*/ */
if ((specified != -1) && (now + 10*24*3600 < specified)) if ((specified != -1) && (now + 10*24*3600 < specified))
return 0; return -1;
tm->tm_mon = r->tm_mon; tm->tm_mon = r->tm_mon;
tm->tm_mday = r->tm_mday; tm->tm_mday = r->tm_mday;
if (year != -1) if (year != -1)
tm->tm_year = r->tm_year; tm->tm_year = r->tm_year;
return 1; return 0;
} }
return 0; return -1;
}

static int set_time(long hour, long minute, long second, struct tm *tm)
{
/* We accept 61st second because of leap second */
if (0 <= hour && hour <= 24 &&
0 <= minute && minute < 60 &&
0 <= second && second <= 60) {
tm->tm_hour = hour;
tm->tm_min = minute;
tm->tm_sec = second;
return 0;
}
return -1;
}

static int is_date_known(struct tm *tm)
{
return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;
} }


static int match_multi_number(timestamp_t num, char c, const char *date, static int match_multi_number(timestamp_t num, char c, const char *date,
@ -556,10 +575,14 @@ static int match_multi_number(timestamp_t num, char c, const char *date,
case ':': case ':':
if (num3 < 0) if (num3 < 0)
num3 = 0; num3 = 0;
if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { if (set_time(num, num2, num3, tm) == 0) {
tm->tm_hour = num; /*
tm->tm_min = num2; * If %H:%M:%S was just parsed followed by: .<num4>
tm->tm_sec = num3; * Consider (& discard) it as fractional second
* if %Y%m%d is parsed before.
*/
if (*end == '.' && isdigit(end[1]) && is_date_known(tm))
strtol(end + 1, &end, 10);
break; break;
} }
return 0; return 0;
@ -575,10 +598,10 @@ static int match_multi_number(timestamp_t num, char c, const char *date,


if (num > 70) { if (num > 70) {
/* yyyy-mm-dd? */ /* yyyy-mm-dd? */
if (is_date(num, num2, num3, NULL, now, tm)) if (set_date(num, num2, num3, NULL, now, tm) == 0)
break; break;
/* yyyy-dd-mm? */ /* yyyy-dd-mm? */
if (is_date(num, num3, num2, NULL, now, tm)) if (set_date(num, num3, num2, NULL, now, tm) == 0)
break; break;
} }
/* Our eastern European friends say dd.mm.yy[yy] /* Our eastern European friends say dd.mm.yy[yy]
@ -586,14 +609,14 @@ static int match_multi_number(timestamp_t num, char c, const char *date,
* mm/dd/yy[yy] form only when separator is not '.' * mm/dd/yy[yy] form only when separator is not '.'
*/ */
if (c != '.' && if (c != '.' &&
is_date(num3, num, num2, refuse_future, now, tm)) set_date(num3, num, num2, refuse_future, now, tm) == 0)
break; break;
/* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
if (is_date(num3, num2, num, refuse_future, now, tm)) if (set_date(num3, num2, num, refuse_future, now, tm) == 0)
break; break;
/* Funny European mm.dd.yy */ /* Funny European mm.dd.yy */
if (c == '.' && if (c == '.' &&
is_date(num3, num, num2, refuse_future, now, tm)) set_date(num3, num, num2, refuse_future, now, tm) == 0)
break; break;
return 0; return 0;
} }
@ -664,6 +687,20 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
n++; n++;
} while (isdigit(date[n])); } while (isdigit(date[n]));


/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
/* 6 digits, compact style of ISO-8601's time: HHMMSS */
if (n == 8 || n == 6) {
unsigned int num1 = num / 10000;
unsigned int num2 = (num % 10000) / 100;
unsigned int num3 = num % 100;
if (n == 8)
set_date(num1, num2, num3, NULL, time(NULL), tm);
else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
*end == '.' && isdigit(end[1]))
strtoul(end + 1, &end, 10);
return end - date;
}

/* Four-digit year or a timezone? */ /* Four-digit year or a timezone? */
if (n == 4) { if (n == 4) {
if (num <= 1400 && *offset == -1) { if (num <= 1400 && *offset == -1) {

View File

@ -81,6 +81,11 @@ check_parse 2008-02 bad
check_parse 2008-02-14 bad 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' '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 '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
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'
check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015' check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000' check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'
check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000' check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000'
@ -103,6 +108,7 @@ check_approxidate 5.seconds.ago '2009-08-30 19:19:55'
check_approxidate 10.minutes.ago '2009-08-30 19:10:00' check_approxidate 10.minutes.ago '2009-08-30 19:10:00'
check_approxidate yesterday '2009-08-29 19:20:00' check_approxidate yesterday '2009-08-29 19:20:00'
check_approxidate 3.days.ago '2009-08-27 19:20:00' check_approxidate 3.days.ago '2009-08-27 19:20:00'
check_approxidate '12:34:56.3.days.ago' '2009-08-27 12:34:56'
check_approxidate 3.weeks.ago '2009-08-09 19:20:00' check_approxidate 3.weeks.ago '2009-08-09 19:20:00'
check_approxidate 3.months.ago '2009-05-30 19:20:00' check_approxidate 3.months.ago '2009-05-30 19:20:00'
check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00' check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00'