From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13448 Path: news.gmane.org!.POSTED!not-for-mail From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH V2 libc-test] add strptime basic test Date: Thu, 15 Nov 2018 14:22:37 +0100 Message-ID: <20181115132237.15498-1-zajec5@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1542288064 13550 195.159.176.226 (15 Nov 2018 13:21:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 15 Nov 2018 13:21:04 +0000 (UTC) Cc: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: musl@lists.openwall.com Original-X-From: musl-return-13464-gllmg-musl=m.gmane.org@lists.openwall.com Thu Nov 15 14:20:59 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1gNHZn-0003K7-Ig for gllmg-musl@m.gmane.org; Thu, 15 Nov 2018 14:20:55 +0100 Original-Received: (qmail 7175 invoked by uid 550); 15 Nov 2018 13:23:03 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 6113 invoked from network); 15 Nov 2018 13:23:03 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tBgF0HMBxTfc80+JXZKDItHS9CZJAWdskBj34Le6ITI=; b=TaTfd6M8rulPpAp40M+PUGUZI9JI2Lpf8LXfJg1DjGqozZ+rVsJxZSlTWpsCR4uq+Q /Fv4cMa3UzHFOZoTCn/czhGaNRSfJ9LxA53LIoL9+Qwag4RWRp6qFzsqoS+wudq6e2qH S8p6nlFOd16EPpzqWWaMEiytxIQox62+bJiEQUizuOL3oCQ9CdE2BEgErZSuXzMDRtmH 5pn9h7XDR2OAC7hwbUj4WOySoP6XZHXlkDd4gCi4uwix3lbyDWEZxTW+7IdomDc4hRa6 iN2wSg4chO8WX4h6bKCAfYq7q4/UfR16//VQFwNOJTkzHbSTU2+c08L73rTFQSho9lb6 81zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tBgF0HMBxTfc80+JXZKDItHS9CZJAWdskBj34Le6ITI=; b=c5DdOsHt6bzMqUMY12M9DJQ6dPDaOjiwem3sj3b6NYybnTqUOj/q8ggZ1NOSfGKtqI K9HWa/nLFwJET2m+bEIexqBw2CWPYLIrPOdEmff7cj7fhnf+Rv3fQebaBJBoIjYuuiAr DNslWYI1x6F1KidXuN5Fm9WTVVKEGDEluIzEbmDecQJJfIPh4f1MNYCiFVZscu3VpmJz B3mj8rAheGXbkuxLmYqK0HbKRa0h2v2EuurFTZOMIkASbOittTC2xBqtONkgLSrzuBG4 QrxdudiQ+RAmXJT2G/BreC079w35W1MlQpch1wsDLaNP6xFtR68TMAxCNTrmrJafWvX9 JXqA== X-Gm-Message-State: AGRZ1gK3+uR3QksVvxdHlwGNPf5Px9S3QObghLIrB911B2g00tzUt0kD A1MU/HWrSKjcztAzmBGFlkvfiQYT X-Google-Smtp-Source: AJdET5dsgZYdwmR8ZncDWWLYZ1ZP3G3tHhKEOPG4i++ubbRIYpam3VAE0khqNwQi8rmd/5/aQ5IRjQ== X-Received: by 2002:a50:fe15:: with SMTP id f21mr5521721edt.116.1542288171326; Thu, 15 Nov 2018 05:22:51 -0800 (PST) X-Mailer: git-send-email 2.13.7 Xref: news.gmane.org gmane.linux.lib.musl.general:13448 Archived-At: From: Rafał Miłecki Signed-off-by: Rafał Miłecki --- V2: Modify checkStrptime to compare time & date only. It's required as setting tm_wday and tm_yday (while parsing a date) isn't clearly described by a manual. glibc does it while musl does not. --- src/functional/strptime.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/functional/strptime.c diff --git a/src/functional/strptime.c b/src/functional/strptime.c new file mode 100644 index 0000000..b5f8977 --- /dev/null +++ b/src/functional/strptime.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: MIT + +#define _GNU_SOURCE /* For tm_gmtoff */ +#include +#include +#include +#include +#include "test.h" + +/** + * checkStrptime - parse time and check if it matches expected value + * + * This function compares time and date fields of tm structure only. + * It's because tm_wday and tm_yday may - but don't have to - be set + * while parsing a date. + */ +static void checkStrptime(const char *s, const char *format, const struct tm *expected) { + struct tm tm = { }; + const char *ret; + + ret = strptime(s, format, &tm); + if (!ret || *ret != '\0') { + t_error("\"%s\": failed to parse \"%s\"\n", format, s); + } else if (tm.tm_sec != expected->tm_sec || + tm.tm_min != expected->tm_min || + tm.tm_hour != expected->tm_hour || + tm.tm_mday != expected->tm_mday || + tm.tm_mon != expected->tm_mon || + tm.tm_year != expected->tm_year) { + char buf1[64]; + char buf2[64]; + + strftime(buf1, sizeof(buf1), "%FT%H:%M:%S%Z", expected); + strftime(buf2, sizeof(buf2), "%FT%H:%M:%S%Z", &tm); + + t_error("\"%s\": for \"%s\" expected %s but got %s\n", format, s, buf1, buf2); + } +} + +static void checkStrptimeTz(const char *s, int h, int m) { + long int expected = h * 3600 + m * 60; + struct tm tm = { }; + const char *ret; + + ret = strptime(s, "%z", &tm); + if (!ret || *ret != '\0') { + t_error("\"%%z\": failed to parse \"%s\"\n", s); + } else if (tm.tm_gmtoff != expected) { + t_error("\"%%z\": for \"%s\" expected tm_gmtoff %ld but got %ld\n", s, tm.tm_gmtoff, expected); + } +} + +static struct tm tm1 = { + .tm_sec = 8, + .tm_min = 57, + .tm_hour = 20, + .tm_mday = 0, + .tm_mon = 0, + .tm_year = 0, +}; + +static struct tm tm2 = { + .tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 25, + .tm_mon = 8 - 1, + .tm_year = 1991 - 1900, +}; + +static struct tm tm3 = { + .tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 21, + .tm_mon = 10 - 1, + .tm_year = 2015 - 1900, +}; + +static struct tm tm4 = { + .tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 10, + .tm_mon = 7 - 1, + .tm_year = 1856 - 1900, +}; + +int main() { + setenv("TZ", "UTC0", 1); + + /* Time */ + checkStrptime("20:57:08", "%H:%M:%S", &tm1); + checkStrptime("20:57:8", "%R:%S", &tm1); + checkStrptime("20:57:08", "%T", &tm1); + + /* Format */ + checkStrptime("20:57:08", "%H : %M : %S", &tm1); + checkStrptime("20 57 08", "%H %M %S", &tm1); + checkStrptime("20%57%08", "%H %% %M%%%S", &tm1); + checkStrptime("foo20bar57qux08 ", "foo %Hbar %M qux%S ", &tm1); + + /* Date */ + checkStrptime("1991-08-25", "%Y-%m-%d", &tm2); + checkStrptime("25.08.91", "%d.%m.%y", &tm2); + checkStrptime("08/25/91", "%D", &tm2); + checkStrptime("21.10.15", "%d.%m.%y", &tm3); + checkStrptime("10.7.56 in 18th", "%d.%m.%y in %C th", &tm4); + + /* Glibc */ + checkStrptime("1856-07-10", "%F", &tm4); + checkStrptime("683078400", "%s", &tm2); + checkStrptimeTz("+0200", 2, 0); + checkStrptimeTz("-0530", -5, -30); + checkStrptimeTz("-06", -6, 0); + + return t_status; +} -- 2.13.7