X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=test%2Fparse-time.c;fp=test%2Fparse-time.c;h=901a4dde8ecbfe65444950a283637f0d830c8e70;hb=5102da65c4c797d2a79eb5384b0e0c47176a31f0;hp=0000000000000000000000000000000000000000;hpb=f8950c6020708b2c530426c777785197411959cb;p=obsolete%2Fnotmuch-old
diff --git a/test/parse-time.c b/test/parse-time.c
new file mode 100644
index 00000000..901a4dde
--- /dev/null
+++ b/test/parse-time.c
@@ -0,0 +1,314 @@
+/*
+ * parse time string - user friendly date and time parser
+ * Copyright © 2012 Jani Nikula
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ * Author: Jani Nikula
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "parse-time-string.h"
+
+#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
+
+static const char *parse_time_error_strings[] = {
+ [PARSE_TIME_OK] = "OK",
+ [PARSE_TIME_ERR] = "ERR",
+ [PARSE_TIME_ERR_LIB] = "LIB",
+ [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET",
+ [PARSE_TIME_ERR_FORMAT] = "FORMAT",
+ [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT",
+ [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT",
+ [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE",
+ [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME",
+ [PARSE_TIME_ERR_KEYWORD] = "KEYWORD",
+};
+
+static const char *
+parse_time_strerror (unsigned int errnum)
+{
+ if (errnum < ARRAY_SIZE (parse_time_error_strings))
+ return parse_time_error_strings[errnum];
+ else
+ return NULL;
+}
+
+/*
+ * concat argv[start]...argv[end - 1], separating them by a single
+ * space, to a malloced string
+ */
+static char *
+concat_args (int start, int end, char *argv[])
+{
+ int i;
+ size_t len = 1;
+ char *p;
+
+ for (i = start; i < end; i++)
+ len += strlen (argv[i]) + 1;
+
+ p = malloc (len);
+ if (!p)
+ return NULL;
+
+ *p = 0;
+
+ for (i = start; i < end; i++) {
+ if (i != start)
+ strcat (p, " ");
+ strcat (p, argv[i]);
+ }
+
+ return p;
+}
+
+#define DEFAULT_FORMAT "%a %b %d %T %z %Y"
+
+static void
+usage (const char *name)
+{
+ printf ("Usage: %s [options ...] []\n\n", name);
+ printf (
+ "Parse and display it in given format. If is\n"
+ "not given, parse each line in stdin according to:\n\n"
+ " [(==>|==_>|==^>|==^^>)] [#]\n\n"
+ "and produce output:\n\n"
+ " (==>|==_>|==^>|==^^>)