]> git.notmuchmail.org Git - notmuch/blob - lib/parse-time-vrp.cc
query: bind queries to database objects
[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
35     /* Require date: prefix in start of the range... */
36     if (STRNCMP_LITERAL (begin.c_str (), PREFIX))
37         return Xapian::BAD_VALUENO;
38
39     /* ...and remove it. */
40     begin.erase (0, sizeof (PREFIX) - 1);
41
42     /* Use the same 'now' for begin and end. */
43     if (time (&now) == (time_t) -1)
44         return Xapian::BAD_VALUENO;
45
46     if (!begin.empty ()) {
47         if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN))
48             return Xapian::BAD_VALUENO;
49
50         begin.assign (Xapian::sortable_serialise ((double) t));
51     }
52
53     if (!end.empty ()) {
54         if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
55             return Xapian::BAD_VALUENO;
56
57         end.assign (Xapian::sortable_serialise ((double) t));
58     }
59
60     return valno;
61 }