17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "drizzled/temporal.h"
23
#include "drizzled/error.h"
24
#include "drizzled/function/time/hour.h"
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/time/hour.h>
29
24
int64_t Item_func_hour::val_int()
33
if (args[0]->is_null())
35
/* For NULL argument, we return a NULL result */
41
* Because of the ridiculous way in which MySQL handles
42
* TIME values (it does implicit integer -> string conversions
43
* but only for DATETIME, not TIME values) we must first
44
* try a conversion into a TIME from a string. If this
45
* fails, we fall back on a DATETIME conversion. This is
46
* necessary because of the fact that DateTime::from_string()
47
* looks first for DATETIME, then DATE regex matches. 6 consecutive
48
* numbers, say 231130, will match the DATE regex YYMMDD
49
* with no TIME part, but MySQL actually implicitly treats
50
* parameters to SECOND(), HOUR(), and MINUTE() as TIME-only
51
* values and matches 231130 as HHmmSS!
53
* Oh, and Brian Aker MADE me do this. :) --JRP
57
char time_buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
58
String tmp_time(time_buff,sizeof(time_buff), &my_charset_utf8_bin);
59
String *time_res= args[0]->val_str(&tmp_time);
61
if (time_res && (time_res != &tmp_time))
63
tmp_time.copy(*time_res);
66
if (! temporal_time.from_string(tmp_time.c_ptr(), tmp_time.length()))
69
* OK, we failed to match the first argument as a string
70
* representing a time value, so we grab the first argument
71
* as a DateTime object and try that for a match...
73
DateTime temporal_datetime;
74
Item_result arg0_result_type= args[0]->result_type();
76
switch (arg0_result_type)
80
* For doubles supplied, interpret the arg as a string,
81
* so intentionally fall-through here...
82
* This allows us to accept double parameters like
83
* 19971231235959.01 and interpret it the way MySQL does:
84
* as a TIMESTAMP-like thing with a microsecond component.
85
* Ugh, but need to keep backwards-compat.
89
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
90
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
91
String *res= args[0]->val_str(&tmp);
93
if (res && (res != &tmp))
98
if (! temporal_datetime.from_string(tmp.c_ptr(), tmp.length()))
101
* Could not interpret the function argument as a temporal value,
102
* so throw an error and return 0
104
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
110
if (temporal_datetime.from_int64_t(args[0]->val_int()))
112
/* Intentionally fall-through on invalid conversion from integer */
116
* Could not interpret the function argument as a temporal value,
117
* so throw an error and return 0
120
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
121
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
124
res= args[0]->val_str(&tmp);
126
if (res && (res != &tmp))
131
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
135
return (int64_t) temporal_datetime.hours();
137
return (int64_t) temporal_time.hours();
28
(void) get_arg0_time(<ime);
140
} /* namespace drizzled */