Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: ztrftime: Handle all non-zsh format chars with strftime if present
- X-seq: zsh-workers 35734
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: PATCH: ztrftime: Handle all non-zsh format chars with strftime if present
- Date: Wed, 8 Jul 2015 16:39:43 +0200
- 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==
- In-reply-to: <4FE37754-AA20-405E-8925-6C4F9E0043AE@kba.biglobe.ne.jp>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <4FE37754-AA20-405E-8925-6C4F9E0043AE@kba.biglobe.ne.jp>
Jun wrote:
>2015/07/08 08:21, Mikael Magnusson <mikachu@xxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author