How to convert std :: chrono :: system_clock :: duration to struct timeval

The title says it all.

I need to implement a function that gets the value std :: chrono :: system_clock :: duration, and it needs to convert it to a timeval structure so that I can pass it to some system function.

+4
source share
2 answers

More general implementation.

template<typename Duration> void to_timeval(Duration&& d, struct timeval & tv) { std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d); tv.tv_sec = sec.count(); tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - sec).count(); } 

UPDATE:

Separate methods like to_timeval() are not very convenient. Where is the overload? We only have the hard code timeval in the function name to_timeval() . This is not C ++. I want to pass the struct timeval to, for example, std::chrono::duration_cast() and get my chrono result and vice versa.

So, we can expand std::chrono::duration_cast (of course, at our own peril and risk). Enjoy it.

 namespace std { namespace chrono { namespace detail { template<typename From, typename To> struct posix_duration_cast; // chrono -> timeval caster template<typename Rep, typename Period> struct posix_duration_cast< std::chrono::duration<Rep, Period>, struct timeval > { static struct timeval cast(std::chrono::duration<Rep, Period> const& d) { struct timeval tv; std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d); tv.tv_sec = sec.count(); tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - sec).count(); return std::move(tv); } }; // timeval -> chrono caster template<typename Rep, typename Period> struct posix_duration_cast< struct timeval, std::chrono::duration<Rep, Period> > { static std::chrono::duration<Rep, Period> cast(struct timeval const & tv) { return std::chrono::duration_cast< std::chrono::duration<Rep, Period> >( std::chrono::seconds(tv.tv_sec) + std::chrono::microseconds(tv.tv_usec) ); } }; } // chrono -> timeval template<typename T, typename Rep, typename Period> auto duration_cast(std::chrono::duration<Rep, Period> const& d) -> std::enable_if_t< std::is_same<T, struct timeval>::value, struct timeval > { return detail::posix_duration_cast< std::chrono::duration<Rep, Period>, timeval >::cast(d); } // timeval -> chrono template<typename Duration> Duration duration_cast(struct timeval const& tv) { return detail::posix_duration_cast< struct timeval, Duration >::cast(tv); } } // chrono } // std 

This is just an example. Alternatively, we can implement our own duration_cast() and in some cases forward it to std::chrono::duration_cast() .

And we remember about struct timespec .

+7
source

Let d be the value of the input duration, and tv be the structure of the output time filled with the conversion function. Note: the function sets the timeval value to 0 if the duration is negative.

 void convert( const std::chrono::system_clock::duration &d, timeval &tv ) { chrono::microseconds usec = chrono::duration_cast<chrono::microseconds>(d); if( usec <= chrono::microseconds(0) ) tv.tv_sec = tv.tv_usec = 0; else { tv.tv_sec = usec.count()/1000000; tv.tv_usec = usec.count()%1000000; } } 
+4
source

Source: https://habr.com/ru/post/1489073/


All Articles