28 if (duration.empty()) {
29 errmsg =
"cannot parse empty string as a time duration";
32 if (duration ==
"0") {
36 struct timespec
ts = {0, 0};
37 auto strValue = duration;
38 while (!strValue.empty()) {
42 value = std::stod(strValue, &pos);
43 }
catch (std::invalid_argument
const &exc) {
44 errmsg =
"Invalid number provided as timeout: " + strValue;
46 }
catch (std::out_of_range
const &exc) {
47 errmsg =
"Provided timeout out of representable range: " + std::string(exc.what());
51 errmsg =
"Provided timeout was negative";
54 strValue = strValue.substr(pos);
55 char unit[3] = {
'\0',
'\0',
'\0'};
56 if (!strValue.empty()) {
57 unit[0] = strValue[0];
58 if (unit[0] >=
'0' && unit[0] <=
'9') {unit[0] =
'\0';}
60 if (strValue.size() > 1) {
61 unit[1] = strValue[1];
62 if (unit[1] >=
'0' && unit[1] <=
'9') {unit[1] =
'\0';}
64 if (!strncmp(unit,
"ns", 2)) {
66 }
else if (!strncmp(unit,
"us", 2)) {
67 auto value_s = (
static_cast<long long>(value)) / 1'000'000;
69 value -= value_s * 1'000'000;
70 ts.tv_nsec += value * 1'000'000;
71 }
else if (!strncmp(unit,
"ms", 2)) {
72 auto value_s = (
static_cast<long long>(value)) / 1'000;
74 value -= value_s * 1'000;
75 ts.tv_nsec += value * 1'000'000;
76 }
else if (!strncmp(unit,
"s", 1)) {
77 auto value_s = (
static_cast<long long>(value));
80 ts.tv_nsec += value * 1'000'000'000;
81 }
else if (!strncmp(unit,
"m", 1)) {
83 auto value_s = (
static_cast<long long>(value));
86 ts.tv_nsec += value * 1'000'000'000;
87 }
else if (!strncmp(unit,
"h", 1)) {
89 auto value_s = (
static_cast<long long>(value));
92 ts.tv_nsec += value * 1'000'000'000;
93 }
else if (strlen(unit) > 0) {
94 errmsg =
"Unknown unit in duration: " + std::string(unit);
97 errmsg =
"Unit missing from duration: " + duration;
100 if (
ts.tv_nsec > 1'000'000'000) {
101 ts.tv_sec +=
ts.tv_nsec / 1'000'000'000;
102 ts.tv_nsec =
ts.tv_nsec % 1'000'000'000;
104 strValue = strValue.substr(strlen(unit));
106 result.tv_nsec =
ts.tv_nsec;
107 result.tv_sec =
ts.tv_sec;