mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4683 clang-format on all files in ApplicationCode
This commit is contained in:
@@ -1,66 +1,52 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017 Statoil ASA
|
||||
//
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RiaDateStringParser.h"
|
||||
|
||||
#include "RiaStdStringTools.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
#include "RiaStdStringTools.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
const std::string MONTH_NAMES[] =
|
||||
{
|
||||
"january",
|
||||
"february",
|
||||
"march",
|
||||
"april",
|
||||
"may",
|
||||
"june",
|
||||
"july",
|
||||
"august",
|
||||
"september",
|
||||
"october",
|
||||
"november",
|
||||
"december"
|
||||
};
|
||||
{"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaDateStringParser::parseDateString(const QString& dateString)
|
||||
QDateTime RiaDateStringParser::parseDateString( const QString& dateString )
|
||||
{
|
||||
return RiaDateStringParser::parseDateString(dateString.toStdString());
|
||||
return RiaDateStringParser::parseDateString( dateString.toStdString() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaDateStringParser::parseDateString(const std::string& dateString)
|
||||
QDateTime RiaDateStringParser::parseDateString( const std::string& dateString )
|
||||
{
|
||||
int year, month, day;
|
||||
bool parsedOk =
|
||||
tryParseYearFirst(dateString, year, month, day) ||
|
||||
tryParseDayFirst(dateString, year, month, day) ||
|
||||
tryParseMonthFirst(dateString, year, month, day);
|
||||
int year, month, day;
|
||||
bool parsedOk = tryParseYearFirst( dateString, year, month, day ) ||
|
||||
tryParseDayFirst( dateString, year, month, day ) ||
|
||||
tryParseMonthFirst( dateString, year, month, day );
|
||||
|
||||
QDateTime dt;
|
||||
dt.setTimeSpec(RiaQDateTimeTools::currentTimeSpec());
|
||||
if (parsedOk) dt.setDate(QDate(year, month, day));
|
||||
dt.setTimeSpec( RiaQDateTimeTools::currentTimeSpec() );
|
||||
if ( parsedOk ) dt.setDate( QDate( year, month, day ) );
|
||||
|
||||
return dt;
|
||||
}
|
||||
@@ -77,25 +63,25 @@ QDateTime RiaDateStringParser::parseDateString(const std::string& dateString)
|
||||
/// 'yyyy.MMM.dd'
|
||||
/// MMM is month name (shortened)
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseYearFirst(const std::string& s, int& year, int& month, int& day)
|
||||
bool RiaDateStringParser::tryParseYearFirst( const std::string& s, int& year, int& month, int& day )
|
||||
{
|
||||
auto firstSep = s.find_first_of(" -_.");
|
||||
auto lastSep = s.find_first_of(" -_.", firstSep + 1);
|
||||
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;
|
||||
if ( firstSep == std::string::npos || lastSep == std::string::npos ) return false;
|
||||
|
||||
auto sYear = s.substr(0, firstSep);
|
||||
auto sMonth = s.substr(firstSep + 1, lastSep - firstSep - 1);
|
||||
auto sDay = s.substr(lastSep + 1);
|
||||
auto sYear = s.substr( 0, firstSep );
|
||||
auto sMonth = s.substr( firstSep + 1, lastSep - firstSep - 1 );
|
||||
auto sDay = s.substr( lastSep + 1 );
|
||||
|
||||
return tryParseYear(sYear, year) && tryParseMonth(sMonth, month) && tryParseDay(sDay, day);
|
||||
return tryParseYear( sYear, year ) && tryParseMonth( sMonth, month ) && tryParseDay( sDay, day );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Try parse formats
|
||||
/// 'dd mm yyyy'
|
||||
/// 'dd mm yyyy'
|
||||
/// 'dd MMM yyyy'
|
||||
/// 'dd_mm_yyyy'
|
||||
/// 'dd_mm_yyyy'
|
||||
/// 'dd_MMM_yyyy'
|
||||
/// 'dd-mm-yyyy'
|
||||
/// 'dd-MMM-yyyy'
|
||||
@@ -103,47 +89,47 @@ bool RiaDateStringParser::tryParseYearFirst(const std::string& s, int& year, int
|
||||
/// 'dd.MMM.yyyy'
|
||||
/// MMM is month name (shortened)
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseDayFirst(const std::string& s, int& year, int& month, int& day)
|
||||
bool RiaDateStringParser::tryParseDayFirst( const std::string& s, int& year, int& month, int& day )
|
||||
{
|
||||
auto firstSep = s.find_first_of(" -_.");
|
||||
auto lastSep = s.find_first_of(" -_.", firstSep + 1);
|
||||
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;
|
||||
if ( firstSep == std::string::npos || lastSep == std::string::npos ) return false;
|
||||
|
||||
auto sDay = s.substr(0, firstSep);
|
||||
auto sMonth = s.substr(firstSep + 1, lastSep - firstSep - 1);
|
||||
auto sYear = s.substr(lastSep + 1);
|
||||
auto sDay = s.substr( 0, firstSep );
|
||||
auto sMonth = s.substr( firstSep + 1, lastSep - firstSep - 1 );
|
||||
auto sYear = s.substr( lastSep + 1 );
|
||||
|
||||
return tryParseYear(sYear, year) && tryParseMonth(sMonth, month) && tryParseDay(sDay, day);
|
||||
return tryParseYear( sYear, year ) && tryParseMonth( sMonth, month ) && tryParseDay( sDay, day );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseMonthFirst(const std::string& s, int& year, int& month, int& 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);
|
||||
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;
|
||||
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);
|
||||
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);
|
||||
return tryParseYear( sYear, year ) && tryParseMonth( sMonth, month ) && tryParseDay( sDay, day );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseYear(const std::string& s, int &year)
|
||||
bool RiaDateStringParser::tryParseYear( const std::string& s, int& year )
|
||||
{
|
||||
if (RiaStdStringTools::containsAlphabetic(s)) return false;
|
||||
if ( RiaStdStringTools::containsAlphabetic( s ) ) return false;
|
||||
|
||||
auto today = QDate::currentDate();
|
||||
int y = RiaStdStringTools::toInt(s);
|
||||
if (y > 1970 && y <= today.year())
|
||||
int y = RiaStdStringTools::toInt( s );
|
||||
if ( y > 1970 && y <= today.year() )
|
||||
{
|
||||
year = y;
|
||||
|
||||
@@ -154,20 +140,21 @@ bool RiaDateStringParser::tryParseYear(const std::string& s, int &year)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
|
||||
bool RiaDateStringParser::tryParseMonth( const std::string& s, int& month )
|
||||
{
|
||||
if (RiaStdStringTools::containsAlphabetic(s))
|
||||
if ( RiaStdStringTools::containsAlphabetic( s ) )
|
||||
{
|
||||
auto sMonth = s;
|
||||
sMonth = trimString(sMonth);
|
||||
std::transform(sMonth.begin(), sMonth.end(), sMonth.begin(),
|
||||
[](const char c) -> char { return (char)::tolower(c); });
|
||||
sMonth = trimString( sMonth );
|
||||
std::transform( sMonth.begin(), sMonth.end(), sMonth.begin(), []( const char c ) -> char {
|
||||
return (char)::tolower( c );
|
||||
} );
|
||||
|
||||
for (int i = 0; i < 12; i++)
|
||||
for ( int i = 0; i < 12; i++ )
|
||||
{
|
||||
if (MONTH_NAMES[i].compare(0, sMonth.size(), sMonth) == 0)
|
||||
if ( MONTH_NAMES[i].compare( 0, sMonth.size(), sMonth ) == 0 )
|
||||
{
|
||||
month = i + 1;
|
||||
return true;
|
||||
@@ -176,11 +163,11 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
|
||||
}
|
||||
else
|
||||
{
|
||||
int m = RiaStdStringTools::toInt(s);
|
||||
if (m >= 1 && m <= 12)
|
||||
int m = RiaStdStringTools::toInt( s );
|
||||
if ( m >= 1 && m <= 12 )
|
||||
{
|
||||
month = m;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -189,14 +176,14 @@ bool RiaDateStringParser::tryParseMonth(const std::string& s, int &month)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaDateStringParser::tryParseDay(const std::string& s, int &day)
|
||||
bool RiaDateStringParser::tryParseDay( const std::string& s, int& day )
|
||||
{
|
||||
if (RiaStdStringTools::containsAlphabetic(s)) return false;
|
||||
if ( RiaStdStringTools::containsAlphabetic( s ) ) return false;
|
||||
|
||||
int d = RiaStdStringTools::toInt(s);
|
||||
if (d >= 1 && d <= 31)
|
||||
int d = RiaStdStringTools::toInt( s );
|
||||
if ( d >= 1 && d <= 31 )
|
||||
{
|
||||
day = d;
|
||||
|
||||
@@ -207,11 +194,11 @@ bool RiaDateStringParser::tryParseDay(const std::string& s, int &day)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::string RiaDateStringParser::trimString(const std::string& s)
|
||||
std::string RiaDateStringParser::trimString( const std::string& s )
|
||||
{
|
||||
auto sCopy = s.substr(0, s.find_last_not_of(' ') + 1);
|
||||
sCopy = sCopy.substr(sCopy.find_first_not_of(' '));
|
||||
auto sCopy = s.substr( 0, s.find_last_not_of( ' ' ) + 1 );
|
||||
sCopy = sCopy.substr( sCopy.find_first_not_of( ' ' ) );
|
||||
return sCopy;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user