I am trying to parse a date-time string and put the result into a std::tm structure. Below is the code,
#include <iomanip>
#include <ctime>
#include <sstream>
#include <string>
std::stringstream ss;
struct std::tm when;
ss.str("8/14/2015 3:04:23 PM");
ss >> std::get_time(&when, "%m/%d/%Y %r");
After run the code, the when.tm_hour is 27. Is this a bug, or I did something wrong?
I am using Visual Studio 2013 on windows 7.
Thanks.
You are running into a bug in Microsoft's implementation of the std::num_get::do_get
function, specifically the section which parses the AM/PM (%p
) part of the time:
case 'p':
_Ans = _Getloctxt(_First, _Last, (size_t)0, ":AM:am:PM:pm");
if (_Ans < 0)
_State |= ios_base::failbit;
else
_Pt->tm_hour += _Ans * 12;
break;
The problem is that _Getloctxt returns an int in the range [0,3] and not in the expected range [0,1].
This bug has been reported to Microsoft (ID:808162) who claim to have fixed it in Visual Studio 2015.
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