Boost logo

Boost-Commit :

From: jeff_at_[hidden]
Date: 2007-10-14 22:22:39


Author: az_sw_dude
Date: 2007-10-14 22:22:38 EDT (Sun, 14 Oct 2007)
New Revision: 40039
URL: http://svn.boost.org/trac/boost/changeset/40039

Log:
add implementation and tests for to_timeval conversion function
Text files modified:
   sandbox/committee/LWG/ref_impl/date_time | 23 ++++++++++++++++++++++-
   sandbox/committee/LWG/ref_impl/test/test_datetime.cpp | 29 ++++++++++++++++++++++++++++-
   2 files changed, 50 insertions(+), 2 deletions(-)

Modified: sandbox/committee/LWG/ref_impl/date_time
==============================================================================
--- sandbox/committee/LWG/ref_impl/date_time (original)
+++ sandbox/committee/LWG/ref_impl/date_time 2007-10-14 22:22:38 EDT (Sun, 14 Oct 2007)
@@ -382,7 +382,28 @@
    return system_time(time_since_epoch);
  }
 
+} //namespace std
+
+
+
+
+ namespace detail {
+
+ template<typename time_duration_type>
+ inline
+ timeval to_timeval(const time_duration_type& td) {
+ std::nanoseconds ns(td);
+ int64_t count = ns.get_count();
+ int64_t sec_count = count/int64_t(1000000000);
+ //remainder after sec -- adjusted from nano back to micro
+ count = (count%int64_t(1000000000))/int64_t(1000);
+ timeval tv;
+ tv.tv_sec = sec_count;
+ tv.tv_usec = count;
+
+ return tv;
+ }
+ }
 
-}
 
 #endif

Modified: sandbox/committee/LWG/ref_impl/test/test_datetime.cpp
==============================================================================
--- sandbox/committee/LWG/ref_impl/test/test_datetime.cpp (original)
+++ sandbox/committee/LWG/ref_impl/test/test_datetime.cpp 2007-10-14 22:22:38 EDT (Sun, 14 Oct 2007)
@@ -57,6 +57,32 @@
 
 };
 
+void check_native_conversion()
+{
+ {
+ std::nanoseconds ns(0);
+ timeval tv = detail::to_timeval(ns);
+ std::cout << "Timeval is : " << tv.tv_sec << " " << tv.tv_usec << std::endl;
+ }
+ {
+ std::seconds s(1);
+ timeval tv = detail::to_timeval(s);
+ std::cout << "Timeval is : " << tv.tv_sec << " " << tv.tv_usec << std::endl;
+ }
+ {
+ std::microseconds ms(1);
+ timeval tv = detail::to_timeval(ms);
+ std::cout << "Timeval is : " << tv.tv_sec << " " << tv.tv_usec << std::endl;
+ }
+ {
+ std::microseconds ms(1);
+ ms += seconds(1);
+ timeval tv = detail::to_timeval(ms);
+ std::cout << "Timeval is : " << tv.tv_sec << " " << tv.tv_usec << std::endl;
+ }
+
+}
+
 
 
 int
@@ -104,7 +130,8 @@
 
 
   do_conversions();
-
+ check_native_conversion();
+
   return 0;
 
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk