Skip to content

Commit 5ade648

Browse files
WL#1253: LAST_DAY
1 parent a1199c7 commit 5ade648

7 files changed

Lines changed: 72 additions & 1 deletion

File tree

mysql-test/r/func_time.result

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,23 @@ select date_add(time,INTERVAL 1 SECOND) from t1;
468468
date_add(time,INTERVAL 1 SECOND)
469469
2006-07-08 00:00:01
470470
drop table t1;
471+
select last_day('2000-02-05') as f1, last_day('2002-12-31') as f2,
472+
last_day('2003-03-32') as f3, last_day('2003-04-01') as f4,
473+
last_day('2001-01-01 01:01:01') as f5, last_day(NULL),
474+
last_day('2001-02-12');
475+
f1 f2 f3 f4 f5 last_day(NULL) last_day('2001-02-12')
476+
2000-02-29 2002-12-31 NULL 2003-04-30 2001-01-31 NULL 2001-02-28
477+
create table t1 select last_day('2000-02-05') as a;
478+
describe t1;
479+
Field Type Null Key Default Extra
480+
a date 0000-00-00
481+
select * from t1;
482+
a
483+
2000-02-29
484+
drop table t1;
485+
select last_day('2000-02-05');
486+
last_day('2000-02-05')
487+
2000-02-29
471488
select strcmp(date_sub(localtimestamp(), interval 3 hour), utc_timestamp())=0;
472489
strcmp(date_sub(localtimestamp(), interval 3 hour), utc_timestamp())=0
473490
1

mysql-test/t/func_time.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ select date_add(date,INTERVAL "1 1:1:1" DAY_SECOND) from t1;
224224
select date_add(time,INTERVAL 1 SECOND) from t1;
225225
drop table t1;
226226

227+
# test for last_day
228+
select last_day('2000-02-05') as f1, last_day('2002-12-31') as f2,
229+
last_day('2003-03-32') as f3, last_day('2003-04-01') as f4,
230+
last_day('2001-01-01 01:01:01') as f5, last_day(NULL),
231+
last_day('2001-02-12');
232+
233+
create table t1 select last_day('2000-02-05') as a;
234+
describe t1;
235+
select * from t1;
236+
drop table t1;
237+
select last_day('2000-02-05');
238+
239+
227240
# Test SAPDB UTC_% functions. This part is TZ dependant (It is supposed that
228241
# TZ variable set to GMT-3
229242
select strcmp(date_sub(localtimestamp(), interval 3 hour), utc_timestamp())=0;

sql/item_create.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,8 @@ Item *create_func_str_to_date(Item* a,Item* b)
707707
{
708708
return new Item_func_str_to_date(a, b);
709709
}
710+
711+
Item *create_func_last_day(Item *a)
712+
{
713+
return new Item_func_last_day(a);
714+
}

sql/item_create.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,4 @@ Item *create_func_subtime(Item* a,Item* b);
150150
Item *create_func_timediff(Item* a,Item* b);
151151
Item *create_func_maketime(Item* a,Item* b,Item* c);
152152
Item *create_func_str_to_date(Item* a,Item* b);
153+
Item *create_func_last_day(Item *a);

sql/item_timefunc.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,3 +2310,19 @@ String *Item_func_str_to_date::val_str(String *str)
23102310
return str;
23112311
return 0;
23122312
}
2313+
2314+
2315+
String *Item_func_last_day::val_str(String *str)
2316+
{
2317+
TIME ltime;
2318+
if (!get_arg0_date(&ltime,0))
2319+
{
2320+
uint month_idx= ltime.month-1;
2321+
ltime.day= days_in_month[month_idx];
2322+
if ( month_idx == 1 && calc_days_in_year(ltime.year) == 366)
2323+
ltime.day+= 1;
2324+
if (!make_datetime(DATE_ONLY, &ltime, str))
2325+
return str;
2326+
}
2327+
return 0;
2328+
}

sql/item_timefunc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,21 @@ class Item_func_str_to_date :public Item_date_func
809809
max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
810810
}
811811
};
812+
813+
class Item_func_last_day :public Item_str_func
814+
{
815+
public:
816+
Item_func_last_day(Item *a) :Item_str_func(a) {}
817+
String *val_str(String *str);
818+
const char *func_name() const { return "last_day"; }
819+
enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
820+
void fix_length_and_dec()
821+
{
822+
decimals=0;
823+
max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
824+
}
825+
Field *tmp_table_field(TABLE *t_arg)
826+
{
827+
return (new Field_date(maybe_null, name, t_arg, &my_charset_bin));
828+
}
829+
};

sql/lex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ static SYMBOL sql_functions[] = {
551551
{ "IS_FREE_LOCK", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_is_free_lock)},
552552
{ "IS_USED_LOCK", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_is_used_lock)},
553553
{ "LAST_INSERT_ID", SYM(LAST_INSERT_ID),0,0},
554-
{ "ISSIMPLE", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_issimple)},
554+
{ "ISSIMPLE", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_issimple)},
555+
{ "LAST_DAY", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_last_day)},
555556
{ "LCASE", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_lcase)},
556557
{ "LEAST", SYM(LEAST_SYM),0,0},
557558
{ "LENGTH", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_length)},

0 commit comments

Comments
 (0)