~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time_functions.cc

  • Committer: Muhammad Umair
  • Date: 2011-08-16 11:47:29 UTC
  • mto: This revision was merged to the branch mainline in revision 2402.
  • Revision ID: umair@remotedesk-20110816114729-w6x88fj0sow4g3z9
mergeĀ lp:~mumair/drizzle/drizzle-IPv6Address

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
/* Functions to handle date and time */
22
22
 
23
 
#include "config.h"
24
 
#include "drizzled/error.h"
25
 
#include "drizzled/util/test.h"
26
 
#include "drizzled/tztime.h"
27
 
#include "drizzled/session.h"
28
 
#include "drizzled/time_functions.h"
 
23
#include <config.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/util/test.h>
 
26
#include <drizzled/session.h>
 
27
#include <drizzled/time_functions.h>
 
28
#include <drizzled/charset.h>
 
29
#include <drizzled/system_variables.h>
 
30
#include <drizzled/sql_string.h>
29
31
 
30
 
namespace drizzled
31
 
{
 
32
namespace drizzled {
32
33
 
33
34
/* Some functions to calculate dates */
34
35
 
39
40
}
40
41
 
41
42
 
42
 
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
 
43
uint32_t calc_week(type::Time *l_time, uint32_t week_behaviour, uint32_t *year)
43
44
{
44
45
  uint32_t days;
45
46
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
107
108
    {
108
109
      if (day_of_year > 31+28)
109
110
      {
110
 
        day_of_year--;
111
 
        if (day_of_year == 31+28)
112
 
          leap_day=1;           /* Handle leapyears leapday */
 
111
        day_of_year--;
 
112
        if (day_of_year == 31+28)
 
113
          leap_day=1;           /* Handle leapyears leapday */
113
114
      }
114
115
    }
115
116
    *ret_month=1;
116
117
    for (month_pos= days_in_month ;
117
 
         day_of_year > (uint32_t) *month_pos ;
118
 
         day_of_year-= *(month_pos++), (*ret_month)++)
 
118
         day_of_year > (uint32_t) *month_pos ;
 
119
         day_of_year-= *(month_pos++), (*ret_month)++)
119
120
      ;
120
121
    *ret_year=year;
121
122
    *ret_day=day_of_year+leap_day;
124
125
}
125
126
 
126
127
 
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)
 
128
type::timestamp_t str_to_datetime_with_warn(Session *session,
 
129
                                            const char *str, 
 
130
                                            uint32_t length, 
 
131
                                            type::Time *l_time,
 
132
                                            uint32_t flags)
132
133
{
133
 
  int was_cut;
134
 
  enum enum_drizzle_timestamp_type ts_type;
135
 
  Session *session= current_session;
 
134
  type::cut_t was_cut= type::VALID;
 
135
  type::timestamp_t ts_type;
136
136
 
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)
 
137
  ts_type= l_time->store(str, length,
 
138
                         (flags | (session->variables.sql_mode &
 
139
                                   (MODE_INVALID_DATES |
 
140
                                    MODE_NO_ZERO_DATE))),
 
141
                         was_cut);
 
142
  if (was_cut || ts_type <= type::DRIZZLE_TIMESTAMP_ERROR)
143
143
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
144
144
                                 str, length, ts_type,  NULL);
 
145
 
145
146
  return ts_type;
146
147
}
147
148
 
148
149
 
149
150
bool
150
 
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
 
151
str_to_time_with_warn(Session *session, const char *str, uint32_t length, type::Time *l_time)
151
152
{
152
153
  int warning;
153
 
  bool ret_val= str_to_time(str, length, l_time, &warning);
 
154
  bool ret_val= l_time->store(str, length, warning, type::DRIZZLE_TIMESTAMP_TIME);
154
155
  if (ret_val || warning)
155
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
156
 
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
 
156
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
157
                                 str, length, type::DRIZZLE_TIMESTAMP_TIME, NULL);
157
158
  return ret_val;
158
159
}
159
160
 
160
161
 
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
162
void make_truncated_value_warning(Session *session, 
192
163
                                  DRIZZLE_ERROR::enum_warning_level level,
193
164
                                  const char *str_val,
194
165
                                                          uint32_t str_length,
195
 
                                  enum enum_drizzle_timestamp_type time_type,
 
166
                                  type::timestamp_t time_type,
196
167
                                  const char *field_name)
197
168
{
198
169
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
199
170
  const char *type_str;
200
 
  CHARSET_INFO *cs= &my_charset_utf8_general_ci;
 
171
  charset_info_st *cs= &my_charset_utf8_general_ci;
201
172
  char buff[128];
202
173
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
203
174
  str.copy(str_val, str_length, system_charset_info);
204
175
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
205
176
 
206
177
  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;
 
178
  case type::DRIZZLE_TIMESTAMP_DATE:
 
179
    type_str= "date";
 
180
    break;
 
181
 
 
182
  case type::DRIZZLE_TIMESTAMP_TIME:
 
183
    type_str= "time";
 
184
    break;
 
185
 
 
186
  case type::DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
 
187
  default:
 
188
    type_str= "datetime";
 
189
    break;
217
190
  }
 
191
 
218
192
  if (field_name)
219
193
  {
220
194
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
224
198
  }
225
199
  else
226
200
  {
227
 
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
 
201
    if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
 
202
    {
228
203
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
229
204
                         ER(ER_TRUNCATED_WRONG_VALUE),
230
205
                         type_str, str.c_ptr());
 
206
    }
231
207
    else
 
208
    {
232
209
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
233
210
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
 
211
    }
234
212
  }
235
213
  push_warning(session, level,
236
214
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
238
216
 
239
217
 
240
218
bool
241
 
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
 
219
calc_time_diff(type::Time *l_time1, type::Time *l_time2, int l_sign, int64_t *seconds_out,
242
220
               long *microseconds_out)
243
221
{
244
222
  long days;
250
228
    the second argument should be TIMESTAMP_TIME also.
251
229
    We should check it before calc_time_diff call.
252
230
  */
253
 
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
 
231
  if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME)  // Time value
254
232
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
255
233
  else
256
234
  {
257
235
    days= calc_daynr((uint32_t) l_time1->year,
258
 
                     (uint32_t) l_time1->month,
259
 
                     (uint32_t) l_time1->day);
260
 
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
 
236
                     (uint32_t) l_time1->month,
 
237
                     (uint32_t) l_time1->day);
 
238
    if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME)
261
239
      days-= l_sign * (long)l_time2->day;
262
240
    else
263
241
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
264
 
                               (uint32_t) l_time2->month,
265
 
                               (uint32_t) l_time2->day);
 
242
                               (uint32_t) l_time2->month,
 
243
                               (uint32_t) l_time2->day);
266
244
  }
267
245
 
268
246
  microseconds= ((int64_t)days*86400L +
269
247
                 (int64_t)(l_time1->hour*3600L +
270
 
                            l_time1->minute*60L +
271
 
                            l_time1->second) -
 
248
                           l_time1->minute*60L +
 
249
                           l_time1->second) -
272
250
                 l_sign*(int64_t)(l_time2->hour*3600L +
273
 
                                   l_time2->minute*60L +
274
 
                                   l_time2->second)) * 1000000L +
275
 
                (int64_t)l_time1->second_part -
276
 
                l_sign*(int64_t)l_time2->second_part;
 
251
                                  l_time2->minute*60L +
 
252
                                  l_time2->second)) * 1000000L +
 
253
    (int64_t)l_time1->second_part -
 
254
    l_sign*(int64_t)l_time2->second_part;
277
255
 
278
256
  neg= 0;
279
257
  if (microseconds < 0)