Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is this a bug in std::get_time?

Tags:

c++

c++11

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.

like image 575
Benjamin Xu Avatar asked Aug 14 '15 21:08

Benjamin Xu


1 Answers

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.

like image 192
andypea Avatar answered Oct 19 '22 02:10

andypea