From a3a5e9d0c6478dfd001aeaf3d18d3d990258942c Mon Sep 17 00:00:00 2001
From: "bar@bar.mysql.r18.ru" <>
Date: Tue, 12 Nov 2002 17:32:36 +0400
Subject: [PATCH] These functions are now UCS2 compatible: CURDATE()
 FROM_DAYS() CURTIME() NOW() SEC_TO_TIME() FROM_UNIXTIME() DATE_ADD_INTERVAL()

---
 sql/item_timefunc.cc |  82 ++++++++++++++++-------
 sql/item_timefunc.h  | 151 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 181 insertions(+), 52 deletions(-)

diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index b208713eea0..cec83428e24 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -402,16 +402,16 @@ String *Item_date::val_str(String *str)
     return (String*) 0;
   if (!value)					// zero daynr
   {
-    str->copy("0000-00-00",10,my_charset_latin1);
+    str->copy("0000-00-00",10,my_charset_latin1,thd_charset());
     return str;
   }
-  if (str->alloc(11))
-    return &empty_string;			/* purecov: inspected */
-  sprintf((char*) str->ptr(),"%04d-%02d-%02d",
+  
+  char tmpbuff[11];
+  sprintf(tmpbuff,"%04d-%02d-%02d",
 	  (int) (value/10000L) % 10000,
 	  (int) (value/100)%100,
 	  (int) (value%100));
-  str->length(10);
+  str->copy(tmpbuff,10,my_charset_latin1,thd_charset());
   return str;
 }
 
