~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_timefunc.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* Copyright (C) 2000-2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
15
 
20
16
 
21
17
/**
29
25
*/
30
26
#include <drizzled/server_includes.h>
31
27
#include <time.h>
32
 
#include <drizzled/error.h>
33
 
#include <drizzled/tztime.h>
 
28
#include <drizzled/drizzled_error_messages.h>
34
29
 
35
30
/** Day number for Dec 31st, 9999. */
36
31
#define MAX_DAY_NUMBER 3652424L
58
53
{
59
54
  char *buff;
60
55
  const CHARSET_INFO * const cs= &my_charset_bin;
61
 
  uint32_t length= MAX_DATE_STRING_REP_LENGTH;
 
56
  uint length= MAX_DATE_STRING_REP_LENGTH;
62
57
 
63
58
  if (str->alloc(length))
64
59
    return 1;
124
119
  if (!warning)
125
120
    return 0;
126
121
 
127
 
  make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
122
  make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
128
123
                               str->ptr(), str->length(),
129
 
                               DRIZZLE_TIMESTAMP_TIME, NULL);
 
124
                               DRIZZLE_TIMESTAMP_TIME, NullS);
130
125
  return make_datetime(format, ltime, str);
131
126
}
132
127
 
151
146
    return 1;
152
147
  if (warning)
153
148
  {
154
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
149
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
155
150
                                 str->ptr(), str->length(),
156
 
                                 DRIZZLE_TIMESTAMP_TIME, NULL);
 
151
                                 DRIZZLE_TIMESTAMP_TIME, NullS);
157
152
    make_time(format, l_time, str);
158
153
  }
159
154
 
183
178
  
184
179
static bool sec_to_time(int64_t seconds, bool unsigned_flag, DRIZZLE_TIME *ltime)
185
180
{
186
 
  uint32_t sec;
 
181
  uint sec;
187
182
 
188
183
  memset(ltime, 0, sizeof(*ltime));
189
184
  
190
185
  if (seconds < 0)
191
186
  {
 
187
    if (unsigned_flag)
 
188
      goto overflow;
192
189
    ltime->neg= 1;
193
190
    if (seconds < -3020399)
194
191
      goto overflow;
212
209
  char buf[22];
213
210
  int len= (int)(int64_t10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
214
211
                 - buf);
215
 
  make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
212
  make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
216
213
                               buf, len, DRIZZLE_TIMESTAMP_TIME,
217
 
                               NULL);
 
214
                               NullS);
218
215
  
219
216
  return 1;
220
217
}
265
262
*/
266
263
 
267
264
static bool extract_date_time(DATE_TIME_FORMAT *format,
268
 
                              const char *val, uint32_t length, DRIZZLE_TIME *l_time,
269
 
                              enum enum_drizzle_timestamp_type cached_timestamp_type,
 
265
                              const char *val, uint length, DRIZZLE_TIME *l_time,
 
266
                              timestamp_type cached_timestamp_type,
270
267
                              const char **sub_pattern_end,
271
268
                              const char *date_time_type)
