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
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "drizzled/function/time/to_days.h"
23
#include "drizzled/error.h"
24
#include "drizzled/temporal.h"
27
* We intepret the first argument as a DateTime and then convert
28
* it to a Julian Day Number and return it.
30
int64_t Item_func_to_days::val_int()
34
/* We return NULL from FROM_DAYS() only when supplied a NULL argument */
35
if (args[0]->null_value)
42
* We attempt to convert the first argument into a
43
* temporal value. If the conversion is successful,
44
* we know that a conversion to a Julian Day Number
45
* is always possible. Upon successful conversion,
46
* we return the Julian Day Number. If no conversion
47
* was possible into a temporal value, we throw an
48
* error and return 0, setting the null_value flag to true.
50
/* Grab the first argument as a DateTime object */
51
drizzled::DateTime temporal;
52
Item_result arg0_result_type= args[0]->result_type();
54
switch (arg0_result_type)
59
* For doubles supplied, interpret the arg as a string,
60
* so intentionally fall-through here...
61
* This allows us to accept double parameters like
62
* 19971231235959.01 and interpret it the way MySQL does:
63
* as a TIMESTAMP-like thing with a microsecond component.
64
* Ugh, but need to keep backwards-compat.
68
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
69
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
70
String *res= args[0]->val_str(&tmp);
75
* Likely a nested function issue where the nested
76
* function had bad input. We rely on the nested
77
* function my_error() and simply return false here.
82
if (! temporal.from_string(res->c_ptr(), res->length()))
85
* Could not interpret the function argument as a temporal value,
86
* so throw an error and return 0
88
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
94
if (temporal.from_int64_t(args[0]->val_int()))
96
/* Intentionally fall-through on invalid conversion from integer */
100
* Could not interpret the function argument as a temporal value,
101
* so throw an error and return 0
104
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
105
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
108
res= args[0]->val_str(&tmp);
113
* Likely a nested function issue where the nested
114
* function had bad input. We rely on the nested
115
* function my_error() and simply return false here.
120
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
124
int64_t julian_day_number;
125
temporal.to_julian_day_number(&julian_day_number);
126
return julian_day_number;
129
int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
134
* We attempt to convert the first argument into a
135
* temporal value. If the conversion is successful,
136
* we know that a conversion to a Julian Day Number
137
* is always possible. Depending on whether the
138
* first argument is a Date, or a DateTime with no
139
* time-portion, we return the Julian Day Number or
140
* the appropriate end-point integer.
142
/* Grab the first argument as a DateTime object */
143
drizzled::DateTime temporal;
144
Item_result arg0_result_type= args[0]->result_type();
146
switch (arg0_result_type)
151
* For doubles supplied, interpret the arg as a string,
152
* so intentionally fall-through here...
153
* This allows us to accept double parameters like
154
* 19971231235959.01 and interpret it the way MySQL does:
155
* as a TIMESTAMP-like thing with a microsecond component.
156
* Ugh, but need to keep backwards-compat.
160
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
161
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
162
String *res= args[0]->val_str(&tmp);
167
* Likely a nested function issue where the nested
168
* function had bad input. We rely on the nested
169
* function my_error() and simply return false here.
174
if (! temporal.from_string(res->c_ptr(), res->length()))
177
* Could not interpret the function argument as a temporal value,
178
* so throw an error and return 0
180
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
186
if (temporal.from_int64_t(args[0]->val_int()))
188
/* Intentionally fall-through on invalid conversion from integer */
192
* Could not interpret the function argument as a temporal value,
193
* so throw an error and return 0
196
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
197
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
200
res= args[0]->val_str(&tmp);
205
* Likely a nested function issue where the nested
206
* function had bad input. We rely on the nested
207
* function my_error() and simply return false here.
212
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
217
if (null_value == true)
219
/* got NULL, leave the incl_endp intact */
223
int64_t julian_day_number;
224
temporal.to_julian_day_number(&julian_day_number);
226
if (args[0]->field_type() == DRIZZLE_TYPE_DATE)
228
/* TO_DAYS() is strictly monotonic for dates, leave incl_endp intact */
229
return julian_day_number;
233
Handle the special but practically useful case of datetime values that
234
point to day bound ("strictly less" comparison stays intact):
236
col < '2007-09-15 00:00:00' -> TO_DAYS(col) < TO_DAYS('2007-09-15')
238
which is different from the general case ("strictly less" changes to
241
col < '2007-09-15 12:34:56' -> TO_DAYS(col) <= TO_DAYS('2007-09-15')
243
if (!left_endp && ! (
245
|| temporal.minutes()
246
|| temporal.seconds()
247
|| temporal.useconds()
248
|| temporal.nseconds()
254
return julian_day_number;