From 428d4c41f0abe99c91378e9aeff2bf7853e4ae81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Tue, 15 Oct 2019 01:09:59 -0500 Subject: [PATCH] Reimplement TimeMap in Terms of TimeStampUTC --- .../eclipse/EclipseState/Schedule/TimeMap.hpp | 2 + .../eclipse/EclipseState/Schedule/TimeMap.cpp | 25 ++-- tests/parser/TimeMapTest.cpp | 108 +++++++----------- 3 files changed, 61 insertions(+), 74 deletions(-) diff --git a/opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp b/opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp index 6d50f63b8..63c244483 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp @@ -25,6 +25,8 @@ #include #include +#include + namespace Opm { class Deck; diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/TimeMap.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/TimeMap.cpp index 76bc74b6a..dfa6b5295 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/TimeMap.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/TimeMap.cpp @@ -18,8 +18,9 @@ */ #include +#include -#include +#include #include #include @@ -27,7 +28,6 @@ #include #include - namespace Opm { namespace { @@ -119,9 +119,14 @@ namespace { const std::time_t lastTime = m_timeList.back(); const size_t step = m_timeList.size(); if (newTime > lastTime) { - int new_day, new_month, new_year, last_day, last_month, last_year; - util_set_date_values_utc(newTime, &new_day, &new_month, &new_year); - util_set_date_values_utc(lastTime, &last_day, &last_month, &last_year); + const auto nw = TimeStampUTC{ newTime }; + const auto last = TimeStampUTC{ lastTime }; + + const auto new_month = nw .month(); + const auto last_month = last.month(); + + const auto new_year = nw .year(); + const auto last_year = last.year(); if (new_month != last_month) m_first_timestep_months.push_back(step); @@ -305,16 +310,18 @@ namespace { } std::time_t TimeMap::mkdatetime(int in_year, int in_month, int in_day, int hour, int minute, int second) { - std::time_t t = util_make_datetime_utc(second, minute, hour, in_day, in_month, in_year); + const auto tp = TimeStampUTC{ TimeStampUTC::YMD { in_year, in_month, in_day } } + .hour(hour).minutes(minute).seconds(second); + + std::time_t t = asTimeT(tp); { /* The underlying mktime( ) function will happily wrap around dates like January 33, this function will check that no such wrap-around has taken place. */ - int out_year, out_day, out_month; - util_set_date_values_utc( t, &out_day , &out_month, &out_year); - if ((in_day != out_day) || (in_month != out_month) || (in_year != out_year)) + const auto check = TimeStampUTC{ t }; + if ((in_day != check.day()) || (in_month != check.month()) || (in_year != check.year())) throw std::invalid_argument("Invalid input arguments for date."); } return t; diff --git a/tests/parser/TimeMapTest.cpp b/tests/parser/TimeMapTest.cpp index 1fef34d08..a9abcbf4d 100644 --- a/tests/parser/TimeMapTest.cpp +++ b/tests/parser/TimeMapTest.cpp @@ -25,7 +25,7 @@ #include - +#include #include #include @@ -35,7 +35,6 @@ #include #include -#include const std::time_t startDateJan1st2010 = Opm::TimeMap::mkdate(2010, 1, 1); @@ -346,14 +345,11 @@ BOOST_AUTO_TEST_CASE(initTimestepsLongStep) { 0 1 jan 1983 START 1 14 dec 2052*/ - const std::time_t tEnd = tmap.getEndTime(); + const auto tEnd = Opm::TimeStampUTC { tmap.getEndTime() }; - int year, day, month; - - util_set_date_values_utc(tEnd, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2052); - BOOST_CHECK_EQUAL(month, 12); - BOOST_CHECK_EQUAL(day, 14); + BOOST_CHECK_EQUAL(tEnd.year(), 2052); + BOOST_CHECK_EQUAL(tEnd.month(), 12); + BOOST_CHECK_EQUAL(tEnd.day(), 14); } @@ -375,14 +371,11 @@ BOOST_AUTO_TEST_CASE(TimestepsLabUnit) { 0 1 jan 1983 START 1 11 jan 1983*/ - const std::time_t tEnd = tmap.getEndTime(); + const auto tEnd = Opm::TimeStampUTC { tmap.getEndTime() }; - int year, day, month; - - util_set_date_values_utc(tEnd, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 1983); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 11); + BOOST_CHECK_EQUAL(tEnd.year(), 1983); + BOOST_CHECK_EQUAL(tEnd.month(), 1); + BOOST_CHECK_EQUAL(tEnd.day(), 11); } @@ -405,73 +398,58 @@ BOOST_AUTO_TEST_CASE(initTimestepsDistantDates) { 1 1 jan 2040 2 1 jan 2050*/ - const std::time_t t1 = tmap.getStartTime(1); - const std::time_t t2 = tmap.getEndTime(); + const auto t1 = Opm::TimeStampUTC { tmap.getStartTime(1) }; + const auto t2 = Opm::TimeStampUTC { tmap.getEndTime() }; - int year, day, month; + BOOST_CHECK_EQUAL(t1.year(), 2040); + BOOST_CHECK_EQUAL(t1.month(), 1); + BOOST_CHECK_EQUAL(t1.day(), 1); - util_set_date_values_utc(t1, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2040); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 1); - - util_set_date_values_utc(t2, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2050); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 1); + BOOST_CHECK_EQUAL(t2.year(), 2050); + BOOST_CHECK_EQUAL(t2.month(), 1); + BOOST_CHECK_EQUAL(t2.day(), 1); } BOOST_AUTO_TEST_CASE(mkdate) { BOOST_CHECK_THROW( Opm::TimeMap::mkdate( 2010 , 0 , 0 ) , std::invalid_argument); - std::time_t t0 = Opm::TimeMap::mkdate( 2010 , 1, 1); - std::time_t t1 = Opm::TimeMap::forward( t0 , 24*3600); + auto t0 = Opm::TimeStampUTC { Opm::TimeMap::mkdate( 2010 , 1, 1) }; + auto t1 = Opm::TimeStampUTC { Opm::TimeMap::forward( asTimeT(t0) , 24*3600) }; - int year, day, month; + BOOST_CHECK_EQUAL( t1.year() , 2010 ); + BOOST_CHECK_EQUAL( t1.month() , 1 ); + BOOST_CHECK_EQUAL( t1.day() , 2 ); - util_set_date_values_utc( t1, &day , &month, &year); - BOOST_CHECK_EQUAL( year , 2010 ); - BOOST_CHECK_EQUAL( month , 1 ); - BOOST_CHECK_EQUAL( day , 2 ); + t1 = Opm::TimeMap::forward( asTimeT(t1) , -24*3600); + BOOST_CHECK_EQUAL( t1.year() , 2010 ); + BOOST_CHECK_EQUAL( t1.month() , 1 ); + BOOST_CHECK_EQUAL( t1.day() , 1 ); - t1 = Opm::TimeMap::forward( t1 , -24*3600); - util_set_date_values_utc( t1, &day , &month, &year); - BOOST_CHECK_EQUAL( year , 2010 ); - BOOST_CHECK_EQUAL( month , 1 ); - BOOST_CHECK_EQUAL( day , 1 ); - - - t1 = Opm::TimeMap::forward( t0 , 23, 55 , 300); - util_set_date_values_utc( t1, &day , &month, &year); - BOOST_CHECK_EQUAL( year , 2010 ); - BOOST_CHECK_EQUAL( month , 1 ); - BOOST_CHECK_EQUAL( day , 2 ); + t1 = Opm::TimeMap::forward( asTimeT(t0) , 23, 55 , 300); + BOOST_CHECK_EQUAL( t1.year() , 2010 ); + BOOST_CHECK_EQUAL( t1.month() , 1 ); + BOOST_CHECK_EQUAL( t1.day() , 2 ); } BOOST_AUTO_TEST_CASE(mkdatetime) { BOOST_CHECK_THROW(Opm::TimeMap::mkdatetime(2010, 0, 0, 0, 0, 0), std::invalid_argument); - std::time_t t0 = Opm::TimeMap::mkdatetime(2010, 1, 1, 0, 0, 0); - std::time_t t1 = Opm::TimeMap::forward(t0, 24 * 3600); + auto t0 = Opm::TimeStampUTC { Opm::TimeMap::mkdatetime(2010, 1, 1, 0, 0, 0) }; + auto t1 = Opm::TimeStampUTC { Opm::TimeMap::forward(asTimeT(t0), 24 * 3600) }; - int year, day, month; + BOOST_CHECK_EQUAL(t1.year(), 2010); + BOOST_CHECK_EQUAL(t1.month(), 1); + BOOST_CHECK_EQUAL(t1.day(), 2); - util_set_date_values_utc(t1, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2010); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 2); + t1 = Opm::TimeMap::forward(asTimeT(t1), -24 * 3600); + BOOST_CHECK_EQUAL(t1.year(), 2010); + BOOST_CHECK_EQUAL(t1.month(), 1); + BOOST_CHECK_EQUAL(t1.day(), 1); - t1 = Opm::TimeMap::forward(t1, -24 * 3600); - util_set_date_values_utc(t1, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2010); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 1); - - t1 = Opm::TimeMap::forward(t0, 23, 55, 300); - util_set_date_values_utc(t1, &day, &month, &year); - BOOST_CHECK_EQUAL(year, 2010); - BOOST_CHECK_EQUAL(month, 1); - BOOST_CHECK_EQUAL(day, 2); + t1 = Opm::TimeMap::forward(asTimeT(t0), 23, 55, 300); + BOOST_CHECK_EQUAL(t1.year(), 2010); + BOOST_CHECK_EQUAL(t1.month(), 1); + BOOST_CHECK_EQUAL(t1.day(), 2); } Opm::DeckRecord createDeckRecord(int day, const std::string &month, int year, const std::string &time) {