DateStringParser. Handle month-first when date is bigger than 12

This commit is contained in:
Bjørn Erik Jensen
2017-10-24 11:28:00 +02:00
parent a35329555b
commit 3cd9333fd5
2 changed files with 20 additions and 1 deletions

View File

@@ -46,7 +46,8 @@ QDateTime RiaDateStringParser::parseDateString(const QString& dateString)
std::string s = dateString.toStdString();
bool parsedOk =
tryParseYearFirst(s, year, month, day) ||
tryParseDayFirst(s, year, month, day);
tryParseDayFirst(s, year, month, day) ||
tryParseMonthFirst(s, year, month, day);
QDateTime dt;
dt.setTimeSpec(RiaQDateTimeTools::currentTimeSpec());
@@ -106,6 +107,23 @@ bool RiaDateStringParser::tryParseDayFirst(const std::string& s, int& year, int&
return tryParseYear(sYear, year) && tryParseMonth(sMonth, month) && tryParseDay(sDay, day);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaDateStringParser::tryParseMonthFirst(const std::string& s, int& year, int& month, int& day)
{
auto firstSep = s.find_first_of(" -_.");
auto lastSep = s.find_first_of(" -_.", firstSep + 1);
if (firstSep == std::string::npos || lastSep == std::string::npos) return false;
auto sMonth = s.substr(0, firstSep);
auto sDay = s.substr(firstSep + 1, lastSep - firstSep - 1);
auto sYear = s.substr(lastSep + 1);
return tryParseYear(sYear, year) && tryParseMonth(sMonth, month) && tryParseDay(sDay, day);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -35,6 +35,7 @@ public:
private:
static bool tryParseYearFirst(const std::string& s, int& year, int& month, int& day);
static bool tryParseDayFirst(const std::string& s, int& year, int& month, int& day);
static bool tryParseMonthFirst(const std::string& s, int& year, int& month, int& day);
static bool tryParseYear(const std::string& s, int &year);
static bool tryParseMonth(const std::string& s, int &month);