git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] pretty: Provide a strict ISO8601 date format
@ 2014-08-29 16:58 Beat Bolli
  2014-08-29 18:59 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Beat Bolli @ 2014-08-29 16:58 UTC (permalink / raw)
  To: git

There has been concern that Git's "ISO" date format does not really
conform to the ISO 8601 standard. Thus, it cannot be parsed by
ISO 8601 compliant parsers, e.g. those of XML toolchains.

The differences between the two formats are the following:
  - a space instead of the `T` date/time delimiter
  - a space between time and time zone
  - no colon between hours and minutes of the time zone

This commit adds a strict ISO 8601 date format for displaying committer
and author dates. It uses the '%aI' and '%cI' format specifiers (note
the uppercase 'I') and the '--date=iso-strict' or
'--date=iso8601-strict' date format names.

See http://thread.gmane.org/gmane.comp.version-control.git/255879 and
http://thread.gmane.org/gmane.comp.version-control.git/52414/focus=52585
for discussion.

Signed-off-by: Beat Bolli <bbolli@ewanet.ch>
---

v2:
  - Improve commit message
  - Fix time zone display for negative values close to zero

 Documentation/git-rev-list.txt     |  2 +-
 Documentation/pretty-formats.txt   |  6 ++++--
 Documentation/rev-list-options.txt | 13 +++++++++++--
 cache.h                            |  1 +
 date.c                             | 14 +++++++++++++-
 pretty.c                           |  5 ++++-
 t/t4205-log-pretty-formats.sh      |  7 +++++++
 7 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-rev-list.txt b/Documentation/git-rev-list.txt
index 7a1585d..fd7f8b5 100644
--- a/Documentation/git-rev-list.txt
+++ b/Documentation/git-rev-list.txt
@@ -45,7 +45,7 @@ SYNOPSIS
 	     [ \--regexp-ignore-case | -i ]
 	     [ \--extended-regexp | -E ]
 	     [ \--fixed-strings | -F ]
-	     [ \--date=(local|relative|default|iso|rfc|short) ]
+	     [ \--date=(local|relative|default|iso|iso-strict|rfc|short) ]
 	     [ [\--objects | \--objects-edge] [ \--unpacked ] ]
 	     [ \--pretty | \--header ]
 	     [ \--bisect ]
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 85d6353..50a2c30 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -115,7 +115,8 @@ The placeholders are:
 - '%aD': author date, RFC2822 style
 - '%ar': author date, relative
 - '%at': author date, UNIX timestamp
-- '%ai': author date, ISO 8601 format
+- '%ai': author date, ISO 8601-like format
+- '%aI': author date, strict ISO 8601 format
 - '%cn': committer name
 - '%cN': committer name (respecting .mailmap, see
   linkgit:git-shortlog[1] or linkgit:git-blame[1])
@@ -126,7 +127,8 @@ The placeholders are:
 - '%cD': committer date, RFC2822 style
 - '%cr': committer date, relative
 - '%ct': committer date, UNIX timestamp
-- '%ci': committer date, ISO 8601 format
+- '%ci': committer date, ISO 8601-like format
+- '%cI': committer date, strict ISO 8601 format
 - '%d': ref names, like the --decorate option of linkgit:git-log[1]
 - '%e': encoding
 - '%s': subject
diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index deb8cca..5d311b8 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -677,7 +677,7 @@ include::pretty-options.txt[]
 --relative-date::
 	Synonym for `--date=relative`.
 
---date=(relative|local|default|iso|rfc|short|raw)::
+--date=(relative|local|default|iso|iso-strict|rfc|short|raw)::
 	Only takes effect for dates shown in human-readable format, such
 	as when using `--pretty`. `log.date` config variable sets a default
 	value for the log command's `--date` option.
@@ -687,7 +687,16 @@ e.g. ``2 hours ago''.
 +
 `--date=local` shows timestamps in user's local time zone.
 +
-`--date=iso` (or `--date=iso8601`) shows timestamps in ISO 8601 format.
+`--date=iso` (or `--date=iso8601`) shows timestamps in a ISO 8601-like format.
+The differences to the strict ISO 8601 format are:
+
+	- a space instead of the `T` date/time delimiter
+	- a space between time and time zone
+	- no colon between hours and minutes of the time zone
+
++
+`--date=iso-strict` (or `--date=iso8601-strict`) shows timestamps in strict
+ISO 8601 format.
 +
 `--date=rfc` (or `--date=rfc2822`) shows timestamps in RFC 2822
 format, often found in email messages.
diff --git a/cache.h b/cache.h
index fcb511d..fa92aaf 100644
--- a/cache.h
+++ b/cache.h
@@ -1037,6 +1037,7 @@ enum date_mode {
 	DATE_SHORT,
 	DATE_LOCAL,
 	DATE_ISO8601,
+	DATE_ISO8601_STRICT,
 	DATE_RFC2822,
 	DATE_RAW
 };
diff --git a/date.c b/date.c
index 782de95..5d73d9b 100644
--- a/date.c
+++ b/date.c
@@ -200,7 +200,16 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
 				tm->tm_mday,
 				tm->tm_hour, tm->tm_min, tm->tm_sec,
 				tz);
