17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
20
#include "drizzled/server_includes.h"
22
#include <drizzled/function/time/to_days.h>
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.
24
30
int64_t Item_func_to_days::val_int()
28
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
30
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
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;
35
130
Get information about this Item tree monotonicity
60
154
int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
65
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
159
* We attempt to convert the first argument into a
160
* temporal value. If the conversion is successful,
161
* we know that a conversion to a Julian Day Number
162
* is always possible. Depending on whether the
163
* first argument is a Date, or a DateTime with no
164
* time-portion, we return the Julian Day Number or
165
* the appropriate end-point integer.
167
/* Grab the first argument as a DateTime object */
168
drizzled::DateTime temporal;
169
Item_result arg0_result_type= args[0]->result_type();
171
switch (arg0_result_type)
176
* For doubles supplied, interpret the arg as a string,
177
* so intentionally fall-through here...
178
* This allows us to accept double parameters like
179
* 19971231235959.01 and interpret it the way MySQL does:
180
* as a TIMESTAMP-like thing with a microsecond component.
181
* Ugh, but need to keep backwards-compat.
185
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
186
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
187
String *res= args[0]->val_str(&tmp);
192
* Likely a nested function issue where the nested
193
* function had bad input. We rely on the nested
194
* function my_error() and simply return false here.
199
if (! temporal.from_string(res->c_ptr(), res->length()))
202
* Could not interpret the function argument as a temporal value,
203
* so throw an error and return 0
205
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
211
if (temporal.from_int64_t(args[0]->val_int()))
213
/* Intentionally fall-through on invalid conversion from integer */
217
* Could not interpret the function argument as a temporal value,
218
* so throw an error and return 0
221
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
222
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
225
res= args[0]->val_str(&tmp);
230
* Likely a nested function issue where the nested
231
* function had bad input. We rely on the nested
232
* function my_error() and simply return false here.
237
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
242
if (null_value == true)
67
244
/* got NULL, leave the incl_endp intact */
70
res=(int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
248
int64_t julian_day_number;
249
temporal.to_julian_day_number(&julian_day_number);
72
251
if (args[0]->field_type() == DRIZZLE_TYPE_DATE)
74
// TO_DAYS() is strictly monotonic for dates, leave incl_endp intact
253
/* TO_DAYS() is strictly monotonic for dates, leave incl_endp intact */
254
return julian_day_number;