Skip to content

Commit 544ed96

Browse files
sgngitster
authored andcommitted
date.c: allow compact version of ISO-8601 datetime
Signed-off-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b784840 commit 544ed96

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

date.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,20 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
687687
n++;
688688
} while (isdigit(date[n]));
689689

690+
/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
691+
/* 6 digits, compact style of ISO-8601's time: HHMMSS */
692+
if (n == 8 || n == 6) {
693+
unsigned int num1 = num / 10000;
694+
unsigned int num2 = (num % 10000) / 100;
695+
unsigned int num3 = num % 100;
696+
if (n == 8)
697+
set_date(num1, num2, num3, NULL, time(NULL), tm);
698+
else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
699+
*end == '.' && isdigit(end[1]))
700+
strtoul(end + 1, &end, 10);
701+
return end - date;
702+
}
703+
690704
/* Four-digit year or a timezone? */
691705
if (n == 4) {
692706
if (num <= 1400 && *offset == -1) {

t/t0006-date.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ check_parse 2008-02-14 bad
8282
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
8383
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
8484
check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
85+
check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400'
86+
check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400'
87+
check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400'
8588
check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
8689
check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
8790
check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'

0 commit comments

Comments
 (0)