1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
8
* Jay Pipes <jay.pipes@sun.com>
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.
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 USA
28
* Implementation of the server's date and time string matching utility.
31
#include "drizzled/global.h"
33
#include "drizzled/temporal_format.h"
34
#include "drizzled/temporal.h"
36
#include <string> /** C++ string class used */
44
TemporalFormat::TemporalFormat(const char *pattern)
50
, _month_part_index(0)
53
, _minute_part_index(0)
54
, _second_part_index(0)
55
, _usecond_part_index(0)
56
, _nsecond_part_index(0)
58
/* Make sure we've got no junk in the match_vector. */
59
memset(_match_vector, 0, sizeof(_match_vector));
61
/* Compile our regular expression */
62
_re= pcre_compile(pattern
63
, 0 /* Default options */
66
, NULL /* Use default character table */
70
bool TemporalFormat::matches(const char *data, size_t data_len, Temporal *to)
75
/* Simply check the subject against the compiled regular expression */
76
int32_t result= pcre_exec(_re
77
, NULL /* No extra data */
80
, 0 /* Start at offset 0 of subject...*/
81
, 0 /* Default options */
89
case PCRE_ERROR_NOMATCH:
90
return false; /* No match, just return false */
97
int32_t expected_match_count= (_year_part_index > 1 ? 1 : 0)
98
+ (_month_part_index > 1 ? 1 : 0)
99
+ (_day_part_index > 1 ? 1 : 0)
100
+ (_hour_part_index > 1 ? 1 : 0)
101
+ (_minute_part_index > 1 ? 1 : 0)
102
+ (_second_part_index > 1 ? 1 : 0)
103
+ (_usecond_part_index > 1 ? 1 : 0)
104
+ (_nsecond_part_index > 1 ? 1 : 0)
105
+ 1; /* Add one for the entire match... */
106
if (result != expected_match_count)
109
/* C++ string class easy to use substr() method is very useful here */
110
std::string copy_data(data, data_len);
112
* OK, we have the expected substring matches, so grab
113
* the various temporal parts from the subject string
117
* TemporalFormatMatch is a friend class to Temporal, so
118
* we can access the temporal instance's protected data.
120
if (_year_part_index > 1)
122
size_t year_start= _match_vector[_year_part_index];
123
size_t year_len= _match_vector[_year_part_index + 1] - _match_vector[_year_part_index];
124
to->_years= atoi(copy_data.substr(year_start, year_len).c_str());
126
to->_years+= (to->_years >= DRIZZLE_YY_PART_YEAR ? 1900 : 2000);
128
if (_month_part_index > 1)
130
size_t month_start= _match_vector[_month_part_index];
131
size_t month_len= _match_vector[_month_part_index + 1] - _match_vector[_month_part_index];
132
to->_months= atoi(copy_data.substr(month_start, month_len).c_str());
134
if (_day_part_index > 1)
136
size_t day_start= _match_vector[_day_part_index];
137
size_t day_len= _match_vector[_day_part_index + 1] - _match_vector[_day_part_index];
138
to->_days= atoi(copy_data.substr(day_start, day_len).c_str());
140
if (_hour_part_index > 1)
142
size_t hour_start= _match_vector[_hour_part_index];
143
size_t hour_len= _match_vector[_hour_part_index + 1] - _match_vector[_hour_part_index];
144
to->_hours= atoi(copy_data.substr(hour_start, hour_len).c_str());
146
if (_minute_part_index > 1)
148
size_t minute_start= _match_vector[_minute_part_index];
149
size_t minute_len= _match_vector[_minute_part_index + 1] - _match_vector[_minute_part_index];
150
to->_minutes= atoi(copy_data.substr(minute_start, minute_len).c_str());
152
if (_second_part_index > 1)
154
size_t second_start= _match_vector[_second_part_index];
155
size_t second_len= _match_vector[_second_part_index + 1] - _match_vector[_second_part_index];
156
to->_seconds= atoi(copy_data.substr(second_start, second_len).c_str());
158
if (_usecond_part_index > 1)
160
size_t usecond_start= _match_vector[_usecond_part_index];
161
size_t usecond_len= _match_vector[_usecond_part_index + 1] - _match_vector[_usecond_part_index];
163
* For microseconds, which are millionth of 1 second,
164
* we must ensure that we produce a correct result,
165
* even if < 6 places were specified. For instance, if we get .1,
166
* we must produce 100000. .11 should produce 110000, etc.
168
uint32_t multiplier= 1;
169
int32_t x= usecond_len;
175
to->_useconds= atoi(copy_data.substr(usecond_start, usecond_len).c_str()) * multiplier;
177
if (_nsecond_part_index > 1)
179
size_t nsecond_start= _match_vector[_nsecond_part_index];
180
size_t nsecond_len= _match_vector[_nsecond_part_index + 1] - _match_vector[_nsecond_part_index];
182
* For nanoseconds, which are 1 billionth of a second,
183
* we must ensure that we produce a correct result,
184
* even if < 9 places were specified. For instance, if we get .1,
185
* we must produce 100000000. .11 should produce 110000000, etc.
187
uint32_t multiplier= 1;
188
int32_t x= nsecond_len;
194
to->_nseconds= atoi(copy_data.substr(nsecond_start, nsecond_len).c_str()) * multiplier;
199
} /* end namespace drizzled */
201
#define COUNT_KNOWN_FORMATS 19
203
struct temporal_format_args
206
int32_t year_part_index;
207
int32_t month_part_index;
208
int32_t day_part_index;
209
int32_t hour_part_index;
210
int32_t minute_part_index;
211
int32_t second_part_index;
212
int32_t usecond_part_index;
213
int32_t nsecond_part_index;
217
* A collection of all known format strings.
221
* IMPORTANT: Make sure TIMESTAMP and DATETIME formats precede DATE formats and TIME formats,
222
* as the matching functionality matches on the first hit.
226
* Remember to increment COUNT_KNOWN_FORMATS when you add a known format!
228
static struct temporal_format_args __format_args[COUNT_KNOWN_FORMATS]=
230
{"^(\\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 */
231
, {"^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YYYYMMDDHHmmSS */
232
, {"^(\\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 */
233
, {"^(\\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 */
234
, {"^(\\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 */
235
, {"^(\\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 */
236
, {"^(\\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 */
237
, {"^(\\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 */
238
, {"^(\\d{4})(\\d{2})(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYYMMDD */
239
, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{4})$", 3, 1, 2, 0, 0, 0, 0, 0} /* MM[-/.]DD[-/.]YYYY (US common format)*/
240
, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.]MM[-/.]DD */
241
, {"^(\\d{2})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.][M]M[-/.][D]D */
242
, {"^(\\d{4})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYY[-/.][M]M[-/.][D]D */
243
, {"^(\\d{2}):*(\\d{2}):*(\\d{2})\\.(\\d{1,6})$", 0, 0, 0, 1, 2, 3, 4, 0} /* HHmmSS.uuuuuu, HH:mm:SS.uuuuuu */
244
, {"^(\\d{1,2}):*(\\d{2}):*(\\d{2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]HmmSS, [H]H:mm:SS */
245
, {"^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]H:[m]m:[S]S */
246
, {"^(\\d{1,2}):*(\\d{2})$", 0, 0, 0, 0, 1, 2, 0, 0} /* [m]mSS, [m]m:SS */
247
, {"^(\\d{1,2})$", 0, 0, 0, 0, 0, 1, 0, 0} /* SS, S */
248
, {"^(\\d{1,2})\\.(\\d{1,6})$", 0, 0, 0, 0, 0, 1, 2, 0} /* [S]S.uuuuuu */
251
std::vector<drizzled::TemporalFormat *> known_datetime_formats;
252
std::vector<drizzled::TemporalFormat *> known_date_formats;
253
std::vector<drizzled::TemporalFormat *> known_time_formats;
254
std::vector<drizzled::TemporalFormat *> all_temporal_formats;
257
* We allocate and initialize all known date/time formats.
259
* @TODO Cut down calls to new. Allocate as a block...
261
bool init_temporal_formats()
263
/* Compile all the regular expressions for the datetime formats */
264
drizzled::TemporalFormat *tmp;
265
struct temporal_format_args current_format_args;
268
for (x= 0; x<COUNT_KNOWN_FORMATS; ++x)
270
current_format_args= __format_args[x];
271
tmp= new drizzled::TemporalFormat(current_format_args.pattern);
272
tmp->set_year_part_index(current_format_args.year_part_index);
273
tmp->set_month_part_index(current_format_args.month_part_index);
274
tmp->set_day_part_index(current_format_args.day_part_index);
275
tmp->set_hour_part_index(current_format_args.hour_part_index);
276
tmp->set_minute_part_index(current_format_args.minute_part_index);
277
tmp->set_second_part_index(current_format_args.second_part_index);
278
tmp->set_usecond_part_index(current_format_args.usecond_part_index);
279
tmp->set_nsecond_part_index(current_format_args.nsecond_part_index);
282
* We store the pointer in all_temporal_formats because we
283
* delete pointers from that vector and only that vector
285
all_temporal_formats.push_back(tmp);
287
if (current_format_args.year_part_index > 0) /* A date must have a year */
289
known_datetime_formats.push_back(tmp);
290
if (current_format_args.second_part_index == 0) /* A time must have seconds. */
291
known_date_formats.push_back(tmp);
293
if (current_format_args.second_part_index > 0) /* A time must have seconds, but may not have minutes or hours */
294
if (current_format_args.year_part_index == 0) /* A time may not have a date part, and date parts must have a year */
295
known_time_formats.push_back(tmp);
300
/** Free all allocated temporal formats */
301
void deinit_temporal_formats()
303
std::vector<drizzled::TemporalFormat *>::iterator p= all_temporal_formats.begin();
304
while (p != all_temporal_formats.end())
309
known_date_formats.clear();
310
known_datetime_formats.clear();
311
known_time_formats.clear();
312
all_temporal_formats.clear();