@matheusbsbs / matheus-bagliano / commits / 544ed961a5
Fork 0

date.c: allow compact version of ISO-8601 datetime

Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Đoàn Trần Công Danh committed Apr 24, 2020 at 22:07 UTC 544ed961a50c3442a3ec5643f2c443cd3e17311a
2 files changed +17
date.c
+14
@@ -687,6 +687,20 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
687 n++;
688 } while (isdigit(date[n]));
689
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 +
704 /* Four-digit year or a timezone? */
705 if (n == 4) {
706 if (num <= 1400 && *offset == -1) {
t/t0006-date.sh
+3
@@ -82,6 +82,9 @@ check_parse 2008-02-14 bad
82 check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
83 check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
84 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'
88 check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
89 check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
90 check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'