From defdabbe82a35cf656f25c01d0768d24680f6ec8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 1 Oct 2020 11:14:49 -0400 Subject: time: Don't call tzset() on every x{local,gm}time() It turns out tzset() checks /etc/localtime every time you call it. --- time.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/time.c b/time.c index d955a6c..c7331b5 100644 --- a/time.c +++ b/time.c @@ -21,9 +21,15 @@ #include #include +/** Whether tzset() has been called. */ +static bool tz_is_set = false; + int xlocaltime(const time_t *timep, struct tm *result) { // Should be called before localtime_r() according to POSIX.1-2004 - tzset(); + if (!tz_is_set) { + tzset(); + tz_is_set = true; + } if (localtime_r(timep, result)) { return 0; @@ -34,7 +40,10 @@ int xlocaltime(const time_t *timep, struct tm *result) { int xgmtime(const time_t *timep, struct tm *result) { // Should be called before gmtime_r() according to POSIX.1-2004 - tzset(); + if (!tz_is_set) { + tzset(); + tz_is_set = true; + } if (gmtime_r(timep, result)) { return 0; -- cgit v1.2.3