~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/time.cc

  • Committer: Brian Aker
  • Date: 2008-07-01 23:01:40 UTC
  • mfrom: (28.1.27 libtool-patch)
  • Revision ID: brian@tangent.org-20080701230140-8bs081cfx1mxugp4
More Cleanup around dead files/GNU Makefile warnings (just a merge from me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* Functions to handle date and time */
18
18
 
19
 
#include <drizzled/server_includes.h>
20
 
#include <drizzled/drizzled_error_messages.h>
21
 
#include <drizzled/util/test.h>
 
19
#include "mysql_priv.h"
 
20
#include <m_ctype.h>
22
21
 
23
22
 
24
23
        /* Some functions to calculate dates */
64
63
 
65
64
int calc_weekday(long daynr,bool sunday_first_day_of_week)
66
65
{
67
 
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
 
66
  DBUG_ENTER("calc_weekday");
 
67
  DBUG_RETURN ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
68
68
}
69
69
 
70
70
/*
96
96
        next week is week 1.
97
97
*/
98
98
 
99
 
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
 
99
uint calc_week(MYSQL_TIME *l_time, uint week_behaviour, uint *year)
100
100
{
101
 
  uint32_t days;
 
101
  uint days;
102
102
  ulong daynr=calc_daynr(l_time->year,l_time->month,l_time->day);
103
103
  ulong first_daynr=calc_daynr(l_time->year,1,1);
104
104
  bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST);
105
105
  bool week_year= test(week_behaviour & WEEK_YEAR);
106
106
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
107
107
 
108
 
  uint32_t weekday=calc_weekday(first_daynr, !monday_first);
 
108
  uint weekday=calc_weekday(first_daynr, !monday_first);
109
109
  *year=l_time->year;
110
110
 
111
111
  if (l_time->month == 1 && l_time->day <= 7-weekday)
139
139
        /* Change a daynr to year, month and day */
140
140
        /* Daynr 0 is returned as date 00.00.00 */
141
141
 
142
 
void get_date_from_daynr(long daynr,uint32_t *ret_year,uint32_t *ret_month,
143
 
                         uint32_t *ret_day)
 
142
void get_date_from_daynr(long daynr,uint *ret_year,uint *ret_month,
 
143
                         uint *ret_day)
144
144
{
145
 
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
146
 
  unsigned char *month_pos;
 
145
  uint year,temp,leap_day,day_of_year,days_in_year;
 
146
  uchar *month_pos;
 
147
  DBUG_ENTER("get_date_from_daynr");
147
148
 
148
149
  if (daynr <= 365L || daynr >= 3652500)
149
150
  {                                             /* Fix if wrong daynr */
177
178
    *ret_year=year;
178
179
    *ret_day=day_of_year+leap_day;
179
180
  }
180
 
  return;
 
181
  DBUG_VOID_RETURN;
181
182
}
182
183
 
183
184
        /* Functions to handle periods */
210
211
 
211
212
 
212
213
/*
213
 
  Convert a timestamp string to a DRIZZLE_TIME value and produce a warning 
 
214
  Convert a timestamp string to a MYSQL_TIME value and produce a warning 
214
215
  if string was truncated during conversion.
215
216
 
216
217
  NOTE
217
218
    See description of str_to_datetime() for more information.
218
219
*/
219
220
 
220
 
enum enum_drizzle_timestamp_type
221
 
str_to_datetime_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time,
222
 
                          uint32_t flags)
 
221
timestamp_type
 
222
str_to_datetime_with_warn(const char *str, uint length, MYSQL_TIME *l_time,
 
223
                          uint flags)
223
224
{
224
225
  int was_cut;
225
226
  THD *thd= current_thd;
226
 
  enum enum_drizzle_timestamp_type ts_type;
 
227
  timestamp_type ts_type;
227
228
  
228
229
  ts_type= str_to_datetime(str, length, l_time,
229
230
                           (flags | (thd->variables.sql_mode &
230
231
                                     (MODE_INVALID_DATES |
231
232
                                      MODE_NO_ZERO_DATE))),
232
233
                           &was_cut);
233
 
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
234
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
235
 
                                 str, length, ts_type,  NULL);
 
234
  if (was_cut || ts_type <= MYSQL_TIMESTAMP_ERROR)
 
235
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
236
                                 str, length, ts_type,  NullS);
236
237
  return ts_type;
237
238
}
238
239
 
239
240
 
