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
20
#include "drizzled/server_includes.h"
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)
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. */