~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time_functions.cc

  • Committer: Brian Aker
  • Date: 2011-01-20 15:27:43 UTC
  • mfrom: (2097.1.7 drizzle-build)
  • Revision ID: brian@tangent.org-20110120152743-x88aq1sj1k9andwm
Merge of all of the time fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
                          type::Time *l_time,
131
 
                          uint32_t flags)
 
127
type::timestamp_t str_to_datetime_with_warn(const char *str, 
 
128
                                            uint32_t length, 
 
129
                                            type::Time *l_time,
 
130
                                            uint32_t flags)
132
131
{
133
132
  int was_cut;
134
 
  enum enum_drizzle_timestamp_type ts_type;
 
133
  type::timestamp_t ts_type;
135
134
  Session *session= current_session;
136
135
 
137
136
  ts_type= str_to_datetime(str, length, l_time,
139
138
                                     (MODE_INVALID_DATES |
140
139
                                      MODE_NO_ZERO_DATE))),
141
140
                           &was_cut);
142
 
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
 
141
  if (was_cut || ts_type <= type::DRIZZLE_TIMESTAMP_ERROR)
143
142
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
144
143
                                 str, length, ts_type,  NULL);
145
144
  return ts_type;
153
152
  bool ret_val= str_to_time(str, length, l_time, &warning);
154
153
  if (ret_val || warning)
155
154
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
156
 
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
 
155
                                 str, length, type::DRIZZLE_TIMESTAMP_TIME, NULL);
157
156
  return ret_val;
158
157
}
159
158
 
160
159
 
161
 
void localtime_to_TIME(type::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_time(const type::Time *l_time, String *str)
174
 
{
175
 
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
176
 
  uint32_t length= (uint32_t) my_time_to_str(l_time, str->c_ptr());
177
 
  str->length(length);
178
 
  str->set_charset(&my_charset_bin);
179
 
}
180
 
 
181
 
void make_date(const type::Time *l_time, String *str)
182
 
{
183
 
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
184
 
  uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr());
185
 
  str->length(length);
186
 
  str->set_charset(&my_charset_bin);
187
 
}
188
 
 
189
 
 
190
 
void make_datetime(const type::Time *l_time, String *str)
191
 
{
192
 
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
193
 
  uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr());
194
 
  str->length(length);
195
 
  str->set_charset(&my_charset_bin);
196
 
}
197
 
 
198
 
 
199
160
void make_truncated_value_warning(Session *session, 
200
161
                                  DRIZZLE_ERROR::enum_warning_level level,
201
162
                                  const char *str_val,
202
163
                                                          uint32_t str_length,
203
 
                                  enum enum_drizzle_timestamp_type time_type,
 
164
                                  type::timestamp_t time_type,
204
165
                                  const char *field_name)
205
166
{
206
167
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
212
173
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
213
174
 
214
175
  switch (time_type) {
215
 
    case DRIZZLE_TIMESTAMP_DATE:
216
 
      type_str= "date";
217
 
      break;
218
 
    case DRIZZLE_TIMESTAMP_TIME:
219
 
      type_str= "time";
220
 
      break;
221
 
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
222
 
    default:
223
 
      type_str= "datetime";
224
 
      break;
 
176
  case type::DRIZZLE_TIMESTAMP_DATE:
 
177
    type_str= "date";
 
178
    break;
 
179
 
 
180
  case type::DRIZZLE_TIMESTAMP_TIME:
 
181
    type_str= "time";
 
182
    break;
 
183
 
 
184
  case type::DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
 
185
  default:
 
186
    type_str= "datetime";
 
187
    break;
225
188
  }
 
189
 
226
190
  if (field_name)
227
191
  {
228
192
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
232
196
  }
233
197
  else
234
198
  {
235
 
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
 
199
    if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
 
200
    {
236
201
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
237
202
                         ER(ER_TRUNCATED_WRONG_VALUE),
238
203
                         type_str, str.c_ptr());
 
204
    }
239
205
    else
 
206
    {
240
207
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
241
208
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
 
209
    }
242
210
  }
243
211
  push_warning(session, level,
244
212
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
258
226
    the second argument should be TIMESTAMP_TIME also.
259
227
    We should check it before calc_time_diff call.
260
228
  */
261
 
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
 
229
  if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME)  // Time value
262
230
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
263
231
  else
264
232
  {
265
233
    days= calc_daynr((uint32_t) l_time1->year,
266
 
                     (uint32_t) l_time1->month,
267
 
                     (uint32_t) l_time1->day);
268
 
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
 
234
                     (uint32_t) l_time1->month,
 
235
                     (uint32_t) l_time1->day);
 
236
    if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME)
269
237
      days-= l_sign * (long)l_time2->day;
270
238
    else
271
239
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
272
 
                               (uint32_t) l_time2->month,
273
 
                               (uint32_t) l_time2->day);
 
240
                               (uint32_t) l_time2->month,
 
241
                               (uint32_t) l_time2->day);
274
242
  }
275
243
 
276
244
  microseconds= ((int64_t)days*86400L +
277
245
                 (int64_t)(l_time1->hour*3600L +
278
 
                            l_time1->minute*60L +
279
 
                            l_time1->second) -
 
246
                           l_time1->minute*60L +
 
247
                           l_time1->second) -
280
248
                 l_sign*(int64_t)(l_time2->hour*3600L +
281
 
                                   l_time2->minute*60L +
282
 
                                   l_time2->second)) * 1000000L +
283
 
                (int64_t)l_time1->second_part -
284
 
                l_sign*(int64_t)l_time2->second_part;
 
249
                                  l_time2->minute*60L +
 
250
                                  l_time2->second)) * 1000000L +
 
251
    (int64_t)l_time1->second_part -
 
252
    l_sign*(int64_t)l_time2->second_part;
285
253
 
286
254
  neg= 0;
287
255
  if (microseconds < 0)