version 3.10-dev
chrono.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_IO_CHRONO_HH
13#define DUMUX_IO_CHRONO_HH
14
15#include <array>
16#include <cctype>
17#include <chrono>
18#include <string>
19#include <string_view>
20#include <algorithm>
21
22#include <dune/common/exceptions.hh>
23
24namespace Dumux::Chrono {
25
27template<typename Rep, typename Period>
28void toDuration(std::chrono::duration<Rep, Period>& duration, const std::string& s)
29{
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{{
34 {"", S(1)}, // assume seconds if no unit is given
35 {"s", S(1)},
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))},
41 // After requiring cpp20, we can use the aliases in std::chrono
42 {"d", duration_cast<S>(std::chrono::hours(24))},
43 {"y", duration_cast<S>(std::chrono::seconds(31556952))} // to match cpp20, see https://en.cppreference.com/w/cpp/chrono/duration
44 }};
45
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) {
52 return p.first == u;
53 });
54 if (it == unitMap.end())
55 DUNE_THROW(Dune::InvalidStateException, "Unsupported unit " << unit << ".");
56 return it->second;
57 } ();
58 duration = duration_cast<std::chrono::duration<Rep, Period>>(number*conversion);
59}
60
62template<typename Rep = double>
63std::chrono::duration<Rep> toSeconds(const std::string& s)
64{
65 std::chrono::duration<Rep> result;
66 toDuration(result, s);
67 return result;
68}
69
70} // end namespace Dumux::Chrono
71
72#endif
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
Definition: chrono.hh:24
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