Parse JSON ISO8601 date in C++/CX

2019-07-02 01:08发布

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条回答
老娘就宠你
2楼-- · 2019-07-02 01:44

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?

查看更多
登录 后发表回答