240
241
/*
241
 
  Convert a datetime from broken-down DRIZZLE_TIME representation to corresponding 
 
242
  Convert a datetime from broken-down MYSQL_TIME representation to corresponding 
242
243
  TIMESTAMP value.
243
244
 
244
245
  SYNOPSIS
254
255
     0 - t contains datetime value which is out of TIMESTAMP range.
255
256
     
256
257
*/
257
 
my_time_t TIME_to_timestamp(THD *thd, const DRIZZLE_TIME *t, bool *in_dst_time_gap)
 
258
my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, my_bool *in_dst_time_gap)
258
259
{
259
260
  my_time_t timestamp;
260
261
 
273
274
 
274
275
 
275
276
/*
276
 
  Convert a time string to a DRIZZLE_TIME struct and produce a warning
 
277
  Convert a time string to a MYSQL_TIME struct and produce a warning
277
278
  if string was cut during conversion.
278
279
 
279
280
  NOTE
280
281
    See str_to_time() for more info.
281
282
*/
282
283
bool
283
 
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
 
284
str_to_time_with_warn(const char *str, uint length, MYSQL_TIME *l_time)
284
285
{
285
286
  int warning;
286
287
  bool ret_val= str_to_time(str, length, l_time, &warning);
287
288
  if (ret_val || warning)
288
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
289
 
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
 
289
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
290
                                 str, length, MYSQL_TIMESTAMP_TIME, NullS);
290
291
  return ret_val;
291
292
}
292
293
 
295
296
  Convert a system time structure to TIME
296
297
*/
297
298
 
298
 
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
 
299
void localtime_to_TIME(MYSQL_TIME *to, struct tm *from)
299
300
{
300
301
  to->neg=0;
301
302
  to->second_part=0;
307
308
  to->second=   (int) from->tm_sec;
308
309
}
309
310
 
310
 
void calc_time_from_sec(DRIZZLE_TIME *to, long seconds, long microseconds)
 
