Move timespec2seconds to utils

This commit is contained in:
Richard Berger
2020-08-03 16:54:53 -04:00
parent 944ac44b44
commit 4126ba24c1
4 changed files with 59 additions and 27 deletions

View File

@ -18,6 +18,7 @@
#include "comm.h"
#include "error.h"
#include "force.h"
#include "utils.h"
#ifdef _WIN32
#include <windows.h>
@ -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;

View File

@ -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" {

View File

@ -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);
}
}

View File

@ -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);
}