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

Re: timezone related bug of git

From
Jeff King <peff@peff.net>
Date
Nov 2, 2021, 01:43 UTC
Message-ID
<YYCXu/lpp0jtaIMk@coredump.intra.peff.net>
In-Reply-To
<xmqq1r3zd9k5.fsf@gitster.g>
On Mon, Nov 01, 2021 at 11:18:02AM -0700, Junio C Hamano wrote:
> I have to wonder why gm_time_t() needs to use two separate codepaths
> for positive and negative tz_offset, while the new code here can get
> away without.  Does it have something to do with the direction of
> truncation during division and modulo operation?

Hmm. Unless I am missing something, this part of gm_time_t() is simply over-complicating things:

  minutes = tz < 0 ? -tz : tz;
  minutes = (minutes / 100)*60 + (minutes % 100);
  minutes = tz < 0 ? -minutes : minutes;

We switch to doing the computation in absolute-value units, but then restore the sign. But just:

  minutes = (tz / 100) * 60 + (tz % 100);

is equivalent and shorter. If tz is negative, then both terms will be negative, which is what you want (they sum to a larger absolute-value negative number). This comes from f80cd783c6 (date.c: add "show_date()" function., 2005-05-06), so I don't see any sign that there was specific thought given to some obscure handling. And indeed later fixes like fbab835c03 ([PATCH] fix show_date() for positive timezones, 2005-05-18) imply to me that the original was just confused.

Later we do:
  if (minutes > 0) {
          if (unsigned_add_overflows(time, minutes * 60))
                  die("Timestamp+tz too large: %"PRItime" +%04d",
                      time, tz);
  } else if (time < -minutes * 60)
          die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);

And that does need separate paths for the overflow check, since we're checking different boundaries. I suspect for the strftime() code that we wouldn't need similar checks, because the earlier ones would have caught any problems (i.e., we would not get as far as having a "struct tm" that represented something outside the range of our time_t).

-Peff
Previous: Junio C HamanoNext: Jeff King
Message 8 of 12 in “timezone related bug of git”
  1. Dongsheng SongOct 31, 2021
  2. Jeff KingOct 31, 2021
  3. Dongsheng SongOct 31, 2021
  4. Junio C HamanoOct 31, 2021
  5. Jeff KingNov 1, 2021
  6. Dongsheng SongNov 1, 2021
  7. Junio C HamanoNov 1, 2021
  8. Jeff KingNov 2, 2021
  9. strbuf_addftime(): handle "%s" manuallyJeff King, Nov 2, 2021
  10. Jeff KingNov 2, 2021
  11. Junio C HamanoNov 3, 2021
  12. Jeff KingNov 4, 2021

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.