diff mbox series

[v3,2/2] date.c: allow compact version of ISO-8601 datetime

Message ID 225b6401bd1f7eddc245acfd2c4b37c50c978491.1587559135.git.congdanhqx@gmail.com (mailing list archive)
State New, archived
Headers show
Series More ISO-8601 support | expand

Commit Message

Đoàn Trần Công Danh April 22, 2020, 1:15 p.m. UTC
Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
---
 date.c          | 22 ++++++++++++++++++++++
 t/t0006-date.sh |  3 +++
 2 files changed, 25 insertions(+)

Comments

Junio C Hamano April 22, 2020, 5:17 p.m. UTC | #1
Đoàn Trần Công Danh  <congdanhqx@gmail.com> writes:

> Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
> ---
>  date.c          | 22 ++++++++++++++++++++++
>  t/t0006-date.sh |  3 +++
>  2 files changed, 25 insertions(+)
>
> diff --git a/date.c b/date.c
> index 62f23b4702..882242c2db 100644
> --- a/date.c
> +++ b/date.c
> @@ -672,6 +672,28 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
>  		n++;
>  	} while (isdigit(date[n]));
>  
> +	/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
> +	/* 6 digits, compact style of ISO-8601's time: HHMMSS */
> +	if (n == 8 || n == 6) {
> +		unsigned int num1 = num / 10000;
> +		unsigned int num2 = (num % 10000) / 100;
> +		unsigned int num3 = num % 100;
> +		if (n == 8 && num1 > 1900 &&
> +		    num2 > 0 && num2 <= 12 &&
> +		    num3 > 0  && num3 <= 31) {
> +			tm->tm_year = num1 - 1900;
> +			tm->tm_mon  = num2 - 1;
> +			tm->tm_mday = num3;
> +		} else if (n == 6 && num1 < 60 && num2 < 60 && num3 <= 60) {
> +			tm->tm_hour = num1;
> +			tm->tm_min  = num2;
> +			tm->tm_sec  = num3;
> +			if (*end == '.' && isdigit(end[1]))
> +				strtoul(end + 1, &end, 10);
> +		}
> +		return end - date;
> +	}
> +

Looks sensible except that on our planet, one day has only 24 hours
;-).

I think we should try to reuse existing helpers as much as possible
in date.c to avoid such stupid errors.  During my review of [1/2] I
found is_date() would be a good thing to try reusing and also
extracted is_hms() as another candidate we could reuse.

>  	/* Four-digit year or a timezone? */
>  	if (n == 4) {
>  		if (num <= 1400 && *offset == -1) {
> diff --git a/t/t0006-date.sh b/t/t0006-date.sh
> index 80917c81c3..75ee9a96b8 100755
> --- a/t/t0006-date.sh
> +++ b/t/t0006-date.sh
> @@ -82,6 +82,9 @@ check_parse 2008-02-14 bad
>  check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
>  check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
>  check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
> +check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400'
> +check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400'
> +check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400'
>  check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
>  check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
>  check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'
Đoàn Trần Công Danh April 23, 2020, 1:20 a.m. UTC | #2
On 2020-04-22 10:17:35-0700, Junio C Hamano <gitster@pobox.com> wrote:
> Đoàn Trần Công Danh  <congdanhqx@gmail.com> writes:
> 
> > Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
> > ---
> >  date.c          | 22 ++++++++++++++++++++++
> >  t/t0006-date.sh |  3 +++
> >  2 files changed, 25 insertions(+)
> >
> > diff --git a/date.c b/date.c
> > index 62f23b4702..882242c2db 100644
> > --- a/date.c
> > +++ b/date.c
> > @@ -672,6 +672,28 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
> >  		n++;
> >  	} while (isdigit(date[n]));
> >  
> > +	/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
> > +	/* 6 digits, compact style of ISO-8601's time: HHMMSS */
> > +	if (n == 8 || n == 6) {
> > +		unsigned int num1 = num / 10000;
> > +		unsigned int num2 = (num % 10000) / 100;
> > +		unsigned int num3 = num % 100;
> > +		if (n == 8 && num1 > 1900 &&
> > +		    num2 > 0 && num2 <= 12 &&
> > +		    num3 > 0  && num3 <= 31) {
> > +			tm->tm_year = num1 - 1900;
> > +			tm->tm_mon  = num2 - 1;
> > +			tm->tm_mday = num3;
> > +		} else if (n == 6 && num1 < 60 && num2 < 60 && num3 <= 60) {
> > +			tm->tm_hour = num1;
> > +			tm->tm_min  = num2;
> > +			tm->tm_sec  = num3;
> > +			if (*end == '.' && isdigit(end[1]))
> > +				strtoul(end + 1, &end, 10);
> > +		}
> > +		return end - date;
> > +	}
> > +
> 
> Looks sensible except that on our planet, one day has only 24 hours
> ;-).

My bad, I admit that I wouldn't run into this error if we have the
helper is_hms (or is_time)

> 
> I think we should try to reuse existing helpers as much as possible
> in date.c to avoid such stupid errors.  During my review of [1/2] I
> found is_date() would be a good thing to try reusing and also

I'll look into this and see which value should be passed to is_date

> extracted is_hms() as another candidate we could reuse.
diff mbox series

Patch

diff --git a/date.c b/date.c
index 62f23b4702..882242c2db 100644
--- a/date.c
+++ b/date.c
@@ -672,6 +672,28 @@  static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
 		n++;
 	} while (isdigit(date[n]));
 
+	/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
+	/* 6 digits, compact style of ISO-8601's time: HHMMSS */
+	if (n == 8 || n == 6) {
+		unsigned int num1 = num / 10000;
+		unsigned int num2 = (num % 10000) / 100;
+		unsigned int num3 = num % 100;
+		if (n == 8 && num1 > 1900 &&
+		    num2 > 0 && num2 <= 12 &&
+		    num3 > 0  && num3 <= 31) {
+			tm->tm_year = num1 - 1900;
+			tm->tm_mon  = num2 - 1;
+			tm->tm_mday = num3;
+		} else if (n == 6 && num1 < 60 && num2 < 60 && num3 <= 60) {
+			tm->tm_hour = num1;
+			tm->tm_min  = num2;
+			tm->tm_sec  = num3;
+			if (*end == '.' && isdigit(end[1]))
+				strtoul(end + 1, &end, 10);
+		}
+		return end - date;
+	}
+
 	/* Four-digit year or a timezone? */
 	if (n == 4) {
 		if (num <= 1400 && *offset == -1) {
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index 80917c81c3..75ee9a96b8 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -82,6 +82,9 @@  check_parse 2008-02-14 bad
 check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
 check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
 check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
+check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400'
+check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400'
+check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400'
 check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
 check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
 check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'