From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Dongsheng Song <dongsheng.song@gmail.com>,
Git Mailing List <git@vger.kernel.org>
Subject: Re: timezone related bug of git
Date: Mon, 1 Nov 2021 21:43:23 -0400 [thread overview]
Message-ID: <YYCXu/lpp0jtaIMk@coredump.intra.peff.net> (raw)
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
next prev parent reply other threads:[~2021-11-02 1:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-31 3:23 timezone related bug of git Dongsheng Song
2021-10-31 8:53 ` Jeff King
2021-10-31 13:18 ` Dongsheng Song
2021-10-31 18:46 ` Junio C Hamano
2021-11-01 4:03 ` Jeff King
2021-11-01 14:31 ` Dongsheng Song
2021-11-01 18:18 ` Junio C Hamano
2021-11-02 1:43 ` Jeff King [this message]
2021-11-02 11:35 ` [PATCH] strbuf_addftime(): handle "%s" manually Jeff King
2021-11-02 15:43 ` Jeff King
2021-11-03 20:28 ` Junio C Hamano
2021-11-04 2:11 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YYCXu/lpp0jtaIMk@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=dongsheng.song@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).