c++ - std::chrono calculate the difference in different ratio -
my title may not helpful, have std::chrono::nanosecond, asked serialise , provide second, , nanosecond different values in json.
so although struct holds:
struct time { ... std::chrono::nanoseconds timepoint; };
when asked seconds, do
uint32_t sec() const { return std::chrono::duration_cast<std::chrono::seconds>(timepoint_).count(); }
yet when asked nanoseconds, want resolution in nanoseconds, without seconds (only least significant values?) however, casting in nanoseconds returns both seconds , higher resolution.
uint64_t nanosec() const { return std::chrono::duration_cast<std::chrono::nanoseconds>(timepoint_).count(); }
how can calculate actual increased resolution (e.g., nanoseconds without actual seconds)?
you can retrieve seconds using std::duration_cast
std::seconds
, nanoseconds using modulo operator:
template <typename t> std::pair<t, t> split (std::chrono::duration<t, std::nano> const& duration) { using seconds = std::chrono::duration<t>; return {std::chrono::duration_cast<seconds>(duration).count(), (duration % seconds{1}).count()}; }
there example on this page showing use of arithmetic operators on std::chrono::duration
achieve want.
Comments
Post a Comment