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