~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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