I'm trying to convert a VARIANT from VT_DATE to an invariant VT_BSTR. The following code works on Windows XP:
VARIANT va;
::VariantInit(&va);
// set the variant to VT_DATE
SYSTEMTIME st;
memset(&st, 0, sizeof(SYSTEMTIME));
st.wYear = 2008;
st.wMonth = 9;
st.wDay = 22;
st.wHour = 12;
st.wMinute = 30;
DATE date;
SystemTimeToVariantTime(&st, &date);
va.vt = VT_DATE;
va.date = date;
// change to a string
err = ::VariantChangeTypeEx(&va,
&va,
LOCALE_INVARIANT,
0,
VT_BSTR);
But on PPC 2003 and Windows Mobile 5, the above code returns E_FAIL. Can someone correct the above code or provide an alternative?
EDIT: After converting the date to a string, I'm using the string to do a SQL update. I want the update to work regardless of the device's regional settings, so that's why I'm trying to convert it to an "invariant" format.
I'm now using the following to convert the date to a format that appears to work:
err = ::VariantTimeToSystemTime(va.date, &time);
if (FAILED(err))
goto cleanup;
err = strDate.PrintF(_T("%04d-%02d-%02d %02d:%02d:%02d.%03d"),
time.wYear,
time.wMonth,
time.wDay,
time.wHour,
time.wMinute,
time.wSecond,
time.wMilliseconds);