311
void calc_time_from_sec(MYSQL_TIME *to, long seconds, long microseconds)
311
312
{
312
313
  long t_seconds;
313
314
  // to->neg is not cleared, it may already be set to a useful value
314
 
  to->time_type= DRIZZLE_TIMESTAMP_TIME;
 
315
  to->time_type= MYSQL_TIMESTAMP_TIME;
315
316
  to->year= 0;
316
317
  to->month= 0;
317
318
  to->day= 0;
349
350
    1   error
350
351
*/
351
352
 
352
 
bool parse_date_time_format(enum enum_drizzle_timestamp_type format_type, 
353
 
                            const char *format, uint32_t format_length,
 
353
bool parse_date_time_format(timestamp_type format_type, 
 
354
                            const char *format, uint format_length,
354
355
                            DATE_TIME_FORMAT *date_time_format)
355
356
{
356
 
  uint32_t offset= 0, separators= 0;
 
357
  uint offset= 0, separators= 0;
357
358
  const char *ptr= format, *format_str;
358
359
  const char *end= ptr+format_length;
359
 
  unsigned char *dt_pos= date_time_format->positions;
 
360
  uchar *dt_pos= date_time_format->positions;
360
361
  /* need_p is set if we are using AM/PM format */
361
362
  bool need_p= 0, allow_separator= 0;
362
 
  uint32_t part_map= 0, separator_map= 0;
 
363
  ulong part_map= 0, separator_map= 0;
363
364
  const char *parts[16];
364
365
 
365
366
  date_time_format->time_separator= 0;
376
377
  {
377
378
    if (*ptr == '%' && ptr+1 != end)
378
379
    {
379
 
      uint32_t position;
 
380
      uint position;
380
381
      switch (*++ptr) {
381
382
      case 'y':                                 // Year
382
383
      case 'Y':
429
430
      */
430
431
      if (part_map && position <= 2 && !(part_map & (1 | 2 | 4)))
431
432
        offset=5;
432
 
      part_map|= UINT32_C(1) << position;
 
433
      part_map|= (ulong) 1 << position;
433
434
      dt_pos[position]= offset++;
434
435
      allow_separator= 1;
435
436
    }
444
445
      allow_separator= 0;                       // Don't allow two separators
445
446
      separators++;
446
447
      /* Store in separator_map which parts are punct characters */
447
 
      if (my_ispunct(&my_charset_utf8_general_ci, *ptr))
 
448
      if (my_ispunct(&my_charset_latin1, *ptr))
448
449
        separator_map|= (ulong) 1 << (offset-1);
449
 
      else if (!my_isspace(&my_charset_utf8_general_ci, *ptr))
 
450
      else if (!my_isspace(&my_charset_latin1, *ptr))
450
451
        return 1;
451
452
    }
452
453
  }
467
468
    The last test is to ensure that %p is used if and only if
468
469
    it's needed.
469
470
  */
470
 
  if ((format_type == DRIZZLE_TIMESTAMP_DATETIME &&
471
 
       !test_all_bits(part_map, (uint32_t) (1 | 2 | 4 | 8 | 16 | 32))) ||
472
 
      (format_type == DRIZZLE_TIMESTAMP_DATE && part_map != (1 | 2 | 4)) ||
473
 
      (format_type == DRIZZLE_TIMESTAMP_TIME &&
474
 
       !test_all_bits(part_map, (uint32_t) (8 | 16 | 32))) ||
 
471
  if ((format_type == MYSQL_TIMESTAMP_DATETIME &&
 
472
       !test_all_bits(part_map, (1 | 2 | 4 | 8 | 16 | 32))) ||
 
473
      (format_type == MYSQL_TIMESTAMP_DATE && part_map != (1 | 2 | 4)) ||
 
474
      (format_type == MYSQL_TIMESTAMP_TIME &&
 
475
       !test_all_bits(part_map, 8 | 16 | 32)) ||
475
476
      !allow_separator ||                       // %option should be last
476
477
      (need_p && dt_pos[6] +1 != dt_pos[7]) ||
477
478
      (need_p ^ (dt_pos[7] != 255)))
480
481
  if (dt_pos[6] != 255)                         // If fractional seconds
481
482
  {
482
483
    /* remove fractional seconds from later tests */
483
 
    uint32_t pos= dt_pos[6] -1;
 
484
    uint pos= dt_pos[6] -1;
484
485
    /* Remove separator before %f from sep map */
485
486
    separator_map= ((separator_map & ((ulong) (1 << pos)-1)) |
486
487
                    ((separator_map & ~((ulong) (1 << pos)-1)) >> 1));
513
514
 
514
515
  format_str= 0;
515
516
  switch (format_type) {
516
 
  case DRIZZLE_TIMESTAMP_DATE:
 
517
  case MYSQL_TIMESTAMP_DATE:
517
518
    format_str= known_date_time_formats[INTERNAL_FORMAT].date_format;
518
519
    /* fall through */
519
 
  case DRIZZLE_TIMESTAMP_TIME:
 
520
  case MYSQL_TIMESTAMP_TIME:
520
521
    if (!format_str)
521
522
      format_str=known_date_time_formats[INTERNAL_FORMAT].time_format;
522
523
 
526
527
    */
527
528
    if (format_length == 6 && !need_p &&
528
529
        !my_strnncoll(&my_charset_bin,
529
 
                      (const unsigned char *) format, 6, 
530
 
                      (const unsigned char *) format_str, 6))
 
530
                      (const uchar *) format, 6, 
 
531
                      (const uchar *) format_str, 6))
531
532
      return 0;
532
533
    if (separator_map == (1 | 2))
533
534
    {
534
 
      if (format_type == DRIZZLE_TIMESTAMP_TIME)
 
535
      if (format_type == MYSQL_TIMESTAMP_TIME)
535
536
      {
536
537
        if (*(format+2) != *(format+5))
537
538
          break;                                // Error
541
542
      return 0;
542
543
    }
543
544
    break;
544
 
  case DRIZZLE_TIMESTAMP_DATETIME:
 
545
  case MYSQL_TIMESTAMP_DATETIME:
545
546
    /*
546
547
      If there is no separators, allow the internal format as we can read
547
548
      this.  If separators are used, they must be between each part.
549
550
    */
550
551
    if ((format_length == 12 && !need_p &&
551
552
         !my_strnncoll(&my_charset_bin, 
552
 
                       (const unsigned char *) format, 12,
553
 
                       (const unsigned char*) known_date_time_formats[INTERNAL_FORMAT].datetime_format,
 
553
                       (const uchar *) format, 12,
 
554
                       (const uchar*) known_date_time_formats[INTERNAL_FORMAT].datetime_format,
554
555
                       12)) ||
555
556
        (separators == 5 && separator_map == (1 | 2 | 8 | 16)))
556
557
      return 0;
557
558
    break;
558
559
  default:
559
 
    assert(1);
 
560
    DBUG_ASSERT(1);
560
561
    break;
561
562
  }
562
563
  return 1;                                     // Error
573
574
    format_length       Length of string
574
575
 
575
576
  NOTES
576
 
    The returned object should be freed with free()
 
577
    The returned object should be freed with my_free()
577
578
 
578
579
  RETURN
579
580
    NULL ponter:        Error
581
582
*/
582
583
 
583
584
DATE_TIME_FORMAT
584
 
*date_time_format_make(enum enum_drizzle_timestamp_type format_type,
585
 
                       const char *format_str, uint32_t format_length)
 
585
*date_time_format_make(timestamp_type format_type,
 
586
                       const char *format_str, uint format_length)
586
587
{
587
588
  DATE_TIME_FORMAT tmp;
588
589
 
607
608
    format              format to copy
608
609
 
609
610
  NOTES
610
 
    The returned object should be freed with free()
 
611
    The returned object should be freed with my_free()
611
612
 
612
613
  RETURN
613
614
    NULL ponter:        Error
627
628
  {
628
629
    /* Put format string after current pos */
629
630
    new_format->format.str= (char*) (new_format+1);
630
 
    memcpy(new_format->positions, format->positions,
 
631
    memcpy((char*) new_format->positions, (char*) format->positions,
631
632
           sizeof(format->positions));
632
633
    new_format->time_separator= format->time_separator;
633
634
    /* We make the string null terminated for easy printf in SHOW VARIABLES */
634
 
    memcpy(new_format->format.str, format->format.str,
 
635
    memcpy((char*) new_format->format.str, format->format.str,
635
636
           format->format.length);
636
637
    new_format->format.str[format->format.length]= 0;
637
638
    new_format->format.length= format->format.length;
657
658
*/
658
659
 
659
660
const char *get_date_time_format_str(KNOWN_DATE_TIME_FORMAT *format,
660
 
                                     enum enum_drizzle_timestamp_type type)
 
661
                                     timestamp_type type)
661
662
{
662
663
  switch (type) {
663
 
  case DRIZZLE_TIMESTAMP_DATE:
 
664
  case MYSQL_TIMESTAMP_DATE:
664
665
    return format->date_format;
665
 
  case DRIZZLE_TIMESTAMP_DATETIME:
 
666
  case MYSQL_TIMESTAMP_DATETIME:
666
667
    return format->datetime_format;
667
 
  case DRIZZLE_TIMESTAMP_TIME:
 
668
  case MYSQL_TIMESTAMP_TIME:
668
669
    return format->time_format;
669
670
  default:
670
 
    assert(0);                          // Impossible
 
671
    DBUG_ASSERT(0);                             // Impossible
671
672
    return 0;
672
673
  }
673
674
}
680
681
    MySQL doesn't support comparing of date/time/datetime strings that
681
682
    are not in arbutary order as dates are compared as strings in some
682
683
    context)
683
 
    This functions don't check that given DRIZZLE_TIME structure members are
 
684
    This functions don't check that given MYSQL_TIME structure members are
684
685
    in valid range. If they are not, return value won't reflect any 
685
686
    valid date either. Additionally, make_time doesn't take into
686
687
    account time->day member: it's assumed that days have been converted
688
689
****************************************************************************/
689
690
 
690
691
void make_time(const DATE_TIME_FORMAT *format __attribute__((unused)),
691
 
               const DRIZZLE_TIME *l_time, String *str)
 
692
               const MYSQL_TIME *l_time, String *str)
692
693
{
693
 
  uint32_t length= (uint) my_time_to_str(l_time, (char*) str->ptr());
 
694
  uint length= (uint) my_time_to_str(l_time, (char*) str->ptr());
694
695
  str->length(length);
695
696
  str->set_charset(&my_charset_bin);
696
697
}
697
698
 
698
699
 
699
700
void make_date(const DATE_TIME_FORMAT *format __attribute__((unused)),
700
 
               const DRIZZLE_TIME *l_time, String *str)
 
701
               const MYSQL_TIME *l_time, String *str)
701
702
{
702
 
  uint32_t length= (uint) my_date_to_str(l_time, (char*) str->ptr());
 
703
  uint length= (uint) my_date_to_str(l_time, (char*) str->ptr());
703
704
  str->length(length);
704
705
  str->set_charset(&my_charset_bin);
705
706
}
706
707
 
707
708
 
708
709
void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)),
709
 
                   const DRIZZLE_TIME *l_time, String *str)
 
710
                   const MYSQL_TIME *l_time, String *str)
