1 /* parse-time-vrp.cc - date range query glue
3 * This file is part of notmuch.
5 * Copyright © 2012 Jani Nikula
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see https://www.gnu.org/licenses/ .
20 * Author: Jani Nikula <jani@nikula.org>
23 #include "database-private.h"
24 #include "parse-time-vrp.h"
25 #include "parse-time-string.h"
28 ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end)
30 double from = DBL_MIN, to = DBL_MAX;
31 time_t parsed_time, now;
34 /* Use the same 'now' for begin and end. */
35 if (time (&now) == (time_t) -1)
36 throw Xapian::QueryParserError ("unable to get current time");
38 if (! begin.empty ()) {
39 if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN))
40 throw Xapian::QueryParserError ("Didn't understand date specification '" + begin + "'");
42 from = (double) parsed_time;
46 if (end == "!" && ! begin.empty ())
51 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
52 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
54 to = (double) parsed_time;
57 return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
58 Xapian::sortable_serialise (from),
59 Xapian::sortable_serialise (to));
62 /* XXX TODO: is throwing an exception the right thing to do here? */
64 DateFieldProcessor::operator() (const std::string & str)
66 double from = DBL_MIN, to = DBL_MAX;
67 time_t parsed_time, now;
69 /* Use the same 'now' for begin and end. */
70 if (time (&now) == (time_t) -1)
71 throw Xapian::QueryParserError ("Unable to get current time");
73 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN))
74 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
76 from = (double) parsed_time;
78 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
79 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
81 to = (double) parsed_time;
83 return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
84 Xapian::sortable_serialise (from),
85 Xapian::sortable_serialise (to));