Skip to content

Commit 9ea21fa

Browse files
committed
Merge branch 'jk/approxidate-avoid-y-d-m-over-future-dates' into maint
* 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
2 parents ba1edc9 + d372395 commit 9ea21fa

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

date.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
405405
return 0;
406406
}
407407

408-
static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
408+
static int match_multi_number(unsigned long num, char c, const char *date,
409+
char *end, struct tm *tm, time_t now)
409410
{
410-
time_t now;
411411
struct tm now_tm;
412412
struct tm *refuse_future;
413413
long num2, num3;
@@ -433,17 +433,18 @@ static int match_multi_number(unsigned long num, char c, const char *date, char
433433
case '-':
434434
case '/':
435435
case '.':
436-
now = time(NULL);
436+
if (!now)
437+
now = time(NULL);
437438
refuse_future = NULL;
438439
if (gmtime_r(&now, &now_tm))
439440
refuse_future = &now_tm;
440441

441442
if (num > 70) {
442443
/* yyyy-mm-dd? */
443-
if (is_date(num, num2, num3, refuse_future, now, tm))
444+
if (is_date(num, num2, num3, NULL, now, tm))
444445
break;
445446
/* yyyy-dd-mm? */
446-
if (is_date(num, num3, num2, refuse_future, now, tm))
447+
if (is_date(num, num3, num2, NULL, now, tm))
447448
break;
448449
}
449450
/* Our eastern European friends say dd.mm.yy[yy]
@@ -513,7 +514,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
513514
case '/':
514515
case '-':
515516
if (isdigit(end[1])) {
516-
int match = match_multi_number(num, *end, date, end, tm);
517+
int match = match_multi_number(num, *end, date, end, tm, 0);
517518
if (match)
518519
return match;
519520
}
@@ -1013,7 +1014,8 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
10131014
return end;
10141015
}
10151016

1016-
static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
1017+
static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1018+
time_t now)
10171019
{
10181020
char *end;
10191021
unsigned long number = strtoul(date, &end, 10);
@@ -1024,7 +1026,8 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
10241026
case '/':
10251027
case '-':
10261028
if (isdigit(end[1])) {
1027-
int match = match_multi_number(number, *end, date, end, tm);
1029+
int match = match_multi_number(number, *end, date, end,
1030+
tm, now);
10281031
if (match)
10291032
return date + match;
10301033
}
@@ -1087,7 +1090,7 @@ static unsigned long approxidate_str(const char *date,
10871090
date++;
10881091
if (isdigit(c)) {
10891092
pending_number(&tm, &number);
1090-
date = approxidate_digit(date-1, &tm, &number);
1093+
date = approxidate_digit(date-1, &tm, &number, time_sec);
10911094
touched = 1;
10921095
continue;
10931096
}

t/t0006-date.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ check_approxidate 'Jun 6, 5AM' '2009-06-06 05:00:00'
8282
check_approxidate '5AM Jun 6' '2009-06-06 05:00:00'
8383
check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
8484

85+
check_approxidate '2008-12-01' '2008-12-01 19:20:00'
86+
check_approxidate '2009-12-01' '2009-12-01 19:20:00'
87+
8588
test_done

0 commit comments

Comments
 (0)