~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time.cc

  • Committer: Monty Taylor
  • Date: 2008-07-31 18:25:59 UTC
  • mto: (202.3.5 gettextize)
  • mto: This revision was merged to the branch mainline in revision 243.
  • Revision ID: monty@inaugust.com-20080731182559-txjt9f3dfhusxfp8
Renamed MYSQL_TIME to DRIZZLE_TIME.

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
        next week is week 1.
95
95
*/
96
96
 
97
 
uint calc_week(MYSQL_TIME *l_time, uint week_behaviour, uint *year)
 
97
uint calc_week(DRIZZLE_TIME *l_time, uint week_behaviour, uint *year)
98
98
{
99
99
  uint days;
100
100
  ulong daynr=calc_daynr(l_time->year,l_time->month,l_time->day);
208
208
 
209
209
 
210
210
/*
211
 
  Convert a timestamp string to a MYSQL_TIME value and produce a warning 
 
211
  Convert a timestamp string to a DRIZZLE_TIME value and produce a warning 
212
212
  if string was truncated during conversion.
213
213
 
214
214
  NOTE
216
216
*/
217
217
 
218
218
timestamp_type
219
 
str_to_datetime_with_warn(const char *str, uint length, MYSQL_TIME *l_time,
 
219
str_to_datetime_with_warn(const char *str, uint length, DRIZZLE_TIME *l_time,
220
220
                          uint flags)
221
221
{
222
222
  int was_cut;
228
228
                                     (MODE_INVALID_DATES |
229
229
                                      MODE_NO_ZERO_DATE))),
230
230
                           &was_cut);
231
 
  if (was_cut || ts_type <= MYSQL_TIMESTAMP_ERROR)
 
231
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
232
232
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
233
233
                                 str, length, ts_type,  NullS);
234
234
  return ts_type;
236
236
 
237
237
 
238
238
/*
239
 
  Convert a datetime from broken-down MYSQL_TIME representation to corresponding 
 
239
  Convert a datetime from broken-down DRIZZLE_TIME representation to corresponding 
240
240
  TIMESTAMP value.
241
241
 
242
242
  SYNOPSIS
252
252
     0 - t contains datetime value which is out of TIMESTAMP range.
253
253
     
254
254
*/
255
 
my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, bool *in_dst_time_gap)
 
255
my_time_t TIME_to_timestamp(THD *thd, const DRIZZLE_TIME *t, bool *in_dst_time_gap)
256
256
{
257
257
  my_time_t timestamp;
258
258
 
271
271
 
272
272
 
273
273
/*
274
 
  Convert a time string to a MYSQL_TIME struct and produce a warning
 
274
  Convert a time string to a DRIZZLE_TIME struct and produce a warning
275
275
  if string was cut during conversion.
276
276
 
277
277
  NOTE
278
278
    See str_to_time() for more info.
279
279
*/
280
280
bool
281
 
str_to_time_with_warn(const char *str, uint length, MYSQL_TIME *l_time)
 
281
str_to_time_with_warn(const char *str, uint length, DRIZZLE_TIME *l_time)
282
282
{
283
283
  int warning;
284
284
  bool ret_val= str_to_time(str, length, l_time, &warning);
285
285
  if (ret_val || warning)
286
286
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
287
 
                                 str, length, MYSQL_TIMESTAMP_TIME, NullS);
 
287
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NullS);
288
288
  return ret_val;
289
289
}
290
290
 
293
293
  Convert a system time structure to TIME
294
294
*/
295
295
 
296
 
void localtime_to_TIME(MYSQL_TIME *to, struct tm *from)
 
296
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
297
297
{
298
298
  to->neg=0;
299
299
  to->second_part=0;
305
305
  to->second=   (int) from->tm_sec;
306
306
}
307
307
 
308
 
void calc_time_from_sec(MYSQL_TIME *to, long seconds, long microseconds)
 
