diff options
Diffstat (limited to 'sql/time.cc')
-rw-r--r-- | sql/time.cc | 214 |
1 files changed, 29 insertions, 185 deletions
diff --git a/sql/time.cc b/sql/time.cc index e76b169b336..5069031081d 100644 --- a/sql/time.cc +++ b/sql/time.cc @@ -33,15 +33,6 @@ int calc_weekday(long daynr,bool sunday_first_day_of_week) DBUG_RETURN ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7)); } - /* Calc days in one year. works with 0 <= year <= 99 */ - -uint calc_days_in_year(uint year) -{ - return (year & 3) == 0 && (year%100 || (year%400 == 0 && year)) ? - 366 : 365; -} - - /* The bits in week_format has the following meaning: WEEK_MONDAY_FIRST (0) If not set Sunday is first day of week @@ -195,14 +186,22 @@ ulong convert_month_to_period(ulong month) NOTE See description of str_to_datetime() for more information. */ + timestamp_type str_to_datetime_with_warn(const char *str, uint length, TIME *l_time, uint flags) { int was_cut; - timestamp_type ts_type= str_to_datetime(str, length, l_time, flags, &was_cut); - if (was_cut) - make_truncated_value_warning(current_thd, str, length, ts_type); + THD *thd= current_thd; + timestamp_type ts_type; + + ts_type= str_to_datetime(str, length, l_time, + (flags | (thd->variables.sql_mode & + (MODE_INVALID_DATES | + MODE_NO_ZERO_DATE))), + &was_cut); + if (was_cut || ts_type <= MYSQL_TIMESTAMP_ERROR) + make_truncated_value_warning(current_thd, str, length, ts_type, NullS); return ts_type; } @@ -224,7 +223,7 @@ str_to_datetime_with_warn(const char *str, uint length, TIME *l_time, 0 - t contains datetime value which is out of TIMESTAMP range. */ -my_time_t TIME_to_timestamp(THD *thd, const TIME *t, bool *in_dst_time_gap) +my_time_t TIME_to_timestamp(THD *thd, const TIME *t, my_bool *in_dst_time_gap) { my_time_t timestamp; @@ -258,101 +257,13 @@ str_to_time_with_warn(const char *str, uint length, TIME *l_time) int was_cut; bool ret_val= str_to_time(str, length, l_time, &was_cut); if (was_cut) - make_truncated_value_warning(current_thd, str, length, MYSQL_TIMESTAMP_TIME); + make_truncated_value_warning(current_thd, str, length, + MYSQL_TIMESTAMP_TIME, NullS); return ret_val; } /* - Convert datetime value specified as number to broken-down TIME - representation and form value of DATETIME type as side-effect. - - SYNOPSIS - number_to_TIME() - nr - datetime value as number - time_res - pointer for structure for broken-down representation - fuzzy_date - indicates whenever we allow fuzzy dates - was_cut - set ot 1 if there was some kind of error during - conversion or to 0 if everything was OK. - - DESCRIPTION - Convert a datetime value of formats YYMMDD, YYYYMMDD, YYMMDDHHMSS, - YYYYMMDDHHMMSS to broken-down TIME representation. Return value in - YYYYMMDDHHMMSS format as side-effect. - - This function also checks if datetime value fits in DATETIME range. - - RETURN VALUE - Datetime value in YYYYMMDDHHMMSS format. - If input value is not valid datetime value then 0 is returned. -*/ - -longlong number_to_TIME(longlong nr, TIME *time_res, bool fuzzy_date, - int *was_cut) -{ - long part1,part2; - - *was_cut= 0; - - if (nr == LL(0) || nr >= LL(10000101000000)) - goto ok; - if (nr < 101) - goto err; - if (nr <= (YY_PART_YEAR-1)*10000L+1231L) - { - nr= (nr+20000000L)*1000000L; // YYMMDD, year: 2000-2069 - goto ok; - } - if (nr < (YY_PART_YEAR)*10000L+101L) - goto err; - if (nr <= 991231L) - { - nr= (nr+19000000L)*1000000L; // YYMMDD, year: 1970-1999 - goto ok; - } - if (nr < 10000101L) - goto err; - if (nr <= 99991231L) - { - nr= nr*1000000L; - goto ok; - } - if (nr < 101000000L) - goto err; - if (nr <= (YY_PART_YEAR-1)*LL(10000000000)+LL(1231235959)) - { - nr= nr+LL(20000000000000); // YYMMDDHHMMSS, 2000-2069 - goto ok; - } - if (nr < YY_PART_YEAR*LL(10000000000)+ LL(101000000)) - goto err; - if (nr <= LL(991231235959)) - nr= nr+LL(19000000000000); // YYMMDDHHMMSS, 1970-1999 - - ok: - part1=(long) (nr/LL(1000000)); - part2=(long) (nr - (longlong) part1*LL(1000000)); - time_res->year= (int) (part1/10000L); part1%=10000L; - time_res->month= (int) part1 / 100; - time_res->day= (int) part1 % 100; - time_res->hour= (int) (part2/10000L); part2%=10000L; - time_res->minute=(int) part2 / 100; - time_res->second=(int) part2 % 100; - - if (time_res->year <= 9999 && time_res->month <= 12 && - time_res->day <= 31 && time_res->hour <= 23 && - time_res->minute <= 59 && time_res->second <= 59 && - (fuzzy_date || (time_res->month != 0 && time_res->day != 0) || nr==0)) - return nr; - - err: - - *was_cut= 1; - return LL(0); -} - - -/* Convert a system time structure to TIME */ @@ -772,16 +683,15 @@ void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)), void make_truncated_value_warning(THD *thd, const char *str_val, - uint str_length, timestamp_type time_type) + uint str_length, timestamp_type time_type, + const char *field_name) { char warn_buff[MYSQL_ERRMSG_SIZE]; const char *type_str; - + CHARSET_INFO *cs= &my_charset_latin1; char buff[128]; String str(buff,(uint32) sizeof(buff), system_charset_info); - str.length(0); - str.append(str_val, str_length); - str.append('\0'); + str.copy(str_val, str_length, system_charset_info); switch (time_type) { case MYSQL_TIMESTAMP_DATE: @@ -795,84 +705,18 @@ void make_truncated_value_warning(THD *thd, const char *str_val, type_str= "datetime"; break; } - sprintf(warn_buff, ER(ER_TRUNCATED_WRONG_VALUE), - type_str, str.ptr()); - push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_TRUNCATED_WRONG_VALUE, warn_buff); -} - - -/* Convert time value to integer in YYYYMMDDHHMMSS format */ - -ulonglong TIME_to_ulonglong_datetime(const TIME *time) -{ - return ((ulonglong) (time->year * 10000UL + - time->month * 100UL + - time->day) * ULL(1000000) + - (ulonglong) (time->hour * 10000UL + - time->minute * 100UL + - time->second)); -} - - -/* Convert TIME value to integer in YYYYMMDD format */ - -ulonglong TIME_to_ulonglong_date(const TIME *time) -{ - return (ulonglong) (time->year * 10000UL + time->month * 100UL + time->day); -} - - -/* - Convert TIME value to integer in HHMMSS format. - This function doesn't take into account time->day member: - it's assumed that days have been converted to hours already. -*/ - -ulonglong TIME_to_ulonglong_time(const TIME *time) -{ - return (ulonglong) (time->hour * 10000UL + - time->minute * 100UL + - time->second); + if (field_name) + cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), + ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), + type_str, str.c_ptr(), field_name, + (ulong) thd->row_count); + else + cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), + ER(ER_TRUNCATED_WRONG_VALUE), + type_str, str.c_ptr()); + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_TRUNCATED_WRONG_VALUE, warn_buff); } -/* - Convert struct TIME (date and time split into year/month/day/hour/... - to a number in format YYYYMMDDHHMMSS (DATETIME), - YYYYMMDD (DATE) or HHMMSS (TIME). - - SYNOPSIS - TIME_to_ulonglong() - - DESCRIPTION - The function is used when we need to convert value of time item - to a number if it's used in numeric context, i. e.: - SELECT NOW()+1, CURDATE()+0, CURTIMIE()+0; - SELECT ?+1; - - NOTE - This function doesn't check that given TIME structure members are - in valid range. If they are not, return value won't reflect any - valid date either. -*/ - -ulonglong TIME_to_ulonglong(const TIME *time) -{ - switch (time->time_type) { - case MYSQL_TIMESTAMP_DATETIME: - return TIME_to_ulonglong_datetime(time); - case MYSQL_TIMESTAMP_DATE: - return TIME_to_ulonglong_date(time); - case MYSQL_TIMESTAMP_TIME: - return TIME_to_ulonglong_time(time); - case MYSQL_TIMESTAMP_NONE: - case MYSQL_TIMESTAMP_ERROR: - return ULL(0); - default: - DBUG_ASSERT(0); - } - return 0; -} - #endif |