diff mbox series

[v3] date: make "iso-strict" conforming for the UTC timezone

Message ID 20240313225423.11373-1-dev+git@drbeat.li (mailing list archive)
State Accepted
Commit 69e2bee1a3ba2f9367d55992f401e6365be100ea
Headers show
Series [v3] date: make "iso-strict" conforming for the UTC timezone | expand

Commit Message

Beat Bolli March 13, 2024, 10:54 p.m. UTC
ISO 8601-1:2020-12 specifies that a zero timezone offset must be denoted
with a "Z" suffix instead of the numeric "+00:00". Add the correponding
special case to show_date() and a new test.

Changing an established output format which might be depended on by
scripts is always problematic, but here we choose to adhere more closely
to the published standard.

Reported-by: Michael Osipov <michael.osipov@innomotics.com>
Signed-off-by: Beat Bolli <dev+git@drbeat.li>
---
Changes from v2:

- changed the rationale according to Junio's feedback

Changes from v1:

- added a comment why the change is fine
- removed the Link: trailer

 date.c          | 14 +++++++++-----
 t/t0006-date.sh |  1 +
 2 files changed, 10 insertions(+), 5 deletions(-)

Comments

Junio C Hamano March 13, 2024, 11:06 p.m. UTC | #1
"Beat Bolli" <bb@drbeat.li> writes:

> ISO 8601-1:2020-12 specifies that a zero timezone offset must be denoted
> with a "Z" suffix instead of the numeric "+00:00". Add the correponding
> special case to show_date() and a new test.
>
> Changing an established output format which might be depended on by
> scripts is always problematic, but here we choose to adhere more closely
> to the published standard.

Perfect.  Thanks for following it through.  Will replace (I've
queued v2 already but it is easy to replace).

>
> Reported-by: Michael Osipov <michael.osipov@innomotics.com>
> Signed-off-by: Beat Bolli <dev+git@drbeat.li>
> ---
> Changes from v2:
>
> - changed the rationale according to Junio's feedback
>
> Changes from v1:
>
> - added a comment why the change is fine
> - removed the Link: trailer
>
>  date.c          | 14 +++++++++-----
>  t/t0006-date.sh |  1 +
>  2 files changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/date.c b/date.c
> index 619ada5b2044..44cf2221d81f 100644
> --- a/date.c
> +++ b/date.c
> @@ -342,14 +342,18 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
>  				tm->tm_hour, tm->tm_min, tm->tm_sec,
>  				tz);
>  	else if (mode->type == DATE_ISO8601_STRICT) {
> -		char sign = (tz >= 0) ? '+' : '-';
> -		tz = abs(tz);
> -		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
> +		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d",
>  				tm->tm_year + 1900,
>  				tm->tm_mon + 1,
>  				tm->tm_mday,
> -				tm->tm_hour, tm->tm_min, tm->tm_sec,
> -				sign, tz / 100, tz % 100);
> +				tm->tm_hour, tm->tm_min, tm->tm_sec);
> +		if (tz == 0) {
> +			strbuf_addch(&timebuf, 'Z');
> +		} else {
> +			strbuf_addch(&timebuf, tz >= 0 ? '+' : '-');
> +			tz = abs(tz);
> +			strbuf_addf(&timebuf, "%02d:%02d", tz / 100, tz % 100);
> +		}
>  	} else if (mode->type == DATE_RFC2822)
>  		strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
>  			weekday_names[tm->tm_wday], tm->tm_mday,
> diff --git a/t/t0006-date.sh b/t/t0006-date.sh
> index e18b1602864e..1d228a981ee9 100755
> --- a/t/t0006-date.sh
> +++ b/t/t0006-date.sh
> @@ -46,6 +46,7 @@ check_show () {
>  TIME='1466000000 +0200'
>  check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
>  check_show iso8601-strict "$TIME" '2016-06-15T16:13:20+02:00'
> +check_show iso8601-strict "$(echo "$TIME" | sed 's/+0200$/+0000/')" '2016-06-15T14:13:20Z'
>  check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
>  check_show short "$TIME" '2016-06-15'
>  check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
diff mbox series

Patch

diff --git a/date.c b/date.c
index 619ada5b2044..44cf2221d81f 100644
--- a/date.c
+++ b/date.c
@@ -342,14 +342,18 @@  const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 				tm->tm_hour, tm->tm_min, tm->tm_sec,
 				tz);
 	else if (mode->type == DATE_ISO8601_STRICT) {
-		char sign = (tz >= 0) ? '+' : '-';
-		tz = abs(tz);
-		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
+		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d",
 				tm->tm_year + 1900,
 				tm->tm_mon + 1,
 				tm->tm_mday,
-				tm->tm_hour, tm->tm_min, tm->tm_sec,
-				sign, tz / 100, tz % 100);
+				tm->tm_hour, tm->tm_min, tm->tm_sec);
+		if (tz == 0) {
+			strbuf_addch(&timebuf, 'Z');
+		} else {
+			strbuf_addch(&timebuf, tz >= 0 ? '+' : '-');
+			tz = abs(tz);
+			strbuf_addf(&timebuf, "%02d:%02d", tz / 100, tz % 100);
+		}
 	} else if (mode->type == DATE_RFC2822)
 		strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
 			weekday_names[tm->tm_wday], tm->tm_mday,
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index e18b1602864e..1d228a981ee9 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -46,6 +46,7 @@  check_show () {
 TIME='1466000000 +0200'
 check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
 check_show iso8601-strict "$TIME" '2016-06-15T16:13:20+02:00'
+check_show iso8601-strict "$(echo "$TIME" | sed 's/+0200$/+0000/')" '2016-06-15T14:13:20Z'
 check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
 check_show short "$TIME" '2016-06-15'
 check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'