-	else if (mode == DATE_RFC2822)
+	else if (mode == DATE_ISO8601_STRICT) {
+		char sign = (tz >= 0) ? '+' : '-';
+		tz = abs(tz);
+		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%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);
+	} else if (mode == DATE_RFC2822)
 		strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
 			weekday_names[tm->tm_wday], tm->tm_mday,
 			month_names[tm->tm_mon], tm->tm_year + 1900,
@@ -751,6 +760,9 @@ enum date_mode parse_date_format(const char *format)
 	else if (!strcmp(format, "iso8601") ||
 		 !strcmp(format, "iso"))
 		return DATE_ISO8601;
+	else if (!strcmp(format, "iso8601-strict") ||
+		 !strcmp(format, "iso-strict"))
+		return DATE_ISO8601_STRICT;
 	else if (!strcmp(format, "rfc2822") ||
 		 !strcmp(format, "rfc"))
 		return DATE_RFC2822;
diff --git a/pretty.c b/pretty.c
index 3a1da6f..7dd5601 100644
--- a/pretty.c
+++ b/pretty.c
@@ -731,9 +731,12 @@ static size_t format_person_part(struct strbuf *sb, char part,
 	case 'r':	/* date, relative */
 		strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
 		return placeholder_len;
-	case 'i':	/* date, ISO 8601 */
+	case 'i':	/* date, ISO 8601-like */
 		strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
 		return placeholder_len;
+	case 'I':	/* date, ISO 8601 strict */
+		strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601_STRICT));
+		return placeholder_len;
 	}
 
 skip:
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 349c531..aad7a80 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -431,6 +431,13 @@ EOF
 	test_cmp expected actual
 '
 
+# ISO strict date format
+test_expect_success 'ISO and ISO-strict date formats display the same values' '
+	git log --format=%ai%n%ci | sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
+	git log --format=%aI%n%cI >actual &&
+	test_cmp expected actual
+'
+
 # get new digests (with no abbreviations)
 head1=$(git rev-parse --verify HEAD~0) &&
 head2=$(git rev-parse --verify HEAD~1) &&
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 16:58 [PATCH v2] pretty: Provide a strict ISO8601 date format Beat Bolli
@ 2014-08-29 18:59 ` Junio C Hamano
  2014-08-29 19:04   ` Junio C Hamano
  2014-08-29 21:02   ` Beat Bolli
  0 siblings, 2 replies; 7+ messages in thread
From: Junio C Hamano @ 2014-08-29 18:59 UTC (permalink / raw)
  To: Beat Bolli; +Cc: git

Beat Bolli <bbolli@ewanet.ch> writes:

> Subject: Re: [PATCH v2] pretty: Provide a strict ISO8601 date format

"pretty: add --date=iso-strict, a strict ISO-8601 date format"

> The differences between the two formats are the following:

"The --date=iso format Git uses deviates from ISO-8601 in these
ways" may make it clear which one has "T" and which doesn't.

>   - a space instead of the `T` date/time delimiter
>   - a space between time and time zone
>   - no colon between hours and minutes of the time zone

> This commit adds a strict ISO 8601 date format for displaying committer
> and author dates. It uses the '%aI' and '%cI' format specifiers (note
> the uppercase 'I') and the '--date=iso-strict' or
> '--date=iso8601-strict' date format names.

"Add a .... author dates. Use '%aI' and '%cI' format specifiers ...".

> See http://thread.gmane.org/gmane.comp.version-control.git/255879 and
> http://thread.gmane.org/gmane.comp.version-control.git/52414/focus=52585
> for discussion.

Thanks for the pointers, especially for the older one.

> +	else if (mode == DATE_ISO8601_STRICT) {
> +		char sign = (tz >= 0) ? '+' : '-';
> +		tz = abs(tz);
> +		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%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);

OK.

> +# ISO strict date format
> +test_expect_success 'ISO and ISO-strict date formats display the same values' '
> +	git log --format=%ai%n%ci | sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
> +	git log --format=%aI%n%cI >actual &&
> +	test_cmp expected actual
> +'

This is saying that as long as --date=iso-strict format is
bug-to-bug compatible with --date=iso format it is OK.

Which is fine, especially knowing the implementation ;-)

> +
>  # get new digests (with no abbreviations)
>  head1=$(git rev-parse --verify HEAD~0) &&
>  head2=$(git rev-parse --verify HEAD~1) &&

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 18:59 ` Junio C Hamano
@ 2014-08-29 19:04   ` Junio C Hamano
  2014-08-29 19:34     ` Andreas Schwab
  2014-08-29 21:30     ` Beat Bolli
  2014-08-29 21:02   ` Beat Bolli
  1 sibling, 2 replies; 7+ messages in thread
From: Junio C Hamano @ 2014-08-29 19:04 UTC (permalink / raw)
  To: Beat Bolli; +Cc: git

I've queued with this fix squashed in.

diff --git a/builtin/blame.c b/builtin/blame.c
index 17d30d0..f9e6481 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2580,6 +2580,9 @@ parse_done:
 	case DATE_RFC2822:
 		blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
 		break;
