问题
I have a date string coming from JSON "2012-08-01T15:42:06Z" and want to parse that in Windows Runtime. As far as I know, only COleDateTime is available to handle this.
I can only get it to correctly parse the string when I take out the 'T' & 'Z' characters, but that adds an extra parsing step on my end.
WORKS:
COleDateTime dateTime;
dateTime.ParseDateTime(L"2012-08-01 15:42:06", 0UL, 1033UL);
FAILS:
COleDateTime dateTime;
dateTime.ParseDateTime(L"2012-08-01T15:42:06Z", 0UL, 1033UL);
Anyone have any idea?
回答1:
If your date string is formatted consistently, you can use std::get_time to parse the time into a tm struct, copy the relevant bits into a SYSTEMTIME and from there convert to a FILETIME and then to Windows::Foundation::DateTime.
Info on std::get_time: http://en.cppreference.com/w/cpp/io/manip/get_time
Code for converting from SYSTEMTIME to DateTime: How do I parse a date in a Metro (C++/CX) app?
来源:https://stackoverflow.com/questions/11768099/parse-json-iso8601-date-in-c-cx