~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time.cc

  • Committer: Monty Taylor
  • Date: 2008-10-23 23:53:49 UTC
  • mto: This revision was merged to the branch mainline in revision 557.
  • Revision ID: monty@inaugust.com-20081023235349-317wgwqwgccuacmq
SplitĀ outĀ nested_join.h.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
/* Functions to handle date and time */
18
18
 
19
19
#include <drizzled/server_includes.h>
20
 
#include <drizzled/drizzled_error_messages.h>
 
20
#include <drizzled/error.h>
 
21
#include <drizzled/util/test.h>
21
22
 
22
23
 
23
24
        /* Some functions to calculate dates */
98
99
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
99
100
{
100
101
  uint32_t days;
101
 
  ulong daynr=calc_daynr(l_time->year,l_time->month,l_time->day);
102
 
  ulong first_daynr=calc_daynr(l_time->year,1,1);
 
102
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
 
103
  uint32_t first_daynr= calc_daynr(l_time->year,1,1);
103
104
  bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST);
104
105
  bool week_year= test(week_behaviour & WEEK_YEAR);
105
106
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
181
182
 
182
183
        /* Functions to handle periods */
183
184
 
184
 
ulong convert_period_to_month(ulong period)
 
185
uint32_t convert_period_to_month(uint32_t period)
185
186
{
186
 
  ulong a,b;
 
187
  uint32_t a,b;
187
188
  if (period == 0)
188
189
    return 0L;
189
190
  if ((a=period/100) < YY_PART_YEAR)
195
196
}
196
197
 
197
198
 
198
 
ulong convert_month_to_period(ulong month)
 
199
uint32_t convert_month_to_period(uint32_t month)
199
200
{
200
 
  ulong year;
 
201
  uint32_t year;
201
202
  if (month == 0L)
202
203
    return 0L;
203
204
  if ((year=month/12) < 100)
221
222
                          uint32_t flags)
