threads / patch / 37440

patchpretty: Provide a strict ISO8601 date format

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

## tl;dr

6 messages between Aug 28, 2014 and Aug 29, 2014. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Beat Bolli· Aug 28, 2014, 17:49 UTC · lore

It uses the '%aI' and '%cI' format specifiers or the '--date=iso-strict' date format name.

See http://article.gmane.org/gmane.comp.version-control.git/255879 for discussion.

Signed-off-by: Beat Bolli <bbolli@ewanet.ch>
---
 Documentation/git-rev-list.txt     |  2 +-
 Documentation/pretty-formats.txt   |  6 ++++--
 Documentation/rev-list-options.txt | 13 +++++++++++--
 cache.h                            |  1 +
 date.c                             | 10 ++++++++++
 pretty.c                           |  5 ++++-
 t/t4205-log-pretty-formats.sh      |  7 +++++++
 7 files changed, 38 insertions(+), 6 deletions(-)
Show changes to 7 files +38 −5

Documentation/git-rev-list.txt, Documentation/pretty-formats.txt, Documentation/rev-list-options.txt, cache.h, date.c, pretty.c, t/t4205-log-pretty-formats.sh

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..d545ee6 100644
--- a/date.c
+++ b/date.c
@@ -200,6 +200,13 @@ 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_ISO8601_STRICT)
+		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
+				tm->tm_year + 1900,
+				tm->tm_mon + 1,
+				tm->tm_mday,
+				tm->tm_hour, tm->tm_min, tm->tm_sec,
+				tz / 100, abs(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,
@@ -751,6 +758,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
Junio C Hamano· Aug 28, 2014, 22:53 UTC · re: Beat Bolli · lore

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

Beat Bolli <bbolli@ewanet.ch> writes:
> It uses the '%aI' and '%cI' format specifiers or the '--date=iso-strict'
> date format name.
OK.
>
> See http://article.gmane.org/gmane.comp.version-control.git/255879 for
> discussion.

Please think of a way to explain/justify your changes better before forcing readers to go online. In this case, I think what you wrote in the updates to the documentation would serve as a good basis for it (describe it backwards).

Show 9 quoted lines
> +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` (or `--date=iso8601`) shows timestamps in ISO 8601 format.
> +`--date=iso` (or `--date=iso8601`) shows timestamps in a ISO 8601-like format.
Should it be s/a ISO/an ISO/?
Show 7 quoted lines
> +	else if (mode == DATE_ISO8601_STRICT)
> +		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
> +				tm->tm_year + 1900,
> +				tm->tm_mon + 1,
> +				tm->tm_mday,
> +				tm->tm_hour, tm->tm_min, tm->tm_sec,
> +				tz / 100, abs(tz % 100));

Wouldn't this misidentify a zone that is 30 minutes off of GMT, i.e. tz == -30? tz/100 would not be negative and "%+03d:" would happily show "+00:", no?

Scott Schmit· Aug 29, 2014, 17:44 UTC · re: Junio C Hamano · lore

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

On Thu, Aug 28, 2014 at 03:53:13PM -0700, Junio C Hamano wrote:
Show 12 quoted lines
> Beat Bolli writes:
> > +	else if (mode == DATE_ISO8601_STRICT)
> > +		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
> > +				tm->tm_year + 1900,
> > +				tm->tm_mon + 1,
> > +				tm->tm_mday,
> > +				tm->tm_hour, tm->tm_min, tm->tm_sec,
> > +				tz / 100, abs(tz % 100));
> 
> Wouldn't this misidentify a zone that is 30 minutes off of GMT,
> i.e. tz == -30?  tz/100 would not be negative and "%+03d:" would
> happily show "+00:", no?

No. strbuf_addf uses strbuf_vaddf which uses vsnprintf(3). From man vsnprintf(3):

Show 8 quoted lines
>   The flag characters
>       The character % is followed by zero or more of the following
>       flags:
>
>       +      A sign (+ or -) should always be placed before a number
>              produced by a signed conversion.  By default a sign is
>              used only for negative numbers.  A + overrides a space if
>              both are used.
Perhaps you misread "%+03d:" as "+%02d:"?
-- 
Scott Schmit
Junio C Hamano· Aug 29, 2014, 18:41 UTC · re: Scott Schmit · lore

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

Scott Schmit <i.grok@comcast.net> writes:
Show 26 quoted lines
> On Thu, Aug 28, 2014 at 03:53:13PM -0700, Junio C Hamano wrote:
>> Beat Bolli writes:
>> > +	else if (mode == DATE_ISO8601_STRICT)
>> > +		strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
>> > +				tm->tm_year + 1900,
>> > +				tm->tm_mon + 1,
>> > +				tm->tm_mday,
>> > +				tm->tm_hour, tm->tm_min, tm->tm_sec,
>> > +				tz / 100, abs(tz % 100));
>> 
>> Wouldn't this misidentify a zone that is 30 minutes off of GMT,
>> i.e. tz == -30?  tz/100 would not be negative and "%+03d:" would
>> happily show "+00:", no?
>
> No.  strbuf_addf uses strbuf_vaddf which uses vsnprintf(3).  From man
> vsnprintf(3):
>>   The flag characters
>>       The character % is followed by zero or more of the following
>>       flags:
>>
>>       +      A sign (+ or -) should always be placed before a number
>>              produced by a signed conversion.  By default a sign is
>>              used only for negative numbers.  A + overrides a space if
>>              both are used.
>
> Perhaps you misread "%+03d:" as "+%02d:"?

I do not think 03 vs 02 makes any difference wrt what I was wondering.

You feed tz/100 to "%+03d:" (the "sign and hour" part of the timezone). What if tz is -30, i.e. less than an hour but still a negative offset? tz/100 would be zero and tz % 100 would be -30.

    tz = -30;
    printf("%+03d:%02d", tz / 100, abs(tz % 100));
would show what?
Scott Schmit· Aug 29, 2014, 20:29 UTC · re: Junio C Hamano · lore

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

On Fri, Aug 29, 2014 at 11:41:39AM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> You feed tz/100 to "%+03d:" (the "sign and hour" part of the
> timezone).  What if tz is -30, i.e. less than an hour but still a
> negative offset?  tz/100 would be zero and tz % 100 would be -30.
> 
>     tz = -30;
>     printf("%+03d:%02d", tz / 100, abs(tz % 100));
> 
> would show what?
+00:30 because zero can't be negative in two's complement arithmetic.

The "-30 / 100 = 0" part didn't click for some reason. Sorry for the noise.

Junio C Hamano· Aug 29, 2014, 20:57 UTC · re: Scott Schmit · lore

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

Scott Schmit <i.grok@comcast.net> writes:
> +00:30 because zero can't be negative in two's complement arithmetic.
Yet to meet negative zero yet myself ;-)
> The "-30 / 100 = 0" part didn't click for some reason.  Sorry for the
> noise.

That's OK. You are not the only one who didn't get this right the first time. The important thing is that we get it right in the version we will use in the final product.

Thanks.

← back to recent threads