1

I have a COleDateTime object and I want to parse a date string in the format YYYY-MM-DD.

The string variable, for example, is:

std::string strDate = "2022-07-04";

COleDateTime allows me to use ParseDateTime to parse a string, but I see no way to tell it what format the components of the date string are. In C# I can do such with DateTime.Parse....

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

3 Answers3

2

Why not just a simple formatted input.

std::stringstream ss(strDate);
int year, month, day;
char dash;
ss >> year >> dash >> month >> dash >> day;
COleDateTime(year, month, day, 0, 0, 0);
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
2

Based on the suggestion by @xMRi in the comments I have decided to use:

CString strDutyMeetingDate = CString(tinyxml2::attribute_value(pDutyWeek, "Date").c_str());
int iDay{}, iMonth{}, iYear{};
if(_stscanf_s(strDutyMeetingDate, L"%d-%d-%d", &iYear, &iMonth, &iDay) == 3)
{
    const auto datDutyMeetingDate = COleDateTime(iYear, iMonth, iDay, 0, 0, 0);
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
1

COleDateTime::ParseDateTime uses default parameter LANG_USER_DEFAULT, it can be called as

COleDateTime dt;
dt.ParseDateTime("2022-07-04");

Or

dt.ParseDateTime("2022-07-04", VAR_DATEVALUEONLY, LANG_USER_DEFAULT);

"2022-07-04" uses long date format so it should be safe, because it is clear that the year is at the start, and month is expected to be in the middle. I believe any LCID should return 2022-July-4th (I am 60% sure!)

If the date string was short, it could get confused with MM/DD/YY format, but that's not a problem here.

To make the lcid manually, see the English-US example below, although it should not be necessary in this case.

LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Thanks for your answer. I don't doubt you when you say I can just use the string verbatim. But, since the application supports 50+ languages, and you are only 60% sure ... I have left it as I coded it. – Andrew Truckle Jul 05 '22 at 07:48