From patchwork Thu Sep 17 07:56:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Vincent Bernat X-Patchwork-Id: 8739 X-Patchwork-Delegate: vapier@gentoo.org Received: (qmail 23692 invoked by alias); 17 Sep 2015 07:56:20 -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 23680 invoked by uid 89); 17 Sep 2015 07:56:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL, BAYES_00, KAM_MX4, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 X-HELO: smtp-sh.infomaniak.ch From: Vincent Bernat To: libc-alpha@sourceware.org Cc: James Perkins Subject: Re: [PATCH 2/2] time: in strptime(), make %z accept [+-]HH:MM tz [BZ #17887] References: <20150911174300.GB2802@gmail.com> <1442393243-30956-1-git-send-email-Vincent.Bernat@exoscale.ch> <1442393243-30956-2-git-send-email-Vincent.Bernat@exoscale.ch> <20150916171955.GH20252@vapier.lan> Date: Thu, 17 Sep 2015 09:56:13 +0200 In-Reply-To: <20150916171955.GH20252@vapier.lan> (Mike Frysinger's message of "Wed, 16 Sep 2015 13:19:55 -0400") Message-ID: <87io795vj6.fsf@zoro.exoscale.ch> User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 X-Antivirus-Code: 0x100000 ❦ 16 septembre 2015 13:19 -0400, Mike Frysinger  : >> --- a/time/tst-strptime2.c >> +++ b/time/tst-strptime2.c >> >> - snprintf (buf + i, ndigits + 1, "%04u", hhmm); >> + snprintf (buf + i, >> + (colon && ndigits >= 2) ? ndigits + 2 : ndigits + 1, >> + "%02u%s%02u", hh, colon ? ":" : "", mm); > > i think the inlining of |colon| makes it hard to read. just use an if > statement to do it: > if (colon) > snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm); > else > snprintf (buf + i, ndigits + 1, "%04u", hhmm); Yes, far better. Updated patch: From accaced9fe52cc3a872dbe55c1fa49a5ce44f2c5 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Wed, 16 Sep 2015 09:54:05 +0200 Subject: [PATCH 2/2] time: in strptime(), make %z accept [+-]HH:MM tz [BZ #17887] In ISO 8601, +03:30 is a valid time zone. Currently, strptime() only parses it as a 2-digit time zone an believes this is +03:00. This change makes it accept a single colon. This fixes BZ #17887. 2015-09-04 Vincent Bernat [BZ #17887] * time/strptime_l.c (__strptime_internal): Make %z accept [+-]HH:MM time zones. --- time/strptime_l.c | 10 +++++++--- time/tst-strptime2.c | 25 ++++++++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/time/strptime_l.c b/time/strptime_l.c index 989edd6144e6..c3ce50fe10ab 100644 --- a/time/strptime_l.c +++ b/time/strptime_l.c @@ -749,9 +749,11 @@ __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM) rp++; break; case 'z': - /* We recognize three formats: if two digits are given, these - specify hours. If fours digits are used, minutes are - also specified. 'Z' is equivalent to +0000. */ + /* We recognize four formats: + 1. Two digits specify hours. + 2. Four digits specify hours and minutes. + 3. Two digits, ':', and two digits specify hours and minutes. + 4. 'Z' is equivalent to +0000. */ { val = 0; while (ISSPACE (*rp)) @@ -770,6 +772,8 @@ __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM) { val = val * 10 + *rp++ - '0'; ++n; + if (*rp == ':' && n == 2 && isdigit (*(rp + 1))) + ++rp; } if (n == 2) val *= 100; diff --git a/time/tst-strptime2.c b/time/tst-strptime2.c index 3d906dec74e7..7fe7350024c2 100644 --- a/time/tst-strptime2.c +++ b/time/tst-strptime2.c @@ -35,7 +35,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 +57,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 +67,10 @@ 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 (colon) + snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm); + else + snprintf (buf + i, ndigits + 1, "%04u", hhmm); if (mm <= mm_max && (ndigits == 2 || ndigits == 4)) { @@ -177,11 +181,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) -- 2.5.1