msvcrt: Correct behavior of time in strftime formats %X %c.

Signed-off-by: Jeff Smith <whydoubt@gmail.com>
Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Jeff Smith 2019-11-21 07:04:22 -06:00 committed by Alexandre Julliard
parent 96b6e1d77c
commit 6f13ac30b9
3 changed files with 69 additions and 69 deletions

View File

@ -667,27 +667,26 @@ static void test_strftime(void)
} tests_td[] = {
{ "%c", "x z", "x", "y", "z", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%#c", "y z", "x", "y", "z", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "m1", 0, 0, "MMM", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "m1", 0, 0, "MMM", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "1", 0, 0, "h", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "01", 0, 0, "hh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "h01", 0, 0, "hhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "hh01", 0, 0, "hhhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "h01", 0, 0, "hhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "hh01", 0, 0, "hhhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "1", 0, 0, "H", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "01", 0, 0, "HH", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "H13", 0, 0, "HHH", { 0, 0, 13, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "H13", 0, 0, "HHH", { 0, 0, 13, 1, 0, 70, 0, 0, 0 }},
{ "%X", "0", 0, 0, "m", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "00", 0, 0, "mm", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "m00", 0, 0, "mmm", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "0", 0, 0, "s", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "00", 0, 0, "ss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "s00", 0, 0, "sss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "t", 0, 0, "t", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "tt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "ttttttttt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "a", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "aaaaa", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "A", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "tam", 0, 0, "AAAAA", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "s00", 0, 0, "sss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "t", 0, 0, "t", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "tt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "ttttttttt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "a", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "aaaaa", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "A", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "tam", 0, 0, "AAAAA", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "1", "d", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "01", "dd", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "d1", "ddd", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},

View File

@ -956,53 +956,12 @@ char ** CDECL __p__tzname(void)
#if _MSVCR_VER <= 90
#define STRFTIME_CHAR char
#define STRFTIME_FUNC(name) name ## A
#define STRFTIME_TD(td, name) td->str.names.name
#else
#define STRFTIME_CHAR MSVCRT_wchar_t
#define STRFTIME_FUNC(name) name ## W
#define STRFTIME_TD(td, name) td->wstr.names.name
#endif
static inline BOOL strftime_time(STRFTIME_CHAR *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
{
SYSTEMTIME st;
MSVCRT_size_t ret;
LCID lcid;
st.wYear = mstm->tm_year + 1900;
st.wMonth = mstm->tm_mon + 1;
st.wDayOfWeek = mstm->tm_wday;
st.wDay = mstm->tm_mday;
st.wHour = mstm->tm_hour;
st.wMinute = mstm->tm_min;
st.wSecond = mstm->tm_sec;
st.wMilliseconds = 0;
#if _MSVCR_VER < 110
lcid = time_data->lcid;
#else
lcid = LocaleNameToLCID(time_data->locname, 0);
#endif
ret = STRFTIME_FUNC(GetTimeFormat)(lcid, 0, &st, STRFTIME_TD(time_data, time), NULL, 0);
if(ret && ret<max-*pos)
ret = STRFTIME_FUNC(GetTimeFormat)(lcid, 0, &st, STRFTIME_TD(time_data, time),
str+*pos, max-*pos);
if(!ret) {
*str = 0;
*MSVCRT__errno() = MSVCRT_EINVAL;
return FALSE;
}else if(ret > max-*pos) {
*str = 0;
*MSVCRT__errno() = MSVCRT_ERANGE;
return FALSE;
}
*pos += ret-1;
return TRUE;
}
#define strftime_str(a,b,c,d) strftime_nstr(a,b,c,d,MSVCRT_SIZE_MAX)
static inline BOOL strftime_nstr(STRFTIME_CHAR *str, MSVCRT_size_t *pos,
MSVCRT_size_t max, const STRFTIME_CHAR *src, MSVCRT_size_t len)
@ -1154,6 +1113,48 @@ static inline BOOL strftime_format(STRFTIME_CHAR *str, MSVCRT_size_t *pos, MSVCR
break;
}
break;
case 'h':
if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
{
*str = 0;
return FALSE;
}
if(count > 2)
ret = strftime_nstr(str, pos, max, format, count-2);
if(ret)
ret = strftime_int(str, pos, max, (mstm->tm_hour + 11) % 12 + 1,
count == 1 ? 0 : 2, 1, 12);
break;
case 'H':
if(count > 2)
ret = strftime_nstr(str, pos, max, format, count-2);
if(ret)
ret = strftime_int(str, pos, max, mstm->tm_hour, count == 1 ? 0 : 2, 0, 23);
break;
case 'm':
if(count > 2)
ret = strftime_nstr(str, pos, max, format, count-2);
if(ret)
ret = strftime_int(str, pos, max, mstm->tm_min, count == 1 ? 0 : 2, 0, 59);
break;
case 's':
if(count > 2)
ret = strftime_nstr(str, pos, max, format, count-2);
if(ret)
ret = strftime_int(str, pos, max, mstm->tm_sec, count == 1 ? 0 : 2, 0, 59);
break;
case 'a':
case 'A':
case 't':
if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
{
*str = 0;
return FALSE;
}
ret = strftime_nstr(str, pos, max,
mstm->tm_hour < 12 ? STRFTIME_TD(time_data, am) : STRFTIME_TD(time_data, pm),
(*format == 't' && count == 1) ? 1 : MSVCRT_SIZE_MAX);
break;
default:
ret = strftime_nstr(str, pos, max, format, count);
break;
@ -1230,7 +1231,7 @@ static MSVCRT_size_t strftime_impl(STRFTIME_CHAR *str, MSVCRT_size_t max,
return 0;
if(ret < max)
str[ret++] = ' ';
if(!strftime_time(str, &ret, max, mstm, time_data))
if(!strftime_format(str, &ret, max, mstm, time_data, STRFTIME_TD(time_data, time)))
return 0;
break;
case 'x':
@ -1239,7 +1240,7 @@ static MSVCRT_size_t strftime_impl(STRFTIME_CHAR *str, MSVCRT_size_t max,
return 0;
break;
case 'X':
if(!strftime_time(str, &ret, max, mstm, time_data))
if(!strftime_format(str, &ret, max, mstm, time_data, STRFTIME_TD(time_data, time)))
return 0;
break;
case 'a':

View File

@ -1018,27 +1018,27 @@ static void test_strftime(void)
} tests_td[] = {
{ "%c", "x z", L"x", L"y", L"z", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%#c", "y z", L"x", L"y", L"z", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "M1", 0, 0, L"MMM", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "M1", 0, 0, L"MMM", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "1", 0, 0, L"h", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "01", 0, 0, L"hh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "h01", 0, 0, L"hhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "hh01", 0, 0, L"hhhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "h01", 0, 0, L"hhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "hh01", 0, 0, L"hhhh", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "1", 0, 0, L"H", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "01", 0, 0, L"HH", { 0, 0, 1, 1, 0, 70, 0, 0, 0 }},
{ "%X", "H13", 0, 0, L"HHH", { 0, 0, 13, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "H13", 0, 0, L"HHH", { 0, 0, 13, 1, 0, 70, 0, 0, 0 }},
{ "%X", "0", 0, 0, L"m", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "00", 0, 0, L"mm", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "m00", 0, 0, L"mmm", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "m00", 0, 0, L"mmm", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "0", 0, 0, L"s", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "00", 0, 0, L"ss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "s00", 0, 0, L"sss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "T", 0, 0, L"t", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"tt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"ttttttttt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"a", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"aaaaa", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"A", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "TAM", 0, 0, L"AAAAA", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }, TRUE},
{ "%X", "s00", 0, 0, L"sss", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "T", 0, 0, L"t", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"tt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"ttttttttt", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"a", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"aaaaa", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"A", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%X", "TAM", 0, 0, L"AAAAA", { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "1", L"d", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "01", L"dd", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},
{ "%x", "D1", L"ddd", 0, 0, { 0, 0, 0, 1, 0, 70, 0, 0, 0 }},