summaryrefslogtreecommitdiff
path: root/src/stat.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2016-03-17 10:35:18 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2016-03-17 10:36:33 -0700
commitdf88fce71651afb2c3456967a142db0ae4bf9906 (patch)
treeaa18160fb6d2ef669fcfc04e914da9d10b649287 /src/stat.c
parentc18b3699e1b8c4ad2739761f2b9c306ff2303322 (diff)
downloadcoreutils-df88fce71651afb2c3456967a142db0ae4bf9906.tar.xz
date ls pr: fix time zone abbrs on SysV platforms
The problematic code computed a struct tm in one time zone, and then printed it or converted it to a string in another. To be portable the same time zone needs to be used for both operations. On GNU platforms this is not an issue, but incorrect output can be generated on System V style platforms like AIX where time zone abbreviations are available only in the 'tzname' global variable. Problem reported by Assaf Gordon in: http://bugs.gnu.org/23035 * NEWS: Document the bug. * src/date.c (show_date): * src/ls.c (long_time_expected_width, print_long_format): * src/pr.c (init_header): * src/stat.c (human_time): Use localtime_rz instead of localtime, so that the time zone information is consistent for both localtime and time-formatting functions like fprintftime and nstrftime. For 'stat' this change is mostly just a code cleanup but it also causes stat to also print nanoseconds when printing time stamps that are out of localtime range, as this is more consistent with what other programs do. For programs other than 'stat' this fixes bugs with time zone formats that use %Z. * src/du.c, src/pr.c (localtz): New static var. (main): Initialize it. * src/du.c (show_date): New time zone argument, so that localtime and fprintftime use the same time zone information. All callers changed. * tests/misc/time-style.sh: New file. * tests/local.mk (all_tests): Add it. * tests/misc/date.pl: Test alphabetic time zone abbreviations.
Diffstat (limited to 'src/stat.c')
-rw-r--r--src/stat.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/stat.c b/src/stat.c
index e11e4318f..1742ff128 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -557,17 +557,27 @@ human_access (struct stat const *statbuf)
static char * ATTRIBUTE_WARN_UNUSED_RESULT
human_time (struct timespec t)
{
- static char str[MAX (INT_BUFSIZE_BOUND (intmax_t),
- (INT_STRLEN_BOUND (int) /* YYYY */
- + 1 /* because YYYY might equal INT_MAX + 1900 */
- + sizeof "-MM-DD HH:MM:SS.NNNNNNNNN +ZZZZ"))];
+ /* STR must be at least this big, either because localtime_rz fails,
+ or because the time zone is truly outlandish so that %z expands
+ to a long string. */
+ enum { intmax_bufsize = INT_BUFSIZE_BOUND (intmax_t) };
+
+ static char str[intmax_bufsize
+ + INT_STRLEN_BOUND (int) /* YYYY */
+ + 1 /* because YYYY might equal INT_MAX + 1900 */
+ + sizeof "-MM-DD HH:MM:SS.NNNNNNNNN +"];
static timezone_t tz;
if (!tz)
tz = tzalloc (getenv ("TZ"));
- struct tm const *tm = localtime (&t.tv_sec);
- if (tm == NULL)
- return timetostr (t.tv_sec, str);
- nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, tz, t.tv_nsec);
+ struct tm tm;
+ int ns = t.tv_nsec;
+ if (localtime_rz (tz, &t.tv_sec, &tm))
+ nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", &tm, tz, ns);
+ else
+ {
+ char secbuf[INT_BUFSIZE_BOUND (intmax_t)];
+ sprintf (str, "%s.%09d", timetostr (t.tv_sec, secbuf), ns);
+ }
return str;
}