]> git.notmuchmail.org Git - notmuch/blob - lib/parse-time-vrp.cc
lib: use LOG_XAPIAN_EXCEPTION in n_m_get_date
[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 Xapian::Query
28 ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end)
29 {
30     double from = DBL_MIN, to = DBL_MAX;
31     time_t parsed_time, now;
32     std::string str;
33
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");
37
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 + "'");
41         else
42             from = (double) parsed_time;
43     }
44
45     if (!end.empty ()) {
46         if (end == "!" && ! begin.empty ())
47             str = begin;
48         else
49             str = end;
50
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 + "'");
53         else
54             to = (double) parsed_time;
55     }
56
57     return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
58                           Xapian::sortable_serialise (from),
59                           Xapian::sortable_serialise (to));
60 }
61
62 /* XXX TODO: is throwing an exception the right thing to do here? */
63 Xapian::Query
64 DateFieldProcessor::operator() (const std::string & str)
65 {
66     double from = DBL_MIN, to = DBL_MAX;
67     time_t parsed_time, now;
68
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");
72
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 + "'");
75     else
76         from = (double) parsed_time;
77
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 + "'");
80     else
81         to = (double) parsed_time;
82
83     return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
84                           Xapian::sortable_serialise (from),
85                           Xapian::sortable_serialise (to));
86 }