git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] fixup! date.c: allow ISO 8601 reduced precision times

From
Đoàn Trần Công Danh <congdanhqx@gmail.com>
Date
Jan 9, 2023, 11:29 UTC
Message-ID
<Y7v6jThT9GQ8Oav8@danh.dev>
In-Reply-To
<xmqqbkn8um9q.fsf@gitster.g>
On 2023-01-09 17:48:01+0900, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> Phil Hord <phil.hord@gmail.com> writes:
> 
> > Do you have any suggestions about how I can better alleviate your
> > concerns?  I don't think there are real regressions here and I tried
> > to explain why.
> 
> Other than "including it in a released version and waiting for
> people to scream", I do not think there is.  The "next" branch was
> meant to be a test ground for these new features by letting
> volunteer users to use it in their everyday development, and the
> hope was that we can catch regressions by cooking risky topics
> longer than usual in there, but we haven't been very successful, I
> have to say.

While I think we shouldn't care much about ISO-8601, we should declare that we're only conformed to RFC-3339 format instead.

Below fixup could limit the change to only ISO-8601 strings I'm not entirely sure if this heuristics would break those people with 00:00:00.1234 timestamp or not (the added test cases shows that this change doesn't break ISO-8601 parsing, but I don't know).

On top of Hord's patch + Junio's next, all tests pass. ----8<----

Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
---
 date.c          | 21 +++++++++++++--------
 t/t0006-date.sh |  3 ++-
 2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/date.c b/date.c
index b011b9d6b3..19e6787aef 100644
--- a/date.c
+++ b/date.c
@@ -493,6 +493,12 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
 		return 2;
 	}
 
+	/* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
+	if (*date == 'T' && isdigit(date[1])) {
+		tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
+		return strlen("T");
+	}
+
 	/* BAD CRAP */
 	return skip_alpha(date);
 }
@@ -639,15 +645,14 @@ static inline int nodate(struct tm *tm)
 }
 
 /*
- * Have we filled in any part of the time yet?
- * We just do a binary 'and' to see if the sign bit
- * is set in all the values.
+ * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
+ * In those special case, those fields have been set to 0
  */
-static inline int notime(struct tm *tm)
+static inline int maybeiso8601(struct tm *tm)
 {
-	return (tm->tm_hour &
-		tm->tm_min &
-		tm->tm_sec) < 0;
+	return tm->tm_hour == 0 &&
+		tm->tm_min == 0 &&
+		tm->tm_sec == 0;
 }
 
 /*
@@ -704,7 +709,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
 	/* 4 digits, compact style of ISO-8601's time: HHMM */
 	/* 2 digits, compact style of ISO-8601's time: HH */
 	if (n == 8 || n == 6 ||
-		(!nodate(tm) && notime(tm) &&
+		(!nodate(tm) && maybeiso8601(tm) &&
 		(n == 4 || n == 2))) {
 		unsigned int num1 = num / 10000;
 		unsigned int num2 = (num % 10000) / 100;
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index 16fb0bf4bd..130207fc04 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -93,7 +93,8 @@ check_parse '20080214T20:30' '2008-02-14 20:30:00 +0000'
 check_parse '20080214T20' '2008-02-14 20:00:00 +0000'
 check_parse '20080214T203045' '2008-02-14 20:30:45 +0000'
 check_parse '20080214T2030' '2008-02-14 20:30:00 +0000'
-check_parse '20080214T20' '2008-02-14 20:00:00 +0000'
+check_parse '20080214T000000.20' '2008-02-14 00:00:00 +0000'
+check_parse '20080214T00:00:00.20' '2008-02-14 00:00:00 +0000'
 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'
-- 
Danh
Previous: Phil HordNext: Đoàn Trần Công Danh
Message 8 of 12 in “date.c: allow ISO 8601 reduced precision times”
  1. date.c: allow ISO 8601 reduced precision timesPhil Hord, Dec 16, 2022
  2. Junio C HamanoDec 16, 2022
  3. Phil HordDec 16, 2022
  4. Phil HordJan 9, 2023
  5. Junio C HamanoJan 9, 2023
  6. Junio C HamanoJan 9, 2023
  7. Phil HordJan 9, 2023
  8. fixup! date.c: allow ISO 8601 reduced precision timesĐoàn Trần Công Danh, Jan 9, 2023
  9. date.c: limit less precision ISO-8601 with its markerĐoàn Trần Công Danh, Jan 9, 2023
  10. Phil HordJan 9, 2023
  11. date.c: allow ISO 8601 reduced precision timesĐoàn Trần Công Danh, Jan 11, 2023
  12. Junio C HamanoJan 13, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.