strftime: support %q to output the quarter of year
Commit Message
Updated patch attached.
Tested using:
sudo alternatives --set ld /usr/bin/ld.bfd
mkdir build.tmp
cd build.tmp
../configure --quiet --disable-sanity-checks
make PARALLELMFLAGS=-j$(nproc)
make -r PARALLELMFLAGS=-j$(nproc) -C $PWD/../time objdir=$(pwd) check
./testrun.sh time/tst-strftime && echo ok
Note even though the code is trivial here,
%q is useful as from the shell you need to:
$(( ($(date +%-m)-1)/3+1 ))
thanks,
Pádraig.
Comments
Pádraig Brady wrote:
> + DO_NUMBER (1, tp->tm_mon / 3 + 1);
I couldn't resist puzzling this one out. We can assume 0 <= tp->tm_mon < 12, so
this should be a tad more efficient:
DO_NUMBER (1, ((tp->tm_mon * 11) >> 5) + 1);
With gcc 6.2.0 -O2 x86-64, the former uses imull with 6 insns, the latter just
leal with 4 insns. Perhaps GCC should be smart enough to figure this out on its
own, but what would be the fun of that?
From 137cc31d7870e3ecda6c67cda47e29ace2c5acf6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Thu, 3 Nov 2016 19:36:59 +0000
Subject: [PATCH] strftime: support %q to output the quarter of year
This is already supported by gnulib.
* manual/time.texi: Document %q outputs quarter 1..4.
* time/strftime_l.c: Implement %q.
* time/tst-strftime.c: Add a test case.
* NEWS: Mention the new feature.
---
NEWS | 2 ++
manual/time.texi | 6 ++++++
time/strftime_l.c | 4 ++++
time/tst-strftime.c | 27 +++++++++++++++++++++++++++
4 files changed, 39 insertions(+)
@@ -119,6 +119,8 @@ Version 2.25
variable for a particular architecture in the GCC source file
'gcc/config.gcc'.
+* strftime now supports the %q directive to output the quarter of the year.
+
Security related changes:
On ARM EABI (32-bit), generating a backtrace for execution contexts which
@@ -1510,6 +1510,12 @@ most locales @samp{AM}/@samp{PM} format is not supported, in such cases
This format is a GNU extension.
+@item %q
+Quarter of the year (@samp{1}@dots{}@samp{4}),
+with January starting the first quarter.
+
+This format is a GNU extension.
+
@item %r
The complete calendar time using the AM/PM format of the current locale.
@@ -1085,6 +1085,10 @@ __strftime_internal (CHAR_T *s, size_t maxsize, const CHAR_T *format,
goto underlying_strftime;
#endif
+ case L_('q'): /* GNU extension. */
+ DO_NUMBER (1, tp->tm_mon / 3 + 1);
+ break;
+
case L_('R'):
subfmt = L_("%H:%M");
goto subformat;
@@ -154,6 +154,33 @@ do_test (void)
}
}
+ /* Check %q. */
+ for (size_t mon = 1; mon <= 12; mon++)
+ {
+ char out[2];
+ char exp[2] = {0,};
+ struct tm qtm = { .tm_mon = mon - 1 };
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat"
+ size_t r = strftime (out, sizeof (out), "%q", &qtm);
+#pragma GCC diagnostic pop
+ if (r == 0)
+ {
+ puts ("strftime(\"%q\") failed");
+ result = 1;
+ break;
+ }
+
+ exp[0] = mon < 4 ? '1' : mon < 7 ? '2' : mon < 10 ? '3' : '4';
+ if (strcmp (out, exp) != 0)
+ {
+ printf ("strftime %%q: expected \"%s\", got \"%s\"\n", exp, out);
+ result = 1;
+ break;
+ }
+ }
+
return result + do_bz18985 ();
}
--
2.5.5