272
269
{
305
302
      switch (*++ptr) {
306
303
        /* Year */
307
304
      case 'Y':
308
 
        tmp= (char*) val + cmin(4, val_len);
 
305
        tmp= (char*) val + min(4, val_len);
309
306
        l_time->year= (int) my_strtoll10(val, &tmp, &error);
310
307
        if ((int) (tmp-val) <= 2)
311
308
          l_time->year= year_2000_handling(l_time->year);
312
309
        val= tmp;
313
310
        break;
314
311
      case 'y':
315
 
        tmp= (char*) val + cmin(2, val_len);
 
312
        tmp= (char*) val + min(2, val_len);
316
313
        l_time->year= (int) my_strtoll10(val, &tmp, &error);
317
314
        val= tmp;
318
315
        l_time->year= year_2000_handling(l_time->year);
321
318
        /* Month */
322
319
      case 'm':
323
320
      case 'c':
324
 
        tmp= (char*) val + cmin(2, val_len);
 
321
        tmp= (char*) val + min(2, val_len);
325
322
        l_time->month= (int) my_strtoll10(val, &tmp, &error);
326
323
        val= tmp;
327
324
        break;
338
335
        /* Day */
339
336
      case 'd':
340
337
      case 'e':
341
 
        tmp= (char*) val + cmin(2, val_len);
 
338
        tmp= (char*) val + min(2, val_len);
342
339
        l_time->day= (int) my_strtoll10(val, &tmp, &error);
343
340
        val= tmp;
344
341
        break;
345
342
      case 'D':
346
 
        tmp= (char*) val + cmin(2, val_len);
 
343
        tmp= (char*) val + min(2, val_len);
347
344
        l_time->day= (int) my_strtoll10(val, &tmp, &error);
348
345
        /* Skip 'st, 'nd, 'th .. */
349
 
        val= tmp + cmin((int) (val_end-tmp), 2);
 
346
        val= tmp + min((int) (val_end-tmp), 2);
350
347
        break;
351
348
 
352
349
        /* Hour */
357
354
        /* fall through */
358
355
      case 'k':
359
356
      case 'H':
360
 
        tmp= (char*) val + cmin(2, val_len);
 
357
        tmp= (char*) val + min(2, val_len);
361
358
        l_time->hour= (int) my_strtoll10(val, &tmp, &error);
362
359
        val= tmp;
363
360
        break;
364
361
 
365
362
        /* Minute */
366
363
      case 'i':
367
 
        tmp= (char*) val + cmin(2, val_len);
 
364
        tmp= (char*) val + min(2, val_len);
368
365
        l_time->minute= (int) my_strtoll10(val, &tmp, &error);
369
366
        val= tmp;
370
367
        break;
372
369
        /* Second */
373
370
      case 's':
374
371
      case 'S':
375
 
        tmp= (char*) val + cmin(2, val_len);
 
372
        tmp= (char*) val + min(2, val_len);
376
373
        l_time->second= (int) my_strtoll10(val, &tmp, &error);
377
374
        val= tmp;
378
375
        break;
394
391
        if (val_len < 2 || ! usa_time)
395
392
          goto err;
396
393
        if (!my_strnncoll(&my_charset_utf8_general_ci,
397
 
                          (const unsigned char *) val, 2, 
398
 
                          (const unsigned char *) "PM", 2))
 
394
                          (const uchar *) val, 2, 
 
395
                          (const uchar *) "PM", 2))
399
396
          daypart= 12;
400
397
        else if (my_strnncoll(&my_charset_utf8_general_ci,
401
 
                              (const unsigned char *) val, 2, 
402
 
                              (const unsigned char *) "AM", 2))
 
398
                              (const uchar *) val, 2, 
 
399
                              (const uchar *) "AM", 2))
403
400
          goto err;
404
401
        val+= 2;
405
402
        break;
424
421
        val= tmp;
425
422
        break;
426
423
      case 'j':
427
 
        tmp= (char*) val + cmin(val_len, 3);
 
424
        tmp= (char*) val + min(val_len, 3);
428
425
        yearday= (int) my_strtoll10(val, &tmp, &error);
429
426
        val= tmp;
430
427
        break;
436
433
      case 'u':
437
434
        sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
438
435
        strict_week_number= (*ptr=='V' || *ptr=='v');
439
 
        tmp= (char*) val + cmin(val_len, 2);
 
436
        tmp= (char*) val + min(val_len, 2);
440
437
        if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
441
438
            (strict_week_number && !week_number) ||
442
439
            week_number > 53)
448
445
      case 'X':
449
446
      case 'x':
450
447
        strict_week_number_year_type= (*ptr=='X');
451
 
        tmp= (char*) val + cmin(4, val_len);
 
448
        tmp= (char*) val + min(4, val_len);
