From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,T_DKIMWL_WL_MED autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 9a5912a4 for ; Mon, 24 Dec 2018 16:22:17 +0000 (UTC) Received: (qmail 19195 invoked by alias); 24 Dec 2018 16:22:00 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 43939 Received: (qmail 23979 invoked by uid 1010); 24 Dec 2018 16:22:00 -0000 X-Qmail-Scanner-Diagnostics: from mail-it1-f195.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.100.2/25112. spamassassin: 3.4.2. Clear:RC:0(209.85.166.195):SA:0(-1.9/5.0):. Processed in 3.038907 secs); 24 Dec 2018 16:22:00 -0000 X-Envelope-From: dana@dana.is X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=TAKfNLkvaA59dJDj8LNSrkoF7e5A3c7vrB7JEl6Mx8c=; b=0iUUc3UwP8YwU7ToJT24Ga8Ush8ygNm+ladJH7nWkEwUtKVwOoh2Y9MiP2M9D/g+34 jXuAvcyF6kZDaaf2Trrm4ZI5p69HyDxuXMUI095e5l8HTuE9+LPzQO70+TNUTB3AWPvE Xns1cczKYNhP7xI81+EnK0C/wfvoguYsduvWjwzpScpXEmW9gKisFC8tHhSFROfAC+UY ptm2PxkDf+SikQ/cdj4vkCMofsYCYc8/dFKly7mzXYKCR5n1GkZaS2lYIoI94CqHKqIJ MB7jaYMv4YUStXPeZAmcfd8Ap61cnZkFv+F501PbAB2mGqh07cicxaE+uuPTOmRJAw35 paUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=TAKfNLkvaA59dJDj8LNSrkoF7e5A3c7vrB7JEl6Mx8c=; b=RvUjcVvfADP8tACd7DNh1b8RTkjodOli9EN0NwLdNByuu0f4kkUhCYXSiCRBV90pHS ki2X/6xdZIvjwHd2wNmgMB4uwT4cXFpWpbbb/lphDySDdAxop762ntWMWHf14wUR4sGC 3M+nsJwf7746lrRKn5GC3XiTTRAMNRzNWNcUr5EF2EhhNT7X+fyYJWuDr8R7EjDfcLvz Lz2V7V2Z//y92IZnaefnVKp9FMN0iUuZu/A0V0Tjil4AD+XL5i5WG4xeozwTuPKez9M1 WY8MhJQRRVOY2bMoTL5zW/1fxbhoSWnek7R+EhwCYwnbVyZK49wwLZ8/1yjoXNe2mnYz xJLw== X-Gm-Message-State: AJcUukfPzgaY6sOBwb3ef0FCKy2BfdZeJvtWrRF7+UQza7+7e8pDEv5i YoAj7x7YgFUCTaITB3BAflv62wirmWgQeg== X-Google-Smtp-Source: ALg8bN6Ms0itP1AP4a92AlsgJviW49fGAf9a/iL7N4mY8oPay50MeL4skdtTKb95DDjcV62vup64MA== X-Received: by 2002:a05:660c:b12:: with SMTP id f18mr8943738itk.118.1545668514149; Mon, 24 Dec 2018 08:21:54 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.0 \(3445.100.39\)) Subject: Re: [PATCH] ztrftime(): Fix truncation for %. From: dana In-Reply-To: <1545655545.1499531.1617344072.151563DD@webmail.messagingengine.com> Date: Mon, 24 Dec 2018 10:24:16 -0600 Cc: Zsh workers Content-Transfer-Encoding: quoted-printable Message-Id: <4E316F0B-5606-4E93-8988-28A5444612E6@dana.is> References: <20181224054021.GK1941@sym.noone.org> <20181224071421.GL1941@sym.noone.org> <1545655545.1499531.1617344072.151563DD@webmail.messagingengine.com> To: Daniel Shahaf X-Mailer: Apple Mail (2.3445.100.39) On 24 Dec 2018, at 06:45, Daniel Shahaf wrote: >Also, in (unpatched) master: > >% strftime '[%3.]' 123456789 >[000] >% strftime '[%s][%3.]' 0 123456789 >[0][124] >%=20 > >Shouldn't the former print [124]? No, %. only uses the nanoseconds value given to ztrftime(), which is implicitly 0 in the former case. It's not substituting an arbitrary = series of inputs in the style of printf, it only takes those two time arguments to ztrftime(). On 24 Dec 2018, at 06:45, Daniel Shahaf wrote: >but nsec=3D11151 would print '1115'. (The +8 should probably be +5 --- >half the radix.) Oh, wow. That's... extremely obvious in hind sight. Apparently i had = changed the +5 to +8 in the ztrftime() patch that originally added nanosecond = support, i guess mindlessly associating it with the 5 that used to be in the = loop. Embarrassing. ... How about this, then? This also fixes an issue where you can't use = %. more than once in the format string because it overwrites the original value. dana diff --git a/Src/utils.c b/Src/utils.c index e43a3cdb4..70ac7ac8d 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -3334,19 +3334,22 @@ morefmt: #endif switch (*fmt++) { case '.': - if (ztrftimebuf(&bufsize, digs)) - return -1; + { if (digs > 9) digs =3D 9; + if (ztrftimebuf(&bufsize, digs)) + return -1; + long fnsec =3D nsec; if (digs < 9) { int trunc; for (trunc =3D 8 - digs; trunc; trunc--) - nsec /=3D 10; - nsec =3D (nsec + 8) / 10; + fnsec /=3D 10; + fnsec =3D (nsec < 999999500 ? (fnsec + 5) : fnsec) / = 10; } - sprintf(buf, "%0*ld", digs, nsec); + sprintf(buf, "%0*ld", digs, fnsec); buf +=3D digs; break; + } case '\0': /* Guard against premature end of string */ *buf++ =3D '%'; diff --git a/Test/V09datetime.ztst b/Test/V09datetime.ztst index 2041d9b40..3fee49e1d 100644 --- a/Test/V09datetime.ztst +++ b/Test/V09datetime.ztst @@ -114,3 +114,17 @@ =20 strftime -r '%Y' 2> /dev/null 1:-r timestring not optional + + strftime '%Y-%m-%d %H:%M:%S.%3.' 1012615322 $(( 999_999 )) + 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.001 +>%. 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