This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 2/2] time: in strptime(), make %z accept [+-]HH:MM tz [BZ #17887]
- From: Vincent Bernat <Vincent dot Bernat at exoscale dot ch>
- To: libc-alpha at sourceware dot org, James Perkins <opalmirror at gmail dot com>, Mike Frysinger <vapier at gentoo dot org>
- Cc: Vincent Bernat <vincent at bernat dot im>
- Date: Wed, 16 Sep 2015 10:47:23 +0200
- Subject: [PATCH 2/2] time: in strptime(), make %z accept [+-]HH:MM tz [BZ #17887]
- Authentication-results: sourceware.org; auth=none
- References: <20150911174300 dot GB2802 at gmail dot com> <1442393243-30956-1-git-send-email-Vincent dot Bernat at exoscale dot ch>
From: Vincent Bernat <vincent@bernat.im>
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 <vincent@bernat.im>
[BZ #17887]
* time/strptime_l.c (__strptime_internal): Make %z accept
[+-]HH:MM time zones.
---
time/strptime_l.c | 10 +++++++---
time/tst-strptime2.c | 24 +++++++++++++++++++-----
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/time/strptime_l.c b/time/strptime_l.c
index 989edd6144e6..179ae0e210b0 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 cfb7d70c4cc2..0036a99c9390 100644
--- a/time/tst-strptime2.c
+++ b/time/tst-strptime2.c
@@ -34,7 +34,8 @@ static bool verbose;
Note that a valid timezone string contains the 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
@@ -55,7 +56,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 ? '-' : '+';
@@ -65,7 +66,9 @@ 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);
+ snprintf (buf + i,
+ (colon && ndigits >= 2) ? ndigits + 2 : ndigits + 1,
+ "%02u%s%02u", hh, colon ? ":" : "", mm);
if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
{
@@ -176,11 +179,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