[PATCH] honor TIME_STYLE environment variable for formatting ls timestamp

Matt Panaro <[email protected]> Wed, 12 Nov 2025 21:17:10 -0500
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <[email protected]>
addresses https://savannah.gnu.org/bugs/index.php?66299
the ls program itself has both a parameter and a recognized environment variable, TIME_STYLE, that formats the output of the file's timestamp in the listing.  To achieve partial parity for the -ls output of find, the code is refactored to honor the environment variable (a style parameter is not implemented herein).

diff --git a/lib/listfile.c b/lib/listfile.c
index e6ee70e3..78f89991 100644
--- a/lib/listfile.c
+++ b/lib/listfile.c
@@ -341,17 +341,27 @@ list_file (const char *name,
           /* Use strftime rather than ctime, because the former can produce
              locale-dependent names for the month (%b).
 
+	     As ls does, honor the TIME_STYLE environment variable, if present;
+	     otherwise:
              Output the year if the file is fairly old or in the future.
              POSIX says the cutoff is 6 months old;
              approximate this by 6*30 days.
              Allow a 1 hour slop factor for what is considered "the future",
              to allow for NFS server/client clock disagreement.  */
-          char const *fmt =
-            ((current_time - 6 * 30 * 24 * 60 * 60 <= statp->st_mtime
-              && statp->st_mtime <= current_time + 60 * 60)
-             ? "%b %e %H:%M"
-             : "%b %e  %Y");
-
+	  char *ts = getenv("TIME_STYLE");
+          char const *fmt;
+	  if(ts) {
+	    /* ls expects TIME_STYLE to start with a '+'; if it were re-used
+	       here, the '+' should be ignored when passing into strftime,
+	       below. */
+	    if(*ts == '+') ts++;
+	    fmt = ts;
+	  } else {
+	    fmt = ((current_time - 6 * 30 * 24 * 60 * 60 <= statp->st_mtime
+		    && statp->st_mtime <= current_time + 60 * 60)
+		   ? "%b %e %H:%M"
+		   : "%b %e  %Y");
+	  }
           while (!strftime (buf, bufsize, fmt, when_local))
             buf = alloca (bufsize *= 2);
 
-- 
2.51.0