item_timefunc.cc 44.3 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* This file defines all time functions */

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include <m_ctype.h>
#include <time.h>

/*
** Todo: Move month and days to language files
*/

32 33
#define MAX_DAY_NUMBER 3652424L

34 35
static String month_names[] = 
{ 
36 37 38 39 40 41 42 43 44 45 46 47
  String("January",	&my_charset_latin1), 
  String("February",	&my_charset_latin1),
  String("March",	&my_charset_latin1),
  String("April",	&my_charset_latin1),
  String("May",		&my_charset_latin1),
  String("June",	&my_charset_latin1),
  String("July",	&my_charset_latin1),
  String("August",	&my_charset_latin1),
  String("September",	&my_charset_latin1),
  String("October",	&my_charset_latin1),
  String("November",	&my_charset_latin1),
  String("December",	&my_charset_latin1)
48 49 50
};
static String day_names[] = 
{ 
51 52 53 54 55 56 57
  String("Monday",	&my_charset_latin1),
  String("Tuesday",	&my_charset_latin1),
  String("Wednesday",	&my_charset_latin1),
  String("Thursday",	&my_charset_latin1),
  String("Friday",	&my_charset_latin1),
  String("Saturday",	&my_charset_latin1),
  String("Sunday",	&my_charset_latin1)
58
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
59

60 61 62 63 64
enum date_time_format_types 
{ 
  TIME_ONLY= 0, TIME_MICROSECOND,
  DATE_ONLY, DATE_TIME, DATE_TIME_MICROSECOND
};
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
65

66 67
typedef struct date_time_format 
{
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  const char* format_str;
  uint length;
};

static struct date_time_format date_time_formats[]=
{
  {"%s%02d:%02d:%02d", 10},
  {"%s%02d:%02d:%02d.%06d", 17},
  {"%04d-%02d-%02d", 10},
  {"%04d-%02d-%02d %02d:%02d:%02d", 19},
  {"%04d-%02d-%02d %02d:%02d:%02d.%06d", 26}
};


82 83 84 85 86 87 88 89
/*
  OPTIMIZATION TODO:
   - Replace the switch with a function that should be called for each
     date type.
   - Remove sprintf and opencode the conversion, like we do in
     Field_datetime.
*/

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
String *make_datetime(String *str, TIME *ltime, 
		      enum date_time_format_types format)
{
  char *buff;
  CHARSET_INFO *cs= &my_charset_bin;
  uint length= date_time_formats[format].length + 32;
  const char* format_str= date_time_formats[format].format_str;

  if (str->alloc(length))
    return 0;

  buff= (char*) str->ptr();
  switch (format) {
  case TIME_ONLY:
    length= cs->cset->snprintf(cs, buff, length, format_str, ltime->neg ? "-" : "",
			       ltime->hour, ltime->minute, ltime->second);
    break;
  case TIME_MICROSECOND:
    length= cs->cset->snprintf(cs, buff, length, format_str, ltime->neg ? "-" : "",
			       ltime->hour, ltime->minute, ltime->second,
			       ltime->second_part);
    break;
  case DATE_ONLY:
    length= cs->cset->snprintf(cs, buff, length, format_str,
			       ltime->year, ltime->month, ltime->day);
    break;
  case DATE_TIME:
    length= cs->cset->snprintf(cs, buff, length, format_str,
			       ltime->year, ltime->month, ltime->day,
			       ltime->hour, ltime->minute, ltime->second);
    break;
  case DATE_TIME_MICROSECOND:
    length= cs->cset->snprintf(cs, buff, length, format_str,
			       ltime->year, ltime->month, ltime->day,
			       ltime->hour, ltime->minute, ltime->second,
			       ltime->second_part);
    break;
  default:
    return 0;
  }

  str->length(length);
  str->set_charset(cs);
  return str;
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
136 137 138 139 140 141 142 143 144
/*
** Get a array of positive numbers from a string object.
** Each number is separated by 1 non digit character
** Return error if there is too many numbers.
** If there is too few numbers, assume that the numbers are left out
** from the high end. This allows one to give:
** DAY_TO_SECOND as "D MM:HH:SS", "MM:HH:SS" "HH:SS" or as seconds.
*/

145 146
bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs,
			uint count, long *values)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
147 148 149
{
  const char *end=str+length;
  uint i;
150
  while (str != end && !my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
151 152 153 154 155
    str++;

  for (i=0 ; i < count ; i++)
  {
    long value;
156
    for (value=0; str != end && my_isdigit(cs,*str) ; str++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
157 158
      value=value*10L + (long) (*str - '0');
    values[i]= value;
159
    while (str != end && !my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      str++;
    if (str == end && i != count-1)
    {
      i++;
      /* Change values[0...i-1] -> values[0...count-1] */
      bmove_upp((char*) (values+count), (char*) (values+i),
		sizeof(long)*i);
      bzero((char*) values, sizeof(long)*(count-i));
      break;
    }
  }
  return (str != end);
}

longlong Item_func_period_add::val_int()
{
  ulong period=(ulong) args[0]->val_int();
  int months=(int) args[1]->val_int();

  if ((null_value=args[0]->null_value || args[1]->null_value) ||
      period == 0L)
    return 0; /* purecov: inspected */
  return (longlong)
    convert_month_to_period((uint) ((int) convert_period_to_month(period)+
				    months));
}


longlong Item_func_period_diff::val_int()
{
  ulong period1=(ulong) args[0]->val_int();
  ulong period2=(ulong) args[1]->val_int();

  if ((null_value=args[0]->null_value || args[1]->null_value))
    return 0; /* purecov: inspected */
  return (longlong) ((long) convert_period_to_month(period1)-
		     (long) convert_period_to_month(period2));
}



longlong Item_func_to_days::val_int()
{
  TIME ltime;
  if (get_arg0_date(&ltime,0))
    return 0;
  return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
}

longlong Item_func_dayofyear::val_int()
{
  TIME ltime;
  if (get_arg0_date(&ltime,0))
    return 0;
  return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day) -
    calc_daynr(ltime.year,1,1) + 1;
}

longlong Item_func_dayofmonth::val_int()
{
  TIME ltime;
  (void) get_arg0_date(&ltime,1);
  return (longlong) ltime.day;
}

longlong Item_func_month::val_int()
{
  TIME ltime;
  (void) get_arg0_date(&ltime,1);
  return (longlong) ltime.month;
}

String* Item_func_monthname::val_str(String* str)
{
234
  uint   month=(uint) Item_func_month::val_int();
235
  if (!month)					// This is also true for NULL
236 237
  {
    null_value=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
238
    return (String*) 0;
239 240
  }
  null_value=0;
241 242
  
  String *m=&month_names[month-1];
243
  str->copy(m->ptr(), m->length(), m->charset(), default_charset());
244
  return str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}

// Returns the quarter of the year

longlong Item_func_quarter::val_int()
{
  TIME ltime;
  (void) get_arg0_date(&ltime,1);
  return (longlong) ((ltime.month+2)/3);
}

longlong Item_func_hour::val_int()
{
  TIME ltime;
  (void) get_arg0_time(&ltime);
  return ltime.hour;
}

longlong Item_func_minute::val_int()
{
  TIME ltime;
  (void) get_arg0_time(&ltime);
  return ltime.minute;
}
// Returns the second in time_exp in the range of 0 - 59

longlong Item_func_second::val_int()
{
  TIME ltime;
  (void) get_arg0_time(&ltime);
  return ltime.second;
}


279 280 281 282 283 284 285 286 287
/*
  Returns the week of year.

  The bits in week_format has the following meaning:
    0	If not set:	USA format: Sunday is first day of week
        If set:		ISO format: Monday is first day of week
    1   If not set:	Week is in range 0-53
    	If set		Week is in range 1-53.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
288 289 290 291

longlong Item_func_week::val_int()
{
  uint year;
292
  uint week_format;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
293 294 295
  TIME ltime;
  if (get_arg0_date(&ltime,0))
    return 0;
296
  week_format= (uint) args[1]->val_int();
297 298 299 300
  return (longlong) calc_week(&ltime, 
			      (week_format & 2) != 0,
			      (week_format & 1) == 0,
			      &year);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
301 302 303 304 305 306 307 308 309
}


longlong Item_func_yearweek::val_int()
{
  uint year,week;
  TIME ltime;
  if (get_arg0_date(&ltime,0))
    return 0;
310
  week=calc_week(&ltime, 1, (args[1]->val_int() & 1) == 0, &year);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  return week+year*100;
}


/* weekday() has a automatic to_days() on item */

longlong Item_func_weekday::val_int()
{
  ulong tmp_value=(ulong) args[0]->val_int();
  if ((null_value=(args[0]->null_value || !tmp_value)))
    return 0; /* purecov: inspected */

  return (longlong) calc_weekday(tmp_value,odbc_type)+test(odbc_type);
}

String* Item_func_dayname::val_str(String* str)
{
  uint weekday=(uint) val_int();		// Always Item_func_daynr()
  if (null_value)
    return (String*) 0;
331 332
  
  String *d=&day_names[weekday];
333
  str->copy(d->ptr(), d->length(), d->charset(), default_charset());
334
  return str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}


longlong Item_func_year::val_int()
{
  TIME ltime;
  (void) get_arg0_date(&ltime,1);
  return (longlong) ltime.year;
}


longlong Item_func_unix_timestamp::val_int()
{
  if (arg_count == 0)
    return (longlong) current_thd->query_start();
  if (args[0]->type() == FIELD_ITEM)
  {						// Optimize timestamp field
    Field *field=((Item_field*) args[0])->field;
    if (field->type() == FIELD_TYPE_TIMESTAMP)
      return ((Field_timestamp*) field)->get_timestamp();
  }
  String *str=args[0]->val_str(&value);
  if ((null_value=args[0]->null_value))
  {
    return 0; /* purecov: inspected */
  }
  return (longlong) str_to_timestamp(str->ptr(),str->length());
}


longlong Item_func_time_to_sec::val_int()
{
  TIME ltime;
368
  longlong seconds;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
369
  (void) get_arg0_time(&ltime);
370 371
  seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
  return ltime.neg ? -seconds : seconds;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
372 373 374 375
}


/*
376 377
  Convert a string to a interval value
  To make code easy, allow interval objects without separators.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
378 379 380 381 382
*/

static bool get_interval_value(Item *args,interval_type int_type,
			       String *str_value, INTERVAL *t)
{
383
  long array[5],value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
384 385 386
  const char *str;
  uint32 length;
  LINT_INIT(value);  LINT_INIT(str); LINT_INIT(length);
387
  CHARSET_INFO *cs=str_value->charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
388 389

  bzero((char*) t,sizeof(*t));
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
390
  if ((int) int_type <= INTERVAL_MICROSECOND)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  {
    value=(long) args->val_int();
    if (args->null_value)
      return 1;
    if (value < 0)
    {
      t->neg=1;
      value= -value;
    }
  }
  else
  {
    String *res;
    if (!(res=args->val_str(str_value)))
      return (1);

    /* record negative intervalls in t->neg */
    str=res->ptr();
    const char *end=str+res->length();
410
    while (str != end && my_isspace(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      str++;
    if (str != end && *str == '-')
    {
      t->neg=1;
      str++;
    }
    length=(uint32) (end-str);		// Set up pointers to new str
  }

  switch (int_type) {
  case INTERVAL_YEAR:
    t->year=value;
    break;
  case INTERVAL_MONTH:
    t->month=value;
    break;
  case INTERVAL_DAY:
    t->day=value;
    break;
  case INTERVAL_HOUR:
    t->hour=value;
    break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
433 434 435
  case INTERVAL_MICROSECOND:
    t->second_part=value;
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
436 437 438 439 440 441 442
  case INTERVAL_MINUTE:
    t->minute=value;
    break;
  case INTERVAL_SECOND:
    t->second=value;
    break;
  case INTERVAL_YEAR_MONTH:			// Allow YEAR-MONTH YYYYYMM
443
    if (get_interval_info(str,length,cs,2,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
444 445 446 447 448
      return (1);
    t->year=array[0];
    t->month=array[1];
    break;
  case INTERVAL_DAY_HOUR:
449
    if (get_interval_info(str,length,cs,2,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
450 451 452 453
      return (1);
    t->day=array[0];
    t->hour=array[1];
    break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
454 455 456 457 458 459 460 461 462
  case INTERVAL_DAY_MICROSECOND:
    if (get_interval_info(str,length,cs,5,array))
      return (1);
    t->day=array[0];
    t->hour=array[1];
    t->minute=array[2];
    t->second=array[3];
    t->second_part=array[4];
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
463
  case INTERVAL_DAY_MINUTE:
464
    if (get_interval_info(str,length,cs,3,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465 466 467 468 469 470
      return (1);
    t->day=array[0];
    t->hour=array[1];
    t->minute=array[2];
    break;
  case INTERVAL_DAY_SECOND:
471
    if (get_interval_info(str,length,cs,4,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
472 473 474 475 476 477
      return (1);
    t->day=array[0];
    t->hour=array[1];
    t->minute=array[2];
    t->second=array[3];
    break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
478 479 480 481 482 483 484 485
  case INTERVAL_HOUR_MICROSECOND:
    if (get_interval_info(str,length,cs,4,array))
      return (1);
    t->hour=array[0];
    t->minute=array[1];
    t->second=array[2];
    t->second_part=array[3];
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
486
  case INTERVAL_HOUR_MINUTE:
487
    if (get_interval_info(str,length,cs,2,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
488 489 490 491 492
      return (1);
    t->hour=array[0];
    t->minute=array[1];
    break;
  case INTERVAL_HOUR_SECOND:
493
    if (get_interval_info(str,length,cs,3,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
494 495 496 497 498
      return (1);
    t->hour=array[0];
    t->minute=array[1];
    t->second=array[2];
    break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
499 500 501 502 503 504 505
  case INTERVAL_MINUTE_MICROSECOND:
    if (get_interval_info(str,length,cs,3,array))
      return (1);
    t->minute=array[0];
    t->second=array[1];
    t->second_part=array[2];
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
506
  case INTERVAL_MINUTE_SECOND:
507
    if (get_interval_info(str,length,cs,2,array))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
508 509 510 511
      return (1);
    t->minute=array[0];
    t->second=array[1];
    break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
512 513 514 515 516 517
  case INTERVAL_SECOND_MICROSECOND:
    if (get_interval_info(str,length,cs,2,array))
      return (1);
    t->second=array[0];
    t->second_part=array[1];
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
518 519 520 521 522 523 524 525 526 527 528 529
  }
  return 0;
}


String *Item_date::val_str(String *str)
{
  ulong value=(ulong) val_int();
  if (null_value)
    return (String*) 0;
  if (!value)					// zero daynr
  {
530
    str->copy("0000-00-00",10,&my_charset_latin1,default_charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
531 532
    return str;
  }
533 534 535
  
  char tmpbuff[11];
  sprintf(tmpbuff,"%04d-%02d-%02d",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
536 537 538
	  (int) (value/10000L) % 10000,
	  (int) (value/100)%100,
	  (int) (value%100));
539
  str->copy(tmpbuff,10,&my_charset_latin1,default_charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
540 541 542 543
  return str;
}


monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
544
int Item_date::save_in_field(Field *field, bool no_conversions)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
{
  TIME ltime;
  timestamp_type t_type=TIMESTAMP_FULL;
  if (get_date(&ltime,1))
  {
    if (null_value)
      return set_field_to_null(field);
    t_type=TIMESTAMP_NONE;			// Error
  }
  field->set_notnull();
  field->store_time(&ltime,t_type);
  return 0;
}


longlong Item_func_from_days::val_int()
{
  longlong value=args[0]->val_int();
  if ((null_value=args[0]->null_value))
    return 0; /* purecov: inspected */

  uint year,month,day;
  get_date_from_daynr((long) value,&year,&month,&day);
  return (longlong) (year*10000L+month*100+day);
}


void Item_func_curdate::fix_length_and_dec()
{
574
  struct tm start;
575
  
576
  collation.set(default_charset());
577
  decimals=0; 
578
  max_length=10*default_charset()->mbmaxlen;
579 580 581 582 583 584

  store_now_in_tm(current_thd->query_start(),&start);
  
  value=(longlong) ((ulong) ((uint) start.tm_year+1900)*10000L+
		    ((uint) start.tm_mon+1)*100+
		    (uint) start.tm_mday);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
585
  /* For getdate */
586 587 588
  ltime.year=	start.tm_year+1900;
  ltime.month=	start.tm_mon+1;
  ltime.day=	start.tm_mday;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
589 590 591 592 593
  ltime.hour=	0;
  ltime.minute=	0;
  ltime.second=	0;
  ltime.second_part=0;
  ltime.neg=0;
594
  ltime.time_type=TIMESTAMP_DATE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
595 596
}

597

bk@work.mysql.com's avatar
bk@work.mysql.com committed
598 599 600 601 602 603 604
bool Item_func_curdate::get_date(TIME *res,
				 bool fuzzy_date __attribute__((unused)))
{
  *res=ltime;
  return 0;
}

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

/*
    Converts time in time_t to struct tm represenatation for local timezone.
    Defines timezone (local) used for whole CURDATE function
*/
void Item_func_curdate_local::store_now_in_tm(time_t now, struct tm *now_tm)
{
  localtime_r(&now,now_tm);
}


/*
    Converts time in time_t to struct tm represenatation for UTC
    Defines timezone (UTC) used for whole UTC_DATE function
*/
void Item_func_curdate_utc::store_now_in_tm(time_t now, struct tm *now_tm)
{
  gmtime_r(&now,now_tm);
}


626 627
String *Item_func_curtime::val_str(String *str)
{ 
628
  str_value.set(buff,buff_length,default_charset());
629 630 631
  return &str_value;
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
632 633
void Item_func_curtime::fix_length_and_dec()
{
634 635
  struct tm start;
  CHARSET_INFO *cs= default_charset();
636 637 638

  decimals=0; 
  max_length=8*cs->mbmaxlen;
639
  collation.set(cs);
640 641 642 643 644 645
  
  store_now_in_tm(current_thd->query_start(),&start);
  
  value=(longlong) ((ulong) ((uint) start.tm_hour)*10000L+
		    (ulong) (((uint) start.tm_min)*100L+
			     (uint) start.tm_sec));
646

647
  buff_length=cs->cset->snprintf(cs,buff,sizeof(buff),"%02d:%02d:%02d",
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
				 (int) start.tm_hour,
				 (int) start.tm_min,
				 (int) start.tm_sec);
}


/*
    Converts time in time_t to struct tm represenatation for local timezone.
    Defines timezone (local) used for whole CURTIME function
*/
void Item_func_curtime_local::store_now_in_tm(time_t now, struct tm *now_tm)
{
  localtime_r(&now,now_tm);
}


/*
    Converts time in time_t to struct tm represenatation for UTC.
    Defines timezone (UTC) used for whole UTC_TIME function
*/
void Item_func_curtime_utc::store_now_in_tm(time_t now, struct tm *now_tm)
{
  gmtime_r(&now,now_tm);
671 672
}

673

674 675
String *Item_func_now::val_str(String *str)
{
676
  str_value.set(buff,buff_length,default_charset());
677
  return &str_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
678 679
}

680

bk@work.mysql.com's avatar
bk@work.mysql.com committed
681 682
void Item_func_now::fix_length_and_dec()
{
683
  struct tm start;
684
  CHARSET_INFO *cs= &my_charset_bin;
685 686 687
  
  decimals=0;
  max_length=19*cs->mbmaxlen;
688
  collation.set(cs);
689 690 691 692 693 694 695 696 697

  store_now_in_tm(current_thd->query_start(),&start);
  
  value=((longlong) ((ulong) ((uint) start.tm_year+1900)*10000L+
		     (((uint) start.tm_mon+1)*100+
		      (uint) start.tm_mday))*(longlong) 1000000L+
	 (longlong) ((ulong) ((uint) start.tm_hour)*10000L+
		     (ulong) (((uint) start.tm_min)*100L+
			    (uint) start.tm_sec)));
698
  
699
  buff_length= (uint) cs->cset->snprintf(cs,buff, sizeof(buff),
700
				   "%04d-%02d-%02d %02d:%02d:%02d",
701 702 703 704 705 706
				   ((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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
707
  /* For getdate */
708 709 710 711 712 713
  ltime.year=	start.tm_year+1900;
  ltime.month=	start.tm_mon+1;
  ltime.day=	start.tm_mday;
  ltime.hour=	start.tm_hour;
  ltime.minute=	start.tm_min;
  ltime.second=	start.tm_sec;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
714 715
  ltime.second_part=0;
  ltime.neg=0;
716
  ltime.time_type=TIMESTAMP_FULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
717 718 719 720 721 722 723 724 725 726
}

bool Item_func_now::get_date(TIME *res,
			     bool fuzzy_date __attribute__((unused)))
{
  *res=ltime;
  return 0;
}


monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
727
int Item_func_now::save_in_field(Field *to, bool no_conversions)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
728 729 730 731 732 733 734
{
  to->set_notnull();
  to->store_time(&ltime,TIMESTAMP_FULL);
  return 0;
}


735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
/*
    Converts time in time_t to struct tm represenatation for local timezone.
    Defines timezone (local) used for whole CURRENT_TIMESTAMP function
*/
void Item_func_now_local::store_now_in_tm(time_t now, struct tm *now_tm)
{
  localtime_r(&now,now_tm);
}


/*
    Converts time in time_t to struct tm represenatation for UTC.
    Defines timezone (UTC) used for whole UTC_TIMESTAMP function
*/
void Item_func_now_utc::store_now_in_tm(time_t now, struct tm *now_tm)
{
  gmtime_r(&now,now_tm);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
755 756
String *Item_func_sec_to_time::val_str(String *str)
{
757
  char buff[23*2];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
758 759
  const char *sign="";
  longlong seconds=(longlong) args[0]->val_int();
760
  ulong length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762 763 764 765 766 767
  if ((null_value=args[0]->null_value))
    return (String*) 0;
  if (seconds < 0)
  {
    seconds= -seconds;
    sign= "-";
  }
768
  uint sec= (uint) ((ulonglong) seconds % 3600);
769 770
  length= my_sprintf(buff,(buff,"%s%02lu:%02u:%02u",sign,(long) (seconds/3600),
			   sec/60, sec % 60));
771
  str->copy(buff, length, &my_charset_latin1, default_charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  return str;
}


longlong Item_func_sec_to_time::val_int()
{
  longlong seconds=args[0]->val_int();
  longlong sign=1;
  if ((null_value=args[0]->null_value))
    return 0;
  if (seconds < 0)
  {
    seconds= -seconds;
    sign= -1;
  }
  return sign*((seconds / 3600)*10000+((seconds/60) % 60)*100+ (seconds % 60));
}


void Item_func_date_format::fix_length_and_dec()
{
  decimals=0;
  if (args[1]->type() == STRING_ITEM)
  {						// Optimize the normal case
    fixed_length=1;
    max_length=format_length(((Item_string*) args[1])->const_string());
  }
  else
  {
    fixed_length=0;
    max_length=args[1]->max_length*10;
    set_if_smaller(max_length,MAX_BLOB_WIDTH);
  }
  maybe_null=1;					// If wrong date
}


uint Item_func_date_format::format_length(const String *format)
{
  uint size=0;
  const char *ptr=format->ptr();
  const char *end=ptr+format->length();

  for (; ptr != end ; ptr++)
  {
    if (*ptr != '%' || ptr == end-1)
      size++;
    else
    {
      switch(*++ptr) {
      case 'M': /* month, textual */
      case 'W': /* day (of the week), textual */
	size += 9;
	break;
      case 'D': /* day (of the month), numeric plus english suffix */
      case 'Y': /* year, numeric, 4 digits */
      case 'x': /* Year, used with 'v' */
      case 'X': /* Year, used with 'v, where week starts with Monday' */
	size += 4;
	break;
      case 'a': /* locale's abbreviated weekday name (Sun..Sat) */
      case 'b': /* locale's abbreviated month name (Jan.Dec) */
      case 'j': /* day of year (001..366) */
	size += 3;
	break;
      case 'U': /* week (00..52) */
      case 'u': /* week (00..52), where week starts with Monday */
      case 'V': /* week 1..53 used with 'x' */
      case 'v': /* week 1..53 used with 'x', where week starts with Monday */
      case 'H': /* hour (00..23) */
      case 'y': /* year, numeric, 2 digits */
      case 'm': /* month, numeric */
      case 'd': /* day (of the month), numeric */
      case 'h': /* hour (01..12) */
      case 'I': /* --||-- */
      case 'i': /* minutes, numeric */
      case 'k': /* hour ( 0..23) */
      case 'l': /* hour ( 1..12) */
      case 'p': /* locale's AM or PM */
      case 'S': /* second (00..61) */
      case 's': /* seconds, numeric */
      case 'c': /* month (0..12) */
      case 'e': /* day (0..31) */
	size += 2;
	break;
      case 'r': /* time, 12-hour (hh:mm:ss [AP]M) */
	size += 11;
	break;
      case 'T': /* time, 24-hour (hh:mm:ss) */
	size += 8;
	break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
863 864 865
      case 'f': /* microseconds */
	size += 6;
	break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
      case 'w': /* day (of the week), numeric */
      case '%':
      default:
	size++;
	break;
      }
    }
  }
  return size;
}


String *Item_func_date_format::val_str(String *str)
{
  String *format;
  TIME l_time;
  char intbuff[15];
  uint size,weekday;
884
  ulong length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

  if (!date_or_time)
  {
    if (get_arg0_date(&l_time,1))
      return 0;
  }
  else
  {
    String *res;
    if (!(res=args[0]->val_str(str)))
    {
      null_value=1;
      return 0;
    }
    if (str_to_time(res->ptr(),res->length(),&l_time))
    {
      null_value=1;
      return 0;
    }
    l_time.year=l_time.month=l_time.day=0;
    null_value=0;
  }

  if (!(format = args[1]->val_str(str)) || !format->length())
  {
    null_value=1;
    return 0;
  }

  if (fixed_length)
    size=max_length;
  else
    size=format_length(format);
  if (format == str)
monty@hundin.mysql.fi's avatar
merge  
monty@hundin.mysql.fi committed
919
    str= &value;				// Save result here
bk@work.mysql.com's avatar
bk@work.mysql.com committed
920 921 922 923 924 925 926 927 928 929
  if (str->alloc(size))
  {
    null_value=1;
    return 0;
  }
  str->length(0);

  /* Create the result string */
  const char *ptr=format->ptr();
  const char *end=ptr+format->length();
930
  for (; ptr != end ; ptr++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
931 932 933 934 935 936 937
  {
    if (*ptr != '%' || ptr+1 == end)
      str->append(*ptr);
    else
    {
      switch (*++ptr) {
      case 'M':
938
	if (!l_time.month)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
939 940 941 942 943 944 945
	{
	  null_value=1;
	  return 0;
	}
	str->append(month_names[l_time.month-1]);
	break;
      case 'b':
946
	if (!l_time.month)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
947 948 949 950 951 952 953
	{
	  null_value=1;
	  return 0;
	}
	str->append(month_names[l_time.month-1].ptr(),3);
	break;
      case 'W':
954
	if (date_or_time)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
955 956 957 958 959 960 961 962
	{
	  null_value=1;
	  return 0;
	}
	weekday=calc_weekday(calc_daynr(l_time.year,l_time.month,l_time.day),0);
	str->append(day_names[weekday]);
	break;
      case 'a':
963
	if (date_or_time)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
964 965 966 967 968 969 970 971
	{
	  null_value=1;
	  return 0;
	}
	weekday=calc_weekday(calc_daynr(l_time.year,l_time.month,l_time.day),0);
	str->append(day_names[weekday].ptr(),3);
	break;
      case 'D':
972
	if (date_or_time)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973 974 975 976
	{
	  null_value=1;
	  return 0;
	}
977 978
	length= int10_to_str(l_time.day, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
979 980 981 982
	if (l_time.day >= 10 &&  l_time.day <= 19)
	  str->append("th");
	else
	{
983
	  switch (l_time.day %10) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
984
	  case 1:
985
	    str->append("st",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
986 987
	    break;
	  case 2:
988
	    str->append("nd",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
989 990
	    break;
	  case 3:
991
	    str->append("rd",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
992 993
	    break;
	  default:
994
	    str->append("th",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
995 996 997 998 999
	    break;
	  }
	}
	break;
      case 'Y':
1000 1001
	length= int10_to_str(l_time.year, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 4, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1002 1003
	break;
      case 'y':
1004 1005
	length= int10_to_str(l_time.year%100, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1006 1007
	break;
      case 'm':
1008 1009
	length= int10_to_str(l_time.month, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1010 1011
	break;
      case 'c':
1012 1013
	length= int10_to_str(l_time.month, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1014 1015
	break;
      case 'd':
1016 1017
	length= int10_to_str(l_time.day, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1018 1019
	break;
      case 'e':
1020 1021
	length= int10_to_str(l_time.day, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1022
	break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1023
      case 'f':
1024 1025
	length= int10_to_str(l_time.second_part, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 6, '0');
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1026
	break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1027
      case 'H':
1028 1029
	length= int10_to_str(l_time.hour, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1030 1031 1032
	break;
      case 'h':
      case 'I':
1033 1034
	length= int10_to_str((l_time.hour+11)%12+1, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1035 1036
	break;
      case 'i':					/* minutes */
1037 1038
	length= int10_to_str(l_time.minute, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1039 1040
	break;
      case 'j':
1041
	if (date_or_time)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1042 1043 1044 1045
	{
	  null_value=1;
	  return 0;
	}
1046 1047 1048
	length= int10_to_str(calc_daynr(l_time.year,l_time.month,l_time.day) - 
		     calc_daynr(l_time.year,1,1) + 1, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 3, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1049 1050
	break;
      case 'k':
1051 1052
	length= int10_to_str(l_time.hour, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1053 1054
	break;
      case 'l':
1055 1056
	length= int10_to_str((l_time.hour+11)%12+1, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1057 1058
	break;
      case 'p':
1059
	str->append(l_time.hour < 12 ? "AM" : "PM",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1060 1061
	break;
      case 'r':
1062 1063 1064 1065 1066 1067 1068
	length= my_sprintf(intbuff, 
		   (intbuff, 
		    (l_time.hour < 12) ? "%02d:%02d:%02d AM" : "%02d:%02d:%02d PM",
		    (l_time.hour+11)%12+1,
		    l_time.minute,
		    l_time.second));
	str->append(intbuff, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1069 1070 1071
	break;
      case 'S':
      case 's':
1072 1073
	length= int10_to_str(l_time.second, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1074 1075
	break;
      case 'T':
1076 1077 1078 1079 1080 1081 1082
	length= my_sprintf(intbuff, 
		   (intbuff, 
		    "%02d:%02d:%02d", 
		    l_time.hour, 
		    l_time.minute,
		    l_time.second));
	str->append(intbuff, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1083 1084 1085 1086 1087
	break;
      case 'U':
      case 'u':
      {
	uint year;
1088 1089 1090
	length= int10_to_str(calc_week(&l_time, 0, (*ptr) == 'U', &year),
		     intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1091 1092 1093 1094 1095 1096
      }
      break;
      case 'v':
      case 'V':
      {
	uint year;
1097 1098 1099
	length= int10_to_str(calc_week(&l_time, 1, (*ptr) == 'V', &year),
		     intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 2, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1100 1101 1102 1103 1104 1105 1106
      }
      break;
      case 'x':
      case 'X':
      {
	uint year;
	(void) calc_week(&l_time, 1, (*ptr) == 'X', &year);
1107 1108
	length= int10_to_str(year, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 4, '0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1109 1110 1111 1112
      }
      break;
      case 'w':
	weekday=calc_weekday(calc_daynr(l_time.year,l_time.month,l_time.day),1);
1113 1114 1115
	length= int10_to_str(weekday, intbuff, 10) - intbuff;
	str->append_with_prefill(intbuff, length, 1, '0');

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	break;
      default:
	str->append(*ptr);
	break;
      }
    }
  }
  return str;
}


String *Item_func_from_unixtime::val_str(String *str)
{
  struct tm tm_tmp,*start;
  time_t tmp=(time_t) args[0]->val_int();
1131
  uint32 l;
1132
  CHARSET_INFO *cs=default_charset();
1133
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1134 1135 1136 1137
  if ((null_value=args[0]->null_value))
    return 0;
  localtime_r(&tmp,&tm_tmp);
  start=&tm_tmp;
1138 1139 1140
  
  l=20*cs->mbmaxlen+32;
  if (str->alloc(l))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1141
    return str;					/* purecov: inspected */
1142
  l=cs->cset->snprintf(cs,(char*) str->ptr(),l,"%04d-%02d-%02d %02d:%02d:%02d",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1143 1144 1145 1146 1147 1148
	  (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);
1149 1150
  str->length(l);
  str->set_charset(cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
  return str;
}


longlong Item_func_from_unixtime::val_int()
{
  time_t tmp=(time_t) (ulong) args[0]->val_int();
  if ((null_value=args[0]->null_value))
    return 0;
  struct tm tm_tmp,*start;
  localtime_r(&tmp,&tm_tmp);
  start= &tm_tmp;
  return ((longlong) ((ulong) ((uint) start->tm_year+1900)*10000L+
		      (((uint) start->tm_mon+1)*100+
		       (uint) start->tm_mday))*LL(1000000)+
	  (longlong) ((ulong) ((uint) start->tm_hour)*10000L+
		      (ulong) (((uint) start->tm_min)*100L+
			       (uint) start->tm_sec)));
}

bool Item_func_from_unixtime::get_date(TIME *ltime,
				       bool fuzzy_date __attribute__((unused)))
{
  time_t tmp=(time_t) (ulong) args[0]->val_int();
  if ((null_value=args[0]->null_value))
    return 1;
  struct tm tm_tmp,*start;
  localtime_r(&tmp,&tm_tmp);
  start= &tm_tmp;
  ltime->year=	start->tm_year+1900;
  ltime->month=	start->tm_mon+1;
  ltime->day=	start->tm_mday;
  ltime->hour=	start->tm_hour;
  ltime->minute=start->tm_min;
  ltime->second=start->tm_sec;
  ltime->second_part=0;
  ltime->neg=0;
  return 0;
}

1191 1192 1193 1194

void Item_date_add_interval::fix_length_and_dec()
{
  enum_field_types arg0_field_type;
1195
  collation.set(default_charset());
1196
  maybe_null=1;
1197
  max_length=26*MY_CHARSET_BIN_MB_MAXLEN;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  value.alloc(32);

  /*
    The field type for the result of an Item_date function is defined as
    follows:

    - If first arg is a MYSQL_TYPE_DATETIME result is MYSQL_TYPE_DATETIME
    - If first arg is a MYSQL_TYPE_DATE and the interval type uses hours,
      minutes or seconds then type is MYSQL_TYPE_DATETIME.
    - Otherwise the result is MYSQL_TYPE_STRING
      (This is because you can't know if the string contains a DATE, TIME or
      DATETIME argument)
  */
  cached_field_type= MYSQL_TYPE_STRING;
  arg0_field_type= args[0]->field_type();
  if (arg0_field_type == MYSQL_TYPE_DATETIME ||
      arg0_field_type == MYSQL_TYPE_TIMESTAMP)
    cached_field_type= MYSQL_TYPE_DATETIME;
  else if (arg0_field_type == MYSQL_TYPE_DATE)
  {
1218
    if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
1219 1220 1221 1222 1223 1224 1225 1226
      cached_field_type= arg0_field_type;
    else
      cached_field_type= MYSQL_TYPE_DATETIME;
  }
}


/* Here arg[1] is a Item_interval object */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242

bool Item_date_add_interval::get_date(TIME *ltime, bool fuzzy_date)
{
  long period,sign;
  INTERVAL interval;

  if (args[0]->get_date(ltime,0) ||
      get_interval_value(args[1],int_type,&value,&interval))
    goto null_date;
  sign= (interval.neg ? -1 : 1);
  if (date_sub_interval)
    sign = -sign;

  null_value=0;
  switch (int_type) {
  case INTERVAL_SECOND:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1243 1244
  case INTERVAL_SECOND_MICROSECOND:
  case INTERVAL_MICROSECOND:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1245 1246
  case INTERVAL_MINUTE:
  case INTERVAL_HOUR:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1247
  case INTERVAL_MINUTE_MICROSECOND:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1248
  case INTERVAL_MINUTE_SECOND:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1249
  case INTERVAL_HOUR_MICROSECOND:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1250 1251
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_HOUR_MINUTE:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1252
  case INTERVAL_DAY_MICROSECOND:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1253 1254 1255
  case INTERVAL_DAY_SECOND:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_HOUR:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1256
    long sec,days,daynr,microseconds,extra_sec;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1257
    ltime->time_type=TIMESTAMP_FULL;		// Return full date
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1258 1259 1260
    microseconds= ltime->second_part + sign*interval.second_part;
    extra_sec= microseconds/1000000L;
    microseconds= microseconds%1000000L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1261 1262 1263 1264

    sec=((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
	 ltime->second +
	 sign*(interval.day*3600*24L +
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1265 1266 1267 1268 1269 1270 1271 1272
	       interval.hour*3600+interval.minute*60+interval.second))+
      extra_sec;

    if (microseconds < 0)
    {
      microseconds+= 1000000L;	
      sec--;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1273 1274 1275 1276 1277 1278
    days=sec/(3600*24L); sec=sec-days*3600*24L;
    if (sec < 0)
    {
      days--;
      sec+=3600*24L;
    }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1279
    ltime->second_part= microseconds;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1280 1281 1282 1283 1284
    ltime->second=sec % 60;
    ltime->minute=sec/60 % 60;
    ltime->hour=sec/3600;
    daynr= calc_daynr(ltime->year,ltime->month,1) + days;
    get_date_from_daynr(daynr,&ltime->year,&ltime->month,&ltime->day);
1285
    if (daynr < 0 || daynr >= MAX_DAY_NUMBER) // Day number from year 0 to 9999-12-31
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1286 1287 1288 1289 1290
      goto null_date;
    break;
  case INTERVAL_DAY:
    period= calc_daynr(ltime->year,ltime->month,ltime->day) +
      sign*interval.day;
1291
    if (period < 0 || period >= MAX_DAY_NUMBER) // Daynumber from year 0 to 9999-12-31
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
      goto null_date;
    get_date_from_daynr((long) period,&ltime->year,&ltime->month,&ltime->day);
    break;
  case INTERVAL_YEAR:
    ltime->year += sign*interval.year;
    if ((int) ltime->year < 0 || ltime->year >= 10000L)
      goto null_date;
    if (ltime->month == 2 && ltime->day == 29 &&
	calc_days_in_year(ltime->year) != 366)
      ltime->day=28;				// Was leap-year
    break;
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    period= (ltime->year*12 + sign*interval.year*12 +
	     ltime->month-1 + sign*interval.month);
    if (period < 0 || period >= 120000L)
      goto null_date;
    ltime->year= (uint) (period / 12);
    ltime->month= (uint) (period % 12L)+1;
    /* Adjust day if the new month doesn't have enough days */
    if (ltime->day > days_in_month[ltime->month-1])
    {
      ltime->day = days_in_month[ltime->month-1];
      if (ltime->month == 2 && calc_days_in_year(ltime->year) == 366)
	ltime->day++;				// Leap-year
    }
    break;
  default:
    goto null_date;
  }
  return 0;					// Ok

 null_date:
  return (null_value=1);
}


String *Item_date_add_interval::val_str(String *str)
{
  TIME ltime;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1332
  enum date_time_format_types format;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1333 1334 1335

  if (Item_date_add_interval::get_date(&ltime,0))
    return 0;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1336

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1337
  if (ltime.time_type == TIMESTAMP_DATE)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1338 1339 1340
    format= DATE_ONLY;
  else if (ltime.second_part)
    format= DATE_TIME_MICROSECOND;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1341
  else
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1342 1343 1344 1345
    format= DATE_TIME;

  if (make_datetime(str, &ltime, format))
    return str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

  null_value=1;
  return 0;
}

longlong Item_date_add_interval::val_int()
{
  TIME ltime;
  if (Item_date_add_interval::get_date(&ltime,0))
    return (longlong) 0;
  return ((longlong) (((ulong) ltime.year)*10000L+
		      (((uint) ltime.month)*100+
		       (uint) ltime.day))*(longlong) 1000000L+
	  (longlong) ((ulong) ((uint) ltime.hour)*10000L+
		      (ulong) (((uint)ltime.minute)*100L+
			       (uint) ltime.second)));
}

void Item_extract::fix_length_and_dec()
{
  value.alloc(32);				// alloc buffer

  maybe_null=1;					// If wrong date
  switch (int_type) {
  case INTERVAL_YEAR:		max_length=4; date_value=1; break;
  case INTERVAL_YEAR_MONTH:	max_length=6; date_value=1; break;
  case INTERVAL_MONTH:		max_length=2; date_value=1; break;
  case INTERVAL_DAY:		max_length=2; date_value=1; break;
  case INTERVAL_DAY_HOUR:	max_length=9; date_value=0; break;
  case INTERVAL_DAY_MINUTE:	max_length=11; date_value=0; break;
  case INTERVAL_DAY_SECOND:	max_length=13; date_value=0; break;
  case INTERVAL_HOUR:		max_length=2; date_value=0; break;
  case INTERVAL_HOUR_MINUTE:	max_length=4; date_value=0; break;
  case INTERVAL_HOUR_SECOND:	max_length=6; date_value=0; break;
  case INTERVAL_MINUTE:		max_length=2; date_value=0; break;
  case INTERVAL_MINUTE_SECOND:	max_length=4; date_value=0; break;
  case INTERVAL_SECOND:		max_length=2; date_value=0; break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1383 1384 1385 1386 1387
  case INTERVAL_MICROSECOND:	max_length=2; date_value=0; break;
  case INTERVAL_DAY_MICROSECOND: max_length=20; date_value=0; break;
  case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
  case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
  case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
  }
}


longlong Item_extract::val_int()
{
  TIME ltime;
  long neg;
  if (date_value)
  {
    if (get_arg0_date(&ltime,1))
      return 0;
    neg=1;
  }
  else
  {
    String *res= args[0]->val_str(&value);
    if (!res || str_to_time(res->ptr(),res->length(),&ltime))
    {
      null_value=1;
      return 0;
    }
    neg= ltime.neg ? -1 : 1;
    null_value=0;
  }

  switch (int_type) {
  case INTERVAL_YEAR:		return ltime.year;
  case INTERVAL_YEAR_MONTH:	return ltime.year*100L+ltime.month;
  case INTERVAL_MONTH:		return ltime.month;
  case INTERVAL_DAY:		return ltime.day;
  case INTERVAL_DAY_HOUR:	return (long) (ltime.day*100L+ltime.hour)*neg;
  case INTERVAL_DAY_MINUTE:	return (long) (ltime.day*10000L+
					       ltime.hour*100L+
					       ltime.minute)*neg;
  case INTERVAL_DAY_SECOND:	 return ((longlong) ltime.day*1000000L+
					 (longlong) (ltime.hour*10000L+
						     ltime.minute*100+
						     ltime.second))*neg;
  case INTERVAL_HOUR:		return (long) ltime.hour*neg;
  case INTERVAL_HOUR_MINUTE:	return (long) (ltime.hour*100+ltime.minute)*neg;
  case INTERVAL_HOUR_SECOND:	return (long) (ltime.hour*10000+ltime.minute*100+
					       ltime.second)*neg;
  case INTERVAL_MINUTE:		return (long) ltime.minute*neg;
  case INTERVAL_MINUTE_SECOND:	return (long) (ltime.minute*100+ltime.second)*neg;
  case INTERVAL_SECOND:		return (long) ltime.second*neg;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
  case INTERVAL_MICROSECOND:	return (long) ltime.second_part*neg;
  case INTERVAL_DAY_MICROSECOND: return (((longlong)ltime.day*1000000L +
					  (longlong)ltime.hour*10000L +
					  ltime.minute*100 +
					  ltime.second)*1000000L +
					 ltime.second_part)*neg;
  case INTERVAL_HOUR_MICROSECOND: return (((longlong)ltime.hour*10000L +
					   ltime.minute*100 +
					   ltime.second)*1000000L +
					  ltime.second_part)*neg;
  case INTERVAL_MINUTE_MICROSECOND: return (((longlong)(ltime.minute*100+
							ltime.second))*1000000L+
					    ltime.second_part)*neg;
  case INTERVAL_SECOND_MICROSECOND: return ((longlong)ltime.second*1000000L+
					    ltime.second_part)*neg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1449 1450 1451
  }
  return 0;					// Impossible
}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1452 1453 1454 1455


void Item_typecast::print(String *str)
{
1456
  str->append("CAST(");
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1457 1458 1459 1460 1461
  args[0]->print(str);
  str->append(" AS ");
  str->append(func_name());
  str->append(')');
}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1462

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
String *Item_datetime_typecast::val_str(String *str)
{
  TIME ltime;

  if (!get_arg0_date(&ltime,1) &&
      make_datetime(str, &ltime, ltime.second_part ?
	    DATE_TIME_MICROSECOND : DATE_TIME))
  return str;

null_date:
  null_value=1;
  return 0;
}


bool Item_time_typecast::get_time(TIME *ltime)
{
  bool res= get_arg0_time(ltime);
  ltime->time_type= TIMESTAMP_TIME;
  return res;
}


String *Item_time_typecast::val_str(String *str)
{
  TIME ltime;

  if (!get_arg0_time(&ltime) &&
      make_datetime(str, &ltime, ltime.second_part ? TIME_MICROSECOND : TIME_ONLY))
    return str;

  null_value=1;
  return 0;
}


bool Item_date_typecast::get_date(TIME *ltime, bool fuzzy_date)
{
  bool res= get_arg0_date(ltime,1);
  ltime->time_type= TIMESTAMP_DATE;
  return res;
}


String *Item_date_typecast::val_str(String *str)
{
  TIME ltime;

  if (!get_arg0_date(&ltime,1) &&
      make_datetime(str, &ltime, DATE_ONLY))
  return str;

null_date:
  null_value=1;
  return 0;
}

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
/*
  MAKEDATE(a,b) is a date function that creates a date value 
  from a year and day value.
*/

String *Item_func_makedate::val_str(String *str)
{
  TIME l_time;
  long daynr= args[1]->val_int();
  long yearnr= args[0]->val_int();
  long days;

  if (args[0]->null_value || args[1]->null_value ||
      yearnr < 0 || daynr <= 0)
    goto null_date;

  days= calc_daynr(yearnr,1,1) + daynr - 1;
1537
  if (days > 0 || days < MAX_DAY_NUMBER) // Day number from year 0 to 9999-12-31
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
  {
    null_value=0;
    get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
    if (make_datetime(str, &l_time, DATE_ONLY))
      return str;
  }

null_date:
  null_value=1;
  return 0;
}


void Item_func_add_time::fix_length_and_dec()
{
  enum_field_types arg0_field_type;
  decimals=0;
1555
  max_length=26*MY_CHARSET_BIN_MB_MAXLEN;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

  /*
    The field type for the result of an Item_func_add_time function is defined as
    follows:

    - If first arg is a MYSQL_TYPE_DATETIME or MYSQL_TYPE_TIMESTAMP 
      result is MYSQL_TYPE_DATETIME
    - If first arg is a MYSQL_TYPE_TIME result is MYSQL_TYPE_TIME
    - Otherwise the result is MYSQL_TYPE_STRING
  */

  cached_field_type= MYSQL_TYPE_STRING;
  arg0_field_type= args[0]->field_type();
1569 1570
  if (arg0_field_type == MYSQL_TYPE_DATE ||
      arg0_field_type == MYSQL_TYPE_DATETIME ||
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
      arg0_field_type == MYSQL_TYPE_TIMESTAMP)
    cached_field_type= MYSQL_TYPE_DATETIME;
  else if (arg0_field_type == MYSQL_TYPE_TIME)
    cached_field_type= MYSQL_TYPE_TIME;
}

/*
  ADDTIME(t,a) and SUBTIME(t,a) are time functions that calculate a time/datetime value 

  t: time_or_datetime_expression
  a: time_expression
  
  Result: Time value or datetime value
*/

String *Item_func_add_time::val_str(String *str)
{
  TIME l_time1, l_time2, l_time3;
1589
  bool is_time= 0;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1590 1591 1592 1593 1594
  long microseconds, seconds, days= 0;
  int l_sign= sign;

  null_value=0;
  l_time3.neg= 0;
1595 1596 1597 1598 1599 1600 1601 1602 1603
  if (is_date)                        // TIMESTAMP function
  {
    if (get_arg0_date(&l_time1,1) || 
        args[1]->get_time(&l_time2) ||
        l_time1.time_type == TIMESTAMP_TIME || 
        l_time2.time_type != TIMESTAMP_TIME)
      goto null_date;
  }
  else                                // ADDTIME function
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1604
  {
1605 1606 1607 1608 1609 1610
    if (args[0]->get_time(&l_time1) || 
        args[1]->get_time(&l_time2) ||
        l_time2.time_type == TIMESTAMP_FULL)
      goto null_date;
    is_time= (l_time1.time_type == TIMESTAMP_TIME);
    if (is_time && (l_time2.neg == l_time1.neg && l_time1.neg))
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
      l_time3.neg= 1;
  }
  if (l_time1.neg != l_time2.neg)
    l_sign= -l_sign;

  microseconds= l_time1.second_part + l_sign*l_time2.second_part;
  seconds= (l_time1.hour*3600L + l_time1.minute*60L + l_time1.second +
	    (l_time2.day*86400L + l_time2.hour*3600L +
	     l_time2.minute*60L + l_time2.second)*l_sign);
  if (is_time)
    seconds+= l_time1.day*86400L;
  else
    days+= calc_daynr((uint) l_time1.year,(uint) l_time1.month, (uint) l_time1.day);
  seconds= seconds + microseconds/1000000L;
  microseconds= microseconds%1000000L;
  days+= seconds/86400L;
  seconds= seconds%86400L;

  if (microseconds < 0)
  {
    microseconds+= 1000000L;
    seconds--;
  }
  if (seconds < 0)
  {
    days+= seconds/86400L - 1;
    seconds+= 86400L;
  }
  if (days < 0)
  {
    if (!is_time)
      goto null_date;
    if (microseconds)
    {
      microseconds= 1000000L - microseconds;
      seconds++; 
    }
    seconds= 86400L - seconds;
    days= -(++days);
    l_time3.neg= 1;
  }

  calc_time_from_sec(&l_time3, seconds, microseconds);
  if (!is_time)
  {
    get_date_from_daynr(days,&l_time3.year,&l_time3.month,&l_time3.day);
    if (l_time3.day &&
1658 1659 1660
	make_datetime(str, &l_time3,
	              l_time1.second_part || l_time2.second_part ?
	              DATE_TIME_MICROSECOND : DATE_TIME))
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1661 1662 1663 1664 1665
      return str;
    goto null_date;
  }

  l_time3.hour+= days*24;
1666 1667 1668
  if (make_datetime(str, &l_time3,
	    l_time1.second_part || l_time2.second_part ?
	    TIME_MICROSECOND : TIME_ONLY))
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    return str;

null_date:
  null_value=1;
  return 0;
}

/*
  TIMEDIFF(t,s) is a time function that calculates the 
  time value between a start and end time.

  t and s: time_or_datetime_expression
  Result: Time value
*/

String *Item_func_timediff::val_str(String *str)
{
  longlong seconds;
  long microseconds;
  long days;
  int l_sign= 1;
  TIME l_time1 ,l_time2, l_time3;

  null_value= 0;  
  if (args[0]->get_time(&l_time1) ||
      args[1]->get_time(&l_time2) ||
      l_time1.time_type != l_time2.time_type)
    goto null_date;

  if (l_time1.neg != l_time2.neg)
    l_sign= -l_sign;

  if (l_time1.time_type == TIMESTAMP_TIME)  // Time value
    days= l_time1.day - l_sign*l_time2.day;
  else                                      // DateTime value
    days= (calc_daynr((uint) l_time1.year,
		      (uint) l_time1.month,
		      (uint) l_time1.day) - 
	   l_sign*calc_daynr((uint) l_time2.year,
			     (uint) l_time2.month,
			     (uint) l_time2.day));

  microseconds= l_time1.second_part - l_sign*l_time2.second_part;
  seconds= ((longlong) days*86400L + l_time1.hour*3600L + 
	    l_time1.minute*60L + l_time1.second + microseconds/1000000L -
	    (longlong)l_sign*(l_time2.hour*3600L+l_time2.minute*60L+l_time2.second));

  l_time3.neg= 0;
  if (seconds < 0)
  {
    seconds= -seconds;
    l_time3.neg= 1;
  }
  else if (seconds == 0 && microseconds < 0)
  {
    microseconds= -microseconds;
    l_time3.neg= 1;
  }
  if (microseconds < 0)
  {
    microseconds+= 1000000L;
    seconds--;
  }
  if ((l_time2.neg == l_time1.neg) && l_time1.neg)
    l_time3.neg= l_time3.neg ? 0 : 1;

  calc_time_from_sec(&l_time3, seconds, microseconds);
1736 1737 1738
  if (make_datetime(str, &l_time3, 
	    l_time1.second_part || l_time2.second_part ?
	    TIME_MICROSECOND : TIME_ONLY))
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
    return str;

null_date:
  null_value=1;
  return 0;
}

/*
  MAKETIME(h,m,s) is a time function that calculates a time value 
  from the total number of hours, minutes, and seconds.
  Result: Time value
*/

String *Item_func_maketime::val_str(String *str)
{
  TIME ltime;

  long hour= args[0]->val_int();
  long minute= args[1]->val_int();
  long second= args[2]->val_int();

  if ((null_value=(args[0]->null_value || 
		   args[1]->null_value ||
		   args[2]->null_value || 
		   minute > 59 || minute < 0 || 
		   second > 59 || second < 0)))
    goto null_date;

  ltime.neg= 0;
  if (hour < 0)
  {
    ltime.neg= 1;
    hour= -hour;
  }
  ltime.hour= (ulong)hour;
  ltime.minute= (ulong)minute;
  ltime.second= (ulong)second;
  if (make_datetime(str, &ltime, TIME_ONLY))
    return str;

null_date:
    return 0;
}

/*
  MICROSECOND(a) is a function ( extraction) that extracts the microseconds from a.

  a: Datetime or time value
  Result: int value
*/
longlong Item_func_microsecond::val_int()
{
  TIME ltime;
  if (!get_arg0_time(&ltime))
    return ltime.second_part;
  return 0;
}