From patchwork Fri Mar 15 11:48:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rafal Luzynski X-Patchwork-Id: 31861 Received: (qmail 89334 invoked by alias); 15 Mar 2019 11:48:49 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 88271 invoked by uid 89); 15 Mar 2019 11:48:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=011, 7513, HX-Languages-Length:3612, month X-HELO: shared-ano163.rev.nazwa.pl X-Spam-Score: -1 Date: Fri, 15 Mar 2019 12:48:34 +0100 (CET) From: Rafal Luzynski To: libc-alpha@sourceware.org Cc: TAMUKI Shoichi , Felix Yan Message-ID: <1025917329.248288.1552650514094@poczta.nazwa.pl> In-Reply-To: <988338284.248045.1552650382964@poczta.nazwa.pl> References: <988338284.248045.1552650382964@poczta.nazwa.pl> Subject: [PATCH 2/3] time/tst-strftime2.c: Make the file easier to maintain. MIME-Version: 1.0 Express the years as full Gregorian years (e.g., 1988 instead of 88) and months with natural numbers (1-12 rather than 0-11). Compare actual dates rather than indexes when selecting the era name. Declare the local variable era as a string character pointer rather than an array of chars where the actual string is copied which might lead to potential buffer overflows in future. * time/tst-strftime2.c (date_t): Explicitly define the type. (dates): Use natural month and year numbers to express a date. (is_before): New function to compare dates. (mkreftable): Minor improvements to simplify maintenance. (do_test): Reflect the changes in dates array. --- time/tst-strftime2.c | 62 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 18 deletions(-) @@ -56,10 +75,13 @@ mkreftable (void) for (j = 0; j < array_length (formats); j++) for (k = 0; k < array_length (dates); k++) { - if (i == 0) + if (i == 0) /* ja_JP */ { - sprintf (era, "%s", (k < 2) ? "\xe6\x98\xad\xe5\x92\x8c" - : "\xe5\xb9\xb3\xe6\x88\x90"); + if (is_before (&dates[k], 8, 1, 1989)) + era = "\xe6\x98\xad\xe5\x92\x8c"; + else + era = "\xe5\xb9\xb3\xe6\x88\x90"; + if (yrj[k] == 1) sprintf (ref[i][j][k], "%s\xe5\x85\x83\xe5\xb9\xb4", era); else @@ -72,16 +94,20 @@ mkreftable (void) sprintf (ref[i][j][k], "%s%d\xe5\xb9\xb4", era, yrj[k]); } } - else if (i == 1) + else if (i == 1) /* lo_LA */ { - sprintf (era, "\xe0\xba\x9e\x2e\xe0\xba\xaa\x2e "); + era = "\xe0\xba\x9e\x2e\xe0\xba\xaa\x2e "; sprintf (ref[i][j][k], "%s%d", era, yrb[k]); } - else + else if (i == 2) /* th_TH */ { - sprintf (era, "\xe0\xb8\x9e\x2e\xe0\xb8\xa8\x2e "); + era = "\xe0\xb8\x9e\x2e\xe0\xb8\xa8\x2e "; sprintf (ref[i][j][k], "%s%d", era, yrb[k]); } + else + { + assert (0); /* Unreachable. */ + } } } @@ -107,8 +133,8 @@ do_test (void) for (k = 0; k < array_length (dates); k++) { ttm.tm_mday = dates[k].d; - ttm.tm_mon = dates[k].m; - ttm.tm_year = dates[k].y; + ttm.tm_mon = dates[k].m - 1; + ttm.tm_year = dates[k].y - 1900; strftime (date, sizeof (date), "%F", &ttm); r = strftime (buf, sizeof (buf), formats[j], &ttm); e = strlen (ref[i][j][k]); diff --git a/time/tst-strftime2.c b/time/tst-strftime2.c index 3dca2a9..bf5a66d 100644 --- a/time/tst-strftime2.c +++ b/time/tst-strftime2.c @@ -19,8 +19,10 @@ . */ #include +#include #include #include +#include #include #include @@ -28,27 +30,44 @@ static const char *locales[] = { "ja_JP.UTF-8", "lo_LA.UTF-8", "th_TH.UTF-8" }; static const char *formats[] = { "%EY", "%_EY", "%-EY" }; -static const struct +typedef struct { const int d, m, y; -} dates[] = +} date_t; + +static const date_t dates[] = { - { 1, 3, 88 }, - { 7, 0, 89 }, - { 8, 0, 89 }, - { 1, 3, 90 }, - { 1, 3, 97 }, - { 1, 3, 98 } + { 1, 4, 1988 }, + { 7, 1, 1989 }, + { 8, 1, 1989 }, + { 1, 4, 1990 }, + { 1, 4, 1997 }, + { 1, 4, 1998 } }; static char ref[array_length (locales)][array_length (formats)] [array_length (dates)][100]; +static bool +is_before (const date_t *date, const int d, const int m, const int y) +{ + if (date->y < y) + return true; + else if (date->y > y) + return false; + else if (date->m < m) + return true; + else if (date->m > m) + return false; + else + return date->d < d; +} + static void mkreftable (void) { int i, j, k; - char era[10]; + const char *era; static const int yrj[] = { 63, 64, 1, 2, 9, 10 }; static const int yrb[] = { 2531, 2532, 2532, 2533, 2540, 2541 };