Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] ztrftime(): Fix truncation for %.



Not directly related, but in reviewing workers/43932 further i found that the
way ztrftime() handles truncation for %. is problematic:

  % strftime '%s.%9.' $EPOCHSECONDS $(( 999_999_999 ))
  1545638724.999999999
  % strftime '%s.%6.' $EPOCHSECONDS $(( 999_999_000 ))
  1545638984.999999
  % strftime '%s.%6.' $EPOCHSECONDS $(( 999_999_999 ))
  1545638724.100000

I'm pretty sure it's always been like this, it was just hard to see before
because there was no way to control the (at the time) microsecond input from
'user land'.

I'm not very good at maths — is there a reason it shouldn't just be a straight
power-of-ten division like this?

dana


diff --git a/Src/utils.c b/Src/utils.c
index e43a3cdb4..c3badcf77 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -3340,9 +3340,8 @@ morefmt:
 		    digs = 9;
 		if (digs < 9) {
 		    int trunc;
-		    for (trunc = 8 - digs; trunc; trunc--)
+		    for (trunc = 9 - digs; trunc; trunc--)
 			nsec /= 10;
-		    nsec = (nsec + 8) / 10;
 		}
 		sprintf(buf, "%0*ld", digs, nsec);
 		buf += digs;
diff --git a/Test/V09datetime.ztst b/Test/V09datetime.ztst
index 2041d9b40..ed2d99b2f 100644
--- a/Test/V09datetime.ztst
+++ b/Test/V09datetime.ztst
@@ -114,3 +114,15 @@
 
   strftime -r '%Y' 2> /dev/null
 1:-r timestring not optional
+
+  for 1 in %. %1. %3. %6. %9. %12.; do
+    print -rn - "$1 "
+    strftime "%Y-%m-%d %H:%M:%S.$1" 1012615322 $(( 999_999_999 ))
+  done
+0:%. truncation
+>%. 2002-02-02 02:02:02.999
+>%1. 2002-02-02 02:02:02.9
+>%3. 2002-02-02 02:02:02.999
+>%6. 2002-02-02 02:02:02.999999
+>%9. 2002-02-02 02:02:02.999999999
+>%12. 2002-02-02 02:02:02.999999999



Messages sorted by: Reverse Date, Date, Thread, Author