~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time_functions.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
39
39
}
40
40
 
41
41
 
42
 
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
 
42
uint32_t calc_week(type::Time *l_time, uint32_t week_behaviour, uint32_t *year)
43
43
{
44
44
  uint32_t days;
45
45
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
107
107
    {
108
108
      if (day_of_year > 31+28)
109
109
      {
110
 
        day_of_year--;
111
 
        if (day_of_year == 31+28)
112
 
          leap_day=1;           /* Handle leapyears leapday */
 
110
        day_of_year--;
 
111
        if (day_of_year == 31+28)
 
112
          leap_day=1;           /* Handle leapyears leapday */
113
113
      }
114
114
    }
115
115
    *ret_month=1;
116
116
    for (month_pos= days_in_month ;
117
 
         day_of_year > (uint32_t) *month_pos ;
118
 
         day_of_year-= *(month_pos++), (*ret_month)++)
 
117
         day_of_year > (uint32_t) *month_pos ;
 
118
         day_of_year-= *(month_pos++), (*ret_month)++)
119
119
      ;
120
120
    *ret_year=year;
121
121
    *ret_day=day_of_year+leap_day;
124
124
}
125
125
 
126
126
 
127
 
enum enum_drizzle_timestamp_type
128
 
str_to_datetime_with_warn(const char *str, 
129
 
                          uint32_t length, 
130
 
                          DRIZZLE_TIME *l_time,
131
 
                          uint32_t flags)
 
127
type::timestamp_t str_to_datetime_with_warn(Session *session,
 
128
                                            const char *str, 
 
129
                                            uint32_t length, 
 
130
                                            type::Time *l_time,
 
131
                                            uint32_t flags)
132
132
{
133
 
  int was_cut;
134
 
  Session *session= current_session;
135
 
  enum enum_drizzle_timestamp_type ts_type;
 
133
  type::cut_t was_cut= type::VALID;
 
134
  type::timestamp_t ts_type;
136
135
 
137
 
  ts_type= str_to_datetime(str, length, l_time,
138
 
                           (flags | (session->variables.sql_mode &
139
 
                                     (MODE_INVALID_DATES |
140
 
                                      MODE_NO_ZERO_DATE))),
141
 
                           &was_cut);
142
 
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
143
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
136
  ts_type= l_time->store(str, length,
 
137
                         (flags | (session->variables.sql_mode &
 
138
                                   (MODE_INVALID_DATES |
 
139
                                    MODE_NO_ZERO_DATE))),
 
140
                         was_cut);
 
141
  if (was_cut || ts_type <= type::DRIZZLE_TIMESTAMP_ERROR)
 
142
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
144
143
                                 str, length, ts_type,  NULL);
 
144
 
145
145
  return ts_type;
146
146
}
147
147
 
148
148
 
149
149
bool
150
 
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
 
150
str_to_time_with_warn(Session *session, const char *str, uint32_t length, type::Time *l_time)
151
151
{
152
152
  int warning;
153
 
  bool ret_val= str_to_time(str, length, l_time, &warning);
 
153
  bool ret_val= l_time->store(str, length, warning, type::DRIZZLE_TIMESTAMP_TIME);
154
154
  if (ret_val || warning)
155
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
156
 
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
 
155
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
156
                                 str, length, type::DRIZZLE_TIMESTAMP_TIME, NULL);
157
157
  return ret_val;
158
158
}
159
159
 
160
160
 
161
 
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
162
 
{
163
 
  to->neg=0;
164
 
  to->second_part=0;
165
 
  to->year=     (int) ((from->tm_year+1900) % 10000);
166
 
  to->month=    (int) from->tm_mon+1;
167
 
  to->day=      (int) from->tm_mday;
168
 
  to->hour=     (int) from->tm_hour;
169
 
  to->minute=   (int) from->tm_min;
170
 
  to->second=   (int) from->tm_sec;
171
 
}
172
 
 
173
 
void make_date(const DRIZZLE_TIME *l_time, String *str)
174
 
{
175
 
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
176
 
  uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr());
177
 
  str->length(length);
178
 
  str->set_charset(&my_charset_bin);
179
 
}
180
 
 
181
 
 
182
 
