--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 9E5C6431FD9\r
+ for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:33:15 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 8PnWgvqyyK22 for <notmuch@notmuchmail.org>;\r
+ Tue, 30 Oct 2012 13:33:12 -0700 (PDT)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+ [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 81F8C431FDC\r
+ for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)\r
+Received: by mail-lb0-f181.google.com with SMTP id gg6so567600lbb.26\r
+ for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :in-reply-to:references:mime-version:content-type\r
+ :content-transfer-encoding:x-gm-message-state;\r
+ bh=vMILaKO6smR7vl2i4sM9jBz5pOCabcqzPmPfd6V5VVo=;\r
+ b=RHK+BGp5teXvSQFmYinkRREeL/kiCl5w7i6W4rmmlLSi4dNnbYs5mqLa4n1WV4tnE+\r
+ a2XPwhExvdsQIrelXS+yH69QIIDQs1MerHUE+U24ZomDINw3E2MLDC19EAKlzkG33+rm\r
+ sEDQA/HKurUdAHuFu23Qpe+z0XdLPICsADhOD3GtRHOlhflfaXLD+ya0JVLtOcmf1JEL\r
+ wGF1494vKs2jLH3QpBT/pYYiX+6Z7eYrPLhxZCRW2kigT2hgDV25xmbIdDnXFuoQBZpd\r
+ NfuTwkNqcE2ZfpIabVG3ly6G1Ka8BuN1G+e9VBcKVBUBp4G9XjXKjLG+3hPFgrZRjuTn\r
+ 2Nww==\r
+Received: by 10.152.104.148 with SMTP id ge20mr30985253lab.51.1351629175980;\r
+ Tue, 30 Oct 2012 13:32:55 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
+ [80.223.81.27])\r
+ by mx.google.com with ESMTPS id jk8sm665831lab.7.2012.10.30.13.32.54\r
+ (version=SSLv3 cipher=OTHER); Tue, 30 Oct 2012 13:32:55 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject:\r
+ [PATCH v6 3/9] test: add new test tool parse-time for date/time parser\r
+Date: Tue, 30 Oct 2012 22:32:34 +0200\r
+Message-Id:\r
+ <fe26c805b9e26bba9d93f705539faf13c12eb019.1351626272.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <cover.1351626272.git.jani@nikula.org>\r
+References: <cover.1351626272.git.jani@nikula.org>\r
+In-Reply-To: <cover.1351626272.git.jani@nikula.org>\r
+References: <cover.1351626272.git.jani@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-Gm-Message-State:\r
+ ALoCoQlbgj4AxEGxVu2A+zxoD7Paw61REWkLR5YurUqXGNjrRYj/NYbWbgSjqzT2UJ7KHYgsEViN\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 30 Oct 2012 20:33:16 -0000\r
+\r
+Add a smoke testing tool to support testing the date/time parser\r
+module directly and independent of the rest of notmuch.\r
+\r
+Credits to Michal Sojka <sojkam1@fel.cvut.cz> for the stdin parsing\r
+idea and consequent massive improvement in testability.\r
+---\r
+ test/Makefile.local | 7 +-\r
+ test/basic | 2 +-\r
+ test/parse-time.c | 314 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 321 insertions(+), 2 deletions(-)\r
+ create mode 100644 test/parse-time.c\r
+\r
+diff --git a/test/Makefile.local b/test/Makefile.local\r
+index 45df4c7..9ae130a 100644\r
+--- a/test/Makefile.local\r
++++ b/test/Makefile.local\r
+@@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
+ $(dir)/symbol-test: $(dir)/symbol-test.o\r
+ $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS)\r
+ \r
++$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
++ $(call quiet,CC) $^ -o $@\r
++\r
+ .PHONY: test check\r
+ \r
+-test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test\r
++test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \\r
++ $(dir)/parse-time\r
+ \r
+ test: all test-binaries\r
+ @${dir}/notmuch-test $(OPTIONS)\r
+@@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)\r
+ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
+ $(dir)/symbol-test $(dir)/symbol-test.o \\r
+ $(dir)/arg-test $(dir)/arg-test.o \\r
++ $(dir)/parse-time $(dir)/parse-time.o \\r
+ $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
+diff --git a/test/basic b/test/basic\r
+index 3b635c8..c47197c 100755\r
+--- a/test/basic\r
++++ b/test/basic\r
+@@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
+ eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
+ tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
+ available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \\r
+- sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \\r
++ sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \\r
+ sort)\r
+ test_expect_equal "$tests_in_suite" "$available"\r
+ \r
+diff --git a/test/parse-time.c b/test/parse-time.c\r
+new file mode 100644\r
+index 0000000..901a4dd\r
+--- /dev/null\r
++++ b/test/parse-time.c\r
+@@ -0,0 +1,314 @@\r
++/*\r
++ * parse time string - user friendly date and time parser\r
++ * Copyright © 2012 Jani Nikula\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 2 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
++ *\r
++ * Author: Jani Nikula <jani@nikula.org>\r
++ */\r
++\r
++#include <assert.h>\r
++#include <ctype.h>\r
++#include <getopt.h>\r
++#include <stdio.h>\r
++#include <stdlib.h>\r
++#include <string.h>\r
++\r
++#include "parse-time-string.h"\r
++\r
++#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))\r
++\r
++static const char *parse_time_error_strings[] = {\r
++ [PARSE_TIME_OK] = "OK",\r
++ [PARSE_TIME_ERR] = "ERR",\r
++ [PARSE_TIME_ERR_LIB] = "LIB",\r
++ [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET",\r
++ [PARSE_TIME_ERR_FORMAT] = "FORMAT",\r
++ [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT",\r
++ [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT",\r
++ [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE",\r
++ [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME",\r
++ [PARSE_TIME_ERR_KEYWORD] = "KEYWORD",\r
++};\r
++\r
++static const char *\r
++parse_time_strerror (unsigned int errnum)\r
++{\r
++ if (errnum < ARRAY_SIZE (parse_time_error_strings))\r
++ return parse_time_error_strings[errnum];\r
++ else\r
++ return NULL;\r
++}\r
++\r
++/*\r
++ * concat argv[start]...argv[end - 1], separating them by a single\r
++ * space, to a malloced string\r
++ */\r
++static char *\r
++concat_args (int start, int end, char *argv[])\r
++{\r
++ int i;\r
++ size_t len = 1;\r
++ char *p;\r
++\r
++ for (i = start; i < end; i++)\r
++ len += strlen (argv[i]) + 1;\r
++\r
++ p = malloc (len);\r
++ if (!p)\r
++ return NULL;\r
++\r
++ *p = 0;\r
++\r
++ for (i = start; i < end; i++) {\r
++ if (i != start)\r
++ strcat (p, " ");\r
++ strcat (p, argv[i]);\r
++ }\r
++\r
++ return p;\r
++}\r
++\r
++#define DEFAULT_FORMAT "%a %b %d %T %z %Y"\r
++\r
++static void\r
++usage (const char *name)\r
++{\r
++ printf ("Usage: %s [options ...] [<date/time>]\n\n", name);\r
++ printf (\r
++ "Parse <date/time> and display it in given format. If <date/time> is\n"\r
++ "not given, parse each line in stdin according to:\n\n"\r
++ " <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"\r
++ "and produce output:\n\n"\r
++ " <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"\r
++ "preserving whitespace and comment in input. The operators ==>, ==_>,\n"\r
++ "==^>, and ==^^> define rounding as no rounding, round down, round up\n"\r
++ "inclusive, and round up, respectively.\n\n"\r
++\r
++ " -f, --format=FMT output format, FMT according to strftime(3)\n"\r
++ " (default: \"%s\")\n"\r
++ " -r, --ref=N use N seconds since epoch as reference time\n"\r
++ " (default: now)\n"\r
++ " -u, --^ round result up inclusive (default: no rounding)\n"\r
++ " -U, --^^ round result up (default: no rounding)\n"\r
++ " -d, --_ round result down (default: no rounding)\n"\r
++ " -h, --help print this help\n",\r
++ DEFAULT_FORMAT);\r
++}\r
++\r
++struct {\r
++ const char *operator;\r
++ int round;\r
++} operators[] = {\r
++ { "==>", PARSE_TIME_NO_ROUND },\r
++ { "==_>", PARSE_TIME_ROUND_DOWN },\r
++ { "==^>", PARSE_TIME_ROUND_UP_INCLUSIVE },\r
++ { "==^^>", PARSE_TIME_ROUND_UP },\r
++};\r
++\r
++static const char *\r
++find_operator_in_string (char *str, char **ptr, int *round)\r
++{\r
++ const char *oper = NULL;\r
++ unsigned int i;\r
++\r
++ for (i = 0; i < ARRAY_SIZE (operators); i++) {\r
++ char *p = strstr (str, operators[i].operator);\r
++ if (p) {\r
++ if (round)\r
++ *round = operators[i].round;\r
++ if (ptr)\r
++ *ptr = p;\r
++\r
++ oper = operators[i].operator;\r
++ break;\r
++ }\r
++ }\r
++\r
++ return oper;\r
++}\r
++\r
++static const char *\r
++get_operator (int round)\r
++{\r
++ const char *oper = NULL;\r
++ unsigned int i;\r
++\r
++ for (i = 0; i < ARRAY_SIZE(operators); i++) {\r
++ if (round == operators[i].round) {\r
++ oper = operators[i].operator;\r
++ break;\r
++ }\r
++ }\r
++\r
++ return oper;\r
++}\r
++\r
++static int\r
++parse_stdin (FILE *infile, time_t *ref, int round, const char *format)\r
++{\r
++ char *input = NULL;\r
++ char result[1024];\r
++ size_t inputsize;\r
++ ssize_t len;\r
++ struct tm tm;\r
++ time_t t;\r
++ int r;\r
++\r
++ while ((len = getline (&input, &inputsize, infile)) != -1) {\r
++ const char *oper;\r
++ char *trail, *tmp;\r
++\r
++ /* trail is trailing whitespace and (optional) comment */\r
++ trail = strchr (input, '#');\r
++ if (!trail)\r
++ trail = input + len;\r
++\r
++ while (trail > input && isspace ((unsigned char) *(trail-1)))\r
++ trail--;\r
++\r
++ if (trail == input) {\r
++ printf ("%s", input);\r
++ continue;\r
++ }\r
++\r
++ tmp = strdup (trail);\r
++ if (!tmp) {\r
++ fprintf (stderr, "strdup() failed\n");\r
++ continue;\r
++ }\r
++ *trail = '\0';\r
++ trail = tmp;\r
++\r
++ /* operator */\r
++ oper = find_operator_in_string (input, &tmp, &round);\r
++ if (oper) {\r
++ *tmp = '\0';\r
++ } else {\r
++ oper = get_operator (round);\r
++ assert (oper);\r
++ }\r
++\r
++ r = parse_time_string (input, &t, ref, round);\r
++ if (!r) {\r
++ if (!localtime_r (&t, &tm)) {\r
++ fprintf (stderr, "localtime_r() failed\n");\r
++ free (trail);\r
++ continue;\r
++ }\r
++\r
++ strftime (result, sizeof (result), format, &tm);\r
++ } else {\r
++ const char *errstr = parse_time_strerror (r);\r
++ if (errstr)\r
++ snprintf (result, sizeof (result), "ERROR: %s", errstr);\r
++ else\r
++ snprintf (result, sizeof (result), "ERROR: %d", r);\r
++ }\r
++\r
++ printf ("%s%s %s%s", input, oper, result, trail);\r
++ free (trail);\r
++ }\r
++\r
++ free (input);\r
++\r
++ return 0;\r
++}\r
++\r
++int\r
++main (int argc, char *argv[])\r
++{\r
++ int r;\r
++ struct tm tm;\r
++ time_t result;\r
++ time_t now;\r
++ time_t *nowp = NULL;\r
++ char *argstr;\r
++ int round = PARSE_TIME_NO_ROUND;\r
++ char buf[1024];\r
++ const char *format = DEFAULT_FORMAT;\r
++ struct option options[] = {\r
++ { "help", no_argument, NULL, 'h' },\r
++ { "^", no_argument, NULL, 'u' },\r
++ { "^^", no_argument, NULL, 'U' },\r
++ { "_", no_argument, NULL, 'd' },\r
++ { "format", required_argument, NULL, 'f' },\r
++ { "ref", required_argument, NULL, 'r' },\r
++ { NULL, 0, NULL, 0 },\r
++ };\r
++\r
++ for (;;) {\r
++ int c;\r
++\r
++ c = getopt_long (argc, argv, "huUdf:r:", options, NULL);\r
++ if (c == -1)\r
++ break;\r
++\r
++ switch (c) {\r
++ case 'f':\r
++ /* output format */\r
++ format = optarg;\r
++ break;\r
++ case 'u':\r
++ round = PARSE_TIME_ROUND_UP_INCLUSIVE;\r
++ break;\r
++ case 'U':\r
++ round = PARSE_TIME_ROUND_UP;\r
++ break;\r
++ case 'd':\r
++ round = PARSE_TIME_ROUND_DOWN;\r
++ break;\r
++ case 'r':\r
++ /* specify now in seconds since epoch */\r
++ now = (time_t) strtol (optarg, NULL, 10);\r
++ if (now >= (time_t) 0)\r
++ nowp = &now;\r
++ break;\r
++ case 'h':\r
++ case '?':\r
++ default:\r
++ usage (argv[0]);\r
++ return 1;\r
++ }\r
++ }\r
++\r
++ if (optind == argc)\r
++ return parse_stdin (stdin, nowp, round, format);\r
++\r
++ argstr = concat_args (optind, argc, argv);\r
++ if (!argstr)\r
++ return 1;\r
++\r
++ r = parse_time_string (argstr, &result, nowp, round);\r
++\r
++ free (argstr);\r
++\r
++ if (r) {\r
++ const char *errstr = parse_time_strerror (r);\r
++ if (errstr)\r
++ fprintf (stderr, "ERROR: %s\n", errstr);\r
++ else\r
++ fprintf (stderr, "ERROR: %d\n", r);\r
++\r
++ return r;\r
++ }\r
++\r
++ if (!localtime_r (&result, &tm))\r
++ return 1;\r
++\r
++ strftime (buf, sizeof (buf), format, &tm);\r
++ printf ("%s\n", buf);\r
++\r
++ return 0;\r
++}\r
+-- \r
+1.7.10.4\r
+\r