diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 11:14:49 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 11:14:49 -0400 |
commit | defdabbe82a35cf656f25c01d0768d24680f6ec8 (patch) | |
tree | 66dd9a5efb8b84de91a5cbbb858ba68a0102559a | |
parent | 345fd739496034fcfccf8fb943765a4e3beaedc4 (diff) | |
download | bfs-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.
-rw-r--r-- | time.c | 13 |
1 files changed, 11 insertions, 2 deletions
@@ -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; |