Skip to content

Commit 5ea92bd

Browse files
committed
increase fraction precision
1 parent 5918d30 commit 5ea92bd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

include/mgutility/chrono/parse.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ constexpr int32_t abs(int32_t value) noexcept {
8282
return value >= 0 ? value : -value;
8383
}
8484

85+
// NOLINTNEXTLINE
86+
constexpr auto pow(int32_t base, int32_t exp) noexcept -> int32_t {
87+
// NOLINTNEXTLINE
88+
return exp < 0 ? 0 : exp == 0 ? 1 : base * pow(base, exp - 1);
89+
}
90+
8591
/**
8692
* @brief Checks if a value is within a given range.
8793
*
@@ -320,11 +326,17 @@ MGUTILITY_CNSTXPR auto parse_second(detail::tm &result, string_view date_str,
320326

321327
MGUTILITY_CNSTXPR auto parse_fraction(detail::tm &result, string_view date_str,
322328
uint32_t &next) -> std::errc {
323-
auto error = parse_integer(result.tm_ms, date_str, 3, next);
329+
int32_t digits = 0;
330+
while (next + digits < date_str.size() &&
331+
mgutility::detail::is_digit(date_str[next + digits]) && digits < 9) {
332+
++digits;
333+
}
334+
auto error = parse_integer(result.tm_ms, date_str, digits, next);
324335
if (error != std::errc{}) {
325336
return error;
326337
}
327-
error = check_range(result.tm_ms, 0U, 999U);
338+
result.tm_ms *= static_cast<uint32_t>(pow(10, 9 - digits));
339+
error = check_range(result.tm_ms, 0U, 999999999U);
328340
return error;
329341
}
330342

@@ -536,7 +548,7 @@ auto parse(typename Clock::time_point &time_point, string_view format,
536548
return std::make_error_code(error);
537549
}
538550
time_point = Clock::from_time_t(time_t);
539-
time_point += std::chrono::milliseconds(time_struct.tm_ms);
551+
time_point += std::chrono::nanoseconds{time_struct.tm_ms};
540552
return std::error_code{};
541553
}
542554

0 commit comments

Comments
 (0)