summaryrefslogtreecommitdiffstats
path: root/time.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-10-01 11:14:49 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-10-01 11:14:49 -0400
commitdefdabbe82a35cf656f25c01d0768d24680f6ec8 (patch)
tree66dd9a5efb8b84de91a5cbbb858ba68a0102559a /time.c
parent345fd739496034fcfccf8fb943765a4e3beaedc4 (diff)
downloadbfs-defdabbe82a35cf656f25c01d0768d24680f6ec8.tar.xz
time: Don't call tzset() on every x{local,gm}time()
It turns out tzset() checks /etc/localtime every time you call it.
Diffstat (limited to 'time.c')
-rw-r--r--time.c13
1 files 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 <stdlib.h>
#include <time.h>
+/** 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;