]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - time/tst-strptime2.c
Fix mktime localtime offset confusion
[thirdparty/glibc.git] / time / tst-strptime2.c
index 3d906dec74e79e9a67aa952b81579e582ac08038..8019e7f5d8c4c223226a140b7ec96181dde250b2 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <time.h>
+#include <libc-diag.h>
 
 /* Dummy string is used to match strptime's %s specifier.  */
 
@@ -35,7 +36,8 @@ static bool verbose;
    following fields:
      Sign field consisting of a '+' or '-' sign,
      Hours field in two decimal digits, and
-     optional Minutes field in two decimal digits.
+     optional Minutes field in two decimal digits. Optionally,
+     a ':' is used to seperate hours and minutes.
 
    This function may write test strings with minutes values outside
    the valid range 00-59.  These are invalid strings and useful for
@@ -56,7 +58,7 @@ static bool verbose;
    range of 00 to 59.  */
 
 static long int
-mkbuf (char *buf, bool neg, unsigned int hhmm, size_t ndigits)
+mkbuf (char *buf, bool neg, bool colon, unsigned int hhmm, size_t ndigits)
 {
   const int mm_max = 59;
   char sign = neg ? '-' : '+';
@@ -66,7 +68,20 @@ mkbuf (char *buf, bool neg, unsigned int hhmm, size_t ndigits)
   long int expect = LONG_MAX;
 
   i = sprintf (buf, "%s %c", dummy_string, sign);
-  snprintf (buf + i, ndigits + 1, "%04u", hhmm);
+#if __GNUC_PREREQ (7, 0)
+  /* GCC issues a warning when it thinks the snprintf buffer may be too short.
+     This test is explicitly using short buffers to force snprintf to truncate
+     the output so we ignore the warnings.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
+#endif
+  if (colon)
+    snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm);
+  else
+    snprintf (buf + i, ndigits + 1, "%04u", hhmm);
+#if __GNUC_PREREQ (7, 0)
+  DIAG_POP_NEEDS_COMMENT;
+#endif
 
   if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
     {
@@ -177,11 +192,22 @@ do_test (void)
       {
        /* Test both positive and negative signs.  */
 
-       expect = mkbuf (buf, false, hhmm, ndigits);
+       expect = mkbuf (buf, false, false, hhmm, ndigits);
        result |= compare (buf, expect, nresult);
 
-       expect = mkbuf (buf, true, hhmm, ndigits);
+       expect = mkbuf (buf, true, false, hhmm, ndigits);
        result |= compare (buf, expect, nresult);
+
+       /* Test with colon as well.  */
+
+       if (ndigits >= 3)
+         {
+           expect = mkbuf (buf, false, true, hhmm, ndigits);
+           result |= compare (buf, expect, nresult);
+
+           expect = mkbuf (buf, true, true, hhmm, ndigits);
+           result |= compare (buf, expect, nresult);
+         }
       }
 
   if (result > 0 || verbose)