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

[PATCH] date: use localtime() for "-local" time formats

From
Jeff King <peff@peff.net>
Date
Jun 7, 2017, 09:13 UTC
Message-ID
<20170607091307.2vh2eevgaljxu7ha@sigill.intra.peff.net>
In-Reply-To
<20170607081729.6pz5yo2hmp4fwuas@sigill.intra.peff.net>
On Wed, Jun 07, 2017 at 04:17:29AM -0400, Jeff King wrote:
Show 13 quoted lines
> > Duplicates strbuf_expand to a certain extent, but not too badly, I
> > think.  Leaves the door open for letting strftime handle the local
> > case.
> 
> I guess you'd plan to do that like this in the caller:
> 
>   if (date->local)
> 	tz_name = NULL;
>   else
> 	tz_name = "";
> 
> and then your strftime() doesn't do any %z expansion when tz_name is
> NULL.
And here's a patch that handles the local case.
-- >8 --
Subject: [PATCH] date: use localtime() for "-local" time formats

When we convert seconds-since-epochs timestamps into a broken-down "struct tm", we do so by adjusting the timestamp according to the correct timezone and then using gmtime() to break down the result. This means that the resulting struct "knows" that it's in GMT, even though the time it represents is adjusted for a different zone. The fields where it stores this data are not portably accessible, so we have no way to override them to tell them the real zone info.

For the most part, this works. Our date-formatting routines don't pay attention to these inaccessible fields, and use the the same tz info we provided for adjustment. The one exception is when we call strftime(), whose %z and %Z formats reveal this hidden timezone data.

We can't make this work in the general case, as there's no portable function for setting an arbitrary timezone. But for the special case of the "-local" formats, we can just skip the adjustment and use localtime() instead of gmtime(). This makes --date=format-local:%Z work correctly, showing the local timezone instead of an empty string.

This patch adds three tests:
  1. We check that format:%Z returns an empty string. This
     isn't what we'd want ideally, but it's important to
     confirm that it doesn't produce nonsense (like GMT when
     we are formatting another zone entirely).
  2. We check that format-local:%Z produces "UTC", which is
     the value of $TZ set by test-lib.sh.
  3. We check that format-local actually produces the
     correct time for zones other than UTC. If we made a
     mistake in the adjustment logic (say, applying the tz
     adjustment even though we are about to call
     localtime()), it wouldn't show up in the second test,
     because the offset for UTC is 0.
     We use the EST5 zone, which is already used elsewhere
     in the script, so is assumed to be available
     everywhere.
     However, this test _doesn't_ check %Z. That expansion
     produces an abbreviation which may not be portable
     across systems (on my system it expands as just "EST").
     Technically "UTC" could suffer from the same problem,
     but presumably it's universal enough to be relied upon.
Signed-off-by: Jeff King <peff@peff.net>
---
 date.c          | 14 ++++++++++++--
 t/t0006-date.sh | 20 ++++++++++++++++++--
 2 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/date.c b/date.c
index 558057733..1fd6d6637 100644
--- a/date.c
+++ b/date.c
@@ -70,6 +70,12 @@ static struct tm *time_to_tm(timestamp_t time, int tz)
 	return gmtime(&t);
 }
 
