~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time_functions.cc

  • Committer: Stewart Smith
  • Date: 2010-02-22 07:44:37 UTC
  • mfrom: (1283.17.4)
  • mto: (1283.19.1)
  • mto: This revision was merged to the branch mainline in revision 1449.
  • Revision ID: stewart@flamingspork.com-20100222074437-1a9x1n030tbtv1qv
Merged embeddded-innodb-store-table-proto into embedded-innodb-write-row.

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, Inc.
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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
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/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>
 
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"
31
29
 
32
 
namespace drizzled {
 
30
namespace drizzled
 
31
{
33
32
 
34
33
/* Some functions to calculate dates */
35
34
 
40
39
}
41
40
 
42
41
 
43
 
uint32_t calc_week(type::Time *l_time, uint32_t week_behaviour, uint32_t *year)
 
42
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
44
43
{
45
44
  uint32_t days;
46
45
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
108
107
    {
109
108
      if (day_of_year > 31+28)
110
109
      {
111
 
        day_of_year--;
112
 
        if (day_of_year == 31+28)
113
 
          leap_day=1;           /* Handle leapyears leapday */
 
110
        day_of_year--;
 
111
        if (day_of_year == 31+28)
 
112
          leap_day=1;           /* Handle leapyears leapday */
114
113
      }
115
114
    }
116
115
    *ret_month=1;
117
116
    for (month_pos= days_in_month ;
118
 
         day_of_year > (uint32_t) *month_pos ;
119
 
         day_of_year-= *(month_pos++), (*ret_month)++)
 
117
         day_of_year > (uint32_t) *month_pos ;
 
118
         day_of_year-= *(month_pos++), (*ret_month)++)
120
119
      ;
121
120
    *ret_year=year;
122
121
    *ret_day=day_of_year+leap_day;
125
124
}
126
125
 
127
126
 
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)
 
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)
133
132
{
134
 
  type::cut_t was_cut= type::VALID;
135
 
  type::timestamp_t ts_type;
 
133
  int was_cut;
 
134
  Session *session= current_session;
 
135
  enum enum_drizzle_timestamp_type ts_type;
136
136
 
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
 
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
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,
144
144
                                 str, length, ts_type,  NULL);
145
 
 
146
145
  return ts_type;
147
146
}
148
147
 
149
148
 
150
149
bool
151
 
str_to_time_with_warn(Session *session, const char *str, uint32_t length, type::Time *l_time)
 
150
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
152
151
{
153
152
  int warning;
154
 
  bool ret_val= l_time->store(str, length, warning, type::DRIZZLE_TIMESTAMP_TIME);
 
153
  bool ret_val= str_to_time(str, length, l_time, &warning);
155
154
  if (ret_val || warning)
156
 
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
157
 
                                 str, length, type::DRIZZLE_TIMESTAMP_TIME, NULL);
 
155
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
156
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
158
157
  return ret_val;
159
158
}
160
159
 
161
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
 
162
191
void make_truncated_value_warning(Session *session, 
163
192
                                  DRIZZLE_ERROR::enum_warning_level level,
164
193
                                  const char *str_val,
165
194
                                                          uint32_t str_length,
166
 
                                  type::timestamp_t time_type,
 
195
                                  enum enum_drizzle_timestamp_type time_type,
167
196
                                  const char *field_name)
168
197
{
169
198
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
170
199
  const char *type_str;
171
 
  charset_info_st *cs= &my_charset_utf8_general_ci;
 
200
  CHARSET_INFO *cs= &my_charset_utf8_general_ci;
172
201
  char buff[128];
173
202
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
174
203
  str.copy(str_val, str_length, system_charset_info);
175
204
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
176
205
 
177
206
  switch (time_type) {
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;
 
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;
190
217
  }
191
 
 
192
218
  if (field_name)
193
 
  {
194
219
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
195
220
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
196
221
                       type_str, str.c_ptr(), field_name,
197
222
                       (uint32_t) session->row_count);
198
 
  }
199
223
  else
200
224
  {
201
 
    if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
202
 
    {
 
225
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
203
226
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
204
227
                         ER(ER_TRUNCATED_WRONG_VALUE),
205
228
                         type_str, str.c_ptr());
206
 
    }
207
229
    else
208
 
    {
209
230
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
210
231
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
211
 
    }
212
232
  }
213
233
  push_warning(session, level,
214
234
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
216
236
 
217
237
 
218
238
bool
219
 
calc_time_diff(type::Time *l_time1, type::Time *l_time2, int l_sign, int64_t *seconds_out,
 
239
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
220
240
               long *microseconds_out)
221
241
{
222
242
  long days;
228
248
    the second argument should be TIMESTAMP_TIME also.
229
249
    We should check it before calc_time_diff call.
230
250
  */
231
 
  if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME)  // Time value
 
251
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
232
252
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
233
253
  else
234
254
  {
235
255
    days= calc_daynr((uint32_t) l_time1->year,
236
 
                     (uint32_t) l_time1->month,
237
 
                     (uint32_t) l_time1->day);
238
 
    if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME)
 
256
                     (uint32_t) l_time1->month,
 
257
                     (uint32_t) l_time1->day);
 
258
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
239
259
      days-= l_sign * (long)l_time2->day;
240
260
    else
241
261
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
242
 
                               (uint32_t) l_time2->month,
243
 
                               (uint32_t) l_time2->day);
 
262
                               (uint32_t) l_time2->month,
 
263
                               (uint32_t) l_time2->day);
244
264
  }
245
265
 
246
266
  microseconds= ((int64_t)days*86400L +
247
267
                 (int64_t)(l_time1->hour*3600L +
248
 
                           l_time1->minute*60L +
249
 
                           l_time1->second) -
 
268
                            l_time1->minute*60L +
 
269
                            l_time1->second) -
250
270
                 l_sign*(int64_t)(l_time2->hour*3600L +
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;
 
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;
255
275
 
256
276
  neg= 0;
257
277
  if (microseconds < 0)