public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Pádraig Brady" <P@draigBrady.com>
To: libc-alpha@sourceware.org
Subject: Re: [PATCH] strftime: support %q to output the quarter of year
Date: Fri, 04 Nov 2016 01:47:00 -0000	[thread overview]
Message-ID: <c16c3a8b-857a-5109-6500-4cfebd86606d@draigBrady.com> (raw)
In-Reply-To: <1478202401-5238-1-git-send-email-P@draigBrady.com>

[-- Attachment #1: Type: text/plain, Size: 442 bytes --]

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.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: glibc-strftime-%q.patch --]
[-- Type: text/x-patch; name="glibc-strftime-%q.patch", Size: 2977 bytes --]

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(+)

diff --git a/NEWS b/NEWS
index 65184b1..9b9fa33 100644
--- a/NEWS
+++ b/NEWS
@@ -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
diff --git a/manual/time.texi b/manual/time.texi
index 6a899b7..b6466d1 100644
--- a/manual/time.texi
+++ b/manual/time.texi
@@ -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.
 
diff --git a/time/strftime_l.c b/time/strftime_l.c
index 1205035..25746a0 100644
--- a/time/strftime_l.c
+++ b/time/strftime_l.c
@@ -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;
diff --git a/time/tst-strftime.c b/time/tst-strftime.c
index af3ff72..d087266 100644
--- a/time/tst-strftime.c
+++ b/time/tst-strftime.c
@@ -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


  parent reply	other threads:[~2016-11-04  1:47 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-03 19:46 Pádraig Brady
2016-11-03 19:58 ` Florian Weimer
2016-11-03 20:28   ` Adhemerval Zanella
2016-11-03 22:00   ` Joseph Myers
2016-11-04  1:47 ` Pádraig Brady [this message]
2016-11-04  4:12   ` Paul Eggert
2016-11-04 11:33 ` Rafal Luzynski
2016-11-04 14:27   ` [PATCH v3] strftime,strptime: have %q represent " Pádraig Brady
2016-11-04 11:41 ` [PATCH] strftime: support %q to output " Szabolcs Nagy
2016-11-04 12:00   ` Pádraig Brady
2016-11-04 12:08     ` Szabolcs Nagy
2016-11-04 12:23       ` Pádraig Brady
2016-11-04 12:41         ` Szabolcs Nagy
2016-11-04 12:58           ` Pádraig Brady
2016-11-04 13:12         ` keld
2016-11-04 14:12           ` Szabolcs Nagy
2016-11-04 14:57             ` Adhemerval Zanella
2016-11-05  7:19             ` keld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c16c3a8b-857a-5109-6500-4cfebd86606d@draigBrady.com \
    --to=p@draigbrady.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).