1 // Copyright (C) 2001-2003
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // boostinspect:nounnamed
10 const int MILLISECONDS_PER_SECOND = 1000;
11 const int NANOSECONDS_PER_SECOND = 1000000000;
12 const int NANOSECONDS_PER_MILLISECOND = 1000000;
14 const int MICROSECONDS_PER_SECOND = 1000000;
15 const int NANOSECONDS_PER_MICROSECOND = 1000;
17 inline void to_time(int milliseconds, boost::xtime& xt)
20 res = boost::xtime_get(&xt, boost::TIME_UTC);
21 assert(res == boost::TIME_UTC);
23 xt.sec += (milliseconds / MILLISECONDS_PER_SECOND);
24 xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) *
25 NANOSECONDS_PER_MILLISECOND);
27 if (xt.nsec >= NANOSECONDS_PER_SECOND)
30 xt.nsec -= NANOSECONDS_PER_SECOND;
34 #if defined(BOOST_HAS_PTHREADS)
35 inline void to_timespec(const boost::xtime& xt, timespec& ts)
37 ts.tv_sec = static_cast<int>(xt.sec);
38 ts.tv_nsec = static_cast<int>(xt.nsec);
39 if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
41 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
42 ts.tv_nsec %= NANOSECONDS_PER_SECOND;
46 inline void to_time(int milliseconds, timespec& ts)
49 to_time(milliseconds, xt);
53 inline void to_timespec_duration(const boost::xtime& xt, timespec& ts)
57 res = boost::xtime_get(&cur, boost::TIME_UTC);
58 assert(res == boost::TIME_UTC);
60 if (boost::xtime_cmp(xt, cur) <= 0)
67 ts.tv_sec = xt.sec - cur.sec;
68 ts.tv_nsec = xt.nsec - cur.nsec;
73 ts.tv_nsec += NANOSECONDS_PER_SECOND;
75 if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
77 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
78 ts.tv_nsec %= NANOSECONDS_PER_SECOND;
84 inline void to_duration(boost::xtime xt, int& milliseconds)
88 res = boost::xtime_get(&cur, boost::TIME_UTC);
89 assert(res == boost::TIME_UTC);
91 if (boost::xtime_cmp(xt, cur) <= 0)
95 if (cur.nsec > xt.nsec)
97 xt.nsec += NANOSECONDS_PER_SECOND;
100 milliseconds = (int)((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
101 (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
102 NANOSECONDS_PER_MILLISECOND);
106 inline void to_microduration(boost::xtime xt, int& microseconds)
110 res = boost::xtime_get(&cur, boost::TIME_UTC);
111 assert(res == boost::TIME_UTC);
113 if (boost::xtime_cmp(xt, cur) <= 0)
117 if (cur.nsec > xt.nsec)
119 xt.nsec += NANOSECONDS_PER_SECOND;
122 microseconds = (int)((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) +
123 (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) /
124 NANOSECONDS_PER_MICROSECOND);
130 // 1 Jun 01 Initial creation.