Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of a time type in chrono

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 :

  1. Should I add a member instead? Should this not be static?
  2. How should the body of this be implemented ? Should I use the implementation dependent, non compile time 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 ?
  3. Is there a more standard / idiomatic way of getting the name of the time type ?
like image 964
Nikos Athanasiou Avatar asked Mar 22 '15 15:03

Nikos Athanasiou


People also ask

What is chrono time?

Chronos time is how we measure our days and our lives quantitatively.

What is std:: chrono in c++?

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).

What library is Chrono in?

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.

Which Chrono function allows us to convert a clock duration from one unit to the other?

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.


2 Answers

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
like image 137
Howard Hinnant Avatar answered Nov 05 '22 06:11

Howard Hinnant


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";        };
like image 37
Nikos Athanasiou Avatar answered Nov 05 '22 05:11

Nikos Athanasiou