~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_timefunc.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
  @todo
24
24
    Move month and days to language files
25
25
*/
26
 
#include <drizzled/server_includes.h>
 
26
 
 
27
#ifdef USE_PRAGMA_IMPLEMENTATION
 
28
#pragma implementation                          // gcc: Class implementation
 
29
#endif
 
30
 
 
31
#include "mysql_priv.h"
 
32
#include <m_ctype.h>
27
33
#include <time.h>
28
 
#include <drizzled/drizzled_error_messages.h>
29
34
 
30
35
/** Day number for Dec 31st, 9999. */
31
36
#define MAX_DAY_NUMBER 3652424L
48
53
  the microseconds twice.
49
54
*/
50
55
 
51
 
static bool make_datetime(date_time_format_types format, DRIZZLE_TIME *ltime,
 
56
static bool make_datetime(date_time_format_types format, MYSQL_TIME *ltime,
52
57
                          String *str)
53
58
{
54
59
  char *buff;
55
 
  const CHARSET_INFO * const cs= &my_charset_bin;
56
 
  uint32_t length= MAX_DATE_STRING_REP_LENGTH;
 
60
  CHARSET_INFO *cs= &my_charset_bin;
 
61
  uint length= MAX_DATE_STRING_REP_LENGTH;
57
62
 
58
63
  if (str->alloc(length))
59
64
    return 1;
97
102
 
98
103
 
99
104
/*
100
 
  Wrapper over make_datetime() with validation of the input DRIZZLE_TIME value
 
105
  Wrapper over make_datetime() with validation of the input MYSQL_TIME value
101
106
 
102
107
  NOTE
103
108
    see make_datetime() for more information
107
112
    0    otherwise
108
113
*/
109
114
 
110
 
static bool make_datetime_with_warn(date_time_format_types format, DRIZZLE_TIME *ltime,
 
115
static bool make_datetime_with_warn(date_time_format_types format, MYSQL_TIME *ltime,
111
116
                                    String *str)
112
117
{
113
118
  int warning= 0;
119
124
  if (!warning)
120
125
    return 0;
121
126
 
122
 
  make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
127
  make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
123
128
                               str->ptr(), str->length(),
124
 
                               DRIZZLE_TIMESTAMP_TIME, NULL);
 
129
                               MYSQL_TIMESTAMP_TIME, NullS);
125
130
  return make_datetime(format, ltime, str);
126
131
}
127
132
 
128
133
 
129
134
/*
130
 
  Wrapper over make_time() with validation of the input DRIZZLE_TIME value
 
135
  Wrapper over make_time() with validation of the input MYSQL_TIME value
131
136
 
132
137
  NOTE
133
138
    see make_time() for more info
138
143
*/
139
144
 
140
145
static bool make_time_with_warn(const DATE_TIME_FORMAT *format,
141
 
                                DRIZZLE_TIME *l_time, String *str)
 
146
                                MYSQL_TIME *l_time, String *str)
142
147
{
143
148
  int warning= 0;
144
149
  make_time(format, l_time, str);
146
151
    return 1;
147
152
  if (warning)
148
153
  {
149
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
154
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
150
155
                                 str->ptr(), str->length(),
151
 
                                 DRIZZLE_TIMESTAMP_TIME, NULL);
 
156
                                 MYSQL_TIMESTAMP_TIME, NullS);
152
157
    make_time(format, l_time, str);
153
158
  }
154
159
 
157
162
 
158
163
 
159
164
/*
160
 
  Convert seconds to DRIZZLE_TIME value with overflow checking
 
165
  Convert seconds to MYSQL_TIME value with overflow checking
161
166
 
162
167
  SYNOPSIS:
163
168
    sec_to_time()
164
169
    seconds          number of seconds
165
170
    unsigned_flag    1, if 'seconds' is unsigned, 0, otherwise
166
 
    ltime            output DRIZZLE_TIME value
 
171
    ltime            output MYSQL_TIME value
167
172
 
168
173
  DESCRIPTION
169
 
    If the 'seconds' argument is inside DRIZZLE_TIME data range, convert it to a
 
174
    If the 'seconds' argument is inside MYSQL_TIME data range, convert it to a
170
175
    corresponding value.
171
176
    Otherwise, truncate the resulting value to the nearest endpoint, and
172
177
    produce a warning message.
176
181
    0                otherwise
177
182
*/
178
183
  
179
 
static bool sec_to_time(int64_t seconds, bool unsigned_flag, DRIZZLE_TIME *ltime)
 
184
static bool sec_to_time(longlong seconds, bool unsigned_flag, MYSQL_TIME *ltime)
180
185
{
181
 
  uint32_t sec;
 
186
  uint sec;
182
187
 
183
 
  memset(ltime, 0, sizeof(*ltime));
 
188
  bzero((char *)ltime, sizeof(*ltime));
184
189
  
185
190
  if (seconds < 0)
186
191
  {
194
199
  else if (seconds > 3020399)
195
200
    goto overflow;
196
201
  
197
 
  sec= (uint) ((uint64_t) seconds % 3600);
 
202
  sec= (uint) ((ulonglong) seconds % 3600);
198
203
  ltime->hour= (uint) (seconds/3600);
199
204
  ltime->minute= sec/60;
200
205
  ltime->second= sec % 60;
207
212
  ltime->second= TIME_MAX_SECOND;
208
213
 
209
214
  char buf[22];
210
 
  int len= (int)(int64_t10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
 
215
  int len= (int)(longlong10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
211
216
                 - buf);
212
 
  make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
213
 
                               buf, len, DRIZZLE_TIMESTAMP_TIME,
214
 
                               NULL);
 
217
  make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
218
                               buf, len, MYSQL_TIMESTAMP_TIME,
 
219
                               NullS);
215
220
  
216
221
  return 1;
217
222
}
229
234
                                            {(char *)"%H:%i:%S", 8}};
230
235
 
231
236
/**
232
 
  Extract datetime value to DRIZZLE_TIME struct from string value
 
237
  Extract datetime value to MYSQL_TIME struct from string value
233
238
  according to format string.
234
239
 
235
240
  @param format         date/time format specification
262
267
*/
263
268
 
264
269
static bool extract_date_time(DATE_TIME_FORMAT *format,
265
 
                              const char *val, uint32_t length, DRIZZLE_TIME *l_time,
266
 
                              enum enum_drizzle_timestamp_type cached_timestamp_type,
 
270
                              const char *val, uint length, MYSQL_TIME *l_time,
 
271
                              timestamp_type cached_timestamp_type,
267
272
                              const char **sub_pattern_end,
268
273
                              const char *date_time_type)
