Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20180725104333.31485-1-zajec5@gmail.com>
Date: Wed, 25 Jul 2018 12:43:33 +0200
From: Rafał Miłecki <zajec5@...il.com>
To: musl@...ts.openwall.com
Cc: Rafał Miłecki <rafal@...ecki.pl>
Subject: [PATCH] strptime: add support for %z field descriptor (ISO 8601 timezone)

From: Rafał Miłecki <rafal@...ecki.pl>

This is glibc extension for parsing ISO 8601 formatted timezone offset.
Supported formats are: ±hh:mm, ±hhmm and ±hh.

While strptime() contains some helpers for parsing (e.g. numeric_range
and numeric_digits) they couldn't be used for %z due to a bigger
complexity of that field (two numbers, optional separator).

Signed-off-by: Rafał Miłecki <rafal@...ecki.pl>
---
 src/time/strptime.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/time/strptime.c b/src/time/strptime.c
index c54a0d8c..f3607bd0 100644
--- a/src/time/strptime.c
+++ b/src/time/strptime.c
@@ -154,6 +154,25 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri
 			adj = 1900;
 			want_century = 0;
 			goto numeric_digits;
+		case 'z':
+			w=2;
+			if (*s == '+') s++;
+			else if (*s == '-') neg=1, s++;
+			else return 0;
+			tm->__tm_gmtoff = 0;
+			for (i=0; i<2 && isdigit(*s); i++)
+				tm->__tm_gmtoff = tm->__tm_gmtoff * 10 + *s++ - '0';
+			if (*s == ':' || isdigit(*s)) {
+				if (*s == ':') s++;
+				for (i=0; i<2 && isdigit(*s); i++)
+					tm->__tm_gmtoff = tm->__tm_gmtoff * 10 + *s++ - '0';
+			} else {
+				tm->__tm_gmtoff *= 100;
+			}
+			tm->__tm_gmtoff = tm->__tm_gmtoff / 100 * 60 + tm->__tm_gmtoff % 100;
+			tm->__tm_gmtoff *= 60;
+			if (neg) tm->__tm_gmtoff = -tm->__tm_gmtoff;
+			break;
 		case '%':
 			if (*s++ != '%') return 0;
 			break;
-- 
2.13.7

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.