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:
@@ -16,7 +16,7 @@ ${CEE_CURRENT_LIST_DIR}RiaLogging.h
|
||||
${CEE_CURRENT_LIST_DIR}RiaProjectModifier.h
|
||||
${CEE_CURRENT_LIST_DIR}RiaRegressionTest.h
|
||||
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.h
|
||||
${CEE_CURRENT_LIST_DIR}RiaDateTimeTools.h
|
||||
${CEE_CURRENT_LIST_DIR}RiaQDateTimeTools.h
|
||||
)
|
||||
|
||||
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}RiaRegressionTest.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RiaImportEclipseCaseTools.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RiaDateTimeTools.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RiaQDateTimeTools.cpp
|
||||
)
|
||||
|
||||
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>
|
||||
|
||||
class QDateTime;
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//==================================================================================================
|
||||
class RiaDateTimeTools
|
||||
class RiaQDateTimeTools
|
||||
{
|
||||
public:
|
||||
static quint64 secondsInMinute() { return 60; }
|
||||
static quint64 secondsInHour() { return 60 * 60; }
|
||||
static quint64 secondsInDay() { return 60 * 60 * 24; }
|
||||
static quint64 secondsInYear() { return 60 * 60 * 24 * 365; }
|
||||
static QDateTime fromString(const QString& dateString, const QString& format);
|
||||
static QDateTime fromYears(double years);
|
||||
|
||||
static QDateTime addDays(const QDateTime& dt, double days);
|
||||
static QDateTime addYears(const QDateTime& dt, double years);
|
||||
|
||||
static quint64 secondsFromUnit(const std::string& unit);
|
||||
|
||||
static time_t createFromSecondsSinceEpoch(quint64 secondsSinceEpoch);
|
||||
static QDateTime epoch();
|
||||
|
||||
private:
|
||||
static quint64 secondsInDay();
|
||||
static quint64 secondsInYear();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user