710
711
{
711
 
  uint32_t length= (uint) my_datetime_to_str(l_time, (char*) str->ptr());
 
712
  uint length= (uint) my_datetime_to_str(l_time, (char*) str->ptr());
712
713
  str->length(length);
713
714
  str->set_charset(&my_charset_bin);
714
715
}
715
716
 
716
717
 
717
 
void make_truncated_value_warning(THD *thd, DRIZZLE_ERROR::enum_warning_level level,
 
718
void make_truncated_value_warning(THD *thd, MYSQL_ERROR::enum_warning_level level,
718
719
                                  const char *str_val,
719
 
                                  uint32_t str_length,
720
 
                                  enum enum_drizzle_timestamp_type time_type,
 
720
                                  uint str_length, timestamp_type time_type,
721
721
                                  const char *field_name)
722
722
{
723
 
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
723
  char warn_buff[MYSQL_ERRMSG_SIZE];
724
724
  const char *type_str;
725
 
  CHARSET_INFO *cs= &my_charset_utf8_general_ci;
 
725
  CHARSET_INFO *cs= &my_charset_latin1;
726
726
  char buff[128];
727
 
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
 
727
  String str(buff,(uint32) sizeof(buff), system_charset_info);
728
728
  str.copy(str_val, str_length, system_charset_info);
729
729
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
730
730
 
731
731
  switch (time_type) {
732
 
    case DRIZZLE_TIMESTAMP_DATE: 
 
732
    case MYSQL_TIMESTAMP_DATE: 
733
733
      type_str= "date";
734
734
      break;
735
 
    case DRIZZLE_TIMESTAMP_TIME:
 
735
    case MYSQL_TIMESTAMP_TIME:
736
736
      type_str= "time";
737
737
      break;
738
 
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
 
738
    case MYSQL_TIMESTAMP_DATETIME:  // FALLTHROUGH
739
739
    default:
740
740
      type_str= "datetime";
741
741
      break;
747
747
                       (ulong) thd->row_count);
748
748
  else
749
749
  {
750
 
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
 
750
    if (time_type > MYSQL_TIMESTAMP_ERROR)
751
751
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
752
752
                         ER(ER_TRUNCATED_WRONG_VALUE),
753
753
                         type_str, str.c_ptr());
762
762
/* Daynumber from year 0 to 9999-12-31 */
763
763
#define MAX_DAY_NUMBER 3652424L
764
764
 
765
 
bool date_add_interval(DRIZZLE_TIME *ltime, interval_type int_type, INTERVAL interval)
 
765
bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type, INTERVAL interval)
766
766
{
767
767
  long period, sign;
768
768
 
786
786
  case INTERVAL_DAY_MINUTE:
787
787
  case INTERVAL_DAY_HOUR:
788
788
  {
789
 
    int64_t sec, days, daynr, microseconds, extra_sec;
790
 
    ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME; // Return full date
 
789
    longlong sec, days, daynr, microseconds, extra_sec;
 
790
    ltime->time_type= MYSQL_TIMESTAMP_DATETIME; // Return full date
791
791
    microseconds= ltime->second_part + sign*interval.second_part;
792
792
    extra_sec= microseconds/1000000L;
793
793
    microseconds= microseconds%1000000L;
794
794
 
795
795
    sec=((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
796
796
         ltime->second +
797
 
         sign* (int64_t) (interval.day*3600*24L +
798
 
                           interval.hour*3600L+interval.minute*60L+
 
797
         sign* (longlong) (interval.day*3600*24L +
 
798
                           interval.hour*LL(3600)+interval.minute*LL(60)+
799
799
                           interval.second))+ extra_sec;
800
800
    if (microseconds < 0)
801
801
    {
802
 
      microseconds+= 1000000L;
 
802
      microseconds+= LL(1000000);
803
803
      sec--;
804
804
    }
805
 
    days= sec/(3600*24L);
806
 
    sec-= days*3600*24L;
 
805
    days= sec/(3600*LL(24));
 
806
    sec-= days*3600*LL(24);
807
807
    if (sec < 0)
808
808
    {
809
809
      days--;
810
 
      sec+= 3600*24L;
 
810
      sec+= 3600*LL(24);
811
811
    }
812
812
    ltime->second_part= (uint) microseconds;
813
813
    ltime->second= (uint) (sec % 60);
815
815
    ltime->hour=   (uint) (sec/3600);
816
816
    daynr= calc_daynr(ltime->year,ltime->month,1) + days;
817
817
    /* Day number from year 0 to 9999-12-31 */
818
 
    if ((uint64_t) daynr > MAX_DAY_NUMBER)
 
818
    if ((ulonglong) daynr > MAX_DAY_NUMBER)
819
819
      goto invalid_date;
820
820
    get_date_from_daynr((long) daynr, &ltime->year, &ltime->month,
821
821
                        &ltime->day);
862
862
  return 0;                                     // Ok
863
863
 
864
864
invalid_date:
865
 
  push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
865
  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
866
866
                      ER_DATETIME_FUNCTION_OVERFLOW,
867
867
                      ER(ER_DATETIME_FUNCTION_OVERFLOW),
868
868
                      "datetime");
888
888
  NOTE
889
889
    This function calculates difference between l_time1 and l_time2 absolute
890
890
    values. So one should set l_sign and correct result if he want to take
891
 
    signs into account (i.e. for DRIZZLE_TIME values).
 
891
    signs into account (i.e. for MYSQL_TIME values).
892
892
 
893
893
  RETURN VALUES
894
894
    Returns sign of difference.
898
898
*/
899
899
 
900
900
bool
901
 
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
 
901
calc_time_diff(MYSQL_TIME *l_time1, MYSQL_TIME *l_time2, int l_sign, longlong *seconds_out,
902
902
               long *microseconds_out)
903
903
{
904
904
  long days;
905
905
  bool neg;
906
 
  int64_t microseconds;
 
906
  longlong microseconds;
907
907
 
908
908
  /*
909
 
    We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
 
909
    We suppose that if first argument is MYSQL_TIMESTAMP_TIME
910
910
    the second argument should be TIMESTAMP_TIME also.
911
911
    We should check it before calc_time_diff call.
912
912
  */
913
 
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
 
913
  if (l_time1->time_type == MYSQL_TIMESTAMP_TIME)  // Time value
914
914
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
915
915
  else
916
916
  {
917
917
    days= calc_daynr((uint) l_time1->year,
918
918
                     (uint) l_time1->month,
919
919
                     (uint) l_time1->day);
920
 
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
 
920
    if (l_time2->time_type == MYSQL_TIMESTAMP_TIME)
921
921
      days-= l_sign * (long)l_time2->day;
922
922
    else
923
923
      days-= l_sign*calc_daynr((uint) l_time2->year,
925
925
                               (uint) l_time2->day);
926
926
  }
927
927
 
928
 
  microseconds= ((int64_t)days*86400L +
929
 
                 (int64_t)(l_time1->hour*3600L +
 
928
  microseconds= ((longlong)days*LL(86400) +
 
929
                 (longlong)(l_time1->hour*3600L +
930
930
                            l_time1->minute*60L +
931
931
                            l_time1->second) -
932
 
                 l_sign*(int64_t)(l_time2->hour*3600L +
 
932
                 l_sign*(longlong)(l_time2->hour*3600L +
933
933
                                   l_time2->minute*60L +
934
 
                                   l_time2->second)) * 1000000L +
935
 
                (int64_t)l_time1->second_part -
936
 
                l_sign*(int64_t)l_time2->second_part;
 
934
                                   l_time2->second)) * LL(1000000) +
 
935
                (longlong)l_time1->second_part -
 
936
                l_sign*(longlong)l_time2->second_part;
937
937
 
938
938
  neg= 0;
939
939
  if (microseconds < 0)
948
948
 
949
949
 
950
950
/*
951
 
  Compares 2 DRIZZLE_TIME structures
 
951
  Compares 2 MYSQL_TIME structures
952
952
 
953
953
  SYNOPSIS
954
954
    my_time_compare()
966
966
*/
967
967
 
968
968
int
969
 
my_time_compare(DRIZZLE_TIME *a, DRIZZLE_TIME *b)
 
969
my_time_compare(MYSQL_TIME *a, MYSQL_TIME *b)
970
970
{
971
 
  uint64_t a_t= TIME_to_uint64_t_datetime(a);
972
 
  uint64_t b_t= TIME_to_uint64_t_datetime(b);
 
971
  my_ulonglong a_t= TIME_to_ulonglong_datetime(a);
 
972
  my_ulonglong b_t= TIME_to_ulonglong_datetime(b);
973
973
 
974
974
  if (a_t > b_t)
975
975
    return 1;