drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
temporal_format.cc
Go to the documentation of this file.
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * Authors:
7 *
8 * Jay Pipes <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
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.
19 *
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 USA
23 */
24
30
31#include <config.h>
32
34#include <drizzled/temporal.h>
35
36#include <string.h>
37
38#include <string>
39#include <vector>
40
41using namespace std;
42
43namespace drizzled {
44
45TemporalFormat::TemporalFormat(const char *pattern) :
46 _pattern(pattern)
48, _error(0)
49, _year_part_index(0)
50, _month_part_index(0)
51, _day_part_index(0)
52, _hour_part_index(0)
53, _minute_part_index(0)
54, _second_part_index(0)
55, _usecond_part_index(0)
56, _nsecond_part_index(0)
57{
58 /* Compile our regular expression */
59 _re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(pattern),
60 PCRE2_ZERO_TERMINATED,
61 0,
62 &_error,
64 NULL);
65}
66
67TemporalFormat::~TemporalFormat()
68{
69 if (_re != NULL)
70 pcre2_code_free(_re);
71}
72
73bool TemporalFormat::matches(const char *data, size_t data_len, Temporal *to)
74{
75 if (! is_valid())
76 return false;
77
78 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(_re, NULL);
79 if (match_data == NULL)
80 return false;
81
82 /* Simply check the subject against the compiled regular expression */
83 int32_t result= pcre2_match(_re,
84 reinterpret_cast<PCRE2_SPTR>(data),
85 data_len,
86 0 /* Start at offset 0 of subject...*/,
87 0 /* Default options */,
88 match_data,
89 NULL);
90 if (result < 0)
91 {
92 pcre2_match_data_free(match_data);
93 switch (result)
94 {
95 case PCRE2_ERROR_NOMATCH:
96 return false; /* No match, just return false */
97 default:
98 return false;
99 }
100 return false;
101 }
102
103 int32_t expected_match_count= (_year_part_index > 1 ? 1 : 0)
104 + (_month_part_index > 1 ? 1 : 0)
105 + (_day_part_index > 1 ? 1 : 0)
106 + (_hour_part_index > 1 ? 1 : 0)
107 + (_minute_part_index > 1 ? 1 : 0)
108 + (_second_part_index > 1 ? 1 : 0)
109 + (_usecond_part_index > 1 ? 1 : 0)
110 + (_nsecond_part_index > 1 ? 1 : 0)
111 + 1; /* Add one for the entire match... */
112 if (result != expected_match_count)
113 {
114 pcre2_match_data_free(match_data);
115 return false;
116 }
117
118 /* C++ string class easy to use substr() method is very useful here */
119 string copy_data(data, data_len);
120 PCRE2_SIZE *match_vector= pcre2_get_ovector_pointer(match_data);
121 /*
122 * OK, we have the expected substring matches, so grab
123 * the various temporal parts from the subject string
124 *
125 * @note
126 *
127 * TemporalFormatMatch is a friend class to Temporal, so
128 * we can access the temporal instance's protected data.
129 */
130 if (_year_part_index > 1)
131 {
132 size_t year_start= match_vector[_year_part_index];
133 size_t year_len= match_vector[_year_part_index + 1] - match_vector[_year_part_index];
134 to->_years= atoi(copy_data.substr(year_start, year_len).c_str());
135 if (year_len == 2)
136 to->_years+= (to->_years >= DRIZZLE_YY_PART_YEAR ? 1900 : 2000);
137 }
138 if (_month_part_index > 1)
139 {
140 size_t month_start= match_vector[_month_part_index];
141 size_t month_len= match_vector[_month_part_index + 1] - match_vector[_month_part_index];
142 to->_months= atoi(copy_data.substr(month_start, month_len).c_str());
143 }
144 if (_day_part_index > 1)
145 {
146 size_t day_start= match_vector[_day_part_index];
147 size_t day_len= match_vector[_day_part_index + 1] - match_vector[_day_part_index];
148 to->_days= atoi(copy_data.substr(day_start, day_len).c_str());
149 }
150 if (_hour_part_index > 1)
151 {
152 size_t hour_start= match_vector[_hour_part_index];
153 size_t hour_len= match_vector[_hour_part_index + 1] - match_vector[_hour_part_index];
154 to->_hours= atoi(copy_data.substr(hour_start, hour_len).c_str());
155 }
156 if (_minute_part_index > 1)
157 {
158 size_t minute_start= match_vector[_minute_part_index];
159 size_t minute_len= match_vector[_minute_part_index + 1] - match_vector[_minute_part_index];
160 to->_minutes= atoi(copy_data.substr(minute_start, minute_len).c_str());
161 }
162 if (_second_part_index > 1)
163 {
164 size_t second_start= match_vector[_second_part_index];
165 size_t second_len= match_vector[_second_part_index + 1] - match_vector[_second_part_index];
166 to->_seconds= atoi(copy_data.substr(second_start, second_len).c_str());
167 }
168 if (_usecond_part_index > 1)
169 {
170 size_t usecond_start= match_vector[_usecond_part_index];
171 size_t usecond_len= match_vector[_usecond_part_index + 1] - match_vector[_usecond_part_index];
172 /*
173 * For microseconds, which are millionth of 1 second,
174 * we must ensure that we produce a correct result,
175 * even if < 6 places were specified. For instance, if we get .1,
176 * we must produce 100000. .11 should produce 110000, etc.
177 */
178 uint32_t multiplier= 1;
179 int32_t x= usecond_len;
180 while (x < 6)
181 {
182 multiplier*= 10;
183 ++x;
184 }
185 to->_useconds= atoi(copy_data.substr(usecond_start, usecond_len).c_str()) * multiplier;
186 }
187 if (_nsecond_part_index > 1)
188 {
189 size_t nsecond_start= match_vector[_nsecond_part_index];
190 size_t nsecond_len= match_vector[_nsecond_part_index + 1] - match_vector[_nsecond_part_index];
191 /*
192 * For nanoseconds, which are 1 billionth of a second,
193 * we must ensure that we produce a correct result,
194 * even if < 9 places were specified. For instance, if we get .1,
195 * we must produce 100000000. .11 should produce 110000000, etc.
196 */
197 uint32_t multiplier= 1;
198 int32_t x= nsecond_len;
199 while (x < 9)
200 {
201 multiplier*= 10;
202 ++x;
203 }
204 to->_nseconds= atoi(copy_data.substr(nsecond_start, nsecond_len).c_str()) * multiplier;
205 }
206 pcre2_match_data_free(match_data);
207 return true;
208}
209
210
211#define COUNT_KNOWN_FORMATS 19
212
214{
215 const char *pattern;
216 int32_t year_part_index;
217 int32_t month_part_index;
218 int32_t day_part_index;
219 int32_t hour_part_index;
220 int32_t minute_part_index;
221 int32_t second_part_index;
222 int32_t usecond_part_index;
223 int32_t nsecond_part_index;
224};
225
238static struct temporal_format_args __format_args[COUNT_KNOWN_FORMATS]=
239{
240 {"^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})\\.(\\d{1,6})$", 1, 2, 3, 4, 5, 6, 7, 0} /* YYYYMMDDHHmmSS.uuuuuu */
241, {"^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YYYYMMDDHHmmSS */
242, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{1,6})$", 1, 2, 3, 4, 5, 6, 7, 0} /* YYYY[/-.]MM[/-.]DD[T]HH:mm:SS.uuuuuu */
243, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YYYY[/-.][M]M[/-.][D]D[T]HH:mm:SS */
244, {"^(\\d{2})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YY[/-.][M]M[/-.][D]D HH:mm:SS */
245, {"^(\\d{2})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 0, 0, 0} /* YY[/-.][M]M[/-.][D]D HH:mm */
246, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 0, 0, 0} /* YYYY[/-.][M]M[/-.][D]D HH:mm */
247, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYY-[M]M-[D]D, YYYY.[M]M.[D]D, YYYY/[M]M/[D]D */
248, {"^(\\d{4})(\\d{2})(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYYMMDD */
249, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{4})$", 3, 1, 2, 0, 0, 0, 0, 0} /* MM[-/.]DD[-/.]YYYY (US common format)*/
250, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.]MM[-/.]DD */
251, {"^(\\d{2})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.][M]M[-/.][D]D */
252, {"^(\\d{4})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYY[-/.][M]M[-/.][D]D */
253, {"^(\\d{2}):*(\\d{2}):*(\\d{2})\\.(\\d{1,6})$", 0, 0, 0, 1, 2, 3, 4, 0} /* HHmmSS.uuuuuu, HH:mm:SS.uuuuuu */
254, {"^(\\d{1,2}):*(\\d{2}):*(\\d{2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]HmmSS, [H]H:mm:SS */
255, {"^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]H:[m]m:[S]S */
256, {"^(\\d{1,2}):*(\\d{2})$", 0, 0, 0, 0, 1, 2, 0, 0} /* [m]mSS, [m]m:SS */
257, {"^(\\d{1,2})$", 0, 0, 0, 0, 0, 1, 0, 0} /* SS, S */
258, {"^(\\d{1,2})\\.(\\d{1,6})$", 0, 0, 0, 0, 0, 1, 2, 0} /* [S]S.uuuuuu */
259};
260
261vector<TemporalFormat *> known_datetime_formats;
262vector<TemporalFormat *> known_date_formats;
263vector<TemporalFormat *> known_time_formats;
264vector<TemporalFormat *> all_temporal_formats;
265
272{
273 /* Compile all the regular expressions for the datetime formats */
274 TemporalFormat *tmp;
275 struct temporal_format_args current_format_args;
276
277 for (int32_t x= 0; x < COUNT_KNOWN_FORMATS; ++x)
278 {
279 current_format_args= __format_args[x];
280 tmp= new TemporalFormat(current_format_args.pattern);
281 tmp->set_year_part_index(current_format_args.year_part_index);
282 tmp->set_month_part_index(current_format_args.month_part_index);
283 tmp->set_day_part_index(current_format_args.day_part_index);
284 tmp->set_hour_part_index(current_format_args.hour_part_index);
285 tmp->set_minute_part_index(current_format_args.minute_part_index);
286 tmp->set_second_part_index(current_format_args.second_part_index);
287 tmp->set_usecond_part_index(current_format_args.usecond_part_index);
288 tmp->set_nsecond_part_index(current_format_args.nsecond_part_index);
289
290 /*
291 * We store the pointer in all_temporal_formats because we
292 * delete pointers from that vector and only that vector
293 */
294 all_temporal_formats.push_back(tmp);
295
296 if (current_format_args.year_part_index > 0) /* A date must have a year */
297 {
298 known_datetime_formats.push_back(tmp);
299 if (current_format_args.second_part_index == 0) /* A time must have seconds. */
300 known_date_formats.push_back(tmp);
301 }
302
303 if (current_format_args.second_part_index > 0) /* A time must have seconds, but may not have minutes or hours */
304 known_time_formats.push_back(tmp);
305 }
306 return true;
307}
308
311{
312 for (TemporalFormat* it : all_temporal_formats)
313 delete it;
314 known_date_formats.clear();
315 known_datetime_formats.clear();
316 known_time_formats.clear();
317 all_temporal_formats.clear();
318}
319
320} /* end namespace drizzled */
void set_year_part_index(int32_t index)
TemporalFormat(const char *pattern)
void set_usecond_part_index(int32_t index)
void set_hour_part_index(int32_t index)
void set_day_part_index(int32_t index)
void set_minute_part_index(int32_t index)
bool matches(const char *data, size_t data_len, Temporal *to)
void set_nsecond_part_index(int32_t index)
void set_month_part_index(int32_t index)
void set_second_part_index(int32_t index)
TODO: Rename this file - func.h is stupid.
static struct temporal_format_args __format_args[COUNT_KNOWN_FORMATS]
bool init_temporal_formats()
void deinit_temporal_formats()
STL namespace.