452
449
        strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
453
450
        val= tmp;
454
451
        break;
518
515
 
519
516
  if (yearday > 0)
520
517
  {
521
 
    uint32_t days;
 
518
    uint days;
522
519
    days= calc_daynr(l_time->year,1,1) +  yearday - 1;
523
520
    if (days <= 0 || days > MAX_DAY_NUMBER)
524
521
      goto err;
528
525
  if (week_number >= 0 && weekday)
529
526
  {
530
527
    int days;
531
 
    uint32_t weekday_b;
 
528
    uint weekday_b;
532
529
 
533
530
    /*
534
531
      %V,%v require %X,%x resprectively,
580
577
    {
581
578
      if (!my_isspace(&my_charset_utf8_general_ci,*val))
582
579
      {
583
 
        make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
580
        make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
584
581
                                     val_begin, length,
585
 
                                     cached_timestamp_type, NULL);
 
582
                                     cached_timestamp_type, NullS);
586
583
        break;
587
584
      }
588
585
    } while (++val != val_end);
592
589
err:
593
590
  {
594
591
    char buff[128];
595
 
    strmake(buff, val_begin, cmin(length, (uint)sizeof(buff)-1));
596
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
592
    strmake(buff, val_begin, min(length, (uint)sizeof(buff)-1));
 
593
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
597
594
                        ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
598
595
                        date_time_type, buff, "str_to_date");
599
596
  }
606
603
*/
607
604
 
608
605
bool make_date_time(DATE_TIME_FORMAT *format, DRIZZLE_TIME *l_time,
609
 
                    enum enum_drizzle_timestamp_type type, String *str)
 
606
                    timestamp_type type, String *str)
610
607
{
611
608
  char intbuff[15];
612
 
  uint32_t hours_i;
613
 
  uint32_t weekday;
 
609
  uint hours_i;
 
610
  uint weekday;
614
611
  ulong length;
615
612
  const char *ptr, *end;
616
 
  Session *session= current_session;
617
 
  MY_LOCALE *locale= session->variables.lc_time_names;
 
613
  THD *thd= current_thd;
 
614
  MY_LOCALE *locale= thd->variables.lc_time_names;
618
615
 
619
616
  str->length(0);
620
617
 
774
771
      case 'U':
775
772
      case 'u':
776
773
      {
777
 
        uint32_t year;
 
774
        uint year;
778
775
        if (type == DRIZZLE_TIMESTAMP_TIME)
779
776
          return 1;
780
777
        length= int10_to_str(calc_week(l_time,
788
785
      case 'v':
789
786
      case 'V':
790
787
      {
791
 
        uint32_t year;
 
788
        uint year;
792
789
        if (type == DRIZZLE_TIMESTAMP_TIME)
793
790
          return 1;
794
791
        length= int10_to_str(calc_week(l_time,
803
800
      case 'x':
804
801
      case 'X':
805
802
      {
806
 
        uint32_t year;
 
803
        uint year;
807
804
        if (type == DRIZZLE_TIMESTAMP_TIME)
808
805
          return 1;
809
806
        (void) calc_week(l_time,
853
850
                         For example, '1.1' -> '1.100000'
854
851
*/
855
852
 
856
 
static bool get_interval_info(const char *str,uint32_t length, const CHARSET_INFO * const cs,
857
 
                              uint32_t count, uint64_t *values,
 
853
static bool get_interval_info(const char *str,uint length, const CHARSET_INFO * const cs,
 
854
                              uint count, uint64_t *values,
858
855
                              bool transform_msec)
859
856
{
860
857
  const char *end=str+length;
861
 
  uint32_t i;
 
858
  uint i;
862
859
  while (str != end && !my_isdigit(cs,*str))
863
860
    str++;
864
861
 
867
864
    int64_t value;
868
865
    const char *start= str;
869
866
    for (value=0; str != end && my_isdigit(cs,*str) ; str++)
870
 
      value= value * 10L + (int64_t) (*str - '0');
 
867
      value= value * 10LL + (int64_t) (*str - '0');
871
868
    if (transform_msec && i == count - 1) // microseconds always last
872
869
    {
873
870
      long msec_length= 6 - (str - start);
881
878
    {
882
879
      i++;
883
880
      /* Change values[0...i-1] -> values[0...count-1] */
884
 
      bmove_upp((unsigned char*) (values+count), (unsigned char*) (values+i),
 
881
      bmove_upp((uchar*) (values+count), (uchar*) (values+i),
885
882
                sizeof(*values)*i);
886
883
      memset(values, 0, sizeof(*values)*(count-i));
887
884
      break;
894
891
int64_t Item_func_period_add::val_int()
895
892
{
896
893
  assert(fixed == 1);
897
 
  uint32_t period= args[0]->val_int();
 
894
  ulong period=(ulong) args[0]->val_int();
898
895
  int months=(int) args[1]->val_int();
899
896
 
900
897
  if ((null_value=args[0]->null_value || args[1]->null_value) ||
909
906
int64_t Item_func_period_diff::val_int()
910
907
{
911
908
  assert(fixed == 1);
912
 
  uint32_t period1= args[0]->val_int();
913
 
  uint32_t period2= args[1]->val_int();
 
909
  ulong period1=(ulong) args[0]->val_int();
 
910
  ulong period2=(ulong) args[1]->val_int();
914
911
 
915
912
  if ((null_value=args[0]->null_value || args[1]->null_value))
916
913
    return 0; /* purecov: inspected */
1026
1023
{
1027
1024
  assert(fixed == 1);
1028
1025
  const char *month_name;
1029
 
  uint32_t   month= (uint) val_int();
1030
 
  Session *session= current_session;
 
1026
  uint   month= (uint) val_int();
 
1027
  THD *thd= current_thd;
1031
1028
 
1032
1029
  if (null_value || !month)
1033
1030
  {
1035
1032
    return (String*) 0;
1036
1033
  }
1037
1034
  null_value=0;
1038
 
  month_name= session->variables.lc_time_names->month_names->type_names[month-1];
 
1035
  month_name= thd->variables.lc_time_names->month_names->type_names[month-1];
1039
1036
  str->set(month_name, strlen(month_name), system_charset_info);
1040
1037
  return str;
1041
1038
}
1082
1079
}
1083
1080
 
1084
1081
 
1085
 
uint32_t week_mode(uint32_t mode)
 
1082
uint week_mode(uint mode)
1086
1083
{
1087
 
  uint32_t week_format= (mode & 7);
 
1084
  uint week_format= (mode & 7);
1088
1085
  if (!(week_format & WEEK_MONDAY_FIRST))
1089
1086
    week_format^= WEEK_FIRST_WEEKDAY;
1090
1087
  return week_format;
1124
1121
int64_t Item_func_week::val_int()
1125
1122
{
1126
1123
  assert(fixed == 1);
1127
 
  uint32_t year;
 
1124
  uint year;
1128
1125
  DRIZZLE_TIME ltime;
1129
1126
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
1130
1127
    return 0;
1137
1134
int64_t Item_func_yearweek::val_int()
1138
1135
{
1139
1136
  assert(fixed == 1);
1140
 
  uint32_t year,week;
 
1137
  uint year,week;
1141
1138
  DRIZZLE_TIME ltime;
1142
1139
  if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
1143
1140
    return 0;
1165
1162
String* Item_func_dayname::val_str(String* str)
1166
1163
{
1167
1164
  assert(fixed == 1);
1168
 
  uint32_t weekday=(uint) val_int();            // Always Item_func_daynr()
 
1165
  uint weekday=(uint) val_int();                // Always Item_func_daynr()
1169
1166
  const char *day_name;
1170
 
  Session *session= current_session;
 
1167
  THD *thd= current_thd;
1171
1168
 
1172
1169
  if (null_value)
1173
1170
    return (String*) 0;
1174
1171
  
1175
 
  day_name= session->variables.lc_time_names->day_names->type_names[weekday];
 
1172
  day_name= thd->variables.lc_time_names->day_names->type_names[weekday];
1176
1173
  str->set(day_name, strlen(day_name), system_charset_info);
1177
1174
  return str;
1178
1175
}
1248
1245
  
1249
1246
  assert(fixed == 1);
1250
1247
  if (arg_count == 0)
1251
 
    return (int64_t) current_session->query_start();
 
1248
    return (int64_t) current_thd->query_start();
1252
1249
  if (args[0]->type() == FIELD_ITEM)
1253
1250
  {                                             // Optimize timestamp field
1254
1251
    Field *field=((Item_field*) args[0])->field;
1267
1264
    return 0;
1268
1265
  }
1269
1266
  
1270
 
  return (int64_t) TIME_to_timestamp(current_session, &ltime, &not_used);
 
1267
  return (int64_t) TIME_to_timestamp(current_thd, &ltime, &not_used);
1271
1268
}
1272
1269
 
1273
1270
 
1466
1463
}
1467
1464
 
1468
1465
 
1469
 
bool Item_func_from_days::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
1466
bool Item_func_from_days::get_date(DRIZZLE_TIME *ltime, uint fuzzy_date __attribute__((unused)))
1470
1467
{
1471
1468
  int64_t value=args[0]->val_int();
1472
1469
  if ((null_value=args[0]->null_value))
1510
1507
*/
1511
1508
void Item_func_curdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1512
1509
{
1513
 
  Session *session= current_session;
1514
 
  session->variables.time_zone->gmt_sec_to_TIME(now_time, 
1515
 
                                             (my_time_t)session->query_start());
1516
 
  session->time_zone_used= 1;
 
1510
  THD *thd= current_thd;
 
1511
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
 
1512
                                             (my_time_t)thd->query_start());
 
1513
  thd->time_zone_used= 1;
1517
1514
}
1518
1515
 
1519
1516
 
1524
1521
void Item_func_curdate_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1525
1522
{
1526
1523
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1527
 
                             (my_time_t)(current_session->query_start()));
 
1524
                             (my_time_t)(current_thd->query_start()));
1528
1525
  /* 
1529
1526
    We are not flagging this query as using time zone, since it uses fixed
1530
1527
    UTC-SYSTEM time-zone.
1533
1530
 
1534
1531
 
1535
1532
bool Item_func_curdate::get_date(DRIZZLE_TIME *res,
1536
 
                                 uint32_t fuzzy_date __attribute__((unused)))
 
1533
                                 uint fuzzy_date __attribute__((unused)))
1537
1534
{
1538
1535
  *res=ltime;
1539
1536
  return 0;
1567
1564
*/
1568
1565
void Item_func_curtime_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1569
1566
{
1570
 
  Session *session= current_session;
1571
 
  session->variables.time_zone->gmt_sec_to_TIME(now_time, 
1572
 
                                             (my_time_t)session->query_start());
1573
 
  session->time_zone_used= 1;
 
1567
  THD *thd= current_thd;
 
1568
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
 
1569
                                             (my_time_t)thd->query_start());
 
1570
  thd->time_zone_used= 1;
1574
1571
}
1575
1572
 
1576
1573
 
1581
1578
void Item_func_curtime_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1582
1579
{
1583
1580
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1584
 
                             (my_time_t)(current_session->query_start()));
 
1581
                             (my_time_t)(current_thd->query_start()));
1585
1582
  /* 
1586
1583
    We are not flagging this query as using time zone, since it uses fixed
1587
1584
    UTC-SYSTEM time-zone.
1616
1613
*/
1617
1614
void Item_func_now_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1618
1615
{
1619
 
  Session *session= current_session;
1620
 
  session->variables.time_zone->gmt_sec_to_TIME(now_time, 
1621
 
                                             (my_time_t)session->query_start());
1622
 
  session->time_zone_used= 1;
 
1616
  THD *thd= current_thd;
 
1617
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, 
 
1618
                                             (my_time_t)thd->query_start());
 
1619
  thd->time_zone_used= 1;
1623
1620
}
1624
1621
 
1625
1622
 
1630
1627
void Item_func_now_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1631
1628
{
1632
1629
  my_tz_UTC->gmt_sec_to_TIME(now_time, 
1633
 
                             (my_time_t)(current_session->query_start()));
 
1630
                             (my_time_t)(current_thd->query_start()));
1634
1631
  /* 
1635
1632
    We are not flagging this query as using time zone, since it uses fixed
1636
1633
    UTC-SYSTEM time-zone.
1639
1636
 
1640
1637
 
1641
1638
bool Item_func_now::get_date(DRIZZLE_TIME *res,
1642
 
                             uint32_t fuzzy_date __attribute__((unused)))
 
1639
                             uint fuzzy_date __attribute__((unused)))
1643
1640
{
1644
1641
  *res= ltime;
1645
1642
  return 0;
1659
1656
*/
1660
1657
void Item_func_sysdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1661
1658
{
1662
 
  Session *session= current_session;
1663
 
  session->variables.time_zone->gmt_sec_to_TIME(now_time, (my_time_t) my_time(0));
1664
 
  session->time_zone_used= 1;
 
1659
  THD *thd= current_thd;
 
1660
  thd->variables.time_zone->gmt_sec_to_TIME(now_time, (my_time_t) my_time(0));
 
1661
  thd->time_zone_used= 1;
1665
1662
}
1666
1663
 
1667
1664
 
1700
1697
 
1701
1698
 
1702
1699
bool Item_func_sysdate_local::get_date(DRIZZLE_TIME *res,
1703
 
                                       uint32_t fuzzy_date __attribute__((unused)))
 
1700
                                       uint fuzzy_date __attribute__((unused)))
1704
1701
{
1705
1702
  store_now_in_TIME(&ltime);
1706
1703
  *res= ltime;
1755
1752
 
1756
1753
void Item_func_date_format::fix_length_and_dec()
1757
1754
{
1758
 
  Session* session= current_session;
 
1755
  THD* thd= current_thd;
1759
1756
  /*
1760
1757
    Must use this_item() in case it's a local SP variable
1761
1758
    (for ->max_length and ->str_value)
1763
1760
  Item *arg1= args[1]->this_item();
1764
1761
 
1765
1762
  decimals=0;
1766
 
  const CHARSET_INFO * const cs= session->variables.collation_connection;
 
1763
  const CHARSET_INFO * const cs= thd->variables.collation_connection;
1767
1764
  uint32_t repertoire= arg1->collation.repertoire;
1768
 
  if (!session->variables.lc_time_names->is_ascii)
 
1765
  if (!thd->variables.lc_time_names->is_ascii)
1769
1766
    repertoire|= MY_REPERTOIRE_EXTENDED;
1770
1767
  collation.set(cs, arg1->collation.derivation, repertoire);
1771
1768
  if (arg1->type() == STRING_ITEM)
1777
1774
  else
1778
1775
  {
1779
1776
    fixed_length=0;
1780
 
    max_length=cmin(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
 
1777
    max_length=min(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
1781
1778
                   collation.collation->mbmaxlen;
1782
1779
    set_if_smaller(max_length,MAX_BLOB_WIDTH);
1783
1780
  }
1810
1807
 
1811
1808
 
1812
1809
 
1813
 
uint32_t Item_func_date_format::format_length(const String *format)
 
1810
uint Item_func_date_format::format_length(const String *format)
1814
1811
{
1815
 
  uint32_t size=0;
 
1812
  uint size=0;
1816
1813
  const char *ptr=format->ptr();
1817
1814
  const char *end=ptr+format->length();
1818
1815
 
1887
1884
{
1888
1885
  String *format;
1889
1886
  DRIZZLE_TIME l_time;
1890
 
  uint32_t size;
 
1887
  uint size;
1891
1888
  assert(fixed == 1);
1892
1889
 
1893
1890
  if (!is_time_format)
1942
1939
 
1943
1940
void Item_func_from_unixtime::fix_length_and_dec()
1944
1941
1945
 
  session= current_session;
 
1942
  thd= current_thd;
1946
1943
  collation.set(&my_charset_bin);
1947
1944
  decimals= DATETIME_DEC;
1948
1945
  max_length=MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
1949
1946
  maybe_null= 1;
1950
 
  session->time_zone_used= 1;
 
1947
  thd->time_zone_used= 1;
1951
1948
}
1952
1949
 
1953
1950
 
1985
1982
}
1986
1983
 
1987
1984
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime,
1988
 
                                       uint32_t fuzzy_date __attribute__((unused)))
 
1985
                                       uint fuzzy_date __attribute__((unused)))
1989
1986
{
1990
1987
  uint64_t tmp= (uint64_t)(args[0]->val_int());
1991
1988
  /*
1995
1992
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
1996
1993
    return 1;
1997
1994
 
1998
 
  session->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)tmp);
 
1995
  thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)tmp);
1999
1996
 
2000
1997
  return 0;
2001
1998
}
2038
2035
 
2039
2036
/* Here arg[1] is a Item_interval object */
2040
2037
 
2041
 
bool Item_date_add_interval::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
2038
bool Item_date_add_interval::get_date(DRIZZLE_TIME *ltime, uint fuzzy_date __attribute__((unused)))
2042
2039
{
2043
2040
  INTERVAL interval;
2044
2041
 
2173
2170
{
2174
2171
  assert(fixed == 1);
2175
2172
  DRIZZLE_TIME ltime;
2176
 
  uint32_t year;
 
2173
  uint year;
2177
2174
  ulong week_format;
2178
2175
  long neg;
2179
2176
  if (date_value)
2200
2197
  case INTERVAL_MONTH:          return ltime.month;
2201
2198
  case INTERVAL_WEEK:
2202
2199
  {
2203
 
    week_format= current_session->variables.default_week_format;
 
2200
    week_format= current_thd->variables.default_week_format;
2204
2201
    return calc_week(&ltime, week_mode(week_format), &year);
2205
2202
  }
2206
2203
  case INTERVAL_DAY:            return ltime.day;
2325
2322
  else
2326
2323
  {
2327
2324
    // Convert character set if differ
2328
 
    uint32_t dummy_errors;
 
2325
    uint dummy_errors;
2329
2326
    if (!(res= args[0]->val_str(&tmp_value)) ||
2330
2327
        str->copy(res->ptr(), res->length(), from_cs,
2331
2328
        cast_cs, &dummy_errors))
2357
2354
        str_value= *res;                        // Not malloced string
2358
2355
        res= &str_value;
2359
2356
      }
2360
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2357
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2361
2358
                          ER_TRUNCATED_WRONG_VALUE,
2362
2359
                          ER(ER_TRUNCATED_WRONG_VALUE), char_type,
2363
2360
                          res->c_ptr_safe());
2489
2486
}
2490
2487
 
2491
2488
 
2492
 
bool Item_date_typecast::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
 
2489
bool Item_date_typecast::get_date(DRIZZLE_TIME *ltime, uint fuzzy_date __attribute__((unused)))
2493
2490
{
2494
2491
  bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
2495
2492
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
2821
2818
 
2822
2819
  /* Check for integer overflows */
2823
2820
  if (hour < 0)
2824
 
    ltime.neg= 1;
2825
 
 
 
2821
  {
 
2822
    if (args[0]->unsigned_flag)
 
2823
      overflow= 1;
 
2824
    else
 
2825
      ltime.neg= 1;
 
2826
  }
2826
2827
  if (-hour > UINT_MAX || hour > UINT_MAX)
2827
2828
    overflow= 1;
2828
2829
 
2841
2842
    char *ptr= int64_t10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
2842
2843
    int len = (int)(ptr - buf) +
2843
2844
      sprintf(ptr, ":%02u:%02u", (uint)minute, (uint)second);
2844
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2845
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2845
2846
                                 buf, len, DRIZZLE_TIMESTAMP_TIME,
2846
 
                                 NULL);
 
2847
                                 NullS);
2847
2848
  }
2848
2849
  
2849
2850
  if (make_time_with_warn((DATE_TIME_FORMAT *) 0, &ltime, str))
2894
2895
      int_type == INTERVAL_QUARTER ||
2895
2896
      int_type == INTERVAL_MONTH)
2896
2897
  {
2897
 
    uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
2898
 
    uint32_t years= 0;
2899
 
    uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
 
2898
    uint year_beg, year_end, month_beg, month_end, day_beg, day_end;
 
2899
    uint years= 0;
 
2900
    uint second_beg, second_end, microsecond_beg, microsecond_end;
2900
2901
 
2901
2902
    if (neg == -1)
2902
2903
    {
3015
3016
    break;
3016
3017
  }
3017
3018
 
3018
 
  for (uint32_t i=0 ; i < 2 ; i++)
 
3019
  for (uint i=0 ; i < 2 ; i++)
3019
3020
  {
3020
3021
    str->append(',');
3021
3022
    args[i]->print(str, query_type);
3040
3041
       (format_name= format->format_name);
3041
3042
       format++)
3042
3043
  {
3043
 
    uint32_t format_name_len;
 
3044
    uint format_name_len;
3044
3045
    format_name_len= strlen(format_name);
3045
3046
    if (val_len == format_name_len &&
3046
3047
        !my_strnncoll(&my_charset_utf8_general_ci, 
3047
 
                      (const unsigned char *) val->ptr(), val_len, 
3048
 
                      (const unsigned char *) format_name, val_len))
 
3048
                      (const uchar *) val->ptr(), val_len, 
 
3049
                      (const uchar *) format_name, val_len))
3049
3050
    {
3050
3051
      const char *format_str= get_date_time_format_str(format, type);
3051
3052
      str->set(format_str, strlen(format_str), &my_charset_bin);
3106
3107
*/
3107
3108
 
3108
3109
static date_time_format_types
3109
 
get_date_time_result_type(const char *format, uint32_t length)
 
3110
get_date_time_result_type(const char *format, uint length)
3110
3111
{
3111
3112
  const char *time_part_frms= "HISThiklrs";
3112
3113
  const char *date_part_frms= "MVUXYWabcjmvuxyw";
3188
3189
}
3189
3190
 
3190
3191
 
3191
 
bool Item_func_str_to_date::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
 
3192
bool Item_func_str_to_date::get_date(DRIZZLE_TIME *ltime, uint fuzzy_date)
3192
3193
{
3193
3194
  DATE_TIME_FORMAT date_time_format;
3194
3195
  char val_buff[64], format_buff[64];
3242
3243
}
3243
3244
 
3244
3245
 
3245
 
bool Item_func_last_day::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
 
3246
bool Item_func_last_day::get_date(DRIZZLE_TIME *ltime, uint fuzzy_date)
3246
3247
{
3247
3248
  if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE) ||
3248
3249
      (ltime->month == 0))
3251
3252
    return 1;
3252
3253
  }
3253
3254
  null_value= 0;
3254
 
  uint32_t month_idx= ltime->month-1;
 
3255
  uint month_idx= ltime->month-1;
3255
3256
  ltime->day= days_in_month[month_idx];
3256
3257
  if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
3257
3258
    ltime->day= 29;