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

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