namespace TimeService with functions advance and makeUTCTime
This commit is contained in:
@@ -25,6 +25,11 @@
|
||||
|
||||
namespace Opm {
|
||||
|
||||
namespace TimeService {
|
||||
std::time_t advance(const std::time_t tp, const double sec);
|
||||
std::time_t makeUTCTime(std::tm timePoint);
|
||||
}
|
||||
|
||||
class TimeStampUTC
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -23,34 +23,36 @@
|
||||
#include <ctime>
|
||||
#include <utility>
|
||||
|
||||
namespace Opm {
|
||||
namespace TimeService {
|
||||
|
||||
|
||||
std::time_t advance(const std::time_t tp, const double sec)
|
||||
{
|
||||
const auto t = Opm::TimeService::from_time_t(tp) + std::chrono::duration_cast<Opm::time_point::duration>(std::chrono::duration<double>(sec));
|
||||
return Opm::TimeService::to_time_t(t);
|
||||
}
|
||||
|
||||
std::time_t makeUTCTime(std::tm timePoint)
|
||||
{
|
||||
const auto ltime = std::mktime(&timePoint);
|
||||
auto tmval = *std::gmtime(<ime); // Mutable.
|
||||
|
||||
// offset = ltime - tmval
|
||||
// == #seconds by which 'ltime' is AHEAD of tmval.
|
||||
const auto offset =
|
||||
std::difftime(ltime, std::mktime(&tmval));
|
||||
|
||||
// Advance 'ltime' by 'offset' so that std::gmtime(return value) will
|
||||
// have the same broken-down elements as 'tp'.
|
||||
return advance(ltime, offset);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::time_t advance(const std::time_t tp, const double sec)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
using TP = time_point<system_clock>;
|
||||
using DoubSec = duration<double, seconds::period>;
|
||||
|
||||
const auto t = system_clock::from_time_t(tp) +
|
||||
duration_cast<TP::duration>(DoubSec(sec));
|
||||
|
||||
return system_clock::to_time_t(t);
|
||||
}
|
||||
|
||||
std::time_t makeUTCTime(std::tm timePoint)
|
||||
{
|
||||
const auto ltime = std::mktime(&timePoint);
|
||||
auto tmval = *std::gmtime(<ime); // Mutable.
|
||||
|
||||
// offset = ltime - tmval
|
||||
// == #seconds by which 'ltime' is AHEAD of tmval.
|
||||
const auto offset =
|
||||
std::difftime(ltime, std::mktime(&tmval));
|
||||
|
||||
// Advance 'ltime' by 'offset' so that std::gmtime(return value) will
|
||||
// have the same broken-down elements as 'tp'.
|
||||
return advance(ltime, offset);
|
||||
}
|
||||
|
||||
|
||||
std::tm makeTm(const Opm::TimeStampUTC& tp) {
|
||||
@@ -144,7 +146,7 @@ Opm::TimeStampUTC& Opm::TimeStampUTC::microseconds(const int us)
|
||||
|
||||
std::time_t Opm::asTimeT(const TimeStampUTC& tp)
|
||||
{
|
||||
return makeUTCTime(makeTm(tp));
|
||||
return Opm::TimeService::makeUTCTime(makeTm(tp));
|
||||
}
|
||||
|
||||
std::time_t Opm::asLocalTimeT(const TimeStampUTC& tp)
|
||||
@@ -154,7 +156,7 @@ std::time_t Opm::asLocalTimeT(const TimeStampUTC& tp)
|
||||
}
|
||||
|
||||
Opm::TimeStampUTC Opm::operator+(const Opm::TimeStampUTC& lhs, std::chrono::duration<double> delta) {
|
||||
return Opm::TimeStampUTC( advance(Opm::asTimeT(lhs) , delta.count()) );
|
||||
return Opm::TimeStampUTC( Opm::TimeService::advance(Opm::asTimeT(lhs) , delta.count()) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -358,7 +358,7 @@ namespace {
|
||||
tm1.tm_sec = 0;
|
||||
tm1.tm_isdst = 0;
|
||||
|
||||
const auto t1 = ::Opm::RestartIO::makeUTCTime(tm1);
|
||||
const auto t1 = Opm::TimeService::makeUTCTime(tm1);
|
||||
|
||||
if (t1 != static_cast<std::time_t>(-1)) {
|
||||
tm1 = *std::gmtime(&t1); // Get new tm_yday.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <opm/output/eclipse/VectorItems/intehead.hpp>
|
||||
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
|
||||
#include <opm/common/utility/TimeService.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
@@ -767,47 +768,11 @@ Opm::RestartIO::InteHEAD::networkDimensions(const NetworkDims& nwdim)
|
||||
|
||||
|
||||
|
||||
// =====================================================================
|
||||
// Free functions (calendar/time utilities)
|
||||
// =====================================================================
|
||||
|
||||
namespace {
|
||||
std::time_t advance(const std::time_t tp, const double sec)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
using TP = time_point<system_clock>;
|
||||
using DoubSec = duration<double, seconds::period>;
|
||||
|
||||
const auto t = system_clock::from_time_t(tp) +
|
||||
duration_cast<TP::duration>(DoubSec(sec));
|
||||
|
||||
return system_clock::to_time_t(t);
|
||||
}
|
||||
}
|
||||
|
||||
std::time_t
|
||||
Opm::RestartIO::makeUTCTime(const std::tm& timePoint)
|
||||
{
|
||||
auto tp = timePoint; // Mutable copy.
|
||||
const auto ltime = std::mktime(&tp);
|
||||
auto tmval = *std::gmtime(<ime); // Mutable.
|
||||
|
||||
// offset = ltime - tmval
|
||||
// == #seconds by which 'ltime' is AHEAD of tmval.
|
||||
const auto offset =
|
||||
std::difftime(ltime, std::mktime(&tmval));
|
||||
|
||||
// Advance 'ltime' by 'offset' so that std::gmtime(return value) will
|
||||
// have the same broken-down elements as 'tp'.
|
||||
return advance(ltime, offset);
|
||||
}
|
||||
|
||||
Opm::RestartIO::InteHEAD::TimePoint
|
||||
Opm::RestartIO::getSimulationTimePoint(const std::time_t start,
|
||||
const double elapsed)
|
||||
{
|
||||
const auto now = advance(start, elapsed);
|
||||
const auto now = TimeService::advance(start, elapsed);
|
||||
const auto tp = *std::gmtime(&now);
|
||||
|
||||
auto sec = 0.0; // Not really used here.
|
||||
|
||||
Reference in New Issue
Block a user