21
21
/* Functions to handle date and time */
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"
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>
33
34
/* Some functions to calculate dates */
127
enum enum_drizzle_timestamp_type
128
str_to_datetime_with_warn(const char *str,
130
DRIZZLE_TIME *l_time,
128
type::timestamp_t str_to_datetime_with_warn(Session *session,
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;
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))),
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))),
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);
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)
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);
161
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
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;
173
void make_date(const DRIZZLE_TIME *l_time, String *str)
175
str->alloc(MAX_DATE_STRING_REP_LENGTH);
176
uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr());
178
str->set_charset(&my_charset_bin);
182
void make_datetime(const DRIZZLE_TIME *l_time, String *str)
184
str->alloc(MAX_DATE_STRING_REP_LENGTH);
185
uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr());
187
str->set_charset(&my_charset_bin);
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)
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;
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
206
177
switch (time_type) {
207
case DRIZZLE_TIMESTAMP_DATE:
210
case DRIZZLE_TIMESTAMP_TIME:
213
case DRIZZLE_TIMESTAMP_DATETIME: // FALLTHROUGH
215
type_str= "datetime";
178
case type::DRIZZLE_TIMESTAMP_DATE:
182
case type::DRIZZLE_TIMESTAMP_TIME:
186
case type::DRIZZLE_TIMESTAMP_DATETIME: // FALLTHROUGH
188
type_str= "datetime";
220
194
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
227
if (time_type > DRIZZLE_TIMESTAMP_ERROR)
201
if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
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());
232
209
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
233
210
ER(ER_WRONG_VALUE), type_str, str.c_ptr());
235
213
push_warning(session, level,
236
214
ER_TRUNCATED_WRONG_VALUE, warn_buff);
250
228
the second argument should be TIMESTAMP_TIME also.
251
229
We should check it before calc_time_diff call.
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;
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;
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);
268
246
microseconds= ((int64_t)days*86400L +
269
247
(int64_t)(l_time1->hour*3600L +
270
l_time1->minute*60L +
248
l_time1->minute*60L +
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;
279
257
if (microseconds < 0)