From 4126ba24c1a6b69f8a990f25211945a564eeaf20 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 3 Aug 2020 16:54:53 -0400 Subject: [PATCH] Move timespec2seconds to utils --- src/timer.cpp | 29 ++--------------------------- src/utils.cpp | 32 ++++++++++++++++++++++++++++++++ src/utils.h | 10 ++++++++++ unittest/utils/test_utils.cpp | 15 +++++++++++++++ 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/timer.cpp b/src/timer.cpp index 41a0166a89..97f0e8abab 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -18,6 +18,7 @@ #include "comm.h" #include "error.h" #include "force.h" +#include "utils.h" #ifdef _WIN32 #include @@ -31,32 +32,6 @@ using namespace LAMMPS_NS; -// convert a timespec ([[HH:]MM:]SS) to seconds -// the strings "off" and "unlimited" result in -1; - -static double timespec2seconds(char *timespec) -{ - double vals[3]; - char *num; - int i = 0; - - // first handle allowed textual inputs - if (strcmp(timespec,"off") == 0) return -1; - if (strcmp(timespec,"unlimited") == 0) return -1; - - vals[0] = vals[1] = vals[2] = 0; - - num = strtok(timespec,":"); - while ((num != NULL) && (i < 3)) { - vals[i] = atoi(num); - ++i; - num = strtok(NULL,":"); - } - - if (i == 3) return (vals[0]*60 + vals[1])*60 + vals[2]; - else if (i == 2) return vals[0]*60 + vals[1]; - else return vals[0]; -} // Return the CPU time for the current process in seconds very // much in the same way as MPI_Wtime() returns the wall time. @@ -297,7 +272,7 @@ void Timer::modify_params(int narg, char **arg) } else if (strcmp(arg[iarg],"timeout") == 0) { ++iarg; if (iarg < narg) { - _timeout = timespec2seconds(arg[iarg]); + _timeout = utils::timespec2seconds(arg[iarg]); } else error->all(FLERR,"Illegal timers command"); } else if (strcmp(arg[iarg],"every") == 0) { ++iarg; diff --git a/src/utils.cpp b/src/utils.cpp index ce6a64f22f..3037c8c748 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -705,6 +705,38 @@ double utils::get_conversion_factor(const int property, const int conversion) return 0.0; } +/* ---------------------------------------------------------------------- + convert a timespec ([[HH:]MM:]SS) to seconds + the strings "off" and "unlimited" result in -1.0; +------------------------------------------------------------------------- */ + +double utils::timespec2seconds(const std::string & timespec) +{ + double vals[3]; + int i = 0; + + // first handle allowed textual inputs + if (timespec == "off") return -1.0; + if (timespec == "unlimited") return -1.0; + + vals[0] = vals[1] = vals[2] = 0; + + ValueTokenizer values(timespec, ":"); + + try { + for (i = 0; i < 3; i++) { + if (!values.has_next()) break; + vals[i] = values.next_int(); + } + } catch (TokenizerException & e) { + return -1.0; + } + + if (i == 3) return (vals[0]*60 + vals[1])*60 + vals[2]; + else if (i == 2) return vals[0]*60 + vals[1]; + return vals[0]; +} + /* ------------------------------------------------------------------ */ extern "C" { diff --git a/src/utils.h b/src/utils.h index 3919f8d4d5..f92bfce591 100644 --- a/src/utils.h +++ b/src/utils.h @@ -291,6 +291,16 @@ namespace LAMMPS_NS { * \return conversion factor */ double get_conversion_factor(const int property, const int conversion); + +// convert a timespec ([[HH:]MM:]SS) to seconds + + /** + * \brief Convert a time string to seconds + * The strings "off" and "unlimited" result in -1 + * \param timespec a string in the following format: ([[HH:]MM:]SS) + * \return total in seconds + */ + double timespec2seconds(const std::string & timespec); } } diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 08aa5df1ff..0a218e2596 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -462,3 +462,18 @@ TEST(Utils, unit_conversion) factor = utils::get_conversion_factor(utils::ENERGY, utils::REAL2METAL); ASSERT_DOUBLE_EQ(factor, 1.0 / 23.060549); } + +TEST(Utils, timespec2seconds_ss) +{ + ASSERT_DOUBLE_EQ(utils::timespec2seconds("45"), 45.0); +} + +TEST(Utils, timespec2seconds_mmss) +{ + ASSERT_DOUBLE_EQ(utils::timespec2seconds("10:45"), 645.0); +} + +TEST(Utils, timespec2seconds_hhmmss) +{ + ASSERT_DOUBLE_EQ(utils::timespec2seconds("2:10:45"), 7845.0); +}