Fix typo in WSA error code
[xapian.git] / xapian-applications / omega / date.cc
blob3bf2a07dff448a7c2bc95fe81d5fb8fdd562245e
1 /** @file
2 * @brief date range parsing routines for omega
3 */
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2001 James Aylett
6 * Copyright 2001,2002 Ananova Ltd
7 * Copyright 2002 Intercede 1749 Ltd
8 * Copyright 2002,2003,2006,2014,2016,2017,2018 Olly Betts
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 * USA
26 #include <config.h>
28 #include "date.h"
30 #include <vector>
32 #include <cstdlib>
33 #include <ctime>
35 using namespace std;
37 static int
38 last_day(int y, int m)
40 static const int l[13] = {
41 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
43 if (m != 2) return l[m];
44 return 28 + (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
47 // Write exactly w chars to buffer p representing integer v.
49 // The result is left padded with zeros if v < pow(10, w - 1).
51 // If v >= pow(10, w), then the output will show v % pow(10, w) (i.e. the
52 // most significant digits are lost).
53 static void
54 format_int_fixed_width(char * p, int v, int w)
56 while (--w >= 0) {
57 p[w] = '0' + (v % 10);
58 v /= 10;
62 static Xapian::Query
63 date_range_filter(int y1, int m1, int d1, int y2, int m2, int d2)
65 if (y1 > y2 || (y1 == y2 && (m1 > m2 || (m1 == m2 && d1 > d2)))) {
66 // Start is after end.
67 return Xapian::Query::MatchNothing;
69 char buf[10];
70 format_int_fixed_width(buf + 1, y1, 4);
71 format_int_fixed_width(buf + 5, m1, 2);
72 vector<Xapian::Query> v;
74 int d_last = last_day(y1, m1);
75 int d_end = d_last;
76 if (d1 == 1 && m1 == 1 && y1 != y2) {
77 --y1;
78 goto whole_year_at_start;
80 if (y1 == y2 && m1 == m2 && d2 < d_last) {
81 d_end = d2;
83 // Deal with any initial partial month
84 if (d1 > 1 || d_end < d_last) {
85 buf[0] = 'D';
86 for ( ; d1 <= d_end; ++d1) {
87 format_int_fixed_width(buf + 7, d1, 2);
88 v.push_back(Xapian::Query(string(buf, 9)));
90 } else {
91 buf[0] = 'M';
92 v.push_back(Xapian::Query(string(buf, 7)));
95 if (y1 == y2 && m1 == m2) {
96 return Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
100 int m_last = (y1 < y2) ? 12 : m2 - 1;
101 while (++m1 <= m_last) {
102 format_int_fixed_width(buf + 5, m1, 2);
103 buf[0] = 'M';
104 v.push_back(Xapian::Query(string(buf, 7)));
108 if (y1 < y2) {
109 whole_year_at_start:
110 while (++y1 < y2) {
111 format_int_fixed_width(buf + 1, y1, 4);
112 buf[0] = 'Y';
113 v.push_back(Xapian::Query(string(buf, 5)));
115 format_int_fixed_width(buf + 1, y2, 4);
116 if (m2 == 12 && d2 >= 31) {
117 v.push_back(Xapian::Query(string(buf, 5)));
118 goto whole_year_at_end;
120 buf[0] = 'M';
121 for (m1 = 1; m1 < m2; ++m1) {
122 format_int_fixed_width(buf + 5, m1, 2);
123 v.push_back(Xapian::Query(string(buf, 7)));
127 format_int_fixed_width(buf + 5, m2, 2);
129 // Deal with any final partial month
130 if (d2 < last_day(y2, m2)) {
131 buf[0] = 'D';
132 for (d1 = 1; d1 <= d2; ++d1) {
133 format_int_fixed_width(buf + 7, d1, 2);
134 v.push_back(Xapian::Query(string(buf, 9)));
136 } else {
137 buf[0] = 'M';
138 v.push_back(Xapian::Query(string(buf, 7)));
141 whole_year_at_end:
142 return Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
145 static int DIGIT(char ch) { return ch - '0'; }
147 static int DIGIT2(const char *p) {
148 return DIGIT(p[0]) * 10 + DIGIT(p[1]);
151 static int DIGIT4(const char *p) {
152 return DIGIT2(p) * 100 + DIGIT2(p + 2);
155 static void
156 parse_date(const string & date, int *y, int *m, int *d, bool start)
158 // Support YYYYMMDD, YYYYMM and YYYY.
159 if (date.size() < 4) {
160 // We default to the start of 1970 when START isn't specified, so it
161 // seems logical to here do that here too.
162 *y = 1970;
163 } else {
164 *y = DIGIT4(date.c_str());
166 if (date.size() < 6) {
167 if (start) {
168 *m = 1;
169 *d = 1;
170 } else {
171 *m = 12;
172 *d = 31;
174 return;
176 *m = DIGIT2(date.c_str() + 4);
177 if (date.size() < 8) {
178 if (start) {
179 *d = 1;
180 } else {
181 *d = last_day(*y, *m);
183 return;
185 *d = DIGIT2(date.c_str() + 6);
188 Xapian::Query
189 date_range_filter(const string & date_start, const string & date_end,
190 const string & date_span)
192 int y1, m1, d1, y2, m2, d2;
193 if (!date_span.empty()) {
194 time_t secs = atoi(date_span.c_str()) * (24 * 60 * 60);
195 if (!date_end.empty()) {
196 parse_date(date_end, &y2, &m2, &d2, false);
197 struct tm t;
198 t.tm_year = y2 - 1900;
199 t.tm_mon = m2 - 1;
200 t.tm_mday = d2;
201 t.tm_hour = 12;
202 t.tm_min = t.tm_sec = 0;
203 t.tm_isdst = -1;
204 time_t then = mktime(&t) - secs;
205 struct tm *t2 = localtime(&then);
206 y1 = t2->tm_year + 1900;
207 m1 = t2->tm_mon + 1;
208 d1 = t2->tm_mday;
209 } else if (!date_start.empty()) {
210 parse_date(date_start, &y1, &m1, &d1, true);
211 struct tm t;
212 t.tm_year = y1 - 1900;
213 t.tm_mon = m1 - 1;
214 t.tm_mday = d1;
215 t.tm_hour = 12;
216 t.tm_min = t.tm_sec = 0;
217 t.tm_isdst = -1;
218 time_t end = mktime(&t) + secs;
219 struct tm *t2 = localtime(&end);
220 y2 = t2->tm_year + 1900;
221 m2 = t2->tm_mon + 1;
222 d2 = t2->tm_mday;
223 } else {
224 time_t end = time(NULL);
225 struct tm *t = localtime(&end);
226 y2 = t->tm_year + 1900;
227 m2 = t->tm_mon + 1;
228 d2 = t->tm_mday;
229 time_t then = end - secs;
230 struct tm *t2 = localtime(&then);
231 y1 = t2->tm_year + 1900;
232 m1 = t2->tm_mon + 1;
233 d1 = t2->tm_mday;
235 } else {
236 if (date_start.empty()) {
237 y1 = 1970;
238 m1 = 1;
239 d1 = 1;
240 } else {
241 parse_date(date_start, &y1, &m1, &d1, true);
243 if (date_end.empty()) {
244 time_t now = time(NULL);
245 struct tm *t = localtime(&now);
246 y2 = t->tm_year + 1900;
247 m2 = t->tm_mon + 1;
248 d2 = t->tm_mday;
249 } else {
250 parse_date(date_end, &y2, &m2, &d2, false);
253 return date_range_filter(y1, m1, d1, y2, m2, d2);