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/last_day.h>
24
bool Item_func_last_day::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
22
#include "drizzled/function/time/last_day.h"
23
#include "drizzled/error.h"
24
#include "drizzled/calendar.h"
25
#include "drizzled/temporal.h"
31
* Interpret the first argument as a DateTime string and then populate
32
* our supplied temporal object with a Date representing the last day of
33
* the corresponding month and year.
35
bool Item_func_last_day::get_temporal(drizzled::Date &to)
26
if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE) ||
33
uint32_t month_idx= ltime->month-1;
34
ltime->day= days_in_month[month_idx];
35
if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
37
ltime->hour= ltime->minute= ltime->second= 0;
38
ltime->second_part= 0;
39
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
39
/* We return NULL from LAST_DAY() only when supplied a NULL argument */
40
if (args[0]->null_value)
46
/* We use a DateTime to match as many temporal formats as possible. */
47
drizzled::DateTime temporal;
48
Item_result arg0_result_type= args[0]->result_type();
50
switch (arg0_result_type)
55
* For doubles supplied, interpret the arg as a string,
56
* so intentionally fall-through here...
57
* This allows us to accept double parameters like
58
* 19971231235959.01 and interpret it the way MySQL does:
59
* as a TIMESTAMP-like thing with a microsecond component.
60
* Ugh, but need to keep backwards-compat.
64
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
65
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
66
String *res= args[0]->val_str(&tmp);
71
* Likely a nested function issue where the nested
72
* function had bad input. We rely on the nested
73
* function my_error() and simply return false here.
78
if (! temporal.from_string(res->c_ptr(), res->length()))
81
* Could not interpret the function argument as a temporal value,
82
* so throw an error and return 0
84
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
90
if (temporal.from_int64_t(args[0]->val_int()))
92
/* Intentionally fall-through on invalid conversion from integer */
96
* Could not interpret the function argument as a temporal value,
97
* so throw an error and return 0
100
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
101
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
104
res= args[0]->val_str(&tmp);
109
* Likely a nested function issue where the nested
110
* function had bad input. We rely on the nested
111
* function my_error() and simply return false here.
116
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
122
/* Now strip to the last day of the month... */
123
temporal.set_days(days_in_gregorian_year_month(temporal.years(), temporal.months()));
124
to= temporal; /* Operator overload in effect for assign DateTime to Date. */