Skip to content

Commit

Permalink
Merge branch 'jk/approxidate-avoid-y-d-m-over-future-dates' into maint
Browse files Browse the repository at this point in the history
* jk/approxidate-avoid-y-d-m-over-future-dates:
  approxidate: allow ISO-like dates far in the future
  pass TIME_DATE_NOW to approxidate future-check
  • Loading branch information
Junio C Hamano committed Jan 12, 2015
2 parents ba1edc9 + d372395 commit 9ea21fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 12 additions & 9 deletions date.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
return 0;
}

static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
static int match_multi_number(unsigned long num, char c, const char *date,
char *end, struct tm *tm, time_t now)
{
time_t now;
struct tm now_tm;
struct tm *refuse_future;
long num2, num3;
Expand All @@ -433,17 +433,18 @@ static int match_multi_number(unsigned long num, char c, const char *date, char
case '-':
case '/':
case '.':
now = time(NULL);
if (!now)
now = time(NULL);
refuse_future = NULL;
if (gmtime_r(&now, &now_tm))
refuse_future = &now_tm;

if (num > 70) {
/* yyyy-mm-dd? */
if (is_date(num, num2, num3, refuse_future, now, tm))
if (is_date(num, num2, num3, NULL, now, tm))
break;
/* yyyy-dd-mm? */
if (is_date(num, num3, num2, refuse_future, now, tm))
if (is_date(num, num3, num2, NULL, now, tm))
break;
}
/* Our eastern European friends say dd.mm.yy[yy]
Expand Down Expand Up @@ -513,7 +514,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
case '/':
case '-':
if (isdigit(end[1])) {
int match = match_multi_number(num, *end, date, end, tm);
int match = match_multi_number(num, *end, date, end, tm, 0);
if (match)
return match;
}
Expand Down Expand Up @@ -1013,7 +1014,8 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
return end;
}

static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
time_t now)
{
char *end;
unsigned long number = strtoul(date, &end, 10);
Expand All @@ -1024,7 +1026,8 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
case '/':
case '-':
if (isdigit(end[1])) {
int match = match_multi_number(number, *end, date, end, tm);
int match = match_multi_number(number, *end, date, end,
tm, now);
if (match)
return date + match;
}
Expand Down Expand Up @@ -1087,7 +1090,7 @@ static unsigned long approxidate_str(const char *date,
date++;
if (isdigit(c)) {
pending_number(&tm, &number);
date = approxidate_digit(date-1, &tm, &number);
date = approxidate_digit(date-1, &tm, &number, time_sec);
touched = 1;
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions t/t0006-date.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ check_approxidate 'Jun 6, 5AM' '2009-06-06 05:00:00'
check_approxidate '5AM Jun 6' '2009-06-06 05:00:00'
check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'

check_approxidate '2008-12-01' '2008-12-01 19:20:00'
check_approxidate '2009-12-01' '2009-12-01 19:20:00'

test_done

0 comments on commit 9ea21fa

Please sign in to comment.