[PATCH v2 2/2] ucrtbase: strftime %y works for years 0 to 9999.

Jeff Smith whydoubt at gmail.com
Wed Nov 13 13:54:09 CST 2019


Signed-off-by: Jeff Smith <whydoubt at gmail.com>
---
 dlls/msvcrt/time.c         |  9 ++++++++-
 dlls/ucrtbase/tests/misc.c | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/dlls/msvcrt/time.c b/dlls/msvcrt/time.c
index 535ea2dbd7..49f134b91b 100644
--- a/dlls/msvcrt/time.c
+++ b/dlls/msvcrt/time.c
@@ -1311,7 +1311,14 @@ static MSVCRT_size_t strftime_helper(char *str, MSVCRT_size_t max, const char *f
                 return 0;
             break;
         case 'y':
-            if(!strftime_int(str, &ret, max, mstm->tm_year%100, alternate ? 0 : 2, 0, 99))
+#if _MSVCR_VER>=140
+            if (mstm->tm_year < -1900 || mstm->tm_year > 8099)
+                goto einval_error;
+            tmp = (mstm->tm_year+1900)%100;
+#else
+            tmp = mstm->tm_year%100;
+#endif
+            if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 0, 99))
                 return 0;
             break;
         case 'Y':
diff --git a/dlls/ucrtbase/tests/misc.c b/dlls/ucrtbase/tests/misc.c
index 014d66d595..4342236f10 100644
--- a/dlls/ucrtbase/tests/misc.c
+++ b/dlls/ucrtbase/tests/misc.c
@@ -902,9 +902,21 @@ static void test_asctime(void)
 
 static void test_strftime(void)
 {
+    const struct {
+        int in;
+        const char *out;
+    } years[] = {
+         {-1900, "00"},
+         {-1, "99"},
+         {0, "00"},
+         {8099, "99"},
+         /* FIXME: -1901 and 8100 should be tested as well, as they are outside
+          * the valid range, but they both cause the test to crash. */
+    };
     const struct tm epoch = { 0, 0, 0, 1, 0, 70, 4, 0, 0 };
     const struct tm tm1 = { 0, 0, 0, 1, 0, 117, 0, 0, 0 };
     const struct tm tm2 = { 0, 0, 14, 1, 0, 121, 6, 0, 0 };
+    struct tm tm3 = { 0, 0, 14, 1, 0, 0, 6, 0, 0 };
     char bufA[256];
     size_t retA;
     int i;
@@ -1011,6 +1023,15 @@ static void test_strftime(void)
         isdigit(bufA[1]) && isdigit(bufA[2]) &&
         isdigit(bufA[3]) && isdigit(bufA[4]), "got %s\n", bufA);
 
+    for(i = 0; i < ARRAY_SIZE(years); i++) {
+        tm3.tm_year = years[i].in;
+        retA = p_strftime(bufA, sizeof(bufA), "%y", &tm3);
+        ok(retA == strlen(years[i].out), "for %d, expected %d, got %d\n",
+            years[i].in, strlen(years[i].out), (int)retA);
+        ok(!strcmp(bufA, years[i].out), "for %d, expected '%s', got '%s'\n",
+            years[i].in, years[i].out, bufA);
+    }
+
     for(i=0; i<14; i++)
     {
         __time32_t t = (365*2 + i - 7) * 24 * 60 * 60;
-- 
2.23.0




More information about the wine-devel mailing list