269
274
{
280
285
  const char *val_end= val + length;
281
286
  const char *ptr= format->format.str;
282
287
  const char *end= ptr + format->format.length;
283
 
  const CHARSET_INFO * const cs= &my_charset_bin;
 
288
  CHARSET_INFO *cs= &my_charset_bin;
 
289
  DBUG_ENTER("extract_date_time");
284
290
 
285
291
  if (!sub_pattern_end)
286
 
    memset(l_time, 0, sizeof(*l_time));
 
292
    bzero((char*) l_time, sizeof(*l_time));
287
293
 
288
294
  for (; ptr != end && val != val_end; ptr++)
289
295
  {
302
308
      switch (*++ptr) {
303
309
        /* Year */
304
310
      case 'Y':
305
 
        tmp= (char*) val + cmin(4, val_len);
 
311
        tmp= (char*) val + min(4, val_len);
306
312
        l_time->year= (int) my_strtoll10(val, &tmp, &error);
307
313
        if ((int) (tmp-val) <= 2)
308
314
          l_time->year= year_2000_handling(l_time->year);
309
315
        val= tmp;
310
316
        break;
311
317
      case 'y':
312
 
        tmp= (char*) val + cmin(2, val_len);
 
318
        tmp= (char*) val + min(2, val_len);
313
319
        l_time->year= (int) my_strtoll10(val, &tmp, &error);
314
320
        val= tmp;
315
321
        l_time->year= year_2000_handling(l_time->year);
318
324
        /* Month */
319
325
      case 'm':
320
326
      case 'c':
321
 
        tmp= (char*) val + cmin(2, val_len);
 
327
        tmp= (char*) val + min(2, val_len);
322
328
        l_time->month= (int) my_strtoll10(val, &tmp, &error);
323
329
        val= tmp;
324
330
        break;
335
341
        /* Day */
336
342
      case 'd':
337
343
      case 'e':
338
 
        tmp= (char*) val + cmin(2, val_len);
 
344
        tmp= (char*) val + min(2, val_len);
339
345
        l_time->day= (int) my_strtoll10(val, &tmp, &error);
340
346
        val= tmp;
341
347
        break;
342
348
      case 'D':
343
 
        tmp= (char*) val + cmin(2, val_len);
 
349
        tmp= (char*) val + min(2, val_len);
344
350
        l_time->day= (int) my_strtoll10(val, &tmp, &error);
345
351
        /* Skip 'st, 'nd, 'th .. */
346
 
        val= tmp + cmin((int) (val_end-tmp), 2);
 
352
        val= tmp + min((int) (val_end-tmp), 2);
347
353
        break;
348
354
 
349
355
        /* Hour */
354
360
        /* fall through */
355
361
      case 'k':
356
362
      case 'H':
357
 
        tmp= (char*) val + cmin(2, val_len);
 
363
        tmp= (char*) val + min(2, val_len);
358
364
        l_time->hour= (int) my_strtoll10(val, &tmp, &error);
359
365
        val= tmp;
360
366
        break;
361
367
 
362
368
        /* Minute */
363
369
      case 'i':
364
 
        tmp= (char*) val + cmin(2, val_len);
 
370
        tmp= (char*) val + min(2, val_len);
365
371
        l_time->minute= (int) my_strtoll10(val, &tmp, &error);
366
372
        val= tmp;
367
373
        break;
369
375
        /* Second */
370
376
      case 's':
371
377
      case 'S':
372
 
        tmp= (char*) val + cmin(2, val_len);
 
378
        tmp= (char*) val + min(2, val_len);
373
379
        l_time->second= (int) my_strtoll10(val, &tmp, &error);
374
380
        val= tmp;
375
381
        break;
390
396
      case 'p':
391
397
        if (val_len < 2 || ! usa_time)
392
398
          goto err;
393
 
        if (!my_strnncoll(&my_charset_utf8_general_ci,
394
 
                          (const unsigned char *) val, 2, 
395
 
                          (const unsigned char *) "PM", 2))
 
399
        if (!my_strnncoll(&my_charset_latin1,
 
400
                          (const uchar *) val, 2, 
 
401
                          (const uchar *) "PM", 2))
396
402
          daypart= 12;
397
 
        else if (my_strnncoll(&my_charset_utf8_general_ci,
398
 
                              (const unsigned char *) val, 2, 
399
 
                              (const unsigned char *) "AM", 2))
 
403
        else if (my_strnncoll(&my_charset_latin1,
 
404
                              (const uchar *) val, 2, 
 
405
                              (const uchar *) "AM", 2))
400
406
          goto err;
401
407
        val+= 2;
402
408
        break;
421
427
        val= tmp;
422
428
        break;
423
429
      case 'j':
424
 
        tmp= (char*) val + cmin(val_len, 3);
 
430
        tmp= (char*) val + min(val_len, 3);
425
431
        yearday= (int) my_strtoll10(val, &tmp, &error);
426
432
        val= tmp;
427
433
        break;
433
439
      case 'u':
434
440
        sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
435
441
        strict_week_number= (*ptr=='V' || *ptr=='v');
436
 
        tmp= (char*) val + cmin(val_len, 2);
 
442
        tmp= (char*) val + min(val_len, 2);
437
443
        if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
438
444
            (strict_week_number && !week_number) ||
439
445
            week_number > 53)
445
451
      case 'X':
446
452
      case 'x':
447
453
        strict_week_number_year_type= (*ptr=='X');
448
 
        tmp= (char*) val + cmin(4, val_len);
 
454
        tmp= (char*) val + min(4, val_len);
449
455
        strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
450
456
        val= tmp;
451
457
        break;
459
465
        if (extract_date_time(&time_ampm_format, val,
460
466
                              (uint)(val_end - val), l_time,
461
467
                              cached_timestamp_type, &val, "time"))
462
 
          return(1);
 
468
          DBUG_RETURN(1);
463
469
        break;
464
470
 
465
471
        /* Time in 24-hour notation */
467
473
        if (extract_date_time(&time_24hrs_format, val,
468
474
                              (uint)(val_end - val), l_time,
469
475
                              cached_timestamp_type, &val, "time"))
470
 
          return(1);
 
476
          DBUG_RETURN(1);
471
477
        break;
472
478
 
473
479
        /* Conversion specifiers that match classes of characters */
510
516
  if (sub_pattern_end)
511
517
  {
512
518
    *sub_pattern_end= val;
513
 
    return(0);
 
519
    DBUG_RETURN(0);
514
520
  }
515
521
 
516
522
  if (yearday > 0)
517
523
  {
518
 
    uint32_t days;
 
524
    uint days;
519
525
    days= calc_daynr(l_time->year,1,1) +  yearday - 1;
520
526
    if (days <= 0 || days > MAX_DAY_NUMBER)
521
527
      goto err;
525
531
  if (week_number >= 0 && weekday)
526
532
  {
527
533
    int days;
528
 
    uint32_t weekday_b;
 
534
    uint weekday_b;
529
535
 
530
536
    /*
531
537
      %V,%v require %X,%x resprectively,
575
581
  {
576
582
    do
577
583
    {
578
 
      if (!my_isspace(&my_charset_utf8_general_ci,*val))
 
584
      if (!my_isspace(&my_charset_latin1,*val))
579
585
      {
580
 
        make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
586
        make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
581
587
                                     val_begin, length,
582
 
                                     cached_timestamp_type, NULL);
 
588
                                     cached_timestamp_type, NullS);
583
589
        break;
584
590
      }
585
591
    } while (++val != val_end);
586
592
  }
587
 
  return(0);
 
593
  DBUG_RETURN(0);
588
594
 
589
595
err:
590
596
  {
591
597
    char buff[128];
592
 
    strmake(buff, val_begin, cmin(length, (uint)sizeof(buff)-1));
593
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
598
    strmake(buff, val_begin, min(length, sizeof(buff)-1));
 
599
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
594
600
                        ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
595
601
                        date_time_type, buff, "str_to_date");
596
602
  }
597
 
  return(1);
 
603
  DBUG_RETURN(1);
598
604
}
599
605
 
600
606
 
602
608
  Create a formated date/time value in a string.
603
609
*/
604
610
 
605
 
bool make_date_time(DATE_TIME_FORMAT *format, DRIZZLE_TIME *l_time,
606
 
                    enum enum_drizzle_timestamp_type type, String *str)
 
611
bool make_date_time(DATE_TIME_FORMAT *format, MYSQL_TIME *l_time,
 
612
                    timestamp_type type, String *str)
607
613
{
608
614
  char intbuff[15];
609
 
  uint32_t hours_i;
610
 
  uint32_t weekday;
 
615
  uint hours_i;
 
616
  uint weekday;
611
617
  ulong length;
612
618
  const char *ptr, *end;
613
619
  THD *thd= current_thd;
641
647
                    system_charset_info);
642
648
        break;
643
649
      case 'W':
644
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
650
        if (type == MYSQL_TIMESTAMP_TIME)
645
651
          return 1;
646
652
        weekday= calc_weekday(calc_daynr(l_time->year,l_time->month,
647
653
                              l_time->day),0);
650
656
                    system_charset_info);
651
657
        break;
652
658
      case 'a':
653
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
659
        if (type == MYSQL_TIMESTAMP_TIME)
654
660
          return 1;
655
661
        weekday=calc_weekday(calc_daynr(l_time->year,l_time->month,
656
662
                             l_time->day),0);
659
665
                    system_charset_info);
660
666
        break;
661
667
      case 'D':
662
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
668
        if (type == MYSQL_TIMESTAMP_TIME)
663
669
          return 1;
664
670
        length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
665
671
        str->append_with_prefill(intbuff, length, 1, '0');
726
732
        str->append_with_prefill(intbuff, length, 2, '0');
727
733
        break;
728
734
      case 'j':
729
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
735
        if (type == MYSQL_TIMESTAMP_TIME)
730
736
          return 1;
731
737
        length= int10_to_str(calc_daynr(l_time->year,l_time->month,
732
738
                                        l_time->day) - 
747
753
        str->append(hours_i < 12 ? "AM" : "PM",2);
748
754
        break;
749
755
      case 'r':
750
 
        length= sprintf(intbuff, 
 
756
        length= my_sprintf(intbuff, 
 
757
                   (intbuff, 
751
758
                    ((l_time->hour % 24) < 12) ?
752
759
                    "%02d:%02d:%02d AM" : "%02d:%02d:%02d PM",
753
760
                    (l_time->hour+11)%12+1,
754
761
                    l_time->minute,
755
 
                    l_time->second);
 
762
                    l_time->second));
756
763
        str->append(intbuff, length);
757
764
        break;
758
765
      case 'S':
761
768
        str->append_with_prefill(intbuff, length, 2, '0');
762
769
        break;
763
770
      case 'T':
764
 
        length= sprintf(intbuff, 
 
771
        length= my_sprintf(intbuff, 
 
772
                   (intbuff, 
765
773
                    "%02d:%02d:%02d", 
766
774
                    l_time->hour, 
767
775
                    l_time->minute,
768
 
                    l_time->second);
 
776
                    l_time->second));
769
777
        str->append(intbuff, length);
770
778
        break;
771
779
      case 'U':
772
780
      case 'u':
773
781
      {
774
 
        uint32_t year;
775
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
782
        uint year;
 
783
        if (type == MYSQL_TIMESTAMP_TIME)
776
784
          return 1;
777
785
        length= int10_to_str(calc_week(l_time,
778
786
                                       (*ptr) == 'U' ?
785
793
      case 'v':
786
794
      case 'V':
787
795
      {
788
 
        uint32_t year;
789
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
796
        uint year;
 
797
        if (type == MYSQL_TIMESTAMP_TIME)
790
798
          return 1;
791
799
        length= int10_to_str(calc_week(l_time,
792
800
                                       ((*ptr) == 'V' ?
800
808
      case 'x':
801
809
      case 'X':
802
810
      {
803
 
        uint32_t year;
804
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
811
        uint year;
 
812
        if (type == MYSQL_TIMESTAMP_TIME)
805
813
          return 1;
806
814
        (void) calc_week(l_time,
807
815
                         ((*ptr) == 'X' ?
813
821
      }
814
822
      break;
815
823
      case 'w':
816
 
        if (type == DRIZZLE_TIMESTAMP_TIME)
 
824
        if (type == MYSQL_TIMESTAMP_TIME)
817
825
          return 1;
818
826
        weekday=calc_weekday(calc_daynr(l_time->year,l_time->month,
819
827
                                        l_time->day),1);
850
858
                         For example, '1.1' -> '1.100000'
851
859
*/
852
860
 
853
 
static bool get_interval_info(const char *str,uint32_t length, const CHARSET_INFO * const cs,
854
 
                              uint32_t count, uint64_t *values,
 
861
static bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs,
 
862
                              uint count, ulonglong *values,
855
863
                              bool transform_msec)
856
864
{
857
865
  const char *end=str+length;
858
 
  uint32_t i;
 
866
  uint i;
859
867
  while (str != end && !my_isdigit(cs,*str))
860
868
    str++;
861
869
 
862
870
  for (i=0 ; i < count ; i++)
863
871
  {
864
 
    int64_t value;
 
872
    longlong value;
865
873
    const char *start= str;
866
874
    for (value=0; str != end && my_isdigit(cs,*str) ; str++)
867
 
      value= value * 10L + (int64_t) (*str - '0');
 
875
      value= value*LL(10) + (longlong) (*str - '0');
868
876
    if (transform_msec && i == count - 1) // microseconds always last
869
877
    {
870
878
      long msec_length= 6 - (str - start);
878
886
    {
879
887
      i++;
880
888
      /* Change values[0...i-1] -> values[0...count-1] */
881
 
      bmove_upp((unsigned char*) (values+count), (unsigned char*) (values+i),
 
889
      bmove_upp((uchar*) (values+count), (uchar*) (values+i),
882
890
                sizeof(*values)*i);
883
 
      memset(values, 0, sizeof(*values)*(count-i));
 
891
      bzero((uchar*) values, sizeof(*values)*(count-i));
884
892
      break;
885
893
    }
886
894
  }
888
896
}
889
897
 
890
898
 
891
 
int64_t Item_func_period_add::val_int()
 
899
longlong Item_func_period_add::val_int()
892
900
{
893
 
  assert(fixed == 1);
 
901
  DBUG_ASSERT(fixed == 1);
894
902
  ulong period=(ulong) args[0]->val_int();
895
903
  int months=(int) args[1]->val_int();
896
904
 
897
905
  if ((null_value=args[0]->null_value || args[1]->null_value) ||
898
906
      period == 0L)
899
907
    return 0; /* purecov: inspected */
900
 
  return (int64_t)
 
908
  return (longlong)
901
909
    convert_month_to_period((uint) ((int) convert_period_to_month(period)+
902
910
                                    months));
903
911
}
904
912
 
905
913
 
906
 
int64_t Item_func_period_diff::val_int()
 
914
longlong Item_func_period_diff::val_int()
907
915
{
908
 
  assert(fixed == 1);
 
916
  DBUG_ASSERT(fixed == 1);
909
917
  ulong period1=(ulong) args[0]->val_int();
910
918
  ulong period2=(ulong) args[1]->val_int();
911
919
 
912
920
  if ((null_value=args[0]->null_value || args[1]->null_value))
913
921
    return 0; /* purecov: inspected */
914
 
  return (int64_t) ((long) convert_period_to_month(period1)-
 
922
  return (longlong) ((long) convert_period_to_month(period1)-
915
923
                     (long) convert_period_to_month(period2));
916
924
}
917
925
 
918
926
 
919
927
 
920
 
int64_t Item_func_to_days::val_int()
 
928
longlong Item_func_to_days::val_int()
921
929
{
922
 
  assert(fixed == 1);
923
 
  DRIZZLE_TIME ltime;
 
930
  DBUG_ASSERT(fixed == 1);
 
931
  MYSQL_TIME ltime;
924
932
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
925
933
    return 0;
926
 
  return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
 
934
  return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
927
935
}
928
936
 
929
937
 
945
953
{
946
954
  if (args[0]->type() == Item::FIELD_ITEM)
947
955
  {
948
 
    if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
 
956
    if (args[0]->field_type() == MYSQL_TYPE_DATE)
949
957
      return MONOTONIC_STRICT_INCREASING;
950
 
    if (args[0]->field_type() == DRIZZLE_TYPE_DATETIME)
 
958
    if (args[0]->field_type() == MYSQL_TYPE_DATETIME)
951
959
      return MONOTONIC_INCREASING;
952
960
  }
953
961
  return NON_MONOTONIC;
954
962
}
955
963
 
956
964
 
957
 
int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
 
965
longlong Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
958
966
{
959
 
  assert(fixed == 1);
960
 
  DRIZZLE_TIME ltime;
961
 
  int64_t res;
 
967
  DBUG_ASSERT(fixed == 1);
 
968
  MYSQL_TIME ltime;
 
969
  longlong res;
962
970
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
963
971
  {
964
972
    /* got NULL, leave the incl_endp intact */
965
 
    return INT64_MIN;
 
973
    return LONGLONG_MIN;
966
974
  }
967
 
  res=(int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
 
975
  res=(longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
968
976
  
969
 
  if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
 
977
  if (args[0]->field_type() == MYSQL_TYPE_DATE)
970
978
  {
971
979
    // TO_DAYS() is strictly monotonic for dates, leave incl_endp intact
972
980
    return res;
987
995
                      ltime.second_part))
988
996
    ; /* do nothing */
989
997
  else
990
 
    *incl_endp= true;
 
998
    *incl_endp= TRUE;
991
999
  return res;
992
1000
}
993
1001
 
994
1002
 
995
 
int64_t Item_func_dayofyear::val_int()
 
1003
longlong Item_func_dayofyear::val_int()
996
1004
{
997
 
  assert(fixed == 1);
998
 
  DRIZZLE_TIME ltime;
 
1005
  DBUG_ASSERT(fixed == 1);
 
1006
  MYSQL_TIME ltime;
999
1007
  if (get_arg0_date(&ltime,TIME_NO_ZERO_DATE))
1000
1008
    return 0;
1001
 
  return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day) -
 
1009
  return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day) -
1002
1010
    calc_daynr(ltime.year,1,1) + 1;
1003
1011
}
1004
1012
 
1005
 
int64_t Item_func_dayofmonth::val_int()
 
1013
longlong Item_func_dayofmonth::val_int()
1006
1014
{
1007
 
  assert(fixed == 1);
1008
 
  DRIZZLE_TIME ltime;
 
1015
  DBUG_ASSERT(fixed == 1);
 
1016
  MYSQL_TIME ltime;
1009
1017
  (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
1010
 
  return (int64_t) ltime.day;
 
1018
  return (longlong) ltime.day;
1011
1019
}
1012
1020
 
1013
 
int64_t Item_func_month::val_int()
 
1021
longlong Item_func_month::val_int()
1014
1022
{
1015
 
  assert(fixed == 1);
1016
 
  DRIZZLE_TIME ltime;
 
1023
  DBUG_ASSERT(fixed == 1);
 
1024
  MYSQL_TIME ltime;
1017
1025
  (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
1018
 
  return (int64_t) ltime.month;
 
1026
  return (longlong) ltime.month;
1019
1027
}
1020
1028
 
1021
1029
 
1022
1030
String* Item_func_monthname::val_str(String* str)
1023
1031
{
1024
 
  assert(fixed == 1);
 
1032
  DBUG_ASSERT(fixed == 1);
1025
1033
  const char *month_name;
1026
 
  uint32_t   month= (uint) val_int();
 
1034
  uint   month= (uint) val_int();
1027
1035
  THD *thd= current_thd;
1028
1036
 
1029
1037
  if (null_value || !month)
1042
1050
  Returns the quarter of the year.
1043
1051
*/
1044
1052
 
1045
 
int64_t Item_func_quarter::val_int()
 
1053
longlong Item_func_quarter::val_int()
1046
1054
{
1047
 
  assert(fixed == 1);
1048
 
  DRIZZLE_TIME ltime;
 
1055
  DBUG_ASSERT(fixed == 1);
 
1056
  MYSQL_TIME ltime;
1049
1057
  if (get_arg0_date(&ltime, TIME_FUZZY_DATE))
1050
1058
    return 0;
1051
 
  return (int64_t) ((ltime.month+2)/3);
 
1059
  return (longlong) ((ltime.month+2)/3);
1052
1060
}
1053
1061
 
1054
 
int64_t Item_func_hour::val_int()
 
1062
longlong Item_func_hour::val_int()
1055
1063
{
1056
 
  assert(fixed == 1);
1057
 
  DRIZZLE_TIME ltime;
 
1064
  DBUG_ASSERT(fixed == 1);
 
1065
  MYSQL_TIME ltime;
1058
1066
  (void) get_arg0_time(&ltime);
1059
1067
  return ltime.hour;
1060
1068
}
1061
1069
 
1062
 
int64_t Item_func_minute::val_int()
 
1070
longlong Item_func_minute::val_int()
1063
1071
{
1064
 
  assert(fixed == 1);
1065
 
  DRIZZLE_TIME ltime;
 
1072
  DBUG_ASSERT(fixed == 1);
 
1073
  MYSQL_TIME ltime;
1066
1074
  (void) get_arg0_time(&ltime);
1067
1075
  return ltime.minute;
1068
1076
}
1070
1078
/**
1071
1079
  Returns the second in time_exp in the range of 0 - 59.
1072
1080
*/
1073
 
int64_t Item_func_second::val_int()
 
1081
longlong Item_func_second::val_int()
1074
1082
{
1075
 
  assert(fixed == 1);
1076
 
  DRIZZLE_TIME ltime;
 
1083
  DBUG_ASSERT(fixed == 1);
 
1084
  MYSQL_TIME ltime;
1077
1085
  (void) get_arg0_time(&ltime);
1078
1086
  return ltime.second;
1079
1087
}
1080
1088
 
1081
1089
 
1082
 
uint32_t week_mode(uint32_t mode)
 
1090
uint week_mode(uint mode)
1083
1091
{
1084
 
  uint32_t week_format= (mode & 7);
 
1092
  uint week_format= (mode & 7);
1085
1093
  if (!(week_format & WEEK_MONDAY_FIRST))
1086
1094
    week_format^= WEEK_FIRST_WEEKDAY;
1087
1095
  return week_format;
1118
1126
 @endverbatim
1119
1127
*/
1120
1128
 
1121
 
int64_t Item_func_week::val_int()
 
1129
longlong Item_func_week::val_int()
1122
1130
{
1123
 
  assert(fixed == 1);
1124
 
  uint32_t year;
1125
 
  DRIZZLE_TIME ltime;
 
1131
  DBUG_ASSERT(fixed == 1);
 
1132
  uint year;
 
1133
  MYSQL_TIME ltime;
1126
1134
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
1127
1135
    return 0;
1128
 
  return (int64_t) calc_week(&ltime,
 
1136
  return (longlong) calc_week(&ltime,
1129
1137
                              week_mode((uint) args[1]->val_int()),
1130
1138
                              &year);
1131
1139
}
1132
1140
 
1133
1141
 
1134
 
int64_t Item_func_yearweek::val_int()
 
1142
longlong Item_func_yearweek::val_int()
1135
1143
{
1136
 
  assert(fixed == 1);
1137
 
  uint32_t year,week;
1138
 
  DRIZZLE_TIME ltime;
 
1144
  DBUG_ASSERT(fixed == 1);
 
1145
  uint year,week;
 
1146
  MYSQL_TIME ltime;
1139
1147
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
1140
1148
    return 0;
1141
1149
  week= calc_week(&ltime, 
1145
1153
}
1146
1154
 
1147
1155
 
1148
 
int64_t Item_func_weekday::val_int()
 
1156
longlong Item_func_weekday::val_int()
1149
1157
{
1150
 
  assert(fixed == 1);
1151
 
  DRIZZLE_TIME ltime;
 
1158
  DBUG_ASSERT(fixed == 1);
 
1159
  MYSQL_TIME ltime;
1152
1160
  
1153
1161
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
1154
1162
    return 0;
1155
1163
 
1156
 
  return (int64_t) calc_weekday(calc_daynr(ltime.year, ltime.month,
 
1164
  return (longlong) calc_weekday(calc_daynr(ltime.year, ltime.month,
1157
1165
                                            ltime.day),
1158
1166
                                 odbc_type) + test(odbc_type);
1159
1167
}
1161
1169
 
1162
1170
String* Item_func_dayname::val_str(String* str)
1163
1171
{
1164
 
  assert(fixed == 1);
1165
 
  uint32_t weekday=(uint) val_int();            // Always Item_func_daynr()
 
1172
  DBUG_ASSERT(fixed == 1);
 
1173
  uint weekday=(uint) val_int();                // Always Item_func_daynr()
1166
1174
  const char *day_name;
1167
1175
  THD *thd= current_thd;
1168
1176
 
1175
1183
}
1176
1184
 
1177
1185
 
1178
 
int64_t Item_func_year::val_int()
 
1186
longlong Item_func_year::val_int()
1179
1187
{
1180
 
  assert(fixed == 1);
1181
 
  DRIZZLE_TIME ltime;
 
1188
  DBUG_ASSERT(fixed == 1);
 
1189
  MYSQL_TIME ltime;
1182
1190
  (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
1183
 
  return (int64_t) ltime.year;
 
1191
  return (longlong) ltime.year;
1184
1192
}
1185
1193
 
1186
1194
 
1201
1209
enum_monotonicity_info Item_func_year::get_monotonicity_info() const
1202
1210
{
1203
1211
  if (args[0]->type() == Item::FIELD_ITEM &&
1204
 
      (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE ||
1205
 
       args[0]->field_type() == DRIZZLE_TYPE_DATETIME))
 
1212
      (args[0]->field_type() == MYSQL_TYPE_DATE ||
 
1213
       args[0]->field_type() == MYSQL_TYPE_DATETIME))
1206
1214
    return MONOTONIC_INCREASING;
1207
1215
  return NON_MONOTONIC;
1208
1216
}
1209
1217
 
1210
1218
 
1211
 
int64_t Item_func_year::val_int_endpoint(bool left_endp, bool *incl_endp)
 
1219
longlong Item_func_year::val_int_endpoint(bool left_endp, bool *incl_endp)
1212
1220
{
1213
 
  assert(fixed == 1);
1214
 
  DRIZZLE_TIME ltime;
 
1221
  DBUG_ASSERT(fixed == 1);
 
1222
  MYSQL_TIME ltime;
1215
1223
  if (get_arg0_date(&ltime, TIME_FUZZY_DATE))
1216
1224
  {
1217
1225
    /* got NULL, leave the incl_endp intact */
1218
 
    return INT64_MIN;
 
1226
    return LONGLONG_MIN;
1219
1227
  }
1220
1228
 
1221
1229
  /*
1233
1241
      !(ltime.hour || ltime.minute || ltime.second || ltime.second_part))
1234
1242
    ; /* do nothing */
1235
1243
  else
1236
 
    *incl_endp= true;
 
1244
    *incl_endp= TRUE;
1237
1245
  return ltime.year;
1238
1246
}
1239
1247
 
1240
1248
 
1241
 
int64_t Item_func_unix_timestamp::val_int()
 
1249
longlong Item_func_unix_timestamp::val_int()
1242
1250
{
1243
 
  DRIZZLE_TIME ltime;
1244
 
  bool not_used;
 
1251
  MYSQL_TIME ltime;
 
1252
  my_bool not_used;
1245
1253
  
1246
 
  assert(fixed == 1);
 
1254
  DBUG_ASSERT(fixed == 1);
1247
1255
  if (arg_count == 0)
1248
 
    return (int64_t) current_thd->query_start();
 
1256
    return (longlong) current_thd->query_start();
1249
1257
  if (args[0]->type() == FIELD_ITEM)
1250
1258
  {                                             // Optimize timestamp field
1251
1259
    Field *field=((Item_field*) args[0])->field;
1252
 
    if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
1260
    if (field->type() == MYSQL_TYPE_TIMESTAMP)
1253
1261
      return ((Field_timestamp*) field)->get_timestamp(&null_value);
1254
1262
  }
1255
1263
  
1264
1272
    return 0;
1265
1273
  }
1266
1274
  
1267
 
  return (int64_t) TIME_to_timestamp(current_thd, &ltime, &not_used);
 
1275
  return (longlong) TIME_to_timestamp(current_thd, &ltime, &not_used);
1268
1276
}
1269
1277
 
1270
1278
 
1271
 
int64_t Item_func_time_to_sec::val_int()
 
1279
longlong Item_func_time_to_sec::val_int()
1272
1280
{
1273
 
  assert(fixed == 1);
1274
 
  DRIZZLE_TIME ltime;
1275
 
  int64_t seconds;
 
1281
  DBUG_ASSERT(fixed == 1);
 
1282
  MYSQL_TIME ltime;
 
1283
  longlong seconds;
1276
1284
  (void) get_arg0_time(&ltime);
1277
1285
  seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
1278
1286
  return ltime.neg ? -seconds : seconds;
1288
1296
bool get_interval_value(Item *args,interval_type int_type,
1289
1297
                               String *str_value, INTERVAL *interval)
1290
1298
{
1291
 
  uint64_t array[5];
1292
 
  int64_t value= 0;
 
1299
  ulonglong array[5];
 
1300
  longlong value= 0;
1293
1301
  const char *str= NULL;
1294
1302
  size_t length= 0;
1295
 
  const CHARSET_INFO * const cs= str_value->charset();
 
1303
  CHARSET_INFO *cs=str_value->charset();
1296
1304
 
1297
 
  memset(interval, 0, sizeof(*interval));
 
1305
  bzero((char*) interval,sizeof(*interval));
1298
1306
  if ((int) int_type <= INTERVAL_MICROSECOND)
1299
1307
  {
1300
1308
    value= args->val_int();
1430
1438
    interval->second_part= array[1];
1431
1439
    break;
1432
1440
  case INTERVAL_LAST: /* purecov: begin deadcode */
1433
 
    assert(0); 
 
1441
    DBUG_ASSERT(0); 
1434
1442
    break;            /* purecov: end */
1435
1443
  }
1436
1444
  return 0;
1439
1447
 
1440
1448
String *Item_date::val_str(String *str)
1441
1449
{
1442
 
  assert(fixed == 1);
1443
 
  DRIZZLE_TIME ltime;
 
1450
  DBUG_ASSERT(fixed == 1);
 
1451
  MYSQL_TIME ltime;
1444
1452
  if (get_date(&ltime, TIME_FUZZY_DATE))
1445
1453
    return (String *) 0;
1446
1454
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
1453
1461
}
1454
1462
 
1455
1463
 
1456
 
int64_t Item_date::val_int()
 
1464
longlong Item_date::val_int()
1457
1465
{
1458
 
  assert(fixed == 1);
1459
 
  DRIZZLE_TIME ltime;
 
1466
  DBUG_ASSERT(fixed == 1);
 
1467
  MYSQL_TIME ltime;
1460
1468
  if (get_date(&ltime, TIME_FUZZY_DATE))
1461
1469
    return 0;
1462
 
  return (int64_t) (ltime.year*10000L+ltime.month*100+ltime.day);
 
1470
  return (longlong) (ltime.year*10000L+ltime.month*100+ltime.day);
1463
1471
}
1464
1472
 
1465
1473
 
1466
 
bool Item_func_from_days::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
1474
bool Item_func_from_days::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
1467
1475
{
1468
 
  int64_t value=args[0]->val_int();
 
1476
  longlong value=args[0]->val_int();
1469
1477
  if ((null_value=args[0]->null_value))
1470
1478
    return 1;
1471
 
  memset(ltime, 0, sizeof(DRIZZLE_TIME));
 
1479
  bzero(ltime, sizeof(MYSQL_TIME));
1472
1480
  get_date_from_daynr((long) value, &ltime->year, &ltime->month, &ltime->day);
1473
 
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
 
1481
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
1474
1482
  return 0;
1475
1483
}
1476
1484
 
1485
1493
  
1486
1494
  /* We don't need to set second_part and neg because they already 0 */
1487
1495
  ltime.hour= ltime.minute= ltime.second= 0;
1488
 
  ltime.time_type= DRIZZLE_TIMESTAMP_DATE;
1489
 
  value= (int64_t) TIME_to_uint64_t_date(&ltime);
 
1496
  ltime.time_type= MYSQL_TIMESTAMP_DATE;
 
1497
  value= (longlong) TIME_to_ulonglong_date(&ltime);
1490
1498
}
1491
1499
 
1492
1500
String *Item_func_curdate::val_str(String *str)
1493
1501
{
1494
 
  assert(fixed == 1);
 
1502
  DBUG_ASSERT(fixed == 1);
1495
1503
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
1496
1504
  {
1497
1505
    null_value= 1;
1502
1510
}
1503
1511
 
1504
1512
/**
1505
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
 
1513
    Converts current time in my_time_t to MYSQL_TIME represenatation for local
1506
1514
    time zone. Defines time zone (local) used for whole CURDATE function.
1507
1515
*/
1508
 
void Item_func_curdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1516
void Item_func_curdate_local::store_now_in_TIME(MYSQL_TIME *now_time)
1509
1517
{
1510
1518
  THD *thd= current_thd;
1511
1519
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
1515
1523
 
1516
1524
 
1517
1525
/**
1518
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
 
1526
    Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1519
1527
    time zone. Defines time zone (UTC) used for whole UTC_DATE function.
1520
1528
*/
1521
 
void Item_func_curdate_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1529
void Item_func_curdate_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1522
1530
{
1523
1531
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1524
1532
                             (my_time_t)(current_thd->query_start()));
1529
1537
}
1530
1538
 
1531
1539
 
1532
 
bool Item_func_curdate::get_date(DRIZZLE_TIME *res,
1533
 
                                 uint32_t fuzzy_date __attribute__((unused)))
 
1540
bool Item_func_curdate::get_date(MYSQL_TIME *res,
 
1541
                                 uint fuzzy_date __attribute__((unused)))
1534
1542
{
1535
1543
  *res=ltime;
1536
1544
  return 0;
1537
1545
}
1538
1546
 
1539
1547
 
1540
 
String *Item_func_curtime::val_str(String *str __attribute__((unused)))
 
1548
String *Item_func_curtime::val_str(String *str)
1541
1549
{
1542
 
  assert(fixed == 1);
 
1550
  DBUG_ASSERT(fixed == 1);
1543
1551
  str_value.set(buff, buff_length, &my_charset_bin);
1544
1552
  return &str_value;
1545
1553
}
1547
1555
 
1548
1556
void Item_func_curtime::fix_length_and_dec()
1549
1557
{
1550
 
  DRIZZLE_TIME ltime;
 
1558
  MYSQL_TIME ltime;
1551
1559
 
1552
1560
  decimals= DATETIME_DEC;
1553
1561
  collation.set(&my_charset_bin);
1554
1562
  store_now_in_TIME(&ltime);
1555
 
  value= TIME_to_uint64_t_time(&ltime);
 
1563
  value= TIME_to_ulonglong_time(&ltime);
1556
1564
  buff_length= (uint) my_time_to_str(&ltime, buff);
1557
1565
  max_length= buff_length;
1558
1566
}
1559
1567
 
1560
1568
 
1561
1569
/**
1562
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
 
1570
    Converts current time in my_time_t to MYSQL_TIME represenatation for local
1563
1571
    time zone. Defines time zone (local) used for whole CURTIME function.
1564
1572
*/
1565
 
void Item_func_curtime_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1573
void Item_func_curtime_local::store_now_in_TIME(MYSQL_TIME *now_time)
1566
1574
{
1567
1575
  THD *thd= current_thd;
1568
1576
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
1572
1580
 
1573
1581
 
1574
1582
/**
1575
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
 
1583
    Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1576
1584
    time zone. Defines time zone (UTC) used for whole UTC_TIME function.
1577
1585
*/
1578
 
void Item_func_curtime_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1586
void Item_func_curtime_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1579
1587
{
1580
1588
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1581
1589
                             (my_time_t)(current_thd->query_start()));
1586
1594
}
1587
1595
 
1588
1596
 
1589
 
String *Item_func_now::val_str(String *str __attribute__((unused)))
 
1597
String *Item_func_now::val_str(String *str)
1590
1598
{
1591
 
  assert(fixed == 1);
 
1599
  DBUG_ASSERT(fixed == 1);
1592
1600
  str_value.set(buff,buff_length, &my_charset_bin);
1593
1601
  return &str_value;
1594
1602
}
1600
1608
  collation.set(&my_charset_bin);
1601
1609
 
1602
1610
  store_now_in_TIME(&ltime);
1603
 
  value= (int64_t) TIME_to_uint64_t_datetime(&ltime);
 
1611
  value= (longlong) TIME_to_ulonglong_datetime(&ltime);
1604
1612
 
1605
1613
  buff_length= (uint) my_datetime_to_str(&ltime, buff);
1606
1614
  max_length= buff_length;
1608
1616
 
1609
1617
 
1610
1618
/**
1611
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
 
1619
    Converts current time in my_time_t to MYSQL_TIME represenatation for local
1612
1620
    time zone. Defines time zone (local) used for whole NOW function.
1613
1621
*/
1614
 
void Item_func_now_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1622
void Item_func_now_local::store_now_in_TIME(MYSQL_TIME *now_time)
1615
1623
{
1616
1624
  THD *thd= current_thd;
1617
1625
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
1621
1629
 
1622
1630
 
1623
1631
/**
1624
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
 
1632
    Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1625
1633
    time zone. Defines time zone (UTC) used for whole UTC_TIMESTAMP function.
1626
1634
*/
1627
 
void Item_func_now_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1635
void Item_func_now_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1628
1636
{
1629
1637
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1630
1638
                             (my_time_t)(current_thd->query_start()));
1635
1643
}
1636
1644
 
1637
1645
 
1638
 
bool Item_func_now::get_date(DRIZZLE_TIME *res,
1639
 
                             uint32_t fuzzy_date __attribute__((unused)))
 
1646
bool Item_func_now::get_date(MYSQL_TIME *res,
 
1647
                             uint fuzzy_date __attribute__((unused)))
1640
1648
{
1641
1649
  *res= ltime;
1642
1650
  return 0;
1643
1651
}
1644
1652
 
1645
1653
 
1646
 
int Item_func_now::save_in_field(Field *to, bool no_conversions __attribute__((unused)))
 
1654
int Item_func_now::save_in_field(Field *to, bool no_conversions)
1647
1655
{
1648
1656
  to->set_notnull();
1649
 
  return to->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
1657
  return to->store_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
1650
1658
}
1651
1659
 
1652
1660
 
1653
1661
/**
1654
 
    Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
 
1662
    Converts current time in my_time_t to MYSQL_TIME represenatation for local
1655
1663
    time zone. Defines time zone (local) used for whole SYSDATE function.
1656
1664
*/
1657
 
void Item_func_sysdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
 
1665
void Item_func_sysdate_local::store_now_in_TIME(MYSQL_TIME *now_time)
1658
1666
{
1659
1667
  THD *thd= current_thd;
1660
1668
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, (my_time_t) my_time(0));
1662
1670
}
1663
1671
 
1664
1672
 
1665
 
String *Item_func_sysdate_local::val_str(String *str __attribute__((unused)))
 
1673
String *Item_func_sysdate_local::val_str(String *str)
1666
1674
{
1667
 
  assert(fixed == 1);
 
1675
  DBUG_ASSERT(fixed == 1);
1668
1676
  store_now_in_TIME(&ltime);
1669
1677
  buff_length= (uint) my_datetime_to_str(&ltime, buff);
1670
1678
  str_value.set(buff, buff_length, &my_charset_bin);
1672
1680
}
1673
1681
 
1674
1682
 
1675
 
int64_t Item_func_sysdate_local::val_int()
 
1683
longlong Item_func_sysdate_local::val_int()
1676
1684
{
1677
 
  assert(fixed == 1);
 
1685
  DBUG_ASSERT(fixed == 1);
1678
1686
  store_now_in_TIME(&ltime);
1679
 
  return (int64_t) TIME_to_uint64_t_datetime(&ltime);
 
1687
  return (longlong) TIME_to_ulonglong_datetime(&ltime);
1680
1688
}
1681
1689
 
1682
1690
 
1683
1691
double Item_func_sysdate_local::val_real()
1684
1692
{
1685
 
  assert(fixed == 1);
 
1693
  DBUG_ASSERT(fixed == 1);
1686
1694
  store_now_in_TIME(&ltime);
1687
 
  return uint64_t2double(TIME_to_uint64_t_datetime(&ltime));
 
1695
  return ulonglong2double(TIME_to_ulonglong_datetime(&ltime));
1688
1696
}
1689
1697
 
1690
1698
 
1696
1704
}
1697
1705
 
1698
1706
 
1699
 
bool Item_func_sysdate_local::get_date(DRIZZLE_TIME *res,
1700
 
                                       uint32_t fuzzy_date __attribute__((unused)))
 
1707
bool Item_func_sysdate_local::get_date(MYSQL_TIME *res,
 
1708
                                       uint fuzzy_date __attribute__((unused)))
1701
1709
{
1702
1710
  store_now_in_TIME(&ltime);
1703
1711
  *res= ltime;
1705
1713
}
1706
1714
 
1707
1715
 
1708
 
int Item_func_sysdate_local::save_in_field(Field *to, bool no_conversions __attribute__((unused)))
 
1716
int Item_func_sysdate_local::save_in_field(Field *to, bool no_conversions)
1709
1717
{
1710
1718
  store_now_in_TIME(&ltime);
1711
1719
  to->set_notnull();
1712
 
  to->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
1720
  to->store_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
1713
1721
  return 0;
1714
1722
}
1715
1723
 
1716
1724
 
1717
1725
String *Item_func_sec_to_time::val_str(String *str)
1718
1726
{
1719
 
  assert(fixed == 1);
1720
 
  DRIZZLE_TIME ltime;
1721
 
  int64_t arg_val= args[0]->val_int(); 
 
1727
  DBUG_ASSERT(fixed == 1);
 
1728
  MYSQL_TIME ltime;
 
1729
  longlong arg_val= args[0]->val_int(); 
1722
1730
 
1723
1731
  if ((null_value=args[0]->null_value) ||
1724
1732
      str->alloc(MAX_DATE_STRING_REP_LENGTH))
1734
1742
}
1735
1743
 
1736
1744
 
1737
 
int64_t Item_func_sec_to_time::val_int()
 
1745
longlong Item_func_sec_to_time::val_int()
1738
1746
{
1739
 
  assert(fixed == 1);
1740
 
  DRIZZLE_TIME ltime;
1741
 
  int64_t arg_val= args[0]->val_int(); 
 
1747
  DBUG_ASSERT(fixed == 1);
 
1748
  MYSQL_TIME ltime;
 
1749
  longlong arg_val= args[0]->val_int(); 
1742
1750
  
1743
1751
  if ((null_value=args[0]->null_value))
1744
1752
    return 0;
1760
1768
  Item *arg1= args[1]->this_item();
1761
1769
 
1762
1770
  decimals=0;
1763
 
  const CHARSET_INFO * const cs= thd->variables.collation_connection;
1764
 
  uint32_t repertoire= arg1->collation.repertoire;
 
1771
  CHARSET_INFO *cs= thd->variables.collation_connection;
 
1772
  uint32 repertoire= arg1->collation.repertoire;
1765
1773
  if (!thd->variables.lc_time_names->is_ascii)
1766
1774
    repertoire|= MY_REPERTOIRE_EXTENDED;
1767
1775
  collation.set(cs, arg1->collation.derivation, repertoire);
1774
1782
  else
1775
1783
  {
1776
1784
    fixed_length=0;
1777
 
    max_length=cmin(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
 
1785
    max_length=min(arg1->max_length, MAX_BLOB_WIDTH) * 10 *
1778
1786
                   collation.collation->mbmaxlen;
1779
1787
    set_if_smaller(max_length,MAX_BLOB_WIDTH);
1780
1788
  }
1807
1815
 
1808
1816
 
1809
1817
 
1810
 
uint32_t Item_func_date_format::format_length(const String *format)
 
1818
uint Item_func_date_format::format_length(const String *format)
1811
1819
{
1812
 
  uint32_t size=0;
 
1820
  uint size=0;
1813
1821
  const char *ptr=format->ptr();
1814
1822
  const char *end=ptr+format->length();
1815
1823
 
1883
1891
String *Item_func_date_format::val_str(String *str)
1884
1892
{
1885
1893
  String *format;
1886
 
  DRIZZLE_TIME l_time;
1887
 
  uint32_t size;
1888
 
  assert(fixed == 1);
 
1894
  MYSQL_TIME l_time;
 
1895
  uint size;
 
1896
  DBUG_ASSERT(fixed == 1);
1889
1897
 
1890
1898
  if (!is_time_format)
1891
1899
  {
1926
1934
  /* Create the result string */
1927
1935
  str->set_charset(collation.collation);
1928
1936
  if (!make_date_time(&date_time_format, &l_time,
1929
 
                      is_time_format ? DRIZZLE_TIMESTAMP_TIME :
1930
 
                                       DRIZZLE_TIMESTAMP_DATE,
 
1937
                      is_time_format ? MYSQL_TIMESTAMP_TIME :
 
1938
                                       MYSQL_TIMESTAMP_DATE,
1931
1939
                      str))
1932
1940
    return str;
1933
1941
 
1950
1958
 
1951
1959
String *Item_func_from_unixtime::val_str(String *str)
1952
1960
{
1953
 
  DRIZZLE_TIME time_tmp;
 
1961
  MYSQL_TIME time_tmp;
1954
1962
 
1955
 
  assert(fixed == 1);
 
1963
  DBUG_ASSERT(fixed == 1);
1956
1964
 
1957
1965
  if (get_date(&time_tmp, 0))
1958
1966
    return 0;
1969
1977
}
1970
1978
 
1971
1979
 
1972
 
int64_t Item_func_from_unixtime::val_int()
 
1980
longlong Item_func_from_unixtime::val_int()
1973
1981
{
1974
 
  DRIZZLE_TIME time_tmp;
 
1982
  MYSQL_TIME time_tmp;
1975
1983
 
1976
 
  assert(fixed == 1);
 
1984
  DBUG_ASSERT(fixed == 1);
1977
1985
 
1978
1986
  if (get_date(&time_tmp, 0))
1979
1987
    return 0;
1980
1988
 
1981
 
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
 
1989
  return (longlong) TIME_to_ulonglong_datetime(&time_tmp);
1982
1990
}
1983
1991
 
1984
 
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime,
1985
 
                                       uint32_t fuzzy_date __attribute__((unused)))
 
1992
bool Item_func_from_unixtime::get_date(MYSQL_TIME *ltime,
 
1993
                                       uint fuzzy_date __attribute__((unused)))
1986
1994
{
1987
 
  uint64_t tmp= (uint64_t)(args[0]->val_int());
 
1995
  ulonglong tmp= (ulonglong)(args[0]->val_int());
1988
1996
  /*
1989
1997
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
1990
1998
    from_unixtime() argument since tmp is unsigned.
2011
2019
    The field type for the result of an Item_date function is defined as
2012
2020
    follows:
2013
2021
 
2014
 
    - If first arg is a DRIZZLE_TYPE_DATETIME result is DRIZZLE_TYPE_DATETIME
2015
 
    - If first arg is a DRIZZLE_TYPE_NEWDATE and the interval type uses hours,
2016
 
      minutes or seconds then type is DRIZZLE_TYPE_DATETIME.
2017
 
    - Otherwise the result is DRIZZLE_TYPE_VARCHAR
2018
 
      (This is because you can't know if the string contains a DATE, DRIZZLE_TIME or
 
2022
    - If first arg is a MYSQL_TYPE_DATETIME result is MYSQL_TYPE_DATETIME
 
2023
    - If first arg is a MYSQL_TYPE_DATE and the interval type uses hours,
 
2024
      minutes or seconds then type is MYSQL_TYPE_DATETIME.
 
2025
    - Otherwise the result is MYSQL_TYPE_STRING
 
2026
      (This is because you can't know if the string contains a DATE, MYSQL_TIME or
2019
2027
      DATETIME argument)
2020
2028
  */
2021
 
  cached_field_type= DRIZZLE_TYPE_VARCHAR;
 
2029
  cached_field_type= MYSQL_TYPE_STRING;
2022
2030
  arg0_field_type= args[0]->field_type();
2023
 
  if (arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2024
 
      arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2025
 
    cached_field_type= DRIZZLE_TYPE_DATETIME;
2026
 
  else if (arg0_field_type == DRIZZLE_TYPE_NEWDATE)
 
2031
  if (arg0_field_type == MYSQL_TYPE_DATETIME ||
 
2032
      arg0_field_type == MYSQL_TYPE_TIMESTAMP)
 
2033
    cached_field_type= MYSQL_TYPE_DATETIME;
 
2034
  else if (arg0_field_type == MYSQL_TYPE_DATE)
2027
2035
  {
2028
2036
    if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
2029
2037
      cached_field_type= arg0_field_type;
2030
2038
    else
2031
 
      cached_field_type= DRIZZLE_TYPE_DATETIME;
 
2039
      cached_field_type= MYSQL_TYPE_DATETIME;
2032
2040
  }
2033
2041
}
2034
2042
 
2035
2043
 
2036
2044
/* Here arg[1] is a Item_interval object */
2037
2045
 
2038
 
bool Item_date_add_interval::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
2046
bool Item_date_add_interval::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
2039
2047
{
2040
2048
  INTERVAL interval;
2041
2049
 
2054
2062
 
2055
2063
String *Item_date_add_interval::val_str(String *str)
2056
2064
{
2057
 
  assert(fixed == 1);
2058
 
  DRIZZLE_TIME ltime;
 
2065
  DBUG_ASSERT(fixed == 1);
 
2066
  MYSQL_TIME ltime;
2059
2067
  enum date_time_format_types format;
2060
2068
 
2061
2069
  if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
2062
2070
    return 0;
2063
2071
 
2064
 
  if (ltime.time_type == DRIZZLE_TIMESTAMP_DATE)
 
2072
  if (ltime.time_type == MYSQL_TIMESTAMP_DATE)
2065
2073
    format= DATE_ONLY;
2066
2074
  else if (ltime.second_part)
2067
2075
    format= DATE_TIME_MICROSECOND;
2076
2084
}
2077
2085
 
2078
2086
 
2079
 
int64_t Item_date_add_interval::val_int()
 
2087
longlong Item_date_add_interval::val_int()
2080
2088
{
2081
 
  assert(fixed == 1);
2082
 
  DRIZZLE_TIME ltime;
2083
 
  int64_t date;
 
2089
  DBUG_ASSERT(fixed == 1);
 
2090
  MYSQL_TIME ltime;
 
2091
  longlong date;
2084
2092
  if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
2085
 
    return (int64_t) 0;
 
2093
    return (longlong) 0;
2086
2094
  date = (ltime.year*100L + ltime.month)*100L + ltime.day;
2087
 
  return ltime.time_type == DRIZZLE_TIMESTAMP_DATE ? date :
 
2095
  return ltime.time_type == MYSQL_TIMESTAMP_DATE ? date :
2088
2096
    ((date*100L + ltime.hour)*100L+ ltime.minute)*100L + ltime.second;
2089
2097
}
2090
2098
 
2161
2169
  case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
2162
2170
  case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
2163
2171
  case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
2164
 
  case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
 
2172
  case INTERVAL_LAST: DBUG_ASSERT(0); break; /* purecov: deadcode */
2165
2173
  }
2166
2174
}
2167
2175
 
2168
2176
 
2169
 
int64_t Item_extract::val_int()
 
2177
longlong Item_extract::val_int()
2170
2178
{
2171
 
  assert(fixed == 1);
2172
 
  DRIZZLE_TIME ltime;
2173
 
  uint32_t year;
 
2179
  DBUG_ASSERT(fixed == 1);
 
2180
  MYSQL_TIME ltime;
 
2181
  uint year;
2174
2182
  ulong week_format;
2175
2183
  long neg;
2176
2184
  if (date_value)
2205
2213
  case INTERVAL_DAY_MINUTE:     return (long) (ltime.day*10000L+
2206
2214
                                               ltime.hour*100L+
2207
2215
                                               ltime.minute)*neg;
2208
 
  case INTERVAL_DAY_SECOND:      return ((int64_t) ltime.day*1000000L+
2209
 
                                         (int64_t) (ltime.hour*10000L+
 
2216
  case INTERVAL_DAY_SECOND:      return ((longlong) ltime.day*1000000L+
 
2217
                                         (longlong) (ltime.hour*10000L+
2210
2218
                                                     ltime.minute*100+
2211
2219
                                                     ltime.second))*neg;
2212
2220
  case INTERVAL_HOUR:           return (long) ltime.hour*neg;
2217
2225
  case INTERVAL_MINUTE_SECOND:  return (long) (ltime.minute*100+ltime.second)*neg;
2218
2226
  case INTERVAL_SECOND:         return (long) ltime.second*neg;
2219
2227
  case INTERVAL_MICROSECOND:    return (long) ltime.second_part*neg;
2220
 
  case INTERVAL_DAY_MICROSECOND: return (((int64_t)ltime.day*1000000L +
2221
 
                                          (int64_t)ltime.hour*10000L +
 
2228
  case INTERVAL_DAY_MICROSECOND: return (((longlong)ltime.day*1000000L +
 
2229
                                          (longlong)ltime.hour*10000L +
2222
2230
                                          ltime.minute*100 +
2223
2231
                                          ltime.second)*1000000L +
2224
2232
                                         ltime.second_part)*neg;
2225
 
  case INTERVAL_HOUR_MICROSECOND: return (((int64_t)ltime.hour*10000L +
 
2233
  case INTERVAL_HOUR_MICROSECOND: return (((longlong)ltime.hour*10000L +
2226
2234
                                           ltime.minute*100 +
2227
2235
                                           ltime.second)*1000000L +
2228
2236
                                          ltime.second_part)*neg;
2229
 
  case INTERVAL_MINUTE_MICROSECOND: return (((int64_t)(ltime.minute*100+
 
2237
  case INTERVAL_MINUTE_MICROSECOND: return (((longlong)(ltime.minute*100+
2230
2238
                                                        ltime.second))*1000000L+
2231
2239
                                            ltime.second_part)*neg;
2232
 
  case INTERVAL_SECOND_MICROSECOND: return ((int64_t)ltime.second*1000000L+
 
2240
  case INTERVAL_SECOND_MICROSECOND: return ((longlong)ltime.second*1000000L+
2233
2241
                                            ltime.second_part)*neg;
2234
 
  case INTERVAL_LAST: assert(0); break;  /* purecov: deadcode */
 
2242
  case INTERVAL_LAST: DBUG_ASSERT(0); break;  /* purecov: deadcode */
2235
2243
  }
2236
2244
  return 0;                                     // Impossible
2237
2245
}
2293
2301
    char buffer[20];
2294
2302
    // my_charset_bin is good enough for numbers
2295
2303
    String st(buffer, sizeof(buffer), &my_charset_bin);
2296
 
    st.set((uint64_t)cast_length, &my_charset_bin);
 
2304
    st.set((ulonglong)cast_length, &my_charset_bin);
2297
2305
    str->append(st);
2298
2306
    str->append(')');
2299
2307
  }
2307
2315
 
2308
2316
String *Item_char_typecast::val_str(String *str)
2309
2317
{
2310
 
  assert(fixed == 1);
 
2318
  DBUG_ASSERT(fixed == 1);
2311
2319
  String *res;
2312
 
  uint32_t length;
 
2320
  uint32 length;
2313
2321
 
2314
2322
  if (!charset_conversion)
2315
2323
  {
2322
2330
  else
2323
2331
  {
2324
2332
    // Convert character set if differ
2325
 
    uint32_t dummy_errors;
 
2333
    uint dummy_errors;
2326
2334
    if (!(res= args[0]->val_str(&tmp_value)) ||
2327
2335
        str->copy(res->ptr(), res->length(), from_cs,
2328
2336
        cast_cs, &dummy_errors))
2342
2350
  */
2343
2351
  if (cast_length >= 0)
2344
2352
  {
2345
 
    if (res->length() > (length= (uint32_t) res->charpos(cast_length)))
 
2353
    if (res->length() > (length= (uint32) res->charpos(cast_length)))
2346
2354
    {                                           // Safe even if const arg
2347
2355
      char char_type[40];
2348
 
      snprintf(char_type, sizeof(char_type), "%s(%lu)",
2349
 
               cast_cs == &my_charset_bin ? "BINARY" : "CHAR",
2350
 
               (ulong) length);
 
2356
      my_snprintf(char_type, sizeof(char_type), "%s(%lu)",
 
2357
                  cast_cs == &my_charset_bin ? "BINARY" : "CHAR",
 
2358
                  (ulong) length);
2351
2359
 
2352
2360
      if (!res->alloced_length())
2353
2361
      {                                         // Don't change const str
2354
2362
        str_value= *res;                        // Not malloced string
2355
2363
        res= &str_value;
2356
2364
      }
2357
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2365
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2358
2366
                          ER_TRUNCATED_WRONG_VALUE,
2359
2367
                          ER(ER_TRUNCATED_WRONG_VALUE), char_type,
2360
2368
                          res->c_ptr_safe());
2368
2376
        str->copy(*res);
2369
2377
        res= str;
2370
2378
      }
2371
 
      memset(res->ptr() + res->length(), 0,
2372
 
             (uint) cast_length - res->length());
 
2379
      bzero((char*) res->ptr() + res->length(),
 
2380
            (uint) cast_length - res->length());
2373
2381
      res->length(cast_length);
2374
2382
    }
2375
2383
  }
2380
2388
 
2381
2389
void Item_char_typecast::fix_length_and_dec()
2382
2390
{
2383
 
  uint32_t char_length;
 
2391
  uint32 char_length;
2384
2392
  /* 
2385
2393
     We always force character set conversion if cast_cs
2386
2394
     is a multi-byte character set. It garantees that the
2406
2414
  from_cs= (args[0]->result_type() == INT_RESULT || 
2407
2415
            args[0]->result_type() == DECIMAL_RESULT ||
2408
2416
            args[0]->result_type() == REAL_RESULT) ?
2409
 
           (cast_cs->mbminlen == 1 ? cast_cs : &my_charset_utf8_general_ci) :
 
2417
           (cast_cs->mbminlen == 1 ? cast_cs : &my_charset_latin1) :
2410
2418
           args[0]->collation.collation;
2411
2419
  charset_conversion= (cast_cs->mbmaxlen > 1) ||
2412
2420
                      (!my_charset_same(from_cs, cast_cs) && from_cs != &my_charset_bin && cast_cs != &my_charset_bin);
2419
2427
 
2420
2428
String *Item_datetime_typecast::val_str(String *str)
2421
2429
{
2422
 
  assert(fixed == 1);
2423
 
  DRIZZLE_TIME ltime;
 
2430
  DBUG_ASSERT(fixed == 1);
 
2431
  MYSQL_TIME ltime;
2424
2432
 
2425
2433
  if (!get_arg0_date(&ltime, TIME_FUZZY_DATE) &&
2426
2434
      !make_datetime(ltime.second_part ? DATE_TIME_MICROSECOND : DATE_TIME, 
2432
2440
}
2433
2441
 
2434
2442
 
2435
 
int64_t Item_datetime_typecast::val_int()
 
2443
longlong Item_datetime_typecast::val_int()
2436
2444
{
2437
 
  assert(fixed == 1);
2438
 
  DRIZZLE_TIME ltime;
 
2445
  DBUG_ASSERT(fixed == 1);
 
2446
  MYSQL_TIME ltime;
2439
2447
  if (get_arg0_date(&ltime,1))
2440
2448
  {
2441
2449
    null_value= 1;
2442
2450
    return 0;
2443
2451
  }
2444
2452
 
2445
 
  return TIME_to_uint64_t_datetime(&ltime);
 
2453
  return TIME_to_ulonglong_datetime(&ltime);
2446
2454
}
2447
2455
 
2448
2456
 
2449
 
bool Item_time_typecast::get_time(DRIZZLE_TIME *ltime)
 
2457
bool Item_time_typecast::get_time(MYSQL_TIME *ltime)
2450
2458
{
2451
2459
  bool res= get_arg0_time(ltime);
2452
2460
  /*
2453
 
    For DRIZZLE_TIMESTAMP_TIME value we can have non-zero day part,
 
2461
    For MYSQL_TIMESTAMP_TIME value we can have non-zero day part,
2454
2462
    which we should not lose.
2455
2463
  */
2456
 
  if (ltime->time_type == DRIZZLE_TIMESTAMP_DATETIME)
 
2464
  if (ltime->time_type == MYSQL_TIMESTAMP_DATETIME)
2457
2465
    ltime->year= ltime->month= ltime->day= 0;
2458
 
  ltime->time_type= DRIZZLE_TIMESTAMP_TIME;
 
2466
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
2459
2467
  return res;
2460
2468
}
2461
2469
 
2462
2470
 
2463
 
int64_t Item_time_typecast::val_int()
 
2471
longlong Item_time_typecast::val_int()
2464
2472
{
2465
 
  DRIZZLE_TIME ltime;
 
2473
  MYSQL_TIME ltime;
2466
2474
  if (get_time(&ltime))
2467
2475
  {
2468
2476
    null_value= 1;
2473
2481
 
2474
2482
String *Item_time_typecast::val_str(String *str)
2475
2483
{
2476
 
  assert(fixed == 1);
2477
 
  DRIZZLE_TIME ltime;
 
2484
  DBUG_ASSERT(fixed == 1);
 
2485
  MYSQL_TIME ltime;
2478
2486
 
2479
2487
  if (!get_arg0_time(&ltime) &&
2480
2488
      !make_datetime(ltime.second_part ? TIME_MICROSECOND : TIME_ONLY,
2486
2494
}
2487
2495
 
2488
2496
 
2489
 
bool Item_date_typecast::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
2497
bool Item_date_typecast::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
2490
2498
{
2491
2499
  bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
2492
2500
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
2493
 
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
 
2501
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
2494
2502
  return res;
2495
2503
}
2496
2504
 
2497
2505
 
2498
 
bool Item_date_typecast::get_time(DRIZZLE_TIME *ltime)
 
2506
bool Item_date_typecast::get_time(MYSQL_TIME *ltime)
2499
2507
{
2500
 
  memset(ltime, 0, sizeof(DRIZZLE_TIME));
 
2508
  bzero((char *)ltime, sizeof(MYSQL_TIME));
2501
2509
  return args[0]->null_value;
2502
2510
}
2503
2511
 
2504
2512
 
2505
2513
String *Item_date_typecast::val_str(String *str)
2506
2514
{
2507
 
  assert(fixed == 1);
2508
 
  DRIZZLE_TIME ltime;
 
2515
  DBUG_ASSERT(fixed == 1);
 
2516
  MYSQL_TIME ltime;
2509
2517
 
2510
2518
  if (!get_arg0_date(&ltime, TIME_FUZZY_DATE) &&
2511
2519
      !str->alloc(MAX_DATE_STRING_REP_LENGTH))
2518
2526
  return 0;
2519
2527
}
2520
2528
 
2521
 
int64_t Item_date_typecast::val_int()
 
2529
longlong Item_date_typecast::val_int()
2522
2530
{
2523
 
  assert(fixed == 1);
2524
 
  DRIZZLE_TIME ltime;
 
2531
  DBUG_ASSERT(fixed == 1);
 
2532
  MYSQL_TIME ltime;
2525
2533
  if ((null_value= args[0]->get_date(&ltime, TIME_FUZZY_DATE)))
2526
2534
    return 0;
2527
 
  return (int64_t) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
 
2535
  return (longlong) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
2528
2536
}
2529
2537
 
2530
2538
/**
2539
2547
 
2540
2548
String *Item_func_makedate::val_str(String *str)
2541
2549
{
2542
 
  assert(fixed == 1);
2543
 
  DRIZZLE_TIME l_time;
 
2550
  DBUG_ASSERT(fixed == 1);
 
2551
  MYSQL_TIME l_time;
2544
2552
  long daynr=  (long) args[1]->val_int();
2545
2553
  long year= (long) args[0]->val_int();
2546
2554
  long days;
2580
2588
    for dates between 0000-01-01 and 0099-12-31
2581
2589
*/
2582
2590
 
2583
 
int64_t Item_func_makedate::val_int()
 
2591
longlong Item_func_makedate::val_int()
2584
2592
{
2585
 
  assert(fixed == 1);
2586
 
  DRIZZLE_TIME l_time;
 
2593
  DBUG_ASSERT(fixed == 1);
 
2594
  MYSQL_TIME l_time;
2587
2595
  long daynr=  (long) args[1]->val_int();
2588
2596
  long year= (long) args[0]->val_int();
2589
2597
  long days;
2601
2609
  {
2602
2610
    null_value=0;
2603
2611
    get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
2604
 
    return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
 
2612
    return (longlong) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
2605
2613
  }
2606
2614
 
2607
2615
err:
2621
2629
    The field type for the result of an Item_func_add_time function is defined
2622
2630
    as follows:
2623
2631
 
2624
 
    - If first arg is a DRIZZLE_TYPE_DATETIME or DRIZZLE_TYPE_TIMESTAMP 
2625
 
      result is DRIZZLE_TYPE_DATETIME
2626
 
    - If first arg is a DRIZZLE_TYPE_TIME result is DRIZZLE_TYPE_TIME
2627
 
    - Otherwise the result is DRIZZLE_TYPE_VARCHAR
 
2632
    - If first arg is a MYSQL_TYPE_DATETIME or MYSQL_TYPE_TIMESTAMP 
 
2633
      result is MYSQL_TYPE_DATETIME
 
2634
    - If first arg is a MYSQL_TYPE_TIME result is MYSQL_TYPE_TIME
 
2635
    - Otherwise the result is MYSQL_TYPE_STRING
2628
2636
  */
2629
2637
 
2630
 
  cached_field_type= DRIZZLE_TYPE_VARCHAR;
 
2638
  cached_field_type= MYSQL_TYPE_STRING;
2631
2639
  arg0_field_type= args[0]->field_type();
2632
 
  if (arg0_field_type == DRIZZLE_TYPE_NEWDATE ||
2633
 
      arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2634
 
      arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2635
 
    cached_field_type= DRIZZLE_TYPE_DATETIME;
2636
 
  else if (arg0_field_type == DRIZZLE_TYPE_TIME)
2637
 
    cached_field_type= DRIZZLE_TYPE_TIME;
 
2640
  if (arg0_field_type == MYSQL_TYPE_DATE ||
 
2641
      arg0_field_type == MYSQL_TYPE_DATETIME ||
 
2642
      arg0_field_type == MYSQL_TYPE_TIMESTAMP)
 
2643
    cached_field_type= MYSQL_TYPE_DATETIME;
 
2644
  else if (arg0_field_type == MYSQL_TYPE_TIME)
 
2645
    cached_field_type= MYSQL_TYPE_TIME;
2638
2646
}
2639
2647
 
2640
2648
/**
2649
2657
 
2650
2658
String *Item_func_add_time::val_str(String *str)
2651
2659
{
2652
 
  assert(fixed == 1);
2653
 
  DRIZZLE_TIME l_time1, l_time2, l_time3;
 
2660
  DBUG_ASSERT(fixed == 1);
 
2661
  MYSQL_TIME l_time1, l_time2, l_time3;
2654
2662
  bool is_time= 0;
2655
2663
  long days, microseconds;
2656
 
  int64_t seconds;
 
2664
  longlong seconds;
2657
2665
  int l_sign= sign;
2658
2666
 
2659
2667
  null_value=0;
2661
2669
  {
2662
2670
    if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) || 
2663
2671
        args[1]->get_time(&l_time2) ||
2664
 
        l_time1.time_type == DRIZZLE_TIMESTAMP_TIME || 
2665
 
        l_time2.time_type != DRIZZLE_TIMESTAMP_TIME)
 
2672
        l_time1.time_type == MYSQL_TIMESTAMP_TIME || 
 
2673
        l_time2.time_type != MYSQL_TIMESTAMP_TIME)
2666
2674
      goto null_date;
2667
2675
  }
2668
2676
  else                                // ADDTIME function
2669
2677
  {
2670
2678
    if (args[0]->get_time(&l_time1) || 
2671
2679
        args[1]->get_time(&l_time2) ||
2672
 
        l_time2.time_type == DRIZZLE_TIMESTAMP_DATETIME)
 
2680
        l_time2.time_type == MYSQL_TIMESTAMP_DATETIME)
2673
2681
      goto null_date;
2674
 
    is_time= (l_time1.time_type == DRIZZLE_TIMESTAMP_TIME);
 
2682
    is_time= (l_time1.time_type == MYSQL_TIMESTAMP_TIME);
2675
2683
  }
2676
2684
  if (l_time1.neg != l_time2.neg)
2677
2685
    l_sign= -l_sign;
2678
2686
  
2679
 
  memset(&l_time3, 0, sizeof(l_time3));
 
2687
  bzero((char *)&l_time3, sizeof(l_time3));
2680
2688
  
2681
2689
  l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
2682
2690
                              &seconds, &microseconds);
2722
2730
{
2723
2731
  if (is_date)
2724
2732
  {
2725
 
    assert(sign > 0);
 
2733
    DBUG_ASSERT(sign > 0);
2726
2734
    str->append(STRING_WITH_LEN("timestamp("));
2727
2735
  }
2728
2736
  else
2749
2757
 
2750
2758
String *Item_func_timediff::val_str(String *str)
2751
2759
{
2752
 
  assert(fixed == 1);
2753
 
  int64_t seconds;
 
2760
  DBUG_ASSERT(fixed == 1);
 
2761
  longlong seconds;
2754
2762
  long microseconds;
2755
2763
  int l_sign= 1;
2756
 
  DRIZZLE_TIME l_time1 ,l_time2, l_time3;
 
2764
  MYSQL_TIME l_time1 ,l_time2, l_time3;
2757
2765
 
2758
2766
  null_value= 0;  
2759
2767
  if (args[0]->get_time(&l_time1) ||
2764
2772
  if (l_time1.neg != l_time2.neg)
2765
2773
    l_sign= -l_sign;
2766
2774
 
2767
 
  memset(&l_time3, 0, sizeof(l_time3));
 
2775
  bzero((char *)&l_time3, sizeof(l_time3));
2768
2776
  
2769
2777
  l_time3.neg= calc_time_diff(&l_time1, &l_time2, l_sign,
2770
2778
                              &seconds, &microseconds);
2771
2779
 
2772
2780
  /*
2773
 
    For DRIZZLE_TIMESTAMP_TIME only:
 
2781
    For MYSQL_TIMESTAMP_TIME only:
2774
2782
      If first argument was negative and diff between arguments
2775
2783
      is non-zero we need to swap sign to get proper result.
2776
2784
  */
2797
2805
 
2798
2806
String *Item_func_maketime::val_str(String *str)
2799
2807
{
2800
 
  assert(fixed == 1);
2801
 
  DRIZZLE_TIME ltime;
 
2808
  DBUG_ASSERT(fixed == 1);
 
2809
  MYSQL_TIME ltime;
2802
2810
  bool overflow= 0;
2803
2811
 
2804
 
  int64_t hour=   args[0]->val_int();
2805
 
  int64_t minute= args[1]->val_int();
2806
 
  int64_t second= args[2]->val_int();
 
2812
  longlong hour=   args[0]->val_int();
 
2813
  longlong minute= args[1]->val_int();
 
2814
  longlong second= args[2]->val_int();
2807
2815
 
2808
2816
  if ((null_value=(args[0]->null_value || 
2809
2817
                   args[1]->null_value ||
2813
2821
                   str->alloc(MAX_DATE_STRING_REP_LENGTH))))
2814
2822
    return 0;
2815
2823
 
2816
 
  memset(&ltime, 0, sizeof(ltime));
 
2824
  bzero((char *)&ltime, sizeof(ltime));
2817
2825
  ltime.neg= 0;
2818
2826
 
2819
2827
  /* Check for integer overflows */
2839
2847
    ltime.minute= TIME_MAX_MINUTE;
2840
2848
    ltime.second= TIME_MAX_SECOND;
2841
2849
    char buf[28];
2842
 
    char *ptr= int64_t10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
 
2850
    char *ptr= longlong10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
2843
2851
    int len = (int)(ptr - buf) +
2844
 
      sprintf(ptr, ":%02u:%02u", (uint)minute, (uint)second);
2845
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2846
 
                                 buf, len, DRIZZLE_TIMESTAMP_TIME,
2847
 
                                 NULL);
 
2852
      my_sprintf(ptr, (ptr, ":%02u:%02u", (uint)minute, (uint)second));
 
2853
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
2854
                                 buf, len, MYSQL_TIMESTAMP_TIME,
 
2855
                                 NullS);
2848
2856
  }
2849
2857
  
2850
2858
  if (make_time_with_warn((DATE_TIME_FORMAT *) 0, &ltime, str))
2864
2872
  Result: int value
2865
2873
*/
2866
2874
 
2867
 
int64_t Item_func_microsecond::val_int()
 
2875
longlong Item_func_microsecond::val_int()
2868
2876
{
2869
 
  assert(fixed == 1);
2870
 
  DRIZZLE_TIME ltime;
 
2877
  DBUG_ASSERT(fixed == 1);
 
2878
  MYSQL_TIME ltime;
2871
2879
  if (!get_arg0_time(&ltime))
2872
2880
    return ltime.second_part;
2873
2881
  return 0;
2874
2882
}
2875
2883
 
2876
2884
 
2877
 
int64_t Item_func_timestamp_diff::val_int()
 
2885
longlong Item_func_timestamp_diff::val_int()
2878
2886
{
2879
 
  DRIZZLE_TIME ltime1, ltime2;
2880
 
  int64_t seconds;
 
2887
  MYSQL_TIME ltime1, ltime2;
 
2888
  longlong seconds;
2881
2889
  long microseconds;
2882
2890
  long months= 0;
2883
2891
  int neg= 1;
2895
2903
      int_type == INTERVAL_QUARTER ||
2896
2904
      int_type == INTERVAL_MONTH)
2897
2905
  {
2898
 
    uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
2899
 
    uint32_t years= 0;
2900
 
    uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
 
2906
    uint year_beg, year_end, month_beg, month_end, day_beg, day_end;
 
2907
    uint years= 0;
 
2908
    uint second_beg, second_end, microsecond_beg, microsecond_end;
2901
2909
 
2902
2910
    if (neg == -1)
2903
2911
    {
2966
2974
  case INTERVAL_MICROSECOND:
2967
2975
    /*
2968
2976
      In MySQL difference between any two valid datetime values
2969
 
      in microseconds fits into int64_t.
 
2977
      in microseconds fits into longlong.
2970
2978
    */
2971
2979
    return (seconds*1000000L+microseconds)*neg;
2972
2980
  default:
3016
3024
    break;
3017
3025
  }
3018
3026
 
3019
 
  for (uint32_t i=0 ; i < 2 ; i++)
 
3027
  for (uint i=0 ; i < 2 ; i++)
3020
3028
  {
3021
3029
    str->append(',');
3022
3030
    args[i]->print(str, query_type);
3027
3035
 
3028
3036
String *Item_func_get_format::val_str(String *str)
3029
3037
{
3030
 
  assert(fixed == 1);
 
3038
  DBUG_ASSERT(fixed == 1);
3031
3039
  const char *format_name;
3032
3040
  KNOWN_DATE_TIME_FORMAT *format;
3033
3041
  String *val= args[0]->val_str(str);
3041
3049
       (format_name= format->format_name);
3042
3050
       format++)
3043
3051
  {
3044
 
    uint32_t format_name_len;
 
3052
    uint format_name_len;
3045
3053
    format_name_len= strlen(format_name);
3046
3054
    if (val_len == format_name_len &&
3047
 
        !my_strnncoll(&my_charset_utf8_general_ci, 
3048
 
                      (const unsigned char *) val->ptr(), val_len, 
3049
 
                      (const unsigned char *) format_name, val_len))
 
3055
        !my_strnncoll(&my_charset_latin1, 
 
3056
                      (const uchar *) val->ptr(), val_len, 
 
3057
                      (const uchar *) format_name, val_len))
3050
3058
    {
3051
3059
      const char *format_str= get_date_time_format_str(format, type);
3052
3060
      str->set(format_str, strlen(format_str), &my_charset_bin);
3065
3073
  str->append('(');
3066
3074
 
3067
3075
  switch (type) {
3068
 
  case DRIZZLE_TIMESTAMP_DATE:
 
3076
  case MYSQL_TIMESTAMP_DATE:
3069
3077
    str->append(STRING_WITH_LEN("DATE, "));
3070
3078
    break;
3071
 
  case DRIZZLE_TIMESTAMP_DATETIME:
 
3079
  case MYSQL_TIMESTAMP_DATETIME:
3072
3080
    str->append(STRING_WITH_LEN("DATETIME, "));
3073
3081
    break;
3074
 
  case DRIZZLE_TIMESTAMP_TIME:
 
3082
  case MYSQL_TIMESTAMP_TIME:
3075
3083
    str->append(STRING_WITH_LEN("TIME, "));
3076
3084
    break;
3077
3085
  default:
3078
 
    assert(0);
 
3086
    DBUG_ASSERT(0);
3079
3087
  }
3080
3088
  args[0]->print(str, query_type);
3081
3089
  str->append(')');
3107
3115
*/
3108
3116
 
3109
3117
static date_time_format_types
3110
 
get_date_time_result_type(const char *format, uint32_t length)
 
3118
get_date_time_result_type(const char *format, uint length)
3111
3119
{
3112
3120
  const char *time_part_frms= "HISThiklrs";
3113
3121
  const char *date_part_frms= "MVUXYWabcjmvuxyw";
3155
3163
{
3156
3164
  maybe_null= 1;
3157
3165
  decimals=0;
3158
 
  cached_field_type= DRIZZLE_TYPE_DATETIME;
 
3166
  cached_field_type= MYSQL_TYPE_DATETIME;
3159
3167
  max_length= MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
3160
 
  cached_timestamp_type= DRIZZLE_TIMESTAMP_NONE;
 
3168
  cached_timestamp_type= MYSQL_TIMESTAMP_NONE;
3161
3169
  if ((const_item= args[1]->const_item()))
3162
3170
  {
3163
3171
    char format_buff[64];
3169
3177
                                                    format->length());
3170
3178
      switch (cached_format_type) {
3171
3179
      case DATE_ONLY:
3172
 
        cached_timestamp_type= DRIZZLE_TIMESTAMP_DATE;
3173
 
        cached_field_type= DRIZZLE_TYPE_NEWDATE; 
 
3180
        cached_timestamp_type= MYSQL_TIMESTAMP_DATE;
 
3181
        cached_field_type= MYSQL_TYPE_DATE; 
3174
3182
        max_length= MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3175
3183
        break;
3176
3184
      case TIME_ONLY:
3177
3185
      case TIME_MICROSECOND:
3178
 
        cached_timestamp_type= DRIZZLE_TIMESTAMP_TIME;
3179
 
        cached_field_type= DRIZZLE_TYPE_TIME; 
 
3186
        cached_timestamp_type= MYSQL_TIMESTAMP_TIME;
 
3187
        cached_field_type= MYSQL_TYPE_TIME; 
3180
3188
        max_length= MAX_TIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3181
3189
        break;
3182
3190
      default:
3183
 
        cached_timestamp_type= DRIZZLE_TIMESTAMP_DATETIME;
3184
 
        cached_field_type= DRIZZLE_TYPE_DATETIME; 
 
3191
        cached_timestamp_type= MYSQL_TIMESTAMP_DATETIME;
 
3192
        cached_field_type= MYSQL_TYPE_DATETIME; 
3185
3193
        break;
3186
3194
      }
3187
3195
    }
3189
3197
}
3190
3198
 
3191
3199
 
3192
 
bool Item_func_str_to_date::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
 
3200
bool Item_func_str_to_date::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
3193
3201
{
3194
3202
  DATE_TIME_FORMAT date_time_format;
3195
3203
  char val_buff[64], format_buff[64];
3202
3210
    goto null_date;
3203
3211
 
3204
3212
  null_value= 0;
3205
 
  memset(ltime, 0, sizeof(*ltime));
 
3213
  bzero((char*) ltime, sizeof(*ltime));
3206
3214
  date_time_format.format.str=    (char*) format->ptr();
3207
3215
  date_time_format.format.length= format->length();
3208
3216
  if (extract_date_time(&date_time_format, val->ptr(), val->length(),
3210
3218
      ((fuzzy_date & TIME_NO_ZERO_DATE) &&
3211
3219
       (ltime->year == 0 || ltime->month == 0 || ltime->day == 0)))
3212
3220
    goto null_date;
3213
 
  if (cached_timestamp_type == DRIZZLE_TIMESTAMP_TIME && ltime->day)
 
3221
  if (cached_timestamp_type == MYSQL_TIMESTAMP_TIME && ltime->day)
3214
3222
  {
3215
3223
    /*
3216
3224
      Day part for time type can be nonzero value and so 
3229
3237
 
3230
3238
String *Item_func_str_to_date::val_str(String *str)
3231
3239
{
3232
 
  assert(fixed == 1);
3233
 
  DRIZZLE_TIME ltime;
 
3240
  DBUG_ASSERT(fixed == 1);
 
3241
  MYSQL_TIME ltime;
3234
3242
 
3235
3243
  if (Item_func_str_to_date::get_date(&ltime, TIME_FUZZY_DATE))
3236
3244
    return 0;
3243
3251
}
3244
3252
 
3245
3253
 
3246
 
bool Item_func_last_day::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
 
3254
bool Item_func_last_day::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
3247
3255
{
3248
3256
  if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE) ||
3249
3257
      (ltime->month == 0))
3252
3260
    return 1;
3253
3261
  }
3254
3262
  null_value= 0;
3255
 
  uint32_t month_idx= ltime->month-1;
 
3263
  uint month_idx= ltime->month-1;
3256
3264
  ltime->day= days_in_month[month_idx];
3257
3265
  if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
3258
3266
    ltime->day= 29;
3259
3267
  ltime->hour= ltime->minute= ltime->second= 0;
3260
3268
  ltime->second_part= 0;
3261
 
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
 
3269
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
3262
3270
  return 0;
3263
3271
}