]> git.notmuchmail.org Git - notmuch/blob - lib/parse-time-vrp.cc
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / parse-time-vrp.cc
1 /* parse-time-vrp.cc - date range query glue
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2012 Jani Nikula
6  *
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.
11  *
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.
16  *
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/ .
19  *
20  * Author: Jani Nikula <jani@nikula.org>
21  */
22
23 #include "database-private.h"
24 #include "parse-time-vrp.h"
25 #include "parse-time-string.h"
26
27 notmuch_status_t
28 _notmuch_date_strings_to_query (Xapian::valueno slot,
29                                 const std::string &begin, const std::string &end,
30                                 Xapian::Query &output, std::string &msg)
31 {
32     double from = DBL_MIN, to = DBL_MAX;
33     time_t parsed_time, now;
34     std::string str;
35
36     /* Use the same 'now' for begin and end. */
37     if (time (&now) == (time_t) -1) {
38         msg = "unable to get current time";
39         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
40     }
41
42     if (! begin.empty ()) {
43         if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) {
44             msg = "Didn't understand date specification '" + begin + "'";
45             return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
46         }
47
48         from = (double) parsed_time;
49     }
50
51     if (! end.empty ()) {
52         if (end == "!" && ! begin.empty ())
53             str = begin;
54         else
55             str = end;
56
57         if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) {
58             msg = "Didn't understand date specification '" + str + "'";
59             return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
60         }
61         to = (double) parsed_time;
62     }
63
64     output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
65                             Xapian::sortable_serialise (from),
66                             Xapian::sortable_serialise (to));
67     return NOTMUCH_STATUS_SUCCESS;
68 }
69
70 Xapian::Query
71 ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end)
72 {
73
74     Xapian::Query output;
75     std::string msg;
76
77     if (_notmuch_date_strings_to_query (slot, begin, end, output, msg))
78         throw Xapian::QueryParserError (msg);
79
80     return output;
81 }
82
83 /* XXX TODO: is throwing an exception the right thing to do here? */
84 Xapian::Query
85 DateFieldProcessor::operator() (const std::string & str)
86 {
87     double from = DBL_MIN, to = DBL_MAX;
88     time_t parsed_time, now;
89
90     /* Use the same 'now' for begin and end. */
91     if (time (&now) == (time_t) -1)
92         throw Xapian::QueryParserError ("Unable to get current time");
93
94     if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN))
95         throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
96     else
97         from = (double) parsed_time;
98
99     if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
100         throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
101     else
102         to = (double) parsed_time;
103
104     return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
105                           Xapian::sortable_serialise (from),
106                           Xapian::sortable_serialise (to));
107 }