12#ifndef DUMUX_IO_CHRONO_HH
13#define DUMUX_IO_CHRONO_HH
22#include <dune/common/exceptions.hh>
27template<
typename Rep,
typename Period>
28void toDuration(std::chrono::duration<Rep, Period>& duration,
const std::string& s)
30 using std::chrono::duration_cast;
31 using namespace std::chrono_literals;
32 using S = std::chrono::duration<Rep>;
33 constexpr std::array<std::pair<std::string_view, S>, 9> unitMap{{
36 {
"ns", duration_cast<S>(std::chrono::nanoseconds(1))},
37 {
"us", duration_cast<S>(std::chrono::microseconds(1))},
38 {
"ms", duration_cast<S>(std::chrono::milliseconds(1))},
39 {
"min", duration_cast<S>(std::chrono::minutes(1))},
40 {
"h", duration_cast<S>(std::chrono::hours(1))},
42 {
"d", duration_cast<S>(std::chrono::hours(24))},
43 {
"y", duration_cast<S>(std::chrono::seconds(31556952))}
46 const auto unitIt = std::find_if(s.rbegin(), s.rend(), [] (
const auto& c) { return !std::isalpha(c); });
47 const auto unitPos = s.size() -
std::distance(s.rbegin(), unitIt);
48 const auto number = std::stod(s.substr(0, unitPos));
49 const auto unit = s.substr(unitPos);
50 const auto conversion = [&] () {
51 const auto it = std::find_if(unitMap.begin(), unitMap.end(), [u=std::string_view{unit}] (
const auto& p) {
54 if (it == unitMap.end())
55 DUNE_THROW(Dune::InvalidStateException,
"Unsupported unit " << unit <<
".");
58 duration = duration_cast<std::chrono::duration<Rep, Period>>(number*conversion);
62template<
typename Rep =
double>
63std::chrono::duration<Rep>
toSeconds(
const std::string& s)
65 std::chrono::duration<Rep> result;
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:282
void toDuration(std::chrono::duration< Rep, Period > &duration, const std::string &s)
Try to construct a std::chrono::duration from a string.
Definition: chrono.hh:28
std::chrono::duration< Rep > toSeconds(const std::string &s)
Try to construct an instance of std::chrono::seconds from a string including a unit suffix.
Definition: chrono.hh:63