+	case DATE_ISO8601_STRICT:
+		blame_date_width = sizeof("2006-10-19T16:00:04-0700");
+		break;
 	case DATE_ISO8601:
 		blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
 		break;

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 19:04   ` Junio C Hamano
@ 2014-08-29 19:34     ` Andreas Schwab
  2014-08-29 21:30     ` Beat Bolli
  1 sibling, 0 replies; 7+ messages in thread
From: Andreas Schwab @ 2014-08-29 19:34 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Beat Bolli, git

Junio C Hamano <gitster@pobox.com> writes:

> I've queued with this fix squashed in.
>
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 17d30d0..f9e6481 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -2580,6 +2580,9 @@ parse_done:
>  	case DATE_RFC2822:
>  		blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
>  		break;
> +	case DATE_ISO8601_STRICT:
> +		blame_date_width = sizeof("2006-10-19T16:00:04-0700");

ITYM ...-07:00

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 18:59 ` Junio C Hamano
  2014-08-29 19:04   ` Junio C Hamano
@ 2014-08-29 21:02   ` Beat Bolli
  2014-08-29 22:12     ` Jeff King
  1 sibling, 1 reply; 7+ messages in thread
From: Beat Bolli @ 2014-08-29 21:02 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 1005 bytes --]

On 29.08.14 20:59, Junio C Hamano wrote:
>> +# ISO strict date format
>> +test_expect_success 'ISO and ISO-strict date formats display the same values' '
>> +	git log --format=%ai%n%ci | sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
>> +	git log --format=%aI%n%cI >actual &&
>> +	test_cmp expected actual
>> +'
> 
> This is saying that as long as --date=iso-strict format is
> bug-to-bug compatible with --date=iso format it is OK.
> 
> Which is fine, especially knowing the implementation ;-)
> 

Yeah, I wasn't sure how to test this, since the commit dates change with
each test run. Maybe it's the easy way out, but on the other hand it's
better than no test at all. Check the known parts with a regex?

Thanks,
Beat
-- 
mail: echo '<bNbolOli@ewaSPnetAM.ch>' | tr -d '[A-S]'
pgp: 0x506A903A; 49D5 794A EA77 F907 764F  D89E 304B 93CF 506A 903A
gsm: 4.7.7.6.0.7.7.9.7.1.4.e164.arpa
icbm: 47.0452 N, 7.2715 E

"It takes love over gold, and mind over matter" -- Dire Straits


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 19:04   ` Junio C Hamano
  2014-08-29 19:34     ` Andreas Schwab
@ 2014-08-29 21:30     ` Beat Bolli
  1 sibling, 0 replies; 7+ messages in thread
From: Beat Bolli @ 2014-08-29 21:30 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 440 bytes --]

On 29.08.14 21:04, Junio C Hamano wrote:
> I've queued with this fix squashed in.

Oops, I just sent v3 with your latest fixes as well. Please ignore it.


Thanks,
Beat
-- 
mail: echo '<bNbolOli@ewaSPnetAM.ch>' | tr -d '[A-S]'
pgp: 0x506A903A; 49D5 794A EA77 F907 764F  D89E 304B 93CF 506A 903A
gsm: 4.7.7.6.0.7.7.9.7.1.4.e164.arpa
icbm: 47.0452 N, 7.2715 E

"It takes love over gold, and mind over matter" -- Dire Straits


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] pretty: Provide a strict ISO8601 date format
  2014-08-29 21:02   ` Beat Bolli
@ 2014-08-29 22:12     ` Jeff King
  0 siblings, 0 replies; 7+ messages in thread
From: Jeff King @ 2014-08-29 22:12 UTC (permalink / raw)
  To: Beat Bolli; +Cc: Junio C Hamano, git

On Fri, Aug 29, 2014 at 11:02:36PM +0200, Beat Bolli wrote:

> On 29.08.14 20:59, Junio C Hamano wrote:
> >> +# ISO strict date format
> >> +test_expect_success 'ISO and ISO-strict date formats display the same values' '
> >> +	git log --format=%ai%n%ci | sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
> >> +	git log --format=%aI%n%cI >actual &&
> >> +	test_cmp expected actual
> >> +'
> > 
> > This is saying that as long as --date=iso-strict format is
> > bug-to-bug compatible with --date=iso format it is OK.
> > 
> > Which is fine, especially knowing the implementation ;-)
> > 
> 
> Yeah, I wasn't sure how to test this, since the commit dates change with
> each test run. Maybe it's the easy way out, but on the other hand it's
> better than no test at all. Check the known parts with a regex?

If you use test_tick (or test_commit, which calls it), the timestamps
are stable from run to run of the tests.

-Peff

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-08-29 22:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-29 16:58 [PATCH v2] pretty: Provide a strict ISO8601 date format Beat Bolli
2014-08-29 18:59 ` Junio C Hamano
2014-08-29 19:04   ` Junio C Hamano
2014-08-29 19:34     ` Andreas Schwab
2014-08-29 21:30     ` Beat Bolli
2014-08-29 21:02   ` Beat Bolli
2014-08-29 22:12     ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).