From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29444 invoked by alias); 8 Jul 2015 14:39:56 -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: X-Seq: 35734 Received: (qmail 12892 invoked from network); 8 Jul 2015 14:39:54 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-type:content-transfer-encoding; bh=dc3J7pP1s4TYoAyWcm5WiwNes4+qKtzOqOoKLg2NvrU=; b=O+KjcKrBjiHxIretaFFeKSbulXVdPt68x5uXQZQmOA7XxVP9T5n23xcvOpDgssWid1 19W0f8YT4gNSoua5QcOcDXhVZWtt2caBQrrIIN4yDdVxWZZFyTD3uWLcEEXhEADnArA/ Njq120Mj7KDYiBOoAuAdhq/DxM/1lp7n6spmxbHMacbZWMKg4mpven+QDoT6WLVdVfOU hUsipQZ9oHmAMSnRM3XmFxPfy4H9cXR9MgRptKfFk95qeThh26NOoCIFoIVW4aBRBWZr rSmqiH5LMMUFIa3xklc6yaIgqrFKorx80VeWYpi8pc+PTASvoXUATSS9StT8G4qvC+DF Fyxw== X-Received: by 10.181.12.111 with SMTP id ep15mr73621511wid.15.1436366390373; Wed, 08 Jul 2015 07:39:50 -0700 (PDT) From: Mikael Magnusson To: zsh-workers@zsh.org Subject: PATCH: ztrftime: Handle all non-zsh format chars with strftime if present Date: Wed, 8 Jul 2015 16:39:43 +0200 Message-Id: <1436366383-23389-1-git-send-email-mikachu@gmail.com> X-Mailer: git-send-email 2.4.0 In-Reply-To: <4FE37754-AA20-405E-8925-6C4F9E0043AE@kba.biglobe.ne.jp> References: <4FE37754-AA20-405E-8925-6C4F9E0043AE@kba.biglobe.ne.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Jun wrote: >2015/07/08 08:21, Mikael Magnusson wrote: >> % print -P %D\{%x\} >> 2015年07月08日 >> % print -P %D\{%Ex\} >> 平成27年07月08日 > > It works for %Ex but not for %Ey. > ztrftime() does not send %Ey to strftime() but ignores E and > handles %y by itself. > > % date +%y > 15 > % date +%Ey > 27 > % print -P '%D{%y %Ey}' > 15 15 > > Is it possible to pass the entire format string to strftime() > if HAVE_STRFTIME is defined? This is one way to do it, but it makes this statement in the manpage untrue. Perhaps this is a good tradeoff though? I'm sure someone on a non-GNU system will disagree :). The GNU extension that a `-' between the % and the format character causes a leading zero or space to be stripped is handled directly by the shell for the format characters d, f, H, k, l, m, M, S and y; any other format characters are provided to strftime() with any leading `-', present, so the handling is system dependent. Further GNU exten‐ sions are not supported at present. We could keep track of which modifiers have been seen and handle %-f (for example), but it'll be a mess... Maybe at the start of the switch, we can check if (fmt - fmtstart is <= 2 && strip), or something like that? I'll poke at it. --- Src/utils.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Src/utils.c b/Src/utils.c index 2d53a00..e03e180 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -2948,6 +2948,12 @@ morefmt: sprintf(buf, "%0*ld", digs, usec); buf += digs; break; + case '\0': + /* Guard against premature end of string */ + *buf++ = '%'; + fmt--; + break; +#ifndef HAVE_STRFTIME case 'd': if (tm->tm_mday > 9 || !strip) *buf++ = '0' + tm->tm_mday / 10; @@ -3012,12 +3018,6 @@ morefmt: *buf++ = '0' + (tm->tm_year / 10) % 10; *buf++ = '0' + tm->tm_year % 10; break; - case '\0': - /* Guard against premature end of string */ - *buf++ = '%'; - fmt--; - break; -#ifndef HAVE_STRFTIME case 'Y': { /* @@ -3065,6 +3065,38 @@ morefmt: case '-': case '0' ... '9': goto morefmt; + case 'f': /* copy of code above */ + strip = 1; + if (tm->tm_mday > 9) + *buf++ = '0' + tm->tm_mday / 10; + else if (!strip) + *buf++ = ' '; + *buf++ = '0' + tm->tm_mday % 10; + break; + case 'K': /* copy of code above */ + strip = 1; + if (tm->tm_hour > 9) + *buf++ = '0' + tm->tm_hour / 10; + else if (!strip) { + if (fmt[-1] == 'H') + *buf++ = '0'; + else + *buf++ = ' '; + } + *buf++ = '0' + tm->tm_hour % 10; + break; + case 'L': /* copy of code above */ + strip = 1; + hr12 = tm->tm_hour % 12; + if (hr12 == 0) + hr12 = 12; + if (hr12 > 9) + *buf++ = '1'; + else if (!strip) + *buf++ = ' '; + + *buf++ = '0' + (hr12 % 10); + break; default: /* * Remember we've already allowed for two characters -- 2.4.0