开发者

Problem with strptime() - %p is not taken into account

I am trying to convert a date in a particular format using strptime, and i realized that the information about AM/PM is los开发者_运维问答t. Not sure why.

Here is the code.

struct tm t;
strptime("Wed 4/18/2007 4:28:22 PM", "%a %m/%d/%Y %H:%M:%S %p", &t);
std::cout<<t.tm_hour<<endl;
strptime("Wed 4/18/2007 4:28:22 AM", "%a %m/%d/%Y %H:%M:%S %p", &t);
std::cout<<t.tm_hour<<endl;

Can anybody tell me whats the purpose of having the %p specifier in strptime?

Thanks in advance, AJ


The problem here is with %H, which will read the hour in 24-hour format and ignore AM/PM. If you want to read the hour in 12-hour format and make use of AM/PM use %I in place of %H.

You can refer to the manual here.


From this site:

%p = The locale's equivalent of a.m or p.m.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