Re: [nmh-workers] Ordinal Indicators.
Hi kre, > LC_TIME says how time (of day) is represented (d/m/y m/d/y, 12 or 24 > hour, etc) I had a need today to have date(1) produce ‘8th’ for today and find its interpreted sequences don't support the https://en.wikipedia.org/wiki/Ordinal_indicator, but then neither does POSIX's LC_TIME, it seems. -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Date:Mon, 08 Jul 2019 11:37:10 +0100 From:Ralph Corderoy Message-ID: <20190708103710.86f3121...@orac.inputplus.co.uk> | I had a need today to have date(1) produce 8th for today Yes, strftime() has no ordinals, and (as best I understand it) locales don't either. To get them you need to code it yourself D=$(date +%d) case "$D" in [023]1) ORD=st;; [02]2) ORD=nd;; [02]3) ORD=rd;; *) ORD=th;; esac case "$D" in 0*) SP=;; *) SP=' ';; esac and then deal with internationalising that for yourself... (good luck). Then date "+%A, the${SP}%e${ORD} of %B" or whatever. kre ps: this is not really an nmh-workers issue! -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Hi kre, > D=$(date +%d) > case "$D" in > [023]1) ORD=st;; > [02]2) ORD=nd;; > [02]3) ORD=rd;; > *) ORD=th;; > esac > case "$D" in > 0*) SP=;; > *) SP=' ';; > esac I ended up with $ cat ~/bin/ordsuff #! /bin/sed -rf # Append an ordinal indicator to the number on each line. s/1$/&st/ s/2$/&nd/ s/3$/&rd/ s/[4-9]$/&th/ s/.0$/&th/ /1(1st|2nd|3rd)$/s/..$/th/ $ > ps: this is not really an nmh-workers issue! This is true. To correct that, I note mh-format(5) too has no function to produce the ordinal suffix. :-) -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
>This is true. To correct that, I note mh-format(5) too has no function >to produce the ordinal suffix. :-) Fixed. --Ken -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Hi Ken, > > This is true. To correct that, I note mh-format(5) too has no > > function to produce the ordinal suffix. :-) > > Fixed. Impressively quick work. + int digit = value % 10; + const char *suffix; + + switch (digit) { + case 1: + suffix = "st"; + break; + case 2: + suffix = "nd"; + break; + case 3: + suffix = "rd"; + break; + default: + suffix = "th"; + } Were you paying attention to kre's sh and my sed? :-) -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
>Were you paying attention to kre's sh and my sed? :-) I mean ... yeah? Steal from the best, and all that! I had a few free minutes, I thought, "Oh, huh, nmh SHOULD have a function to output the ordinal string for dates and such", and I though it should be pretty easy and it was. --Ken -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Hi Ken, > > Were you paying attention to kre's sh and my sed? :-) > > I mean ... yeah? Steal from the best, and all that! I had a few free > minutes, I thought, "Oh, huh, nmh SHOULD have a function to output the > ordinal string for dates and such", and I though it should be pretty > easy and it was. Yep, you certainly made it look easy compared to kre and me. And it saved me raising a bug to say it wasn't implemented. BTW, when would you say peak caffeine is reached in your day? -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Ken, Ralph is being overly subtle (or perhaps I could use another word...) The point is, that if you're going to add this, you should do it correctly, not just any random code that looks close. kre ps: Hint: 11st is not correct. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
>The point is, that if you're going to add this, you should do it >correctly, not just any random code that looks close. Geez, you could have just SAID that (ok, fine, you DID say that, but ... oh, never mind). I missed that part in your examples. Anyway, fixed! --Ken -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
> Geez, you could have just SAID that Now where would be the fun in that. :-) -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Hi, Is -42nd handled? IIRC we demand C99 so we know rounding is towards zero. But C's remainder operator, ‘%’, returns the sign of the dividend, unlike modulo. And then there's two's complement so INT_MIN can't be made positive. $ for a in ' -' ' '; do > for b in ' -' ' '; do > fmttest -raw \ > -format "%(num${a}42) /${b}10 = %(divide${b}10)\t%(num${a}42) %%${b}10 = %(modulo${b}10)" '' > done > done -42 / -10 = 4 -42 % -10 = -2 -42 / 10 = -4 -42 % 10 = -2 42 / -10 = -4 42 % -10 = 2 42 / 10 = 4 42 % 10 = 2 $ -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
>Is -42nd handled? You know where the source code is, Ralph. Have at it. --Ken -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
On Mon, 08 Jul 2019 17:25:42 +0100, Ralph Corderoy said: > Is -42nd handled? I admit being totally mystified as to what situations require proper handling of negative ordinals pgpM4SES7TBLk.pgp Description: PGP signature -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers
Re: [nmh-workers] Ordinal Indicators.
Hi Valdis, > > Is -42nd handled? > > I admit being totally mystified as to what situations require proper > handling of negative ordinals Well, from here the one after next is the 2nd, and the one before last is the -2nd. Regardless, the code and documentation should match, and it seems easiest to fix the code rather than explain the exceptions in the man page. IIRC that was the impetus behind McIlroy adding the BUGS section; his peers found it simpler to fix than document. -- Cheers, Ralph. -- nmh-workers https://lists.nongnu.org/mailman/listinfo/nmh-workers