PocketPC - 将 VT_DATE 转换为不变的 VT_BSTR

发布于 2024-07-05 00:51:06 字数 1094 浏览 11 评论 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月流歌 2024-07-12 00:51:06

不确定您的上下文,但看来您可能走错了路。 为什么不使用 VarBstrFromDate? 这允许使用区域设置(或选择忽略区域设置),并且可能更接近您想要的。

Not certain in your context here, but it seems maybe you're on the wrong path. Why not use VarBstrFromDate? This aloows using a locale (or optionally ignoring one) and is probably far closer to what you want.

巴黎盛开的樱花 2024-07-12 00:51:06

(很抱歉我花了一段时间才回复(“工作”,你知道...))

从 COM 的角度来看,我没有发现代码有任何问题。

也许问题出在 LOCALE_INVARIANT 上。 它是随 Windows XP 引入的; 也许 Windows CE 系列不支持它?

尝试将区域设置更改为 LOCALE_USER_DEFAULT 并检查是否仍然出现错误。 无论如何,大多数时候这都是最合适的区域设置; 特别是当您尝试向用户显示该值时。

如果您确实需要特定格式,因为您需要将值传递到其他地方来解析它,请尝试使用符合您要求的特定区域设置; 也许 en_US。

请让我们知道进展如何。

(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.

雪花飘飘的天空 2024-07-12 00:51:06

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

This isn't really an answer, but changing a date to a string isn't a Locale-invariant task - it highly depends on the locale. In this case, I'd convert the Variant Time to System Time, then use a sprintf-style function to convert it to a string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文