222
223
{
223
224
  int was_cut;
224
 
  THD *thd= current_thd;
 
225
  Session *session= current_session;
225
226
  enum enum_drizzle_timestamp_type ts_type;
226
227
  
227
228
  ts_type= str_to_datetime(str, length, l_time,
228
 
                           (flags | (thd->variables.sql_mode &
 
229
                           (flags | (session->variables.sql_mode &
229
230
                                     (MODE_INVALID_DATES |
230
231
                                      MODE_NO_ZERO_DATE))),
231
232
                           &was_cut);
232
233
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
233
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
234
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
234
235
                                 str, length, ts_type,  NULL);
235
236
  return ts_type;
236
237
}
242
243
 
243
244
  SYNOPSIS
244
245
    TIME_to_timestamp()
245
 
      thd             - current thread
 
246
      session             - current thread
246
247
      t               - datetime in broken-down representation, 
247
248
      in_dst_time_gap - pointer to bool which is set to true if t represents
248
249
                        value which doesn't exists (falls into the spring 
253
254
     0 - t contains datetime value which is out of TIMESTAMP range.
254
255
     
255
256
*/
256
 
my_time_t TIME_to_timestamp(THD *thd, const DRIZZLE_TIME *t, bool *in_dst_time_gap)
 
257
my_time_t TIME_to_timestamp(Session *session, const DRIZZLE_TIME *t, bool *in_dst_time_gap)
257
258
{
258
259
  my_time_t timestamp;
259
260
 
260
261
  *in_dst_time_gap= 0;
261
 
  thd->time_zone_used= 1;
 
262
  session->time_zone_used= 1;
262
263
 
263
 
  timestamp= thd->variables.time_zone->TIME_to_gmt_sec(t, in_dst_time_gap);
 
264
  timestamp= session->variables.time_zone->TIME_to_gmt_sec(t, in_dst_time_gap);
264
265
  if (timestamp)
265
266
  {
266
267
    return timestamp;
284
285
  int warning;
285
286
  bool ret_val= str_to_time(str, length, l_time, &warning);
286
287
  if (ret_val || warning)
287
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
288
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
288
289
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
289
290
  return ret_val;
290
291
}
444
445
      separators++;
445
446
      /* Store in separator_map which parts are punct characters */
446
447
      if (my_ispunct(&my_charset_utf8_general_ci, *ptr))
447
 
        separator_map|= (ulong) 1 << (offset-1);
 
448
        separator_map|= 1 << (offset-1);
448
449
      else if (!my_isspace(&my_charset_utf8_general_ci, *ptr))
449
450
        return 1;
450
451
    }
481
482
    /* remove fractional seconds from later tests */
482
483
    uint32_t pos= dt_pos[6] -1;
483
484
    /* Remove separator before %f from sep map */
484
 
    separator_map= ((separator_map & ((ulong) (1 << pos)-1)) |
485
 
                    ((separator_map & ~((ulong) (1 << pos)-1)) >> 1));
 
485
    separator_map= ((separator_map & ((1 << pos)-1)) |
 
486
                    ((separator_map & ~((1 << pos)-1)) >> 1));
486
487
    if (part_map & 64)                        
487
488
    {
488
489
      separators--;                             // There is always a separator
507
508
  */
508
509
  offset= dt_pos[6] <= 3 ? 3 : 6;
509
510
  /* Remove separator before %p from sep map */
510
 
  separator_map= ((separator_map & ((ulong) (1 << offset)-1)) |
511
 
                  ((separator_map & ~((ulong) (1 << offset)-1)) >> 1));
 
511
  separator_map= ((separator_map & ((1 << offset)-1)) |
 
512
                  ((separator_map & ~((1 << offset)-1)) >> 1));
512
513
 
513
514
  format_str= 0;
514
515
  switch (format_type) {
591
592
  {
592
593
    tmp.format.str=    (char*) format_str;
593
594
    tmp.format.length= format_length;
594
 
    return date_time_format_copy((THD *)0, &tmp);
 
595
    return date_time_format_copy((Session *)0, &tmp);
595
596
  }
596
597
  return 0;
597
598
}
602
603
 
603
604
  SYNOPSIS
604
605
    date_and_time_format_copy()
605
 
    thd                 Set if variable should be allocated in thread mem
 
606
    session                     Set if variable should be allocated in thread mem
606
607
    format              format to copy
607
608
 
608
609
  NOTES
613
614
    new object
614
615
*/
615
616
 
616
 
DATE_TIME_FORMAT *date_time_format_copy(THD *thd, DATE_TIME_FORMAT *format)
 
617
DATE_TIME_FORMAT *date_time_format_copy(Session *session, DATE_TIME_FORMAT *format)
617
618
{
618
619
  DATE_TIME_FORMAT *new_format;
619
 
  ulong length= sizeof(*format) + format->format.length + 1;
 
620
  uint32_t length= sizeof(*format) + format->format.length + 1;
620
621
 
621
 
  if (thd)
622
 
    new_format= (DATE_TIME_FORMAT *) thd->alloc(length);
 
622
  if (session)
 
623
    new_format= (DATE_TIME_FORMAT *) session->alloc(length);
623
624
  else
624
625
    new_format=  (DATE_TIME_FORMAT *) my_malloc(length, MYF(MY_WME));
625
626
  if (new_format)
713
714
}
714
715
 
715
716
 
716
 
void make_truncated_value_warning(THD *thd, DRIZZLE_ERROR::enum_warning_level level,
 
717
void make_truncated_value_warning(Session *session, DRIZZLE_ERROR::enum_warning_level level,
717
718
                                  const char *str_val,
718
719
                                  uint32_t str_length,
719
720
                                  enum enum_drizzle_timestamp_type time_type,
743
744
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
744
745
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
745
746
                       type_str, str.c_ptr(), field_name,
746
 
                       (ulong) thd->row_count);
 
747
                       (uint32_t) session->row_count);
747
748
  else
748
749
  {
749
750
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
754
755
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
755
756
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
756
757
  }
757
 
  push_warning(thd, level,
 
758
  push_warning(session, level,
758
759
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
759
760
}
760
761
 
825
826
    period= (calc_daynr(ltime->year,ltime->month,ltime->day) +
826
827
             sign * (long) interval.day);
827
828
    /* Daynumber from year 0 to 9999-12-31 */
828
 
    if ((ulong) period > MAX_DAY_NUMBER)
 
829
    if (period > MAX_DAY_NUMBER)
829
830
      goto invalid_date;
830
831
    get_date_from_daynr((long) period,&ltime->year,&ltime->month,&ltime->day);
831
832
    break;
832
833
  case INTERVAL_YEAR:
833
834
    ltime->year+= sign * (long) interval.year;
834
 
    if ((ulong) ltime->year >= 10000L)
 
835
    if (ltime->year >= 10000L)
835
836
      goto invalid_date;
836
837
    if (ltime->month == 2 && ltime->day == 29 &&
837
838
        calc_days_in_year(ltime->year) != 366)
842
843
  case INTERVAL_MONTH:
843
844
    period= (ltime->year*12 + sign * (long) interval.year*12 +
844
845
             ltime->month-1 + sign * (long) interval.month);
845
 
    if ((ulong) period >= 120000L)
 
846
    if (period >= 120000L)
846
847
      goto invalid_date;
847
848
    ltime->year= (uint) (period / 12);
848
849
    ltime->month= (uint) (period % 12L)+1;
861
862
  return 0;                                     // Ok
862
863
 
863
864
invalid_date:
864
 
  push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
865
  push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
865
866
                      ER_DATETIME_FUNCTION_OVERFLOW,
866
867
                      ER(ER_DATETIME_FUNCTION_OVERFLOW),
867
868
                      "datetime");