16 #ifndef SQL_TIME_INCLUDED
17 #define SQL_TIME_INCLUDED
19 #include "my_global.h"
21 #include "mysql_time.h"
22 #include "sql_error.h"
25 typedef enum enum_mysql_timestamp_type timestamp_type;
30 #define WEEK_MONDAY_FIRST 1
32 #define WEEK_FIRST_WEEKDAY 4
34 ulong convert_period_to_month(ulong period);
35 ulong convert_month_to_period(ulong month);
37 void get_date_from_daynr(
long daynr,uint *year, uint *month, uint *day);
38 my_time_t TIME_to_timestamp(THD *thd,
const MYSQL_TIME *t, my_bool *not_exist);
39 bool datetime_with_no_zero_in_date_to_timeval(THD *thd,
const MYSQL_TIME *t,
42 bool datetime_to_timeval(THD *thd,
const MYSQL_TIME *t,
43 struct timeval *tm,
int *warnings);
45 bool my_decimal_to_datetime_with_warn(
const my_decimal *decimal,
47 bool my_double_to_datetime_with_warn(
double nr,
MYSQL_TIME *ltime, uint
flags);
48 bool my_longlong_to_datetime_with_warn(longlong nr,
50 bool my_decimal_to_time_with_warn(
const my_decimal *decimal,
52 bool my_double_to_time_with_warn(
double nr,
MYSQL_TIME *ltime);
53 bool my_longlong_to_time_with_warn(longlong nr,
MYSQL_TIME *ltime);
56 inline void datetime_to_time(
MYSQL_TIME *ltime)
58 ltime->year= ltime->month= ltime->day= 0;
59 ltime->time_type= MYSQL_TIMESTAMP_TIME;
61 inline void datetime_to_date(
MYSQL_TIME *ltime)
63 ltime->hour= ltime->minute= ltime->second= ltime->
second_part= 0;
64 ltime->time_type= MYSQL_TIMESTAMP_DATE;
66 inline void date_to_datetime(
MYSQL_TIME *ltime)
68 ltime->time_type= MYSQL_TIMESTAMP_DATETIME;
70 void make_truncated_value_warning(THD *thd,
71 Sql_condition::enum_warning_level
level,
73 timestamp_type time_type,
74 const char *field_name);
76 timestamp_type time_type)
78 make_truncated_value_warning(current_thd, Sql_condition::WARN_LEVEL_WARN,
79 val, time_type, NullS);
82 const char *format_str,
97 bool date_add_interval(
MYSQL_TIME *ltime, interval_type int_type,
100 int l_sign, longlong *seconds_out,
long *microseconds_out);
102 void localtime_to_TIME(
MYSQL_TIME *
to,
struct tm *from);
103 void calc_time_from_sec(
MYSQL_TIME *
to, longlong seconds,
long microseconds);
104 uint calc_week(
MYSQL_TIME *l_time, uint week_behaviour, uint *year);
106 int calc_weekday(
long daynr,
bool sunday_first_day_of_week);
107 bool parse_date_time_format(timestamp_type format_type,
108 const char *format, uint format_length,
111 bool str_to_time(
const CHARSET_INFO *cs,
const char *str, uint length,
117 return str_to_time(str->charset(), str->ptr(), str->length(),
118 ltime,
flags, status);
121 bool time_add_nanoseconds_with_round(
MYSQL_TIME *ltime, uint nanoseconds,
125 const char *str, uint length,
132 return str_to_datetime(str->charset(), str->ptr(), str->length(),
133 ltime,
flags, status);
136 bool datetime_add_nanoseconds_with_round(
MYSQL_TIME *ltime,
137 uint nanoseconds,
int *warnings);
140 inline bool parse_date_time_format(timestamp_type format_type,
143 return parse_date_time_format(format_type,
144 date_time_format->format.str,
145 date_time_format->format.length,
157 inline long my_time_fraction_remainder(
long nr, uint decimals)
159 DBUG_ASSERT(decimals <= DATETIME_MAX_DECIMALS);
160 return nr % (long) log_10_int[DATETIME_MAX_DECIMALS - decimals];
162 inline void my_time_trunc(
MYSQL_TIME *ltime, uint decimals)
166 inline void my_datetime_trunc(
MYSQL_TIME *ltime, uint decimals)
168 return my_time_trunc(ltime, decimals);
170 inline void my_timeval_trunc(
struct timeval *tv, uint decimals)
172 tv->tv_usec-= my_time_fraction_remainder(tv->tv_usec, decimals);
174 bool my_time_round(
MYSQL_TIME *ltime, uint decimals);
175 bool my_datetime_round(
MYSQL_TIME *ltime, uint decimals,
int *warnings);
176 bool my_timeval_round(
struct timeval *tv, uint decimals);
179 inline ulonglong TIME_to_ulonglong_datetime_round(
const MYSQL_TIME *ltime)
183 return TIME_to_ulonglong_datetime(ltime);
184 if (ltime->second < 59)
185 return TIME_to_ulonglong_datetime(ltime) + 1;
189 my_datetime_round(&tmp, 0, &warnings);
190 return TIME_to_ulonglong_datetime(&tmp);
194 inline ulonglong TIME_to_ulonglong_time_round(
const MYSQL_TIME *ltime)
197 return TIME_to_ulonglong_time(ltime);
198 if (ltime->second < 59)
199 return TIME_to_ulonglong_time(ltime) + 1;
202 my_time_round(&tmp, 0);
203 return TIME_to_ulonglong_time(&tmp);
207 inline ulonglong TIME_to_ulonglong_round(
const MYSQL_TIME *ltime)
209 switch (ltime->time_type)
211 case MYSQL_TIMESTAMP_TIME:
212 return TIME_to_ulonglong_time_round(ltime);
213 case MYSQL_TIMESTAMP_DATETIME:
214 return TIME_to_ulonglong_datetime_round(ltime);
215 case MYSQL_TIMESTAMP_DATE:
216 return TIME_to_ulonglong_date(ltime);
224 inline double TIME_microseconds(
const MYSQL_TIME *ltime)
229 inline double TIME_to_double_datetime(
const MYSQL_TIME *ltime)
231 return (
double) TIME_to_ulonglong_datetime(ltime) + TIME_microseconds(ltime);
235 inline double TIME_to_double_time(
const MYSQL_TIME *ltime)
237 return (
double) TIME_to_ulonglong_time(ltime) + TIME_microseconds(ltime);
241 inline double TIME_to_double(
const MYSQL_TIME *ltime)
243 return (
double) TIME_to_ulonglong(ltime) + TIME_microseconds(ltime);
248 check_fuzzy_date(
const MYSQL_TIME *ltime, uint fuzzydate)
250 return !(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day);
256 return ltime->year || ltime->month || ltime->day;
262 return ltime->hour || ltime->minute || ltime->second || ltime->
second_part;
265 longlong TIME_to_longlong_packed(
const MYSQL_TIME *tm,
266 enum enum_field_types
type);
267 void TIME_from_longlong_packed(
MYSQL_TIME *ltime,
268 enum enum_field_types
type,
269 longlong packed_value);
271 enum enum_field_types
type,
272 longlong packed_value);
274 longlong longlong_from_datetime_packed(
enum enum_field_types
type,
275 longlong packed_value);
277 double double_from_datetime_packed(
enum enum_field_types
type,
278 longlong packed_value);
281 timestamp_type field_type_to_timestamp_type(
enum enum_field_types
type)
285 case MYSQL_TYPE_TIME:
return MYSQL_TIMESTAMP_TIME;
286 case MYSQL_TYPE_DATE:
return MYSQL_TIMESTAMP_DATE;
287 case MYSQL_TYPE_TIMESTAMP:
288 case MYSQL_TYPE_DATETIME:
return MYSQL_TIMESTAMP_DATETIME;
289 default:
return MYSQL_TIMESTAMP_NONE;