Suppose I have a time measuring class, parametrizable by the duration type like this
template<typename TimeT = std::chrono::milliseconds>
struct measure
{ /* implementation */ };
What I want is to be able to print out the TimeT
. I'm leaning towards implementing a static member function like this :
static string TimeType() const;
My questions are :
typeinfo / name
combo (in which case I'd have to remove constexpr
above) or should I opt for the creation of several specializations that would return the correct string per time type ? Chronos time is how we measure our days and our lives quantitatively.
C++ includes support for two types of time manipulation: The chrono library, a flexible collection of types that track time with varying degrees of precision (e.g. std::chrono::time_point).
Chrono library in C++ This Chrono library is used for date and time. Timers and clocks are different in different systems. So if we want to improve time over precision we can use this library. In this library, it provides precision-neutral concept, by separating the durations and point of time.
std::chrono::duration_cast Converts the value of dtn into some other duration type, taking into account differences in their periods. The function does not use implicit conversions.
You are welcome to use my <chrono_io>
library. It consists of a single header, "chrono_io.h"
which is linked to in the docs. Example use would be:
#include "chrono_io.h"
#include <iostream>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
TimeT value;
friend
std::ostream&
operator<< (std::ostream& os, const measure& m)
{
using namespace date;
return os << m.value;
}
};
int
main()
{
using namespace std::chrono;
measure<> m1 = {30ms};
std::cout << m1 << '\n';
measure<duration<int, std::ratio<1, 60>>> m2 = {duration<int, std::ratio<1, 60>>{45}};
std::cout << m2 << '\n';
}
which outputs:
30ms
45[1/60]s
A way to do this is specializing over the time type; this way non portability of typeid.name()
stops being a factor :
/// get the name of the chrono time type
template<typename T> string time_type() { return "unknown"; }
template<> string time_type<std::chrono::nanoseconds >() { return "nanoseconds"; }
template<> string time_type<std::chrono::microseconds>() { return "microseconds"; }
template<> string time_type<std::chrono::milliseconds>() { return "milliseconds"; }
template<> string time_type<std::chrono::seconds >() { return "seconds"; }
template<> string time_type<std::chrono::minutes >() { return "minutes"; }
template<> string time_type<std::chrono::hours >() { return "hours"; }
this q didn't get much attention. I posted an answer as a min base for comparison of code quality
Ofcourse the difficult case here, would be to have this info at compile time which would require compile time strings n' stuff
Another version of the above would be
template<class> struct time_type { constexpr static char const *name = "unknown"; };
template<> struct time_type<nanoseconds > { constexpr static char const *name = "nanoseconds"; };
template<> struct time_type<microseconds> { constexpr static char const *name = "microseconds"; };
template<> struct time_type<milliseconds> { constexpr static char const *name = "milliseconds"; };
template<> struct time_type<seconds > { constexpr static char const *name = "seconds"; };
template<> struct time_type<minutes > { constexpr static char const *name = "minutes"; };
template<> struct time_type<hours > { constexpr static char const *name = "hours"; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With