Bill Medland (medbi01@accpac.com) Correct OLE_GetFormatW so that GetDateFormatW actually works. Index: wine/ole/ole2nls.c =================================================================== RCS file: /home/wine/wine/ole/ole2nls.c,v retrieving revision 1.105 diff -u -r1.105 ole2nls.c --- wine/ole/ole2nls.c 3 Oct 2002 22:57:55 -0000 1.105 +++ wine/ole/ole2nls.c 24 Oct 2002 01:55:51 -0000 @@ -1572,11 +1572,13 @@ /*************************************************************************** *** * OLE_GetFormatW [INTERNAL] + * + * dateformat is set TRUE if being called for a date, false for a time */ static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags, const SYSTEMTIME* xtime, LPCWSTR format, - LPWSTR output, INT outlen) + LPWSTR output, INT outlen, int dateformat) { INT inpos, outpos; int count, type=0, inquote; @@ -1587,7 +1589,6 @@ WCHAR arg0[] = {0}, arg1[] = {'%','d',0}; WCHAR arg2[] = {'%','0','2','d',0}; WCHAR *argarr[3]; - int datevars=0, timevars=0; argarr[0] = arg0; argarr[1] = arg1; @@ -1635,20 +1636,11 @@ break; } } - } else if ( (count && (format[inpos] != type)) - || ( (count==4 && type =='y') || - (count==4 && type =='M') || - (count==4 && type =='d') || - (count==2 && type =='g') || - (count==2 && type =='h') || - (count==2 && type =='H') || - (count==2 && type =='m') || - (count==2 && type =='s') || - (count==2 && type =='t') ) ) { + } else if ((count && (format[inpos] != type))) { switch(type) { case 'd': - if (count == 4) { + if (count >= 4) { GetLocaleInfoW(locale, LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7, buf, sizeof(buf)/sizeof(WCHAR) ); @@ -1664,7 +1656,7 @@ break; case 'M': - if (count == 4) { + if (count >= 4) { GetLocaleInfoW(locale, LOCALE_SMONTHNAME1 + xtime->wMonth -1, buf, sizeof(buf)/sizeof(WCHAR) ); @@ -1678,12 +1670,10 @@ } break; case 'y': - if (count == 4) { + if (count >= 4) { sprintf( tmp, "%d", xtime->wYear ); - } else if (count == 3) { - strcpy( tmp, "yyy" ); } else { - sprintf( tmp, "%.*d", count, xtime->wYear % 100 ); + sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wYear % 100 ); } MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) ); break; @@ -1701,22 +1691,22 @@ case 'h': /* hours 1:00-12:00 --- is this right? */ - sprintf( tmp, "%.*d", count, (xtime->wHour-1)%12 +1); + sprintf( tmp, "%.*d", count > 2 ? 2 : count, (xtime->wHour-1)%12 +1); MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) ); break; case 'H': - sprintf( tmp, "%.*d", count, xtime->wHour ); + sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wHour ); MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) ); break; case 'm': - sprintf( tmp, "%.*d", count, xtime->wMinute ); + sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wMinute ); MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) ); break; case 's': - sprintf( tmp, "%.*d", count, xtime->wSecond ); + sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wSecond ); MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) ); break; @@ -1735,7 +1725,7 @@ inpos--; /* cat buf onto the output */ - outlen = strlenW(buf); + buflen = strlenW(buf); if (outpos + buflen < outlen) { strcpyW( output + outpos, buf ); outpos += buflen; @@ -1757,22 +1747,19 @@ } else if (count) { /* how we keep track of the middle of a format spec */ count++; - continue; - } else if ( (datevars && (format[inpos]=='d' || + } else if ( (dateformat && (format[inpos]=='d' || format[inpos]=='M' || format[inpos]=='y' || format[inpos]=='g') ) || - (timevars && (format[inpos]=='H' || + (!dateformat && (format[inpos]=='H' || format[inpos]=='h' || format[inpos]=='m' || format[inpos]=='s' || format[inpos]=='t') ) ) { type = format[inpos]; count = 1; - continue; } else if (format[inpos] == '\'') { inquote = 1; - continue; } else { /* unquoted literals */ output[outpos++] = format[inpos]; @@ -1910,6 +1897,17 @@ TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n", locale,flags,xtime,debugstr_w(format),date,datelen); + /* Tests (could be left until OLE_GetFormatW) */ + if (flags && format) + { + SetLastError (ERROR_INVALID_FLAGS); + return 0; + } + if (datelen && !date) + { + SetLastError (ERROR_INVALID_PARAMETER); + return 0; + } if (!locale) { locale = LOCALE_SYSTEM_DEFAULT; }; @@ -1950,7 +1948,7 @@ ret = OLE_GetFormatW(thislocale, flags, 0, thistime, thisformat, - date, datelen); + date, datelen, 1); TRACE("GetDateFormatW() returning %d, with data=%s\n", @@ -3067,7 +3065,7 @@ } ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat, - timestr, timelen); + timestr, timelen, 0); return ret; } Index: wine/dlls/kernel/tests/locale.c =================================================================== RCS file: /home/wine/wine/dlls/kernel/tests/locale.c,v retrieving revision 1.4 diff -u -r1.4 locale.c --- wine/dlls/kernel/tests/locale.c 4 Jun 2002 22:12:50 -0000 1.4 +++ wine/dlls/kernel/tests/locale.c 24 Oct 2002 01:58:30 -0000 @@ -30,6 +30,7 @@ #define BUFFER_SIZE 50 /* Buffer used by callback function */ char GlobalBuffer[BUFFER_SIZE]; +#define COUNTOF(x) (sizeof(x)/sizeof(x)[0]) /* TODO : * Unicode versions @@ -183,6 +184,58 @@ eq (ret, 0, "GetDateFormat with len=2", "%d"); } +void TestGetDateFormatW() +{ + int ret, error, cmp; + SYSTEMTIME curtime; + WCHAR buffer[BUFFER_SIZE], format[BUFFER_SIZE], Expected[BUFFER_SIZE]; + LCID lcid; + + lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT ); + + /* 1. Error cases */ + + /* 1a If flags is not zero then format must be null. */ + ret = GetDateFormatW (LOCALE_SYSTEM_DEFAULT, DATE_LONGDATE, NULL, format, buffer, sizeof(buffer)/sizeof(buffer[0])); + error = ret ? 0 : GetLastError(); + ok (ret == 0 && error == ERROR_INVALID_FLAGS, "GetDateFormatW allowed flags and format"); + + /* 1b The buffer can only be null if the count is zero */ + /* For the record other bad pointers result in a page fault (Win98) */ + ret = GetDateFormatW (LOCALE_SYSTEM_DEFAULT, 0, NULL, format, NULL, sizeof(buffer)/sizeof(buffer[0])); + error = ret ? 0 : GetLastError(); + ok (ret == 0 && error == ERROR_INVALID_PARAMETER, "GetDateFormatW did not detect null buffer pointer."); + ret = GetDateFormatW (LOCALE_SYSTEM_DEFAULT, 0, NULL, format, NULL, 0); + error = ret ? 0 : GetLastError(); + ok (ret != 0 && error == 0, "GetDateFormatW did not permit null buffer pointer when counting."); + + /* 1c An incorrect day of week is corrected. */ + curtime.wYear = 2002; + curtime.wMonth = 10; + curtime.wDay = 23; + curtime.wDayOfWeek = 5; /* should be 3 - Wednesday */ + curtime.wHour = 0; + curtime.wMinute = 0; + curtime.wSecond = 0; + curtime.wMilliseconds = 234; + MultiByteToWideChar (CP_ACP, 0, "dddd d MMMM yyyy", -1, format, COUNTOF(format)); + ret = GetDateFormatW (lcid, 0, &curtime, format, buffer, COUNTOF(buffer)); + error = ret ? 0 : GetLastError(); + MultiByteToWideChar (CP_ACP, 0, "Wednesday 23 October 2002", -1, Expected, COUNTOF(Expected)); + cmp = ret ? lstrcmpW (buffer, Expected) : 2; + ok (ret != 0 && error == 0 && cmp == 0, "Day of week correction failed\n"); + + /* 1d Invalid year, month or day results in error */ + + /* 1e Insufficient space results in error */ + + /* 2. Standard behaviour */ + /* 1c is a reasonable test */ + + /* 3. Replicated undocumented behaviour */ + /* e.g. unexepected characters are retained. */ +} + void TestGetCurrencyFormat() { @@ -343,6 +396,7 @@ TestGetLocaleInfoA(); TestGetTimeFormatA(); TestGetDateFormatA(); + TestGetDateFormatW(); TestGetNumberFormat(); TestGetCurrencyFormat(); TestCompareStringA();