1 | // Copyright (C) 2001-2003 |
---|
2 | // William E. Kempf |
---|
3 | // |
---|
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) |
---|
6 | |
---|
7 | // boostinspect:nounnamed |
---|
8 | |
---|
9 | namespace { |
---|
10 | const int MILLISECONDS_PER_SECOND = 1000; |
---|
11 | const int NANOSECONDS_PER_SECOND = 1000000000; |
---|
12 | const int NANOSECONDS_PER_MILLISECOND = 1000000; |
---|
13 | |
---|
14 | const int MICROSECONDS_PER_SECOND = 1000000; |
---|
15 | const int NANOSECONDS_PER_MICROSECOND = 1000; |
---|
16 | |
---|
17 | inline void to_time(int milliseconds, boost::xtime& xt) |
---|
18 | { |
---|
19 | int res = 0; |
---|
20 | res = boost::xtime_get(&xt, boost::TIME_UTC); |
---|
21 | assert(res == boost::TIME_UTC); |
---|
22 | |
---|
23 | xt.sec += (milliseconds / MILLISECONDS_PER_SECOND); |
---|
24 | xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) * |
---|
25 | NANOSECONDS_PER_MILLISECOND); |
---|
26 | |
---|
27 | if (xt.nsec >= NANOSECONDS_PER_SECOND) |
---|
28 | { |
---|
29 | ++xt.sec; |
---|
30 | xt.nsec -= NANOSECONDS_PER_SECOND; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | #if defined(BOOST_HAS_PTHREADS) |
---|
35 | inline void to_timespec(const boost::xtime& xt, timespec& ts) |
---|
36 | { |
---|
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) |
---|
40 | { |
---|
41 | ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; |
---|
42 | ts.tv_nsec %= NANOSECONDS_PER_SECOND; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | inline void to_time(int milliseconds, timespec& ts) |
---|
47 | { |
---|
48 | boost::xtime xt; |
---|
49 | to_time(milliseconds, xt); |
---|
50 | to_timespec(xt, ts); |
---|
51 | } |
---|
52 | |
---|
53 | inline void to_timespec_duration(const boost::xtime& xt, timespec& ts) |
---|
54 | { |
---|
55 | boost::xtime cur; |
---|
56 | int res = 0; |
---|
57 | res = boost::xtime_get(&cur, boost::TIME_UTC); |
---|
58 | assert(res == boost::TIME_UTC); |
---|
59 | |
---|
60 | if (boost::xtime_cmp(xt, cur) <= 0) |
---|
61 | { |
---|
62 | ts.tv_sec = 0; |
---|
63 | ts.tv_nsec = 0; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | ts.tv_sec = xt.sec - cur.sec; |
---|
68 | ts.tv_nsec = xt.nsec - cur.nsec; |
---|
69 | |
---|
70 | if( ts.tv_nsec < 0 ) |
---|
71 | { |
---|
72 | ts.tv_sec -= 1; |
---|
73 | ts.tv_nsec += NANOSECONDS_PER_SECOND; |
---|
74 | } |
---|
75 | if(ts.tv_nsec >= NANOSECONDS_PER_SECOND) |
---|
76 | { |
---|
77 | ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; |
---|
78 | ts.tv_nsec %= NANOSECONDS_PER_SECOND; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | inline void to_duration(boost::xtime xt, int& milliseconds) |
---|
85 | { |
---|
86 | boost::xtime cur; |
---|
87 | int res = 0; |
---|
88 | res = boost::xtime_get(&cur, boost::TIME_UTC); |
---|
89 | assert(res == boost::TIME_UTC); |
---|
90 | |
---|
91 | if (boost::xtime_cmp(xt, cur) <= 0) |
---|
92 | milliseconds = 0; |
---|
93 | else |
---|
94 | { |
---|
95 | if (cur.nsec > xt.nsec) |
---|
96 | { |
---|
97 | xt.nsec += NANOSECONDS_PER_SECOND; |
---|
98 | --xt.sec; |
---|
99 | } |
---|
100 | milliseconds = (int)((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) + |
---|
101 | (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) / |
---|
102 | NANOSECONDS_PER_MILLISECOND); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | inline void to_microduration(boost::xtime xt, int& microseconds) |
---|
107 | { |
---|
108 | boost::xtime cur; |
---|
109 | int res = 0; |
---|
110 | res = boost::xtime_get(&cur, boost::TIME_UTC); |
---|
111 | assert(res == boost::TIME_UTC); |
---|
112 | |
---|
113 | if (boost::xtime_cmp(xt, cur) <= 0) |
---|
114 | microseconds = 0; |
---|
115 | else |
---|
116 | { |
---|
117 | if (cur.nsec > xt.nsec) |
---|
118 | { |
---|
119 | xt.nsec += NANOSECONDS_PER_SECOND; |
---|
120 | --xt.sec; |
---|
121 | } |
---|
122 | microseconds = (int)((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) + |
---|
123 | (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) / |
---|
124 | NANOSECONDS_PER_MICROSECOND); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | // Change Log: |
---|
130 | // 1 Jun 01 Initial creation. |
---|