0

我正在尝试将 VARIANT 从 VT_DATE 转换为不变的VT_BSTR。以下代码适用于 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);

但在 PPC 2003 和 Windows Mobile 5 上,上述代码返回 E_FAIL。有人可以更正上述代码或提供替代方法吗?

编辑:将日期转换为字符串后,我使用字符串进行 SQL 更新。无论设备的区域设置如何,我都希望更新能够正常工作,这就是我尝试将其转换为“不变”格式的原因。

我现在使用以下方法将日期转换为似乎可以使用的格式:

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);
4

3 回答 3

1

(I'm sorry it's taken me a while to respond ('work', you know...))

I don't see anything wrong with the code, from the COM point of view.

Maybe the problem is with LOCALE_INVARIANT. It was introduced with Windows XP; maybe it's not supported in the Windows CE family?

Try changing the locale to LOCALE_USER_DEFAULT and check to see if you still get an error. Most of the time this would be the most appropriate locale anyway; especially if you are trying to display the value to the user.

If you truly need a specific format because you need to pass the value somewhere else that will parse it, try using a specific locale that matches your requirements; perhaps en_US.

Please let us know how it goes.

于 2008-09-23T15:32:35.593 回答
1

这不是一个真正的答案,但是将日期更改为字符串不是区域设置不变的任务 - 它高度依赖于区域设置。在这种情况下,我会将 Variant Time 转换为 System Time,然后使用 sprintf 样式的函数将其转换为字符串

于 2008-09-23T03:33:16.353 回答
0

在您的上下文中不确定,但似乎您走错了路。为什么不使用VarBstrFromDate?这 aloows 使用一个语言环境(或可选地忽略一个),并且可能更接近你想要的。

于 2008-09-23T13:31:00.580 回答