]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(long_time_expected_width): New function.
authorJim Meyering <jim@meyering.net>
Wed, 3 Jan 2001 09:15:22 +0000 (09:15 +0000)
committerJim Meyering <jim@meyering.net>
Wed, 3 Jan 2001 09:15:22 +0000 (09:15 +0000)
(print_long_format): Use it, so that we don't assume a
particular width for time stamps in an internationalized
environment.

src/ls.c

index f0b753eb0a79007af71c774771ab655bd0973762..67f75fa2bf4daa228c9eabb0d835fc7807305c3c 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -2342,6 +2342,36 @@ print_current_files (void)
       break;
     }
 }
+/* Return the expected number of columns in a long-format time stamp,
+   or zero if it cannot be calculated.  */
+
+static int
+long_time_expected_width (void)
+{
+  static int width = -1;
+
+  if (width < 0)
+    {
+      time_t epoch = 0;
+      struct tm const *tm = localtime (&epoch);
+      char const *fmt = long_time_format[0];
+      char initbuf[100];
+      char *buf = initbuf;
+      size_t bufsize = sizeof initbuf;
+      size_t len;
+
+      *buf = '\1';
+      while (! (len = strftime (buf, bufsize, fmt, tm)) && *buf)
+       buf = alloca (bufsize *= 2);
+
+      width = mbsnwidth (buf, len, 0);
+      if (width < 0)
+       width = 0;
+    }
+
+  return width;
+}
 
 static void
 print_long_format (const struct fileinfo *f)
@@ -2482,7 +2512,7 @@ print_long_format (const struct fileinfo *f)
       /* The time cannot be represented as a local time;
         print it as a huge integer number of seconds.  */
       char hbuf[LONGEST_HUMAN_READABLE + 1];
-      int width = full_time ? 24 : 12;
+      int width = long_time_expected_width ();
 
       if (when < 0)
        {