From patchwork Mon Apr 1 03:58:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: TAMUKI Shoichi X-Patchwork-Id: 1072279 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-101065-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linet.gr.jp Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="IZ5cwUGs"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44Xdqq5Q5Tz9sPv for ; Mon, 1 Apr 2019 14:58:47 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:from:date:to:subject:in-reply-to :references:mime-version:content-type; q=dns; s=default; b=fVId1 XUJgQus2u9Efognwq36r/rVBoohAWT6R3Hs7u6/6JWUVCwfwA7IBOClSMi1Rv+b+ FfQ92s10qtOmsYYHA+VDunpPGDzIIMKgFDRqDxwSxAtxZPSZgLPuvEuIcAhbp+7l 12LASZMzEuGTBSOOX05XfgA+WBkvc1vGS6LeyM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:from:date:to:subject:in-reply-to :references:mime-version:content-type; s=default; bh=9PYNdzLZKFZ N4gKtWwzMXy1ehT0=; b=IZ5cwUGsLQhtH7HZk69tX3mSaGy9WafWa7zogtoXBdI lmdRAFiXtr9DTVgiKK5CtOzF/ZlHzECSMwjwYfABscXu8bpiP0WpprNc6B6k8cLY 7T5AG5RIAK7AvT6t93e/EnfHe3U3/20LDEzslE3Au2Op7o7HDluZA9PwoY3T5EFs = Received: (qmail 99162 invoked by alias); 1 Apr 2019 03:58:40 -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 99145 invoked by uid 89); 1 Apr 2019 03:58:40 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.4 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=1998 X-HELO: mail.linet.jp Message-Id: <201904010358.AA04317@tamuki.linet.gr.jp> From: TAMUKI Shoichi Date: Mon, 01 Apr 2019 12:58:15 +0900 To: libc-alpha@sourceware.org Subject: [PATCH 2/4] time/tst-strftime2.c: Make the file easier to maintain In-Reply-To: <201904010352.AA04315@tamuki.linet.gr.jp> References: <201904010352.AA04315@tamuki.linet.gr.jp> 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. The original author: Rafal Luzynski ChangeLog: * 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. Reviewed-by: Carlos O'Donell --- time/tst-strftime2.c | 107 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/time/tst-strftime2.c b/time/tst-strftime2.c index 3dca2a997f..f537d93ba4 100644 --- a/time/tst-strftime2.c +++ b/time/tst-strftime2.c @@ -19,69 +19,96 @@ . */ #include +#include +#include +#include #include #include #include #include -static const char *locales[] = { "ja_JP.UTF-8", "lo_LA.UTF-8", "th_TH.UTF-8" }; +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[] = - { - { 1, 3, 88 }, - { 7, 0, 89 }, - { 8, 0, 89 }, - { 1, 3, 90 }, - { 1, 3, 97 }, - { 1, 3, 98 } - }; +} date_t; + +static const date_t dates[] = +{ + { 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 int i, const int d, const int m, const int y) +{ + if (dates[i].y < y) + return true; + else if (dates[i].y > y) + return false; + else if (dates[i].m < m) + return true; + else if (dates[i].m > m) + return false; + else + return dates[i].d < d; +} + static void mkreftable (void) { - int i, j, k; - char era[10]; - static const int yrj[] = { 63, 64, 1, 2, 9, 10 }; - static const int yrb[] = { 2531, 2532, 2532, 2533, 2540, 2541 }; + int i, j, k, yr; + const char *era, *sfx; + /* Japanese era year to be checked. */ + static const int yrj[] = + { + 63, 64, 1, 2, 9, 10 + }; + /* Buddhist calendar year to be checked. */ + static const int yrb[] = + { + 2531, 2532, 2532, 2533, 2540, 2541 + }; for (i = 0; i < array_length (locales); i++) for (j = 0; j < array_length (formats); j++) for (k = 0; k < array_length (dates); k++) { - if (i == 0) - { - sprintf (era, "%s", (k < 2) ? "\xe6\x98\xad\xe5\x92\x8c" - : "\xe5\xb9\xb3\xe6\x88\x90"); - if (yrj[k] == 1) - sprintf (ref[i][j][k], "%s\xe5\x85\x83\xe5\xb9\xb4", era); - else - { - if (j == 0) - sprintf (ref[i][j][k], "%s%02d\xe5\xb9\xb4", era, yrj[k]); - else if (j == 1) - sprintf (ref[i][j][k], "%s%2d\xe5\xb9\xb4", era, yrj[k]); - else - sprintf (ref[i][j][k], "%s%d\xe5\xb9\xb4", era, yrj[k]); - } - } - else if (i == 1) + if (i == 0) /* ja_JP */ { - sprintf (era, "\xe0\xba\x9e\x2e\xe0\xba\xaa\x2e "); - sprintf (ref[i][j][k], "%s%d", era, yrb[k]); + era = (is_before (k, 8, 1, 1989)) ? "\u662d\u548c" + : "\u5e73\u6210"; + yr = yrj[k], sfx = "\u5e74"; } + else if (i == 1) /* lo_LA */ + era = "\u0e9e.\u0eaa. ", yr = yrb[k], sfx = ""; + else if (i == 2) /* th_TH */ + era = "\u0e1e.\u0e28. ", yr = yrb[k], sfx = ""; else - { - sprintf (era, "\xe0\xb8\x9e\x2e\xe0\xb8\xa8\x2e "); - sprintf (ref[i][j][k], "%s%d", era, yrb[k]); - } + assert (0); /* Unreachable. */ + if (yr == 1) + sprintf (ref[i][j][k], "%s\u5143%s", era, sfx); + else if (j == 0) + sprintf (ref[i][j][k], "%s%02d%s", era, abs (yr), sfx); + else if (j == 1) + sprintf (ref[i][j][k], "%s%2d%s", era, abs (yr), sfx); + else if (j == 2) + sprintf (ref[i][j][k], "%s%d%s", era, abs (yr), sfx); + else + assert (0); /* Unreachable. */ } } @@ -107,8 +134,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]);