Skip to content

Commit 073281e

Browse files
peffgitster
authored andcommitted
pass TIME_DATE_NOW to approxidate future-check
The approxidate functions accept an extra "now" parameter to avoid calling time() themselves. We use this in our test suite to make sure we have a consistent time for computing relative dates. However, deep in the bowels of approxidate, we also call time() to check whether possible dates are far in the future. Let's make sure that the "now" override makes it to that spot, too, so we can consistently test that feature. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c4ab27 commit 073281e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

date.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
396396
return 0;
397397
}
398398

399-
static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
399+
static int match_multi_number(unsigned long num, char c, const char *date,
400+
char *end, struct tm *tm, time_t now)
400401
{
401-
time_t now;
402402
struct tm now_tm;
403403
struct tm *refuse_future;
404404
long num2, num3;
@@ -424,7 +424,8 @@ static int match_multi_number(unsigned long num, char c, const char *date, char
424424
case '-':
425425
case '/':
426426
case '.':
427-
now = time(NULL);
427+
if (!now)
428+
now = time(NULL);
428429
refuse_future = NULL;
429430
if (gmtime_r(&now, &now_tm))
430431
refuse_future = &now_tm;
@@ -504,7 +505,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
504505
case '/':
505506
case '-':
506507
if (isdigit(end[1])) {
507-
int match = match_multi_number(num, *end, date, end, tm);
508+
int match = match_multi_number(num, *end, date, end, tm, 0);
508509
if (match)
509510
return match;
510511
}
@@ -1000,7 +1001,8 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
10001001
return end;
10011002
}
10021003

1003-
static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
1004+
static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1005+
time_t now)
10041006
{
10051007
char *end;
10061008
unsigned long number = strtoul(date, &end, 10);
@@ -1011,7 +1013,8 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
10111013
case '/':
10121014
case '-':
10131015
if (isdigit(end[1])) {
1014-
int match = match_multi_number(number, *end, date, end, tm);
1016+
int match = match_multi_number(number, *end, date, end,
1017+
tm, now);
10151018
if (match)
10161019
return date + match;
10171020
}
@@ -1074,7 +1077,7 @@ static unsigned long approxidate_str(const char *date,
10741077
date++;
10751078
if (isdigit(c)) {
10761079
pending_number(&tm, &number);
1077-
date = approxidate_digit(date-1, &tm, &number);
1080+
date = approxidate_digit(date-1, &tm, &number, time_sec);
10781081
touched = 1;
10791082
continue;
10801083
}

0 commit comments

Comments
 (0)