diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index a560dbd7876..34b12ee9b55 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -1091,22 +1091,13 @@ longlong Item_func_yearweek::val_int() } -static uint weekday_from_item(Item *item, bool *null_value, bool week_starts_on_sunday) -{ - MYSQL_TIME ltime; - if ((*null_value= Datetime(current_thd, item, - TIME_NO_ZERO_DATE | TIME_NO_ZERO_IN_DATE). - copy_to_mysql_time(<ime, MYSQL_TIMESTAMP_DATETIME))) - return 0; - return calc_weekday(calc_daynr(ltime.year, ltime.month, ltime.day), week_starts_on_sunday) + - MY_TEST(week_starts_on_sunday); -} - - longlong Item_func_weekday::val_int() { DBUG_ASSERT(fixed == 1); - return (longlong) weekday_from_item(args[0], &null_value, odbc_type); + Datetime dt(current_thd, args[0], TIME_NO_ZERO_DATE | TIME_NO_ZERO_IN_DATE); + if ((null_value= !dt.is_valid_datetime())) + return 0; + return dt.weekday(odbc_type) + MY_TEST(odbc_type); } bool Item_func_dayname::fix_length_and_dec() @@ -1125,14 +1116,14 @@ bool Item_func_dayname::fix_length_and_dec() String* Item_func_dayname::val_str(String* str) { DBUG_ASSERT(fixed == 1); - uint weekday= weekday_from_item(args[0], &null_value, false); const char *day_name; uint err; + Datetime dt(current_thd, args[0], TIME_NO_ZERO_DATE | TIME_NO_ZERO_IN_DATE); - if (null_value) + if ((null_value= !dt.is_valid_datetime())) return (String*) 0; - day_name= locale->day_names->type_names[weekday]; + day_name= locale->day_names->type_names[dt.weekday(false)]; str->copy(day_name, (uint) strlen(day_name), &my_charset_utf8_bin, collation.collation, &err); return str; diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index 7450a94673d..6ab644c579b 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -426,12 +426,12 @@ public: }; -class Item_func_weekday :public Item_int_func +class Item_func_weekday :public Item_long_func { bool odbc_type; public: Item_func_weekday(THD *thd, Item *a, bool type_arg): - Item_int_func(thd, a), odbc_type(type_arg) { } + Item_long_func(thd, a), odbc_type(type_arg) { } longlong val_int(); const char *func_name() const { @@ -441,7 +441,6 @@ public: { return type_handler()->Item_get_date(this, ltime, fuzzydate); } - const Type_handler *type_handler() const { return &type_handler_long; } bool fix_length_and_dec() { decimals= 0; diff --git a/sql/sql_type.h b/sql/sql_type.h index df4b99569c1..75e0bac33c3 100644 --- a/sql/sql_type.h +++ b/sql/sql_type.h @@ -292,6 +292,15 @@ class Temporal_with_date: protected MYSQL_TIME { protected: void make_from_item(THD *thd, Item *item, sql_mode_t flags); + + ulong daynr() const + { + return (ulong) ::calc_daynr((uint) year, (uint) month, (uint) day); + } + int weekday(bool sunday_first_day_of_week) const + { + return ::calc_weekday(daynr(), sunday_first_day_of_week); + } Temporal_with_date(THD *thd, Item *item, sql_mode_t flags) { make_from_item(thd, item, flags); @@ -389,6 +398,11 @@ public: DBUG_ASSERT(is_valid_datetime_slow()); return hour == 0 && minute == 0 && second == 0 && second_part == 0; } + int weekday(bool sunday_first_day_of_week) const + { + DBUG_ASSERT(is_valid_datetime_slow()); + return Temporal_with_date::weekday(sunday_first_day_of_week); + } const MYSQL_TIME *get_mysql_time() const { DBUG_ASSERT(is_valid_datetime_slow());