void make_datetime(const DRIZZLE_TIME *l_time, String *str)
183
 
{
184
 
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
185
 
  uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr());
186
 
  str->length(length);
187
 
  str->set_charset(&my_charset_bin);
188
 
}
189
 
 
190
 
 
191
161
void make_truncated_value_warning(Session *session, 
192
162
                                  DRIZZLE_ERROR::enum_warning_level level,
193
163
                                  const char *str_val,
194
164
                                                          uint32_t str_length,
195
 
                                  enum enum_drizzle_timestamp_type time_type,
 
165
                                  type::timestamp_t time_type,
196
166
                                  const char *field_name)
197
167
{
198
168
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
204
174
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
205
175
 
206
176
  switch (time_type) {
207
 
    case DRIZZLE_TIMESTAMP_DATE:
208
 
      type_str= "date";
209
 
      break;
210
 
    case DRIZZLE_TIMESTAMP_TIME:
211
 
      type_str= "time";
212
 
      break;
213
 
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
214
 
    default:
215
 
      type_str= "datetime";
216
 
      break;
 
177
  case type::DRIZZLE_TIMESTAMP_DATE:
 
178
    type_str= "date";
 
179
    break;
 
180
 
 
181
  case type::DRIZZLE_TIMESTAMP_TIME:
 
182
    type_str= "time";
 
183
    break;
 
184
 
 
185
  case type::DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
 
186
  default:
 
187
    type_str= "datetime";
 
188
    break;
217
189
  }
 
190
 
218
191
  if (field_name)
 
192
  {
219
193
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
220
194
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
221
195
                       type_str, str.c_ptr(), field_name,
222
196
                       (uint32_t) session->row_count);
 
197
  }
223
198
  else
224
199
  {
225
 
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
 
200
    if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
 
201
    {
226
202
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
227
203
                         ER(ER_TRUNCATED_WRONG_VALUE),
228
204
                         type_str, str.c_ptr());
 
205
    }
229
206
    else
 
207
    {
230
208
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
231
209
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
 
210
    }
232
211
  }
233
212
  push_warning(session, level,
234
213
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
236
215
 
237
216
 
238
217
bool
239
 
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
 
218
calc_time_diff(type::Time *l_time1, type::Time *l_time2, int l_sign, int64_t *seconds_out,
240
219
               long *microseconds_out)
241
220
{
242
221
  long days;
248
227
    the second argument should be TIMESTAMP_TIME also.
249
228
    We should check it before calc_time_diff call.
250
229
  */
251
 
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
 
230
  if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME)  // Time value
252
231
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
253
232
  else
254
233
  {
255
234
    days= calc_daynr((uint32_t) l_time1->year,
256
 
                     (uint32_t) l_time1->month,
257
 
                     (uint32_t) l_time1->day);
258
 
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
 
235
                     (uint32_t) l_time1->month,
 
236
                     (uint32_t) l_time1->day);
 
237
    if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME)
259
238
      days-= l_sign * (long)l_time2->day;
260
239
    else
261
240
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
262
 
                               (uint32_t) l_time2->month,
263
 
                               (uint32_t) l_time2->day);
 
241
                               (uint32_t) l_time2->month,
 
242
                               (uint32_t) l_time2->day);
264
243
  }
265
244
 
266
245
  microseconds= ((int64_t)days*86400L +
267
246
                 (int64_t)(l_time1->hour*3600L +
268
 
                            l_time1->minute*60L +
269
 
                            l_time1->second) -
 
247
                           l_time1->minute*60L +
 
248
                           l_time1->second) -
270
249
                 l_sign*(int64_t)(l_time2->hour*3600L +
271
 
                                   l_time2->minute*60L +
272
 
                                   l_time2->second)) * 1000000L +
273
 
                (int64_t)l_time1->second_part -
274
 
                l_sign*(int64_t)l_time2->second_part;
 
250
                                  l_time2->minute*60L +
 
251
                                  l_time2->second)) * 1000000L +
 
252
    (int64_t)l_time1->second_part -
 
253
    l_sign*(int64_t)l_time2->second_part;
275
254
 
276
255
  neg= 0;
277
256
  if (microseconds < 0)