avoid switching $TZ on every location

addresses #444
This commit is contained in:
Jan-Piet Mens
2024-02-01 17:26:38 +01:00
parent d563099b76
commit e2d1359e4b
2 changed files with 43 additions and 14 deletions

19
util.c
View File

@@ -67,22 +67,27 @@ const char *disptime(time_t t) {
#ifdef WITH_TZ
const char *isolocal(long tst, char *tzname)
{
char old_tz[64] = "UTC", *p;
static char last_timezone[BUFSIZ] = "UTC";
static char local[128];
struct tm *tm;
if ((p = getenv("TZ")) != NULL) {
strcpy(old_tz, p);
}
setenv("TZ", tzname, 1);
/*
* Switch $TZ only if we have to, as it's an expensive
* operation. By tuning this we reduced runtime for 2000 positions
* from 3 minutes to < 0.1s
*/
if (strcmp(last_timezone, tzname) != 0) {
// fprintf(stderr, "isolocal: %s -> %s\n", last_timezone, tzname);
strcpy(last_timezone, tzname);
setenv("TZ", tzname, 1);
}
strcpy(local, "-");
if ((tm = localtime(&tst)) != NULL) {
strftime(local, sizeof(local), "%FT%T%z", tm);
}
// setenv("TZ", old_tz, 1);
return local;
return (local);
}
#endif