mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#1962 : Observed Data : Add helper class for QDateTime and use from parsers
This commit is contained in:
parent
f3f78995fa
commit
db981e2eee
@ -16,7 +16,7 @@ ${CEE_CURRENT_LIST_DIR}RiaLogging.h
|
|||||||
${CEE_CURRENT_LIST_DIR}RiaProjectModifier.h
|
${CEE_CURRENT_LIST_DIR}RiaProjectModifier.h
|
||||||
${CEE_CURRENT_LIST_DIR}RiaRegressionTest.h
|
${CEE_CURRENT_LIST_DIR}RiaRegressionTest.h
|
||||||
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.h
|
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.h
|
||||||
${CEE_CURRENT_LIST_DIR}RiaDateTimeTools.h
|
${CEE_CURRENT_LIST_DIR}RiaQDateTimeTools.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set (SOURCE_GROUP_SOURCE_FILES
|
set (SOURCE_GROUP_SOURCE_FILES
|
||||||
@ -30,7 +30,7 @@ ${CEE_CURRENT_LIST_DIR}RiaLogging.cpp
|
|||||||
${CEE_CURRENT_LIST_DIR}RiaProjectModifier.cpp
|
${CEE_CURRENT_LIST_DIR}RiaProjectModifier.cpp
|
||||||
${CEE_CURRENT_LIST_DIR}RiaRegressionTest.cpp
|
${CEE_CURRENT_LIST_DIR}RiaRegressionTest.cpp
|
||||||
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.cpp
|
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.cpp
|
||||||
${CEE_CURRENT_LIST_DIR}RiaDateTimeTools.cpp
|
${CEE_CURRENT_LIST_DIR}RiaQDateTimeTools.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
list(APPEND CODE_HEADER_FILES
|
list(APPEND CODE_HEADER_FILES
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// 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>
|
|
||||||
// for more details.
|
|
||||||
//
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#include "RiaDateTimeTools.h"
|
|
||||||
|
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
#include <QString>
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
quint64 RiaDateTimeTools::secondsFromUnit(const std::string& unit)
|
|
||||||
{
|
|
||||||
QString str = QString::fromStdString(unit).trimmed().toUpper();
|
|
||||||
|
|
||||||
if (str == "DAYS" || str == "DAY")
|
|
||||||
{
|
|
||||||
return RiaDateTimeTools::secondsInDay();
|
|
||||||
}
|
|
||||||
else if (str == "YEARS" || str == "YEAR")
|
|
||||||
{
|
|
||||||
return RiaDateTimeTools::secondsInYear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
time_t RiaDateTimeTools::createFromSecondsSinceEpoch(quint64 secondsSinceEpoch)
|
|
||||||
{
|
|
||||||
return time_t(secondsSinceEpoch);
|
|
||||||
}
|
|
111
ApplicationCode/Application/Tools/RiaQDateTimeTools.cpp
Normal file
111
ApplicationCode/Application/Tools/RiaQDateTimeTools.cpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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>
|
||||||
|
// for more details.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#include "RiaQDateTimeTools.h"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
quint64 RiaQDateTimeTools::secondsInDay()
|
||||||
|
{
|
||||||
|
return 60 * 60 * 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
quint64 RiaQDateTimeTools::secondsInYear()
|
||||||
|
{
|
||||||
|
return 60 * 60 * 24 * 365;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QDateTime RiaQDateTimeTools::fromString(const QString& dateString, const QString& format)
|
||||||
|
{
|
||||||
|
QDateTime dt = QDateTime::fromString(dateString, format);
|
||||||
|
dt.setTimeSpec(Qt::UTC);
|
||||||
|
|
||||||
|
return dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QDateTime RiaQDateTimeTools::fromYears(double years)
|
||||||
|
{
|
||||||
|
double yearsAfterEpoch = years - 1970.0;
|
||||||
|
|
||||||
|
QDateTime dt = RiaQDateTimeTools::epoch();
|
||||||
|
|
||||||
|
return RiaQDateTimeTools::addYears(dt, yearsAfterEpoch);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QDateTime RiaQDateTimeTools::addDays(const QDateTime& dt, double days)
|
||||||
|
{
|
||||||
|
double integerPart = 0.0;
|
||||||
|
double fractionPart = 0.0;
|
||||||
|
|
||||||
|
fractionPart = modf(days, &integerPart);
|
||||||
|
|
||||||
|
QDateTime tmp = dt.addDays(integerPart);
|
||||||
|
tmp = tmp.addSecs(fractionPart * RiaQDateTimeTools::secondsInDay());
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QDateTime RiaQDateTimeTools::addYears(const QDateTime& dt, double years)
|
||||||
|
{
|
||||||
|
double integerPart = 0.0;
|
||||||
|
double fractionPart = 0.0;
|
||||||
|
|
||||||
|
fractionPart = modf(years, &integerPart);
|
||||||
|
|
||||||
|
QDateTime tmp = dt.addYears(integerPart);
|
||||||
|
tmp = tmp.addSecs(fractionPart * RiaQDateTimeTools::secondsInYear());
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QDateTime RiaQDateTimeTools::epoch()
|
||||||
|
{
|
||||||
|
|
||||||
|
// NB: Not able to use QDateTime::fromMSecsSinceEpoch as this was introduced in Qt 4.7
|
||||||
|
|
||||||
|
QDateTime dt;
|
||||||
|
dt.setDate(QDate(1970, 1, 1));
|
||||||
|
dt.setTimeSpec(Qt::UTC);
|
||||||
|
|
||||||
|
return dt;
|
||||||
|
}
|
@ -22,21 +22,23 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
class QDateTime;
|
||||||
|
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
//
|
//
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
class RiaDateTimeTools
|
class RiaQDateTimeTools
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static quint64 secondsInMinute() { return 60; }
|
static QDateTime fromString(const QString& dateString, const QString& format);
|
||||||
static quint64 secondsInHour() { return 60 * 60; }
|
static QDateTime fromYears(double years);
|
||||||
static quint64 secondsInDay() { return 60 * 60 * 24; }
|
|
||||||
static quint64 secondsInYear() { return 60 * 60 * 24 * 365; }
|
static QDateTime addDays(const QDateTime& dt, double days);
|
||||||
|
static QDateTime addYears(const QDateTime& dt, double years);
|
||||||
|
|
||||||
static quint64 secondsFromUnit(const std::string& unit);
|
static QDateTime epoch();
|
||||||
|
|
||||||
static time_t createFromSecondsSinceEpoch(quint64 secondsSinceEpoch);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static quint64 secondsInDay();
|
||||||
|
static quint64 secondsInYear();
|
||||||
};
|
};
|
||||||
|
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include "RifColumnBasedUserData.h"
|
#include "RifColumnBasedUserData.h"
|
||||||
|
|
||||||
#include "RiaDateTimeTools.h"
|
#include "RiaQDateTimeTools.h"
|
||||||
#include "RiaLogging.h"
|
#include "RiaLogging.h"
|
||||||
|
|
||||||
#include "RifColumnBasedUserDataParser.h"
|
#include "RifColumnBasedUserDataParser.h"
|
||||||
@ -93,16 +93,15 @@ bool RifColumnBasedUserData::parse(const QString& data)
|
|||||||
{
|
{
|
||||||
QString dateFormatString = "ddMMyyyy";
|
QString dateFormatString = "ddMMyyyy";
|
||||||
|
|
||||||
startDate = QDateTime::fromString(startDateString, dateFormatString);
|
startDate = RiaQDateTimeTools::fromString(startDateString, dateFormatString);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
startDate.setDate(QDate(1970, 1, 1));
|
startDate = RiaQDateTimeTools::epoch();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_timeSteps.resize(m_timeSteps.size() + 1);
|
m_timeSteps.resize(m_timeSteps.size() + 1);
|
||||||
|
|
||||||
quint64 scaleFactor = RiaDateTimeTools::secondsFromUnit(ci.unitName);
|
|
||||||
std::vector<time_t>& timeSteps = m_timeSteps.back();
|
std::vector<time_t>& timeSteps = m_timeSteps.back();
|
||||||
|
|
||||||
QString unit = QString::fromStdString(ci.unitName).trimmed().toUpper();
|
QString unit = QString::fromStdString(ci.unitName).trimmed().toUpper();
|
||||||
@ -111,8 +110,7 @@ bool RifColumnBasedUserData::parse(const QString& data)
|
|||||||
{
|
{
|
||||||
for (const auto& timeStepValue : ci.values)
|
for (const auto& timeStepValue : ci.values)
|
||||||
{
|
{
|
||||||
QDateTime dateTime = startDate.addDays((int)timeStepValue);
|
QDateTime dateTime = RiaQDateTimeTools::addDays(startDate, timeStepValue);
|
||||||
dateTime.setTimeSpec(Qt::UTC);
|
|
||||||
timeSteps.push_back(dateTime.toTime_t());
|
timeSteps.push_back(dateTime.toTime_t());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,8 +118,7 @@ bool RifColumnBasedUserData::parse(const QString& data)
|
|||||||
{
|
{
|
||||||
for (const auto& timeStepValue : ci.values)
|
for (const auto& timeStepValue : ci.values)
|
||||||
{
|
{
|
||||||
QDateTime dateTime = startDate.addYears((int)timeStepValue);
|
QDateTime dateTime = RiaQDateTimeTools::addYears(startDate, timeStepValue);
|
||||||
dateTime.setTimeSpec(Qt::UTC);
|
|
||||||
timeSteps.push_back(dateTime.toTime_t());
|
timeSteps.push_back(dateTime.toTime_t());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ void RifColumnBasedUserDataParser::parseData(const QString& data)
|
|||||||
qLine = QString::fromStdString(line);
|
qLine = QString::fromStdString(line);
|
||||||
QStringList entries = qLine.split(" ", QString::SkipEmptyParts);
|
QStringList entries = qLine.split(" ", QString::SkipEmptyParts);
|
||||||
|
|
||||||
if (entries.size() < columnCount) break;
|
if (entries.size() < static_cast<int>(columnCount)) break;
|
||||||
|
|
||||||
for (size_t i = 0; i < columnCount; i++)
|
for (size_t i = 0; i < columnCount; i++)
|
||||||
{
|
{
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include "RifKeywordVectorUserData.h"
|
#include "RifKeywordVectorUserData.h"
|
||||||
|
|
||||||
#include "RiaDateTimeTools.h"
|
#include "RiaQDateTimeTools.h"
|
||||||
#include "RiaLogging.h"
|
#include "RiaLogging.h"
|
||||||
|
|
||||||
#include "RifEclipseSummaryAddress.h"
|
#include "RifEclipseSummaryAddress.h"
|
||||||
@ -106,26 +106,28 @@ bool RifKeywordVectorUserData::parse(const QString& data)
|
|||||||
dateFormatString = "DD MM YYYY";
|
dateFormatString = "DD MM YYYY";
|
||||||
}
|
}
|
||||||
|
|
||||||
startDate = QDateTime::fromString(startDateString, dateFormatString);
|
startDate = RiaQDateTimeTools::fromString(startDateString, dateFormatString);
|
||||||
}
|
|
||||||
|
|
||||||
QString unitText = valueForKey(keyValuePairs, "UNITS");
|
|
||||||
quint64 scaleFactor = RiaDateTimeTools::secondsFromUnit(unitText.toStdString());
|
|
||||||
|
|
||||||
if (startDate.isValid())
|
|
||||||
{
|
|
||||||
for (const auto& timeStepValue : m_parser->keywordBasedVectors()[i].values)
|
|
||||||
{
|
|
||||||
QDateTime dateTime = startDate.addSecs(scaleFactor * timeStepValue);
|
|
||||||
|
|
||||||
ts.push_back(dateTime.toTime_t());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
startDate = RiaQDateTimeTools::epoch();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString unitText = valueForKey(keyValuePairs, "UNITS");
|
||||||
|
if (unitText == "DAY" || unitText == "DAYS")
|
||||||
{
|
{
|
||||||
for (const auto& timeStepValue : m_parser->keywordBasedVectors()[i].values)
|
for (const auto& timeStepValue : m_parser->keywordBasedVectors()[i].values)
|
||||||
{
|
{
|
||||||
ts.push_back(scaleFactor * timeStepValue);
|
QDateTime dateTime = RiaQDateTimeTools::addDays(startDate, timeStepValue);
|
||||||
|
ts.push_back(dateTime.toTime_t());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (unitText == "YEAR" || unitText == "YEARS")
|
||||||
|
{
|
||||||
|
for (const auto& timeStepValue : m_parser->keywordBasedVectors()[i].values)
|
||||||
|
{
|
||||||
|
QDateTime dateTime = RiaQDateTimeTools::fromYears(timeStepValue);
|
||||||
|
ts.push_back(dateTime.toTime_t());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user