From: "Rafał Miłecki" <zajec5@gmail.com>
To: musl@lists.openwall.com
Cc: "Rafał Miłecki" <rafal@milecki.pl>
Subject: [PATCH libc-test] add strptime basic test
Date: Thu, 15 Nov 2018 08:34:56 +0100 [thread overview]
Message-ID: <20181115073456.11105-1-zajec5@gmail.com> (raw)
From: Rafał Miłecki <rafal@milecki.pl>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
src/functional/strptime.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 119 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..5f15058
--- /dev/null
+++ b/src/functional/strptime.c
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: MIT
+
+#define _GNU_SOURCE /* For tm_gmtoff */
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include "test.h"
+
+static void checkStrptime(const char *s, const char *format, const struct tm *expected) {
+ size_t n = offsetof(struct tm, tm_isdst);
+ 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 (memcmp(&tm, expected, n)) {
+ char buf1[64];
+ char buf2[64];
+
+ strftime(buf1, sizeof(buf1), "%FT%H:%M:%S%Z (day %j: %a)", expected);
+ strftime(buf2, sizeof(buf2), "%FT%H:%M:%S%Z (day %j: %a)", &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,
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 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,
+ .tm_wday = 0,
+ .tm_yday = 237 - 1,
+ .tm_isdst = 0,
+};
+
+static struct tm tm3 = {
+ .tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 21,
+ .tm_mon = 10 - 1,
+ .tm_year = 2015 - 1900,
+ .tm_wday = 3,
+ .tm_yday = 294 - 1,
+ .tm_isdst = 0,
+};
+
+static struct tm tm4 = {
+ .tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 10,
+ .tm_mon = 7 - 1,
+ .tm_year = 1856 - 1900,
+ .tm_wday = 4,
+ .tm_yday = 192 - 1,
+ .tm_isdst = 0,
+};
+
+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
next reply other threads:[~2018-11-15 7:34 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-15 7:34 Rafał Miłecki [this message]
2018-11-15 10:12 ` Rafał Miłecki
2018-11-15 21:26 ` Szabolcs Nagy
2018-11-15 21:31 ` Rafał Miłecki
2018-11-16 21:36 ` Rich Felker
2018-11-16 21:21 ` Bartosz Brachaczek
2018-11-16 21:34 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181115073456.11105-1-zajec5@gmail.com \
--to=zajec5@gmail.com \
--cc=musl@lists.openwall.com \
--cc=rafal@milecki.pl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).