@@ -448,7 +448,10 @@ void Item_func_curdate::fix_length_and_dec()
 {
   struct tm tm_tmp,*start;
   time_t query_start=current_thd->query_start();
-  decimals=0; max_length=10;
+  
+  set_charset(thd_charset());
+  decimals=0; 
+  max_length=10*thd_charset()->mbmaxlen;
   localtime_r(&query_start,&tm_tmp);
   start=&tm_tmp;
   value=(longlong) ((ulong) ((uint) start->tm_year+1900)*10000L+
@@ -473,27 +476,48 @@ bool Item_func_curdate::get_date(TIME *res,
   return 0;
 }
 
+String *Item_func_curtime::val_str(String *str)
+{ 
+  str_value.set(buff,buff_length,thd_charset());
+  return &str_value;
+}
+
 void Item_func_curtime::fix_length_and_dec()
 {
   struct tm tm_tmp,*start;
   time_t query_start=current_thd->query_start();
-  decimals=0; max_length=8;
+  CHARSET_INFO *cs=thd_charset();
+
+  decimals=0; 
+  max_length=8*cs->mbmaxlen;
   localtime_r(&query_start,&tm_tmp);
   start=&tm_tmp;
+  set_charset(cs);
   value=(longlong) ((ulong) ((uint) start->tm_hour)*10000L+
 		    (ulong) (((uint) start->tm_min)*100L+
 			     (uint) start->tm_sec));
-  buff_length= my_sprintf(buff, (buff,"%02d:%02d:%02d",
+
+  buff_length=cs->snprintf(cs,buff,sizeof(buff),"%02d:%02d:%02d",
 				 (int) start->tm_hour,
 				 (int) start->tm_min,
-				 (int) start->tm_sec));
+				 (int) start->tm_sec);
+}
+
+String *Item_func_now::val_str(String *str)
+{
+  str_value.set(buff,buff_length,thd_charset());
+  return &str_value;
 }
 
 void Item_func_now::fix_length_and_dec()
 {
   struct tm tm_tmp,*start;
   time_t query_start=current_thd->query_start();
-  decimals=0; max_length=19;
+  CHARSET_INFO *cs=thd_charset();
+  
+  decimals=0;
+  max_length=19*cs->mbmaxlen;
+  set_charset(cs);
   localtime_r(&query_start,&tm_tmp);
   start=&tm_tmp;
   value=((longlong) ((ulong) ((uint) start->tm_year+1900)*10000L+
@@ -502,13 +526,14 @@ void Item_func_now::fix_length_and_dec()
 	 (longlong) ((ulong) ((uint) start->tm_hour)*10000L+
 		     (ulong) (((uint) start->tm_min)*100L+
 			    (uint) start->tm_sec)));
-  buff_length= (uint) my_sprintf(buff, (buff,"%04d-%02d-%02d %02d:%02d:%02d",
+  
+  buff_length= (uint) cs->snprintf(cs,buff, sizeof(buff),"%04d-%02d-%02d %02d:%02d:%02d",
 					((int) (start->tm_year+1900)) % 10000,
 					(int) start->tm_mon+1,
 					(int) start->tm_mday,
 					(int) start->tm_hour,
 					(int) start->tm_min,
-					(int) start->tm_sec));
+					(int) start->tm_sec);
   /* For getdate */
   ltime.year=	start->tm_year+1900;
   ltime.month=	start->tm_mon+1;
@@ -539,7 +564,7 @@ int  Item_func_now::save_in_field(Field *to)
 
 String *Item_func_sec_to_time::val_str(String *str)
 {
-  char buff[23];
+  char buff[23*2];
   const char *sign="";
   longlong seconds=(longlong) args[0]->val_int();
   ulong length;
@@ -553,7 +578,7 @@ String *Item_func_sec_to_time::val_str(String *str)
   uint sec= (uint) ((ulonglong) seconds % 3600);
   length= my_sprintf(buff,(buff,"%s%02lu:%02u:%02u",sign,(long) (seconds/3600),
 			   sec/60, sec % 60));
-  str->copy(buff, length, my_charset_latin1);
+  str->copy(buff, length, my_charset_latin1, thd_charset());
   return str;
 }
 
@@ -897,20 +922,26 @@ String *Item_func_from_unixtime::val_str(String *str)
 {
   struct tm tm_tmp,*start;
   time_t tmp=(time_t) args[0]->val_int();
+  uint32 l;
+  CHARSET_INFO *cs=thd_charset();
+  
   if ((null_value=args[0]->null_value))
     return 0;
   localtime_r(&tmp,&tm_tmp);
   start=&tm_tmp;
-  if (str->alloc(20))
+  
+  l=20*cs->mbmaxlen+32;
+  if (str->alloc(l))
     return str;					/* purecov: inspected */
-  sprintf((char*) str->ptr(),"%04d-%02d-%02d %02d:%02d:%02d",
+  l=cs->snprintf(cs,(char*) str->ptr(),l,"%04d-%02d-%02d %02d:%02d:%02d",
 	  (int) start->tm_year+1900,
 	  (int) start->tm_mon+1,
 	  (int) start->tm_mday,
 	  (int) start->tm_hour,
 	  (int) start->tm_min,
 	  (int) start->tm_sec);
-  str->length(19);
+  str->length(l);
+  str->set_charset(cs);
   return str;
 }
 
@@ -1041,26 +1072,31 @@ bool Item_date_add_interval::get_date(TIME *ltime, bool fuzzy_date)
 String *Item_date_add_interval::val_str(String *str)
 {
   TIME ltime;
+  CHARSET_INFO *cs=thd_charset();
+  uint32 l;
 
   if (Item_date_add_interval::get_date(&ltime,0))
     return 0;
   if (ltime.time_type == TIMESTAMP_DATE)
   {
-    if (str->alloc(11))
+    l=11*cs->mbmaxlen+32;
+    if (str->alloc(l))
       goto null_date;
-    sprintf((char*) str->ptr(),"%04d-%02d-%02d",
+    l=cs->snprintf(cs,(char*) str->ptr(),l,"%04d-%02d-%02d",
 	    ltime.year,ltime.month,ltime.day);
-    str->length(10);
+    str->length(l);
   }
   else
   {
-    if (str->alloc(20))
+    l=20*cs->mbmaxlen+32;
+    if (str->alloc(l))
       goto null_date;
-    sprintf((char*) str->ptr(),"%04d-%02d-%02d %02d:%02d:%02d",
+    l=cs->snprintf(cs,(char*) str->ptr(),l,"%04d-%02d-%02d %02d:%02d:%02d",
 	    ltime.year,ltime.month,ltime.day,
 	    ltime.hour,ltime.minute,ltime.second);
-    str->length(19);
+    str->length(l);
   }
+  str->set_charset(cs);
   return str;
 
  null_date:
diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h
index c3dc9bfb6d4..07cdfde115b 100644
--- a/sql/item_timefunc.h
+++ b/sql/item_timefunc.h
@@ -27,7 +27,10 @@ class Item_func_period_add :public Item_int_func
   Item_func_period_add(Item *a,Item *b) :Item_int_func(a,b) {}
   longlong val_int();
   const char *func_name() const { return "period_add"; }
-  void fix_length_and_dec() { max_length=6; }
+  void fix_length_and_dec() 
+  { 
+    max_length=6*thd_charset()->mbmaxlen;
+  }
 };
 
 
@@ -37,7 +40,11 @@ class Item_func_period_diff :public Item_int_func
   Item_func_period_diff(Item *a,Item *b) :Item_int_func(a,b) {}
   longlong val_int();
   const char *func_name() const { return "period_diff"; }
-  void fix_length_and_dec() { decimals=0; max_length=6; }
+  void fix_length_and_dec()
+  { 
+    decimals=0;
+    max_length=6*thd_charset()->mbmaxlen;
+  }
 };
 
 
@@ -47,7 +54,12 @@ class Item_func_to_days :public Item_int_func
   Item_func_to_days(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "to_days"; }
-  void fix_length_and_dec() { decimals=0; max_length=6; maybe_null=1; }
+  void fix_length_and_dec() 
+  { 
+    decimals=0; 
+    max_length=6*thd_charset()->mbmaxlen;
+    maybe_null=1; 
+  }
 };
 
 
@@ -57,7 +69,12 @@ class Item_func_dayofmonth :public Item_int_func
   Item_func_dayofmonth(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "dayofmonth"; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec() 
+  { 
+    decimals=0; 
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1; 
+  }
 };
 
 
@@ -74,7 +91,13 @@ class Item_func_month :public Item_func
   }
   const char *func_name() const { return "month"; }
   enum Item_result result_type () const { return INT_RESULT; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec() 
+  { 
+    set_charset(thd_charset());
+    decimals=0;
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1; 
+  }
 };
 
 
@@ -86,8 +109,9 @@ class Item_func_monthname :public Item_func_month
   String *val_str(String *str);
   enum Item_result result_type () const { return STRING_RESULT; }
   void fix_length_and_dec() 
-  { 
-    decimals=0; 
+  {
+    set_charset(thd_charset());
+    decimals=0;
     max_length=10*thd_charset()->mbmaxlen;
     maybe_null=1; 
   }
@@ -100,7 +124,12 @@ class Item_func_dayofyear :public Item_int_func
   Item_func_dayofyear(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "dayofyear"; }
-  void fix_length_and_dec() { decimals=0; max_length=3; maybe_null=1; }
+  void fix_length_and_dec() 
+  { 
+    decimals=0;
+    max_length=3*thd_charset()->mbmaxlen;
+    maybe_null=1; 
+  }
 };
 
 
@@ -110,7 +139,12 @@ class Item_func_hour :public Item_int_func
   Item_func_hour(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "hour"; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec()
+  {
+    decimals=0;
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 
@@ -120,7 +154,12 @@ class Item_func_minute :public Item_int_func
   Item_func_minute(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "minute"; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec()
+  {
+    decimals=0;
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 
@@ -130,7 +169,12 @@ class Item_func_quarter :public Item_int_func
   Item_func_quarter(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "quarter"; }
-  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1; }
+  void fix_length_and_dec()
+  { 
+     decimals=0;
+     max_length=1*thd_charset()->mbmaxlen;
+     maybe_null=1;
+  }
 };
 
 
@@ -140,7 +184,12 @@ class Item_func_second :public Item_int_func
   Item_func_second(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "second"; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec() 
+  { 
+    decimals=0;
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 
@@ -150,7 +199,12 @@ class Item_func_week :public Item_int_func
   Item_func_week(Item *a,Item *b) :Item_int_func(a,b) {}
   longlong val_int();
   const char *func_name() const { return "week"; }
-  void fix_length_and_dec() { decimals=0; max_length=2; maybe_null=1; }
+  void fix_length_and_dec()
+  { 
+    decimals=0;
+    max_length=2*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 class Item_func_yearweek :public Item_int_func
@@ -159,7 +213,12 @@ class Item_func_yearweek :public Item_int_func
   Item_func_yearweek(Item *a,Item *b) :Item_int_func(a,b) {}
   longlong val_int();
   const char *func_name() const { return "yearweek"; }
-  void fix_length_and_dec() { decimals=0; max_length=6; maybe_null=1; }
+  void fix_length_and_dec()
+  { 
+    decimals=0;
+    max_length=6*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 
@@ -169,7 +228,12 @@ class Item_func_year :public Item_int_func
   Item_func_year(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "year"; }
-  void fix_length_and_dec() { decimals=0; max_length=4; maybe_null=1; }
+  void fix_length_and_dec()
+  { 
+    decimals=0;
+    max_length=4*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 
@@ -181,13 +245,20 @@ class Item_func_weekday :public Item_func
     :Item_func(a), odbc_type(type_arg) {}
   longlong val_int();
   double val() { return (double) val_int(); }
-  String *val_str(String *str) { 
+  String *val_str(String *str)
+  { 
     str->set(val_int(), thd_charset());
     return null_value ? 0 : str;
   }
   const char *func_name() const { return "weekday"; }
   enum Item_result result_type () const { return INT_RESULT; }
-  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1; }
+  void fix_length_and_dec()
+  {
+    set_charset(thd_charset());
+    decimals=0;
+    max_length=1*thd_charset()->mbmaxlen;
+    maybe_null=1;
+  }
 };
 
 class Item_func_dayname :public Item_func_weekday
@@ -199,6 +270,7 @@ class Item_func_dayname :public Item_func_weekday
   enum Item_result result_type () const { return STRING_RESULT; }
   void fix_length_and_dec() 
   { 
+    set_charset(thd_charset());
     decimals=0; 
     max_length=9*thd_charset()->mbmaxlen;
     maybe_null=1; 
@@ -216,7 +288,8 @@ class Item_func_unix_timestamp :public Item_int_func
   const char *func_name() const { return "timestamp"; }
   void fix_length_and_dec()
   {
-    decimals=0; max_length=10;
+    decimals=0;
+    max_length=10*thd_charset()->mbmaxlen;
   }
 };
 
@@ -229,7 +302,8 @@ class Item_func_time_to_sec :public Item_int_func
   const char *func_name() const { return "time_to_sec"; }
   void fix_length_and_dec()
   {
-    decimals=0; max_length=10;
+    decimals=0;
+    max_length=10*thd_charset()->mbmaxlen;
   }
 };
 
@@ -245,7 +319,12 @@ class Item_date :public Item_func
   String *val_str(String *str);
   double val() { return (double) val_int(); }
   const char *func_name() const { return "date"; }
-  void fix_length_and_dec() { decimals=0; max_length=10; }
+  void fix_length_and_dec()
+  { 
+    set_charset(thd_charset());
+    decimals=0;
+    max_length=10*thd_charset()->mbmaxlen;
+  }
   int  save_in_field(Field *to);
   void make_field(Send_field *tmp_field)
   {
@@ -279,7 +358,7 @@ class Item_date_func :public Item_str_func
 class Item_func_curtime :public Item_func
 {
   longlong value;
-  char buff[9];
+  char buff[9*2+32];
   uint buff_length;
 public:
   Item_func_curtime() :Item_func() {}
@@ -287,8 +366,7 @@ class Item_func_curtime :public Item_func
   enum Item_result result_type () const { return STRING_RESULT; }
   double val() { return (double) value; }
   longlong val_int() { return value; }
-  String *val_str(String *str)
-  { str_value.set(buff,buff_length,default_charset_info); return &str_value; }
+  String *val_str(String *str);
   const char *func_name() const { return "curtime"; }
   void fix_length_and_dec();
   void make_field(Send_field *tmp_field)
@@ -319,7 +397,7 @@ class Item_func_curdate :public Item_date
 class Item_func_now :public Item_date_func
 {
   longlong value;
-  char buff[20];
+  char buff[20*2+32];	// +32 to make my_snprintf_{8bit|ucs2} happy
   uint buff_length;
   TIME ltime;
 public:
@@ -329,8 +407,7 @@ class Item_func_now :public Item_date_func
   double val()	     { return (double) value; }
   longlong val_int() { return value; }
   int  save_in_field(Field *to);
-  String *val_str(String *str)
-  { str_value.set(buff,buff_length,default_charset_info); return &str_value; }
+  String *val_str(String *str);
   const char *func_name() const { return "now"; }
   void fix_length_and_dec();
   bool get_date(TIME *res,bool fuzzy_date);
@@ -369,7 +446,12 @@ 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; }
+  void fix_length_and_dec()
+  { 
+    set_charset(thd_charset());
+    decimals=0;
+    max_length=19*thd_charset()->mbmaxlen;
+  }
 //  enum Item_result result_type () const { return STRING_RESULT; }
   bool get_date(TIME *res,bool fuzzy_date);
 };
@@ -382,7 +464,12 @@ class Item_func_sec_to_time :public Item_str_func
   double val() { return (double) Item_func_sec_to_time::val_int(); }
   longlong val_int();
   String *val_str(String *);
-  void fix_length_and_dec() { maybe_null=1; max_length=13; }
+  void fix_length_and_dec()
+  { 
+    set_charset(thd_charset());
+    maybe_null=1;
+    max_length=13*thd_charset()->mbmaxlen;
+  }
   const char *func_name() const { return "sec_to_time"; }
   void make_field(Send_field *tmp_field)
   {
@@ -414,7 +501,13 @@ class Item_date_add_interval :public Item_date_func
     :Item_date_func(a,b),int_type(type_arg), date_sub_interval(neg_arg) {}
   String *val_str(String *);
   const char *func_name() const { return "date_add_interval"; }
-  void fix_length_and_dec() { maybe_null=1; max_length=19; value.alloc(32);}
+  void fix_length_and_dec()
+  {
+    set_charset(thd_charset());
+    maybe_null=1;
+    max_length=19*thd_charset()->mbmaxlen;
+    value.alloc(32);
+  }
   double val() { return (double) val_int(); }
   longlong val_int();
   bool get_date(TIME *res,bool fuzzy_date);
-- 
2.30.9