308
void calc_time_from_sec(DRIZZLE_TIME *to, long seconds, long microseconds)
309
309
{
310
310
  long t_seconds;
311
311
  // to->neg is not cleared, it may already be set to a useful value
312
 
  to->time_type= MYSQL_TIMESTAMP_TIME;
 
312
  to->time_type= DRIZZLE_TIMESTAMP_TIME;
313
313
  to->year= 0;
314
314
  to->month= 0;
315
315
  to->day= 0;
465
465
    The last test is to ensure that %p is used if and only if
466
466
    it's needed.
467
467
  */
468
 
  if ((format_type == MYSQL_TIMESTAMP_DATETIME &&
 
468
  if ((format_type == DRIZZLE_TIMESTAMP_DATETIME &&
469
469
       !test_all_bits(part_map, (1 | 2 | 4 | 8 | 16 | 32))) ||
470
 
      (format_type == MYSQL_TIMESTAMP_DATE && part_map != (1 | 2 | 4)) ||
471
 
      (format_type == MYSQL_TIMESTAMP_TIME &&
 
470
      (format_type == DRIZZLE_TIMESTAMP_DATE && part_map != (1 | 2 | 4)) ||
 
471
      (format_type == DRIZZLE_TIMESTAMP_TIME &&
472
472
       !test_all_bits(part_map, 8 | 16 | 32)) ||
473
473
      !allow_separator ||                       // %option should be last
474
474
      (need_p && dt_pos[6] +1 != dt_pos[7]) ||
511
511
 
512
512
  format_str= 0;
513
513
  switch (format_type) {
514
 
  case MYSQL_TIMESTAMP_DATE:
 
514
  case DRIZZLE_TIMESTAMP_DATE:
515
515
    format_str= known_date_time_formats[INTERNAL_FORMAT].date_format;
516
516
    /* fall through */
517
 
  case MYSQL_TIMESTAMP_TIME:
 
517
  case DRIZZLE_TIMESTAMP_TIME:
518
518
    if (!format_str)
519
519
      format_str=known_date_time_formats[INTERNAL_FORMAT].time_format;
520
520
 
529
529
      return 0;
530
530
    if (separator_map == (1 | 2))
531
531
    {
532
 
      if (format_type == MYSQL_TIMESTAMP_TIME)
 
532
      if (format_type == DRIZZLE_TIMESTAMP_TIME)
533
533
      {
534
534
        if (*(format+2) != *(format+5))
535
535
          break;                                // Error
539
539
      return 0;
540
540
    }
541
541
    break;
542
 
  case MYSQL_TIMESTAMP_DATETIME:
 
542
  case DRIZZLE_TIMESTAMP_DATETIME:
543
543
    /*
544
544
      If there is no separators, allow the internal format as we can read
545
545
      this.  If separators are used, they must be between each part.
658
658
                                     timestamp_type type)
659
659
{
660
660
  switch (type) {
661
 
  case MYSQL_TIMESTAMP_DATE:
 
661
  case DRIZZLE_TIMESTAMP_DATE:
662
662
    return format->date_format;
663
 
  case MYSQL_TIMESTAMP_DATETIME:
 
663
  case DRIZZLE_TIMESTAMP_DATETIME:
664
664
    return format->datetime_format;
665
 
  case MYSQL_TIMESTAMP_TIME:
 
665
  case DRIZZLE_TIMESTAMP_TIME:
666
666
    return format->time_format;
667
667
  default:
668
668
    assert(0);                          // Impossible
678
678
    MySQL doesn't support comparing of date/time/datetime strings that
679
679
    are not in arbutary order as dates are compared as strings in some
680
680
    context)
681
 
    This functions don't check that given MYSQL_TIME structure members are
 
681
    This functions don't check that given DRIZZLE_TIME structure members are
682
682
    in valid range. If they are not, return value won't reflect any 
683
683
    valid date either. Additionally, make_time doesn't take into
684
684
    account time->day member: it's assumed that days have been converted
686
686
****************************************************************************/
687
687
 
688
688
void make_time(const DATE_TIME_FORMAT *format __attribute__((unused)),
689
 
               const MYSQL_TIME *l_time, String *str)
 
689
               const DRIZZLE_TIME *l_time, String *str)
690
690
{
691
691
  uint length= (uint) my_time_to_str(l_time, (char*) str->ptr());
692
692
  str->length(length);
695
695
 
696
696
 
697
697
void make_date(const DATE_TIME_FORMAT *format __attribute__((unused)),
698
 
               const MYSQL_TIME *l_time, String *str)
 
698
               const DRIZZLE_TIME *l_time, String *str)
699
699
{
700
700
  uint length= (uint) my_date_to_str(l_time, (char*) str->ptr());
701
701
  str->length(length);
704
704
 
705
705
 
706
706
void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)),
707
 
                   const MYSQL_TIME *l_time, String *str)
 
707
                   const DRIZZLE_TIME *l_time, String *str)
708
708
{
709
709
  uint length= (uint) my_datetime_to_str(l_time, (char*) str->ptr());
710
710
  str->length(length);
726
726
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
727
727
 
728
728
  switch (time_type) {
729
 
    case MYSQL_TIMESTAMP_DATE: 
 
729
    case DRIZZLE_TIMESTAMP_DATE: 
730
730
      type_str= "date";
731
731
      break;
732
 
    case MYSQL_TIMESTAMP_TIME:
 
732
    case DRIZZLE_TIMESTAMP_TIME:
733
733
      type_str= "time";
734
734
      break;
735
 
    case MYSQL_TIMESTAMP_DATETIME:  // FALLTHROUGH
 
735
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
736
736
    default:
737
737
      type_str= "datetime";
738
738
      break;
744
744
                       (ulong) thd->row_count);
745
745
  else
746
746
  {
747
 
    if (time_type > MYSQL_TIMESTAMP_ERROR)
 
747
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
748
748
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
749
749
                         ER(ER_TRUNCATED_WRONG_VALUE),
750
750
                         type_str, str.c_ptr());
759
759
/* Daynumber from year 0 to 9999-12-31 */
760
760
#define MAX_DAY_NUMBER 3652424L
761
761
 
762
 
bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type, INTERVAL interval)
 
762
bool date_add_interval(DRIZZLE_TIME *ltime, interval_type int_type, INTERVAL interval)
763
763
{
764
764
  long period, sign;
765
765
 
784
784
  case INTERVAL_DAY_HOUR:
785
785
  {
786
786
    int64_t sec, days, daynr, microseconds, extra_sec;
787
 
    ltime->time_type= MYSQL_TIMESTAMP_DATETIME; // Return full date
 
787
    ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME; // Return full date
788
788
    microseconds= ltime->second_part + sign*interval.second_part;
789
789
    extra_sec= microseconds/1000000L;
790
790
    microseconds= microseconds%1000000L;
885
885
  NOTE
886
886
    This function calculates difference between l_time1 and l_time2 absolute
887
887
    values. So one should set l_sign and correct result if he want to take
888
 
    signs into account (i.e. for MYSQL_TIME values).
 
888
    signs into account (i.e. for DRIZZLE_TIME values).
889
889
 
890
890
  RETURN VALUES
891
891
    Returns sign of difference.
895
895
*/
896
896
 
897
897
bool
898
 
calc_time_diff(MYSQL_TIME *l_time1, MYSQL_TIME *l_time2, int l_sign, int64_t *seconds_out,
 
898
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
899
899
               long *microseconds_out)
900
900
{
901
901
  long days;
903
903
  int64_t microseconds;
904
904
 
905
905
  /*
906
 
    We suppose that if first argument is MYSQL_TIMESTAMP_TIME
 
906
    We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
907
907
    the second argument should be TIMESTAMP_TIME also.
908
908
    We should check it before calc_time_diff call.
909
909
  */
910
 
  if (l_time1->time_type == MYSQL_TIMESTAMP_TIME)  // Time value
 
910
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
911
911
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
912
912
  else
913
913
  {
914
914
    days= calc_daynr((uint) l_time1->year,
915
915
                     (uint) l_time1->month,
916
916
                     (uint) l_time1->day);
917
 
    if (l_time2->time_type == MYSQL_TIMESTAMP_TIME)
 
917
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
918
918
      days-= l_sign * (long)l_time2->day;
919
919
    else
920
920
      days-= l_sign*calc_daynr((uint) l_time2->year,
945
945
 
946
946
 
947
947
/*
948
 
  Compares 2 MYSQL_TIME structures
 
948
  Compares 2 DRIZZLE_TIME structures
949
949
 
950
950
  SYNOPSIS
951
951
    my_time_compare()
963
963
*/
964
964
 
965
965
int
966
 
my_time_compare(MYSQL_TIME *a, MYSQL_TIME *b)
 
966
my_time_compare(DRIZZLE_TIME *a, DRIZZLE_TIME *b)
967
967
{
968
968
  uint64_t a_t= TIME_to_uint64_t_datetime(a);
969
969
  uint64_t b_t= TIME_to_uint64_t_datetime(b);