libmoost
/home/mhx/git/github/libmoost/include/moost/thread/xtime_util.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_THREAD_XTIME_UTIL_HPP__
00029 #define MOOST_THREAD_XTIME_UTIL_HPP__
00030 
00031 #include <boost/thread/xtime.hpp>
00032 
00033 namespace moost { namespace thread {
00034 
00036 struct xtime_util
00037 {
00038   static const int NSECS_PER_SEC = 1000000000;
00039   static const int NSECS_PER_MILLISEC = 1000000;
00040 
00041 
00043   static boost::xtime now()
00044   {
00045     boost::xtime ret_val;
00046     xtime_get(&ret_val, boost::TIME_UTC);
00047 
00048     return ret_val;
00049   }
00050 
00052   static boost::xtime add_ms(const boost::xtime & time, int milliseconds)
00053   {
00054     boost::xtime ret_val;
00055 
00056     if ( milliseconds < NSECS_PER_SEC / NSECS_PER_MILLISEC )
00057     {
00058        ret_val.sec  = time.sec;
00059        ret_val.nsec = time.nsec + milliseconds * NSECS_PER_MILLISEC;
00060     }
00061     else
00062     {
00063        ret_val.sec  = time.sec + milliseconds / 1000;
00064        ret_val.nsec = time.nsec + milliseconds % NSECS_PER_SEC;
00065     }
00066 
00067     if (ret_val.nsec >= NSECS_PER_SEC)
00068     {
00069       ret_val.sec += ret_val.nsec / NSECS_PER_SEC;
00070       ret_val.nsec = ret_val.nsec % NSECS_PER_SEC;
00071     }
00072 
00073     return ret_val;
00074   }
00075 
00077   static boost::xtime add_sec(const boost::xtime & time, int seconds)
00078   {
00079     boost::xtime ret_val;
00080 
00081     ret_val.sec = time.sec + seconds;
00082     ret_val.nsec = time.nsec;
00083 
00084     return ret_val;
00085   }
00086 };
00087 
00088 }} // moost::thread
00089 
00090 #endif // MOOST_THREAD_XTIME_UTIL_HPP__