diff options
-rw-r--r-- | mysql-test/r/func_time.result | 7 | ||||
-rw-r--r-- | mysql-test/t/func_time.test | 8 | ||||
-rw-r--r-- | sql/item_timefunc.cc | 10 | ||||
-rw-r--r-- | sql/item_timefunc.h | 3 |
4 files changed, 18 insertions, 10 deletions
diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result index 32034bf289d..c1e75b60e4f 100644 --- a/mysql-test/r/func_time.result +++ b/mysql-test/r/func_time.result @@ -470,9 +470,12 @@ unix_timestamp(@a) select unix_timestamp('1969-12-01 19:00:01'); unix_timestamp('1969-12-01 19:00:01') 0 -select from_unixtime(0); -from_unixtime(0) +select from_unixtime(-1); +from_unixtime(-1) NULL select from_unixtime(2145916800); from_unixtime(2145916800) NULL +select from_unixtime(0); +from_unixtime(0) +1970-01-01 03:00:00 diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test index da18269cf6a..60c7aa49fbf 100644 --- a/mysql-test/t/func_time.test +++ b/mysql-test/t/func_time.test @@ -228,7 +228,11 @@ select unix_timestamp('1969-12-01 19:00:01'); # # Test for bug #6439 "unix_timestamp() function returns wrong datetime -# values for too big argument". It should return error instead. +# values for too big argument" and bug #7515 "from_unixtime(0) now +# returns NULL instead of the epoch". unix_timestamp() should return error +# for too big or negative argument. It should return Epoch value for zero +# argument since it seems that many user's rely on this fact. # -select from_unixtime(0); +select from_unixtime(-1); select from_unixtime(2145916800); +select from_unixtime(0); diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index d188310be24..b03fd151383 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -946,10 +946,12 @@ bool Item_func_from_unixtime::get_date(TIME *ltime, { struct tm tm_tmp; time_t tmp; - longlong arg= args[0]->val_int(); - if ((null_value= (args[0]->null_value || - arg < TIMESTAMP_MIN_VALUE || - arg > TIMESTAMP_MAX_VALUE))) + ulonglong arg= (ulonglong)(args[0]->val_int()); + /* + "arg > TIMESTAMP_MAX_VALUE" check also covers case of negative + from_unixtime() argument since arg is unsigned. + */ + if ((null_value= (args[0]->null_value || arg > TIMESTAMP_MAX_VALUE))) return 1; tmp= arg; localtime_r(&tmp,&tm_tmp); diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index e04e24627d9..8ee2f935a80 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -359,8 +359,7 @@ class Item_func_from_unixtime :public Item_date_func longlong val_int(); String *val_str(String *str); const char *func_name() const { return "from_unixtime"; } - void fix_length_and_dec() { decimals=0; max_length=19; } -// enum Item_result result_type () const { return STRING_RESULT; } + void fix_length_and_dec() { decimals=0; max_length=19; maybe_null= 1; } bool get_date(TIME *res,bool fuzzy_date); }; |