Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] ztrftime(): Fix truncation for %.
- X-seq: zsh-workers 43936
- From: dana <dana@xxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: [PATCH] ztrftime(): Fix truncation for %.
- Date: Mon, 24 Dec 2018 03:16:22 -0600
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=from:content-transfer-encoding:mime-version:subject:date:references :to:in-reply-to:message-id; bh=r+mV6W5GlegvHEkKhoU16fisVWk2fBWn9OqZwlr6D3s=; b=BfJbLOyMH3sX9qbAKXOFsaUhc4JYKvP+Ao1EYhezkLId6UUe8GH+CNLAswhIXct9fA M5veXsltbzeHUhjH5cjITgp4Nt1C5goaHX+isTnHNeEF227ZJaYa+0Cs5a2BQ7AjnUvV JFVdvog2qPiHqx4CdQ/1MFJGwEUWz+KzM6G/QW0Sn7tkZleYY3epoVWby3pDack7cJ02 qgSlgXP9XJ6ppONvj5oOmoS8TLO8dtKWhswaN9q6jdi5o/BKgp+ddb5FBvlRL/DApX0/ KwzQ6hAtivmisqqcngveDiEcCYVUIZTLnHEhRsqawhV8NTOm5zwr8MQ1cAUWcC093dD+ OjEQ==
- In-reply-to: <C7926210-86FE-4626-9A93-4A387C315CC4@dana.is>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <d7b0451f90bdfe61f48cc1361690180e07158900.camel@ntlworld.com> <20181224054021.GK1941@sym.noone.org> <20181224071421.GL1941@sym.noone.org> <C7926210-86FE-4626-9A93-4A387C315CC4@dana.is>
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