coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] have `date` cache the system timezone


From: Michael Greenberg
Subject: [PATCH] have `date` cache the system timezone
Date: Mon, 25 Nov 2024 22:34:36 -0500

If TZ is unset, the date utility will call newfstat("/etc/localtime")
each time it reads a time using date --date or date -f. If reading from
/etc is expensive, you can experience significant slowdowns.

I didn't write a new test---you can observe the phenomenon by running
the following:

$ cat dates  # 4 isn't enough for slowness, but enough to make the trace clear
Mon Nov 25 09:14:54 PM EST 2024
Mon Nov 25 09:15:01 PM EST 2024
Mon Nov 25 09:15:01 PM EST 2024
Mon Nov 25 09:15:01 PM EST 2024
$ strace date -f dates +%a
... see 4 calls to newfstatat on /etc/localtime
$ strace coreutils/src/date -f dates +%a # use the patched version
... see 1 call to newfstatat on /usr/share/zoneinfo/YOURTIMEZONEHERE ...

It might be possible to achieve the same effect by patching gnulib, but
I haven't dug into it.

Cheers,
Michael

---
 src/date.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/date.c b/src/date.c
index ee32403ef..45b111c4e 100644
--- a/src/date.c
+++ b/src/date.c
@@ -575,6 +575,14 @@ main (int argc, char **argv)
   char *format_copy = adjust_resolution (format);
   char const *format_res = format_copy ? format_copy : format;
   char const *tzstring = getenv ("TZ");
+  /* If TZ is unset, set it to the system timezone. This caches the
+     value, so batch_convert won't have to do it over and over
+     again. */
+  if (tzstring == NULL) {
+    tzset();
+    tzstring = tzname[0];
+    setenv("TZ", tzstring, 0);
+  }
   timezone_t tz = tzalloc (tzstring);
 
   if (batch_file != nullptr)
-- 
2.45.2



reply via email to

[Prev in Thread] Current Thread [Next in Thread]