in sql/item_timefunc.cc [178:514]
static bool extract_date_time(DATE_TIME_FORMAT *format,
const char *val, uint length, MYSQL_TIME *l_time,
timestamp_type cached_timestamp_type,
const char **sub_pattern_end,
const char *date_time_type)
{
int weekday= 0, yearday= 0, daypart= 0;
int week_number= -1;
int error= 0;
int strict_week_number_year= -1;
int frac_part;
bool usa_time= 0;
bool UNINIT_VAR(sunday_first_n_first_week_non_iso);
bool UNINIT_VAR(strict_week_number);
bool UNINIT_VAR(strict_week_number_year_type);
const char *val_begin= val;
const char *val_end= val + length;
const char *ptr= format->format.str;
const char *end= ptr + format->format.length;
const CHARSET_INFO *cs= &my_charset_bin;
DBUG_ENTER("extract_date_time");
if (!sub_pattern_end)
memset(l_time, 0, sizeof(*l_time));
for (; ptr != end && val != val_end; ptr++)
{
/* Skip pre-space between each argument */
if ((val+= cs->cset->scan(cs, val, val_end, MY_SEQ_SPACES)) >= val_end)
break;
if (*ptr == '%' && ptr+1 != end)
{
int val_len;
char *tmp;
error= 0;
val_len= (uint) (val_end - val);
switch (*++ptr) {
/* Year */
case 'Y':
tmp= (char*) val + MY_MIN(4, val_len);
l_time->year= (int) my_strtoll10(val, &tmp, &error);
if ((int) (tmp-val) <= 2)
l_time->year= year_2000_handling(l_time->year);
val= tmp;
break;
case 'y':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->year= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
l_time->year= year_2000_handling(l_time->year);
break;
/* Month */
case 'm':
case 'c':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->month= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
case 'M':
if ((l_time->month= check_word(my_locale_en_US.month_names,
val, val_end, &val)) <= 0)
goto err;
break;
case 'b':
if ((l_time->month= check_word(my_locale_en_US.ab_month_names,
val, val_end, &val)) <= 0)
goto err;
break;
/* Day */
case 'd':
case 'e':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->day= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
case 'D':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->day= (int) my_strtoll10(val, &tmp, &error);
/* Skip 'st, 'nd, 'th .. */
val= tmp + MY_MIN((int) (val_end-tmp), 2);
break;
/* Hour */
case 'h':
case 'I':
case 'l':
usa_time= 1;
/* fall through */
case 'k':
case 'H':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->hour= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
/* Minute */
case 'i':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->minute= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
/* Second */
case 's':
case 'S':
tmp= (char*) val + MY_MIN(2, val_len);
l_time->second= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
/* Second part */
case 'f':
tmp= (char*) val_end;
if (tmp - val > 6)
tmp= (char*) val + 6;
l_time->second_part= (int) my_strtoll10(val, &tmp, &error);
frac_part= 6 - (int) (tmp - val);
if (frac_part > 0)
l_time->second_part*= (ulong) log_10_int[frac_part];
val= tmp;
break;
/* AM / PM */
case 'p':
if (val_len < 2 || ! usa_time)
goto err;
if (!my_strnncoll(&my_charset_latin1,
(const uchar *) val, 2,
(const uchar *) "PM", 2))
daypart= 12;
else if (my_strnncoll(&my_charset_latin1,
(const uchar *) val, 2,
(const uchar *) "AM", 2))
goto err;
val+= 2;
break;
/* Exotic things */
case 'W':
if ((weekday= check_word(my_locale_en_US.day_names, val, val_end, &val)) <= 0)
goto err;
break;
case 'a':
if ((weekday= check_word(my_locale_en_US.ab_day_names, val, val_end, &val)) <= 0)
goto err;
break;
case 'w':
tmp= (char*) val + 1;
if ((weekday= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
weekday >= 7)
goto err;
/* We should use the same 1 - 7 scale for %w as for %W */
if (!weekday)
weekday= 7;
val= tmp;
break;
case 'j':
tmp= (char*) val + MY_MIN(val_len, 3);
yearday= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
/* Week numbers */
case 'V':
case 'U':
case 'v':
case 'u':
sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
strict_week_number= (*ptr=='V' || *ptr=='v');
tmp= (char*) val + MY_MIN(val_len, 2);
if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
(strict_week_number && !week_number) ||
week_number > 53)
goto err;
val= tmp;
break;
/* Year used with 'strict' %V and %v week numbers */
case 'X':
case 'x':
strict_week_number_year_type= (*ptr=='X');
tmp= (char*) val + MY_MIN(4, val_len);
strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
val= tmp;
break;
/* Time in AM/PM notation */
case 'r':
/*
We can't just set error here, as we don't want to generate two
warnings in case of errors
*/
if (extract_date_time(&time_ampm_format, val,
(uint)(val_end - val), l_time,
cached_timestamp_type, &val, "time"))
DBUG_RETURN(1);
break;
/* Time in 24-hour notation */
case 'T':
if (extract_date_time(&time_24hrs_format, val,
(uint)(val_end - val), l_time,
cached_timestamp_type, &val, "time"))
DBUG_RETURN(1);
break;
/* Conversion specifiers that match classes of characters */
case '.':
while (my_ispunct(cs, *val) && val != val_end)
val++;
break;
case '@':
while (my_isalpha(cs, *val) && val != val_end)
val++;
break;
case '#':
while (my_isdigit(cs, *val) && val != val_end)
val++;
break;
default:
goto err;
}
if (error) // Error from my_strtoll10
goto err;
}
else if (!my_isspace(cs, *ptr))
{
if (*val != *ptr)
goto err;
val++;
}
}
if (usa_time)
{
if (l_time->hour > 12 || l_time->hour < 1)
goto err;
l_time->hour= l_time->hour%12+daypart;
}
/*
If we are recursively called for parsing string matching compound
specifiers we are already done.
*/
if (sub_pattern_end)
{
*sub_pattern_end= val;
DBUG_RETURN(0);
}
if (yearday > 0)
{
uint days;
days= calc_daynr(l_time->year,1,1) + yearday - 1;
if (days <= 0 || days > MAX_DAY_NUMBER)
goto err;
get_date_from_daynr(days,&l_time->year,&l_time->month,&l_time->day);
}
if (week_number >= 0 && weekday)
{
int days;
uint weekday_b;
/*
%V,%v require %X,%x resprectively,
%U,%u should be used with %Y and not %X or %x
*/
if ((strict_week_number &&
(strict_week_number_year < 0 ||
strict_week_number_year_type != sunday_first_n_first_week_non_iso)) ||
(!strict_week_number && strict_week_number_year >= 0))
goto err;
/* Number of days since year 0 till 1st Jan of this year */
days= calc_daynr((strict_week_number ? strict_week_number_year :
l_time->year),
1, 1);
/* Which day of week is 1st Jan of this year */
weekday_b= calc_weekday(days, sunday_first_n_first_week_non_iso);
/*
Below we are going to sum:
1) number of days since year 0 till 1st day of 1st week of this year
2) number of days between 1st week and our week
3) and position of our day in the week
*/
if (sunday_first_n_first_week_non_iso)
{
days+= ((weekday_b == 0) ? 0 : 7) - weekday_b +
(week_number - 1) * 7 +
weekday % 7;
}
else
{
days+= ((weekday_b <= 3) ? 0 : 7) - weekday_b +
(week_number - 1) * 7 +
(weekday - 1);
}
if (days <= 0 || days > MAX_DAY_NUMBER)
goto err;
get_date_from_daynr(days,&l_time->year,&l_time->month,&l_time->day);
}
if (l_time->month > 12 || l_time->day > 31 || l_time->hour > 23 ||
l_time->minute > 59 || l_time->second > 59)
goto err;
if (val != val_end)
{
do
{
if (!my_isspace(&my_charset_latin1,*val))
{
// TS-TODO: extract_date_time is not UCS2 safe
make_truncated_value_warning(ErrConvString(val_begin, length),
cached_timestamp_type);
break;
}
} while (++val != val_end);
}
DBUG_RETURN(0);
err:
{
char buff[128];
strmake(buff, val_begin, min<size_t>(length, sizeof(buff)-1));
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
date_time_type, buff, "str_to_date");
}
DBUG_RETURN(1);
}