]> Savannah Git Hosting - gnulib.git/commitdiff
strftime,strptime: support %q to represent the quarter
authorPádraig Brady <pbrady@fb.com>
Wed, 19 Oct 2016 21:46:14 +0000 (14:46 -0700)
committerPádraig Brady <P@draigBrady.com>
Sat, 5 Nov 2016 16:59:42 +0000 (16:59 +0000)
* lib/strftime.c (strftime_case_): Add %q case.
* lib/strptime.c (__strptime_internal): Likewise.
* tests/test-strftime.c (quarter_test): A new test case.

ChangeLog
lib/strftime.c
lib/strptime.c
tests/test-strftime.c

index e5d7987f309a9dd4d5c434fed4c8bf7cbce42fcc..ef74cb4a976c564396dc6354da3280770897ac2b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2016-11-05  Pádraig Brady  <pbrady@fb.com>
+
+       strftime,strptime: support %q to represent the quarter
+       * lib/strftime.c (strftime_case_): Add %q case.
+       * lib/strptime.c (__strptime_internal): Likewise.
+       * tests/test-strftime.c (quarter_test): A new test case.
+
 2016-11-03  Eric Blake  <eblake@redhat.com>
 
        bootstrap: Fix get_version() for AIX 5.3
index 4e65190386d5ea311c3f7c2869ef7fc9328b2077..564e819d1d4a453239d1940fcc65835a6196768a 100644 (file)
@@ -1115,6 +1115,10 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s,
           goto underlying_strftime;
 #endif
 
+        case L_('q'):           /* GNU extension.  */
+          DO_SIGNED_NUMBER (1, tp->tm_mon < -3, tp->tm_mon / 3 + 1U);
+          break;
+
         case L_('R'):
           subfmt = L_("%H:%M");
           goto subformat;
index f9f6d46cbf34f25b5e40ce7931497468711a8754..7cedb779f924ebf097201cd1b4c6e36896e9debc 100644 (file)
@@ -525,6 +525,15 @@ __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM)
                 return NULL;
             }
           break;
+        case 'q':
+          /* Match quarter of year.  GNU extension.  */
+          get_number (1, 4, 1);
+          tm->tm_mon = (val - 1) * 3;
+          tm->tm_mday = 1;
+          s.have_mon = 1;
+          s.have_mday = 1;
+          s.want_xday = 1;
+          break;
         case 'r':
 #ifdef _NL_CURRENT
           if (*decided != raw)
@@ -982,6 +991,15 @@ __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM)
               get_alt_number (0, 59, 2);
               tm->tm_min = val;
               break;
+            case 'q':
+              /* Match quarter using alternate numeric symbols.  */
+              get_alt_number (1, 4, 1);
+              tm->tm_mon = (val - 1) * 3;
+              tm->tm_mday = 1;
+              s.have_mon = 1;
+              s.have_mday = 1;
+              s.want_xday = 1;
+              break;
             case 'S':
               /* Match seconds using alternate numeric symbols.  */
               get_alt_number (0, 61, 2);
index 39494e6daa2eaf1dc8120d2643ff3c9fb100192a..41c186149ea821a4548640c925087e39219cd771 100644 (file)
@@ -200,12 +200,48 @@ tzalloc_test (void)
   return fail;
 }
 
+
+static int
+quarter_test (void)
+{
+  int result = 0;
+  size_t mon;
+
+  /* Check %q.  */
+  for (mon = 1; mon <= 12; mon++)
+    {
+      char out[2];
+      char exp[2] = {0,};
+      struct tm qtm = { .tm_mon = mon - 1 };
+      char fmt[3] = {'%','q','\0'};
+
+      size_t r = nstrftime (out, sizeof (out), fmt, &qtm, 0, 0);
+      if (r == 0)
+        {
+          puts ("nstrftime(\"%q\") failed");
+          result = 1;
+          break;
+        }
+
+      exp[0] = mon < 4 ? '1' : mon < 7 ? '2' : mon < 10 ? '3' : '4';
+      if (strcmp (out, exp) != 0)
+        {
+          printf ("nstrftime %%q: expected \"%s\", got \"%s\"\n", exp, out);
+          result = 1;
+          break;
+        }
+    }
+
+  return result;
+}
+
 int
 main (void)
 {
   int fail = 0;
   fail |= posixtm_test ();
   fail |= tzalloc_test ();
+  fail |= quarter_test ();
   return fail;
 }