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?
Date.parse() The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
try { dateValue = DateTime. Parse(dateString, new CultureInfo("fr-FR", false)); Console. WriteLine("'{0}' converted to {1}.", dateString, dateValue); } catch (FormatException) { Console. WriteLine("Unable to convert '{0}'.", dateString); } // Parse string with date but no time component.
Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With