[date_time] monotonic_time() proposal

This proposal suggests adding monotonic_time() static member function to the boost::date_time::microsec_clock and second_clock classes. usage example: #include <unistd.h> // for sleep() #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include "monotonic_time.hpp" int main() { namespace pt = boost::posix_time; typedef ext::microsec_clock<pt::ptime> usclock_t; // namespace ext instead of boost::date_time pt::ptime now = usclock_t::monotonic_time(); sleep(1); pt::ptime later = usclock_t::monotonic_time(); std::cout << later - now << std::endl; } Prototype implementation can be found in the attachment. If there is interest i am willing to finish the implementation and documentation. BR, Dmitry

On 02/26/2010 12:16 PM, Dmitry Goncharov wrote:
This proposal suggests adding monotonic_time() static member function to the boost::date_time::microsec_clock and second_clock classes.
usage example:
#include <unistd.h> // for sleep() #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include "monotonic_time.hpp"
int main() { namespace pt = boost::posix_time;
typedef ext::microsec_clock<pt::ptime> usclock_t; // namespace ext instead of boost::date_time pt::ptime now = usclock_t::monotonic_time(); sleep(1); pt::ptime later = usclock_t::monotonic_time(); std::cout << later - now << std::endl; }
Prototype implementation can be found in the attachment. If there is interest i am willing to finish the implementation and documentation.
I think there is something similar in Boost.Chrono in the sandbox. I think it would be a useful addition to Boost, whether as a part of Boost.DateTime or Boost.Chrono. Just make sure that it doesn't break microsec_clock if a monotonic timer is not available.

Andrey Semashev wrote:
I think there is something similar in Boost.Chrono in the sandbox.
Yes, there is.
I think it would be a useful addition to Boost, whether as a part of Boost.DateTime or Boost.Chrono. Just make sure that it doesn't break microsec_clock if a monotonic timer is not available. Good point. It is possible to fallback to universal_time() when no monotonic timer is available. In this case the users of microsec_clock can safely use monotonic_time() on any platform. Another approach is to remove monotonic_time() away from microsec_clock if no monotonic timer is available. In this case the users'll have to handle this situation. Most of the time they'll end up using universal_time() anyway.
BR, Dmitry

On 02/27/2010 11:52 AM, Dmitry Goncharov wrote:
Just make sure that it doesn't break microsec_clock if a monotonic timer is not available. Good point. It is possible to fallback to universal_time() when no monotonic timer is available. In this case the users of microsec_clock can safely use monotonic_time() on any platform. Another approach is to remove monotonic_time() away from microsec_clock if no monotonic timer is available. In this case the users'll have to handle this situation. Most of the time they'll end up using universal_time() anyway.
Why not introduce monotonic_clock? With a configuration macro to detect its absence.

Andrey Semashev wrote:
On 02/27/2010 11:52 AM, Dmitry Goncharov wrote:
Why not introduce monotonic_clock? With a configuration macro to detect its absence.
There is no need for an additional class. microsec_clock and second_clock have the following interface (simplified) template<typename time_type> class microsec_clock { // or class second_clock. public: static time_type local_time() ; static time_type universal_time() ; template<typename time_zone_type> static time_type local_time(shared_ptr< time_zone_type >) ; ... }; static time_type monotinoc_time() is obviously missing. pt::ptime now = microsec_clock::monotonic_time(); // monotonic time with usec resolution pt::ptime now2 = microsec_clock::universal_time(); // universal time with usec resolution pt::ptime now3 = second_clock::monotonic_time(); // monotonic time with sec resolution BR, Dmitry
participants (2)
-
Andrey Semashev
-
Dmitry Goncharov