+static struct tm *time_to_tm_local(timestamp_t time)
+{
+	time_t t = time;
+	return localtime(&t);
+}
+
 /*
  * What value of "tz" was in effect back then at "time" in the
  * local timezone?
@@ -214,7 +220,10 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 		return timebuf.buf;
 	}
 
-	tm = time_to_tm(time, tz);
+	if (mode->local)
+		tm = time_to_tm_local(time);
+	else
+		tm = time_to_tm(time, tz);
 	if (!tm) {
 		tm = time_to_tm(0, 0);
 		tz = 0;
@@ -246,7 +255,8 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 			month_names[tm->tm_mon], tm->tm_year + 1900,
 			tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
 	else if (mode->type == DATE_STRFTIME)
-		strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz, "");
+		strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
+				mode->local ? NULL : "");
 	else
 		strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
 				weekday_names[tm->tm_wday],
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index 42d4ea61e..3be6692bb 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -31,9 +31,18 @@ check_show () {
 	format=$1
 	time=$2
 	expect=$3
-	test_expect_success $4 "show date ($format:$time)" '
+	prereqs=$4
+	zone=$5
+	test_expect_success $prereqs "show date ($format:$time)" '
 		echo "$time -> $expect" >expect &&
-		test-date show:$format "$time" >actual &&
+		(
+			if test -n "$zone"
+			then
+				TZ=$zone
+				export $TZ
+			fi &&
+			test-date show:"$format" "$time" >actual
+		) &&
 		test_cmp expect actual
 	'
 }
@@ -51,6 +60,13 @@ check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
 check_show raw-local "$TIME" '1466000000 +0000'
 check_show unix-local "$TIME" '1466000000'
 
+check_show "format:%Y-%m-%d %H:%M:%S %z (%Z)" "$TIME" \
+	   '2016-06-15 16:13:20 +0200 ()'
+check_show format-local:"%Y-%m-%d %H:%M:%S %z (%Z)" "$TIME" \
+	   '2016-06-15 14:13:20 +0000 (UTC)'
+check_show format-local:"%Y-%m-%d %H:%M:%S %z" "$TIME" \
+	   '2016-06-15 09:13:20 -0500' '' EST5
+
 # arbitrary time absurdly far in the future
 FUTURE="5758122296 -0400"
 check_show iso       "$FUTURE" "2152-06-19 18:24:56 -0400" TIME_IS_64BIT,TIME_T_IS_64BIT
-- 
2.13.1.664.g1b5a21ec3
Previous: Jeff KingNext: René Scharfe
Message 19 of 44 in “git-2.13.0: log --date=format:%z not working”
  1. Ulrich MuellerMay 26, 2017
  2. Ævar Arnfjörð BjarmasonMay 27, 2017
  3. Jeff KingMay 27, 2017
  4. Ævar Arnfjörð BjarmasonMay 28, 2017
  5. Junio C HamanoMay 29, 2017
  6. René ScharfeMay 28, 2017
  7. Junio C HamanoJun 2, 2017
  8. Jeff KingJun 2, 2017
  9. René ScharfeJun 2, 2017
  10. Jeff KingJun 2, 2017
  11. Ulrich MuellerJun 2, 2017
  12. Jeff KingJun 2, 2017
  13. Ulrich MuellerJun 2, 2017
  14. Jeff KingJun 2, 2017
  15. strbuf: let strbuf_addftime handle %z and %Z itselfRené Scharfe, Jun 3, 2017
  16. Ulrich MuellerJun 3, 2017
  17. René ScharfeJun 3, 2017
  18. Jeff KingJun 7, 2017
  19. date: use localtime() for "-local" time formatsJeff King, Jun 7, 2017
  20. René ScharfeJun 11, 2017
  21. Junio C HamanoJun 12, 2017
  22. Ævar Arnfjörð BjarmasonJun 12, 2017
  23. Ulrich MuellerJun 12, 2017
  24. Ævar Arnfjörð BjarmasonJun 12, 2017
  25. Junio C HamanoJun 12, 2017
  26. Jeff KingJun 12, 2017
  27. Ævar Arnfjörð BjarmasonJun 12, 2017
  28. Jeff KingJun 12, 2017
  29. Linus TorvaldsJun 13, 2017
  30. René ScharfeJun 12, 2017
  31. Ævar Arnfjörð BjarmasonJun 13, 2017
  32. Ulrich MuellerJun 13, 2017
  33. René ScharfeJun 12, 2017
  34. Jeff KingJun 12, 2017
  35. strbuf: let strbuf_addftime handle %z and %Z itselfRené Scharfe, Jun 15, 2017
  36. Ulrich MuellerJun 15, 2017
  37. René ScharfeJun 15, 2017
  38. strbuf: let strbuf_addftime handle %z and %Z itselfRené Scharfe, Jun 15, 2017
  39. Jeff KingJun 15, 2017
  40. 1/2 t0006: check --date=format zone offsetsJeff King, Jun 15, 2017
  41. 2/2 date: use localtime() for "-local" time formatsJeff King, Jun 15, 2017
  42. René ScharfeJun 15, 2017
  43. Junio C HamanoJun 15, 2017
  44. Jeff KingJun 16, 2017

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.