如何在Metro(C ++ / CX)应用程序中解析日期? [英] How do I parse a date in a Metro (C++/CX) app?
问题描述
WinRT的C ++ / CX投影与Managed(C#/ VB)最主要的方法之一是基本类型的预测(例如 Point
, Size
String
和 DateTime
)。
管理投影将这些类型作为.NET类型(具有BCL的所有底层支持)进行投射,而C ++投影通常最低限度地将这些类型投入到interop中,期望用户依靠C ++库支持更高级的功能。
所以,在.NET中,一个32位整数的整数变成一个 System.Int32
(其相关的 .Parse在C ++中,您将获得一个常规的C ++
int
,并且预计将使用CRT功能( _wtoi
)来完成类似的任务。这种差异往往会导致不同预测之间的特征差距,其中一个更为痛苦的是处理 DateTime
结构(其中有非常丰富的支持BCL)
我设法得到的解决方案是从 COleDateTime
类开始(由include ATLComTime.h)和去 COleDateTime
- > SYSTEMTIME
- > FILETIME
- > _ULARGE_INTEGER
- > Windows :: Foundation :: DateTime
。这是严肃的体操,但是, COleDateTime
类具有您需要的语言特定解析功能。
LCID lcid = LocaleNameToLCID(Les-es,LOCALE_ALLOW_NEUTRAL_NAMES); //解析语言标签以获取区域设置ID
COleDateTime dt;
dt.ParseDateTime(L12 enero,2012 10:00:01,0,lcid); //基于语言解析日期字符串
//获取系统时间结构
SYSTEMTIME st;
dt.GetAsSystemTime(st);
// COleDateTime在本地时区,DateTime是UTC,所以我们需要转换
SYSTEMTIME st_utc;
TzSpecificLocalTimeToSystemTime(nullptr,& st,& st_utc);
//获取文件时间结构以获取与DateTime兼容的时间格式
FILETIME ft;
SystemTimeToFileTime(& st_utc,& ft);
//使用_ULARGE_INTEGER获取一个uint64来将DateTime结构设置为
_ULARGE_INTEGER ulint = {ft.dwLowDateTime,ft.dwHighDateTime};
Windows :: Foundation :: DateTime wfdt;
wfdt.UniversalTime = ulint.QuadPart;
我已经询问了 DateTimeFormatter
类,文档不正确;它不支持解析,而不是(仅格式化)。
I have a C++/CX app that is processing some data from a file. It has a string in there representing the culture that was used to save the dates, and it has some dates. I need to convert them from strings to Platform::DateTime. I have heard that Windows::Globalization::DateTimeFormatting is the class to use, but I don't see how to use it for that. Does anyone have an example?
The C++/CX projection of WinRT differs from the Managed (C#/VB) projection in a number of ways, and one of the most major is in the projection of fundamental types (such as Point
, Size
, String
, and DateTime
).
The managed projection projects these types as .NET types (with all the underlying support of the BCL) while the C++ projection generally minimally projects these types for interop, expecting the user to rely on C++ library support for more advanced functionality.
So, where in .NET a signed 32-bit integer becomes a System.Int32
(with its relevant .Parse
functionality) in C++ you get a regular C++ int
and are expected to use CRT functionality (_wtoi
) to accomplish a similar task. This difference often results in a 'feature gap' between the different projections, one of the more painful of which is in dealing with the DateTime
structure (which has very rich support in the BCL).
The solution I've managed to get was to start with the COleDateTime
class (found by including ATLComTime.h) and going COleDateTime
->SYSTEMTIME
->FILETIME
->_ULARGE_INTEGER
->Windows::Foundation::DateTime
. It's serious gymnastics, but the COleDateTime
class has the language-specific parsing capability that you require.
LCID lcid = LocaleNameToLCID(L"es-es", LOCALE_ALLOW_NEUTRAL_NAMES); //parse language tag to get locale ID
COleDateTime dt;
dt.ParseDateTime(L"12 enero, 2012 10:00:01", 0, lcid); //parse date string based on language
//get system time struct
SYSTEMTIME st;
dt.GetAsSystemTime(st);
//COleDateTime is in local timezone, DateTime is in UTC, so we need to convert
SYSTEMTIME st_utc;
TzSpecificLocalTimeToSystemTime(nullptr, &st, &st_utc);
//get filetime struct to get a time format compatible with DateTime
FILETIME ft;
SystemTimeToFileTime(&st_utc, &ft);
//use _ULARGE_INTEGER to get a uint64 to set the DateTime struct to
_ULARGE_INTEGER ulint = {ft.dwLowDateTime, ft.dwHighDateTime};
Windows::Foundation::DateTime wfdt;
wfdt.UniversalTime = ulint.QuadPart;
I've asked around about the DateTimeFormatter
class, and the documentation is incorrect; it does not support parsing and is not intended to (only formatting).
这篇关于如何在Metro(C ++ / CX)应用程序中解析日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!