334
334
ER_TRUNCATED_WRONG_VALUE, warn_buff);
337
bool date_add_interval(DRIZZLE_TIME *ltime, interval_type int_type, INTERVAL interval)
343
sign= (interval.neg ? -1 : 1);
346
case INTERVAL_SECOND:
347
case INTERVAL_SECOND_MICROSECOND:
348
case INTERVAL_MICROSECOND:
349
case INTERVAL_MINUTE:
351
case INTERVAL_MINUTE_MICROSECOND:
352
case INTERVAL_MINUTE_SECOND:
353
case INTERVAL_HOUR_MICROSECOND:
354
case INTERVAL_HOUR_SECOND:
355
case INTERVAL_HOUR_MINUTE:
356
case INTERVAL_DAY_MICROSECOND:
357
case INTERVAL_DAY_SECOND:
358
case INTERVAL_DAY_MINUTE:
359
case INTERVAL_DAY_HOUR:
361
int64_t sec, days, daynr, microseconds, extra_sec;
362
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME; // Return full date
363
microseconds= ltime->second_part + sign*interval.second_part;
364
extra_sec= microseconds/1000000L;
365
microseconds= microseconds%1000000L;
367
sec=((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
369
sign* (int64_t) (interval.day*3600*24L +
370
interval.hour*3600L+interval.minute*60L+
371
interval.second))+ extra_sec;
372
if (microseconds < 0)
374
microseconds+= 1000000L;
377
days= sec/(3600*24L);
384
ltime->second_part= (uint32_t) microseconds;
385
ltime->second= (uint32_t) (sec % 60);
386
ltime->minute= (uint32_t) (sec/60 % 60);
387
ltime->hour= (uint32_t) (sec/3600);
388
daynr= calc_daynr(ltime->year,ltime->month,1) + days;
389
/* Day number from year 0 to 9999-12-31 */
390
if ((uint64_t) daynr > MAX_DAY_NUMBER)
392
get_date_from_daynr((long) daynr, <ime->year, <ime->month,
398
period= (calc_daynr(ltime->year,ltime->month,ltime->day) +
399
sign * (long) interval.day);
400
/* Daynumber from year 0 to 9999-12-31 */
401
if (period > MAX_DAY_NUMBER)
403
get_date_from_daynr((long) period,<ime->year,<ime->month,<ime->day);
406
ltime->year+= sign * (long) interval.year;
407
if (ltime->year >= 10000L)
409
if (ltime->month == 2 && ltime->day == 29 &&
410
calc_days_in_year(ltime->year) != 366)
411
ltime->day=28; // Was leap-year
413
case INTERVAL_YEAR_MONTH:
414
case INTERVAL_QUARTER:
416
period= (ltime->year*12 + sign * (long) interval.year*12 +
417
ltime->month-1 + sign * (long) interval.month);
418
if (period >= 120000L)
420
ltime->year= (uint32_t) (period / 12);
421
ltime->month= (uint32_t) (period % 12L)+1;
422
/* Adjust day if the new month doesn't have enough days */
423
if (ltime->day > days_in_month[ltime->month-1])
425
ltime->day = days_in_month[ltime->month-1];
426
if (ltime->month == 2 && calc_days_in_year(ltime->year) == 366)
427
ltime->day++; // Leap-year
437
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
438
ER_DATETIME_FUNCTION_OVERFLOW,
439
ER(ER_DATETIME_FUNCTION_OVERFLOW),
447
338
Calculate difference between two datetime values as seconds + microseconds.