date.c: switch to reentrant {gm,local}time_r

Originally, git was intended to be single-thread executable.
`gmtime(3)' and `localtime(3)' can be used in such codebase
for cleaner code.

Overtime, we're employing multithread in our code base.

Let's phase out `gmtime(3)' and `localtime(3)' in favour of
`gmtime_r(3)' and `localtime_r(3)'.

Signed-off-by: Doan Tran Cong Danh <congdanhqx@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Doan Tran Cong Danh 2019-11-28 19:25:03 +07:00 коммит произвёл Junio C Hamano
Родитель d9f6f3b619
Коммит ccd469450a
1 изменённых файлов: 10 добавлений и 8 удалений

18
date.c
Просмотреть файл

@ -64,16 +64,16 @@ static time_t gm_time_t(timestamp_t time, int tz)
* thing, which means that tz -0100 is passed in as the integer -100, * thing, which means that tz -0100 is passed in as the integer -100,
* even though it means "sixty minutes off" * even though it means "sixty minutes off"
*/ */
static struct tm *time_to_tm(timestamp_t time, int tz) static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
{ {
time_t t = gm_time_t(time, tz); time_t t = gm_time_t(time, tz);
return gmtime(&t); return gmtime_r(&t, tm);
} }
static struct tm *time_to_tm_local(timestamp_t time) static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
{ {
time_t t = time; time_t t = time;
return localtime(&t); return localtime_r(&t, tm);
} }
/* /*
@ -283,6 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
{ {
struct tm *tm; struct tm *tm;
struct tm tmbuf = { 0 };
struct tm human_tm = { 0 }; struct tm human_tm = { 0 };
int human_tz = -1; int human_tz = -1;
static struct strbuf timebuf = STRBUF_INIT; static struct strbuf timebuf = STRBUF_INIT;
@ -318,11 +319,11 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
} }
if (mode->local) if (mode->local)
tm = time_to_tm_local(time); tm = time_to_tm_local(time, &tmbuf);
else else
tm = time_to_tm(time, tz); tm = time_to_tm(time, tz, &tmbuf);
if (!tm) { if (!tm) {
tm = time_to_tm(0, 0); tm = time_to_tm(0, 0, &tmbuf);
tz = 0; tz = 0;
} }
@ -959,10 +960,11 @@ void datestamp(struct strbuf *out)
{ {
time_t now; time_t now;
int offset; int offset;
struct tm tm = { 0 };
time(&now); time(&now);
offset = tm_to_time_t(localtime(&now)) - now; offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
offset /= 60; offset /= 60;
date_string(now, offset, out); date_string(now, offset, out);