]> git.notmuchmail.org Git - notmuch/blob - lib/parse-time-vrp.cc
lib: add support for date:<expr>..! to mean date:<expr>..<expr>
[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 http://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 #define PREFIX "date:"
28
29 /* See *ValueRangeProcessor in xapian-core/api/valuerangeproc.cc */
30 Xapian::valueno
31 ParseTimeValueRangeProcessor::operator() (std::string &begin, std::string &end)
32 {
33     time_t t, now;
34     std::string b;
35
36     /* Require date: prefix in start of the range... */
37     if (STRNCMP_LITERAL (begin.c_str (), PREFIX))
38         return Xapian::BAD_VALUENO;
39
40     /* ...and remove it. */
41     begin.erase (0, sizeof (PREFIX) - 1);
42     b = begin;
43
44     /* Use the same 'now' for begin and end. */
45     if (time (&now) == (time_t) -1)
46         return Xapian::BAD_VALUENO;
47
48     if (!begin.empty ()) {
49         if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN))
50             return Xapian::BAD_VALUENO;
51
52         begin.assign (Xapian::sortable_serialise ((double) t));
53     }
54
55     if (!end.empty ()) {
56         if (end == "!" && ! b.empty ())
57             end = b;
58
59         if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
60             return Xapian::BAD_VALUENO;
61
62         end.assign (Xapian::sortable_serialise ((double) t));
63     }
64
65     return valno;
66 }