Commit | Line | Data |
---|---|---|
aba4c34d RP |
1 | /** |
2 | * @file | |
3 | * @brief | |
4 | * | |
5 | * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\> | |
6 | * | |
7 | * @copyright © Copyright 2008 Intra2Net AG | |
8 | * @license LGPL | |
9 | * @contact info@intra2net.com | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "asyncio_time_tools.hpp" | |
14 | ||
15 | #include <sys/time.h> | |
16 | #include <unistd.h> | |
17 | #include <sys/timeb.h> | |
18 | #include <sys/syscall.h> | |
19 | ||
20 | ||
21 | // define missing POSIX.1b constants... | |
22 | ||
23 | #ifndef CLOCK_REALTIME | |
24 | #define CLOCK_REALTIME 0 | |
25 | #endif | |
26 | #ifndef CLOCK_MONOTONIC | |
27 | #define CLOCK_MONOTONIC 1 | |
28 | #endif | |
29 | ||
30 | namespace AsyncIo | |
31 | { | |
32 | namespace Utils | |
33 | { | |
34 | ||
35 | ||
36 | namespace | |
37 | { | |
38 | ||
39 | ||
40 | /** | |
41 | * @brief fetches the value from the monotonic clock source. | |
42 | * @param[out] seconds the seconds. | |
43 | * @param[out] nano_seconds the nano seconds. | |
44 | * @return @a true if the clock was successfully read. | |
45 | */ | |
46 | bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds) | |
47 | { | |
48 | struct timespec tp[1]; | |
49 | int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp); | |
50 | if (0 == res) | |
51 | { | |
52 | seconds= tp->tv_sec; | |
53 | nano_seconds= tp->tv_nsec; | |
54 | } | |
55 | return (res==0); | |
56 | } // eo monotonic_clock_gettime(long int&,long int&) | |
57 | ||
58 | ||
59 | ||
60 | ||
61 | void get_current_real_time(long& current_sec, long& current_msec) | |
62 | { | |
63 | struct timeval tv; | |
64 | gettimeofday(&tv,NULL); | |
65 | current_sec= tv.tv_sec; | |
66 | current_msec= (tv.tv_usec / 1000); | |
67 | if (current_msec >= 1000) | |
68 | { | |
69 | current_sec += (current_msec / 1000); | |
70 | current_msec%= 1000; | |
71 | } | |
72 | } // eo get_current_real_time | |
73 | ||
74 | ||
75 | void get_current_monotonic_time(long& current_sec, long& current_msec) | |
76 | { | |
77 | long nsec; | |
78 | if (monotonic_clock_gettime(current_sec,nsec)) | |
79 | { | |
80 | current_msec= nsec / 1000000L; | |
81 | } | |
82 | else | |
83 | { | |
84 | //fallback... | |
85 | get_current_real_time(current_sec,current_msec); | |
86 | } | |
87 | } // eo get_current_monotonic_time | |
88 | ||
89 | ||
90 | ||
91 | } // eo anonymous namespace | |
92 | ||
93 | /**************************************************************************\ | |
94 | \**************************************************************************/ | |
95 | ||
96 | ||
97 | /* | |
98 | * implementation of MilliTime | |
99 | */ | |
100 | ||
101 | MilliTime::MilliTime(long sec, long msec) | |
102 | : mt_sec(sec), mt_msec(msec) | |
103 | { | |
104 | normalize(); | |
105 | } // eo MilliTime::MilliTime | |
106 | ||
107 | ||
108 | void MilliTime::set(long sec, long msec) | |
109 | { | |
110 | mt_sec= sec; | |
111 | mt_msec= msec; | |
112 | normalize(); | |
113 | } // eo MilliTime::set | |
114 | ||
115 | ||
116 | /** | |
117 | * normalizes the values, so that mt_msec has a value between 0 and 999. | |
118 | */ | |
119 | void MilliTime::normalize() | |
120 | { | |
121 | if (mt_msec < 0) | |
122 | { | |
123 | mt_sec += (mt_msec / 1000) - 1; | |
124 | mt_msec = (mt_msec % 1000) + 1000; | |
125 | } | |
126 | else if (mt_msec>=1000) | |
127 | { | |
128 | mt_sec+= (mt_msec / 1000); | |
129 | mt_msec %= 1000; | |
130 | } | |
131 | } // eo MilliTime::normalize | |
132 | ||
133 | ||
134 | /** | |
135 | * determine if the represented point in time is before another one. | |
136 | * @param other the other point in time. | |
137 | * @return true if the own point in time is before the other one. | |
138 | */ | |
139 | bool MilliTime::operator < (MilliTime& other) | |
140 | { | |
141 | normalize(); | |
142 | other.normalize(); | |
143 | return | |
144 | (mt_sec < other.mt_sec) | |
145 | || (( mt_sec == other.mt_sec) && (mt_msec < other.mt_msec)); | |
146 | } // eo MilliTime::operator < | |
147 | ||
148 | ||
149 | /** | |
150 | * determine if two point in times are equal. | |
151 | * @param other the point in time to compare with. | |
152 | * @return true if the represented times are equal. | |
153 | */ | |
154 | bool MilliTime::operator == (MilliTime& other) | |
155 | { | |
156 | normalize(); | |
157 | other.normalize(); | |
158 | return (( mt_sec == other.mt_sec) && (mt_msec == other.mt_msec)); | |
159 | } // eo MilliTime::operator < | |
160 | ||
161 | /** | |
162 | * @brief subtracts a time delta from the object. | |
163 | * @param lhs the time delta to subtract. | |
164 | * @return reference to the object itself. | |
165 | */ | |
166 | MilliTime& MilliTime::operator -= (const MilliTime& lhs) | |
167 | { | |
168 | mt_sec -= lhs.mt_sec; | |
169 | mt_msec -= lhs.mt_msec; | |
54a4d0ab | 170 | return *this; |
aba4c34d RP |
171 | } // eo operator -= |
172 | ||
173 | ||
174 | /** | |
175 | * @brief adds a time delta from the object. | |
176 | * @param lhs the time delta to add. | |
177 | * @return reference to the object itself. | |
178 | */ | |
179 | MilliTime& MilliTime::operator += (const MilliTime& lhs) | |
180 | { | |
181 | mt_sec += lhs.mt_sec; | |
182 | mt_msec += lhs.mt_msec; | |
54a4d0ab | 183 | return *this; |
aba4c34d RP |
184 | } // eo operator += |
185 | ||
186 | ||
187 | ||
188 | /** | |
189 | * @brief gets the current time as MilliTime structure. | |
190 | * @param[out] mt reference to the MilliTime strcucture which is filled with the result. | |
191 | */ | |
192 | void get_current_real_time(MilliTime& mt) | |
193 | { | |
194 | long sec, msec; | |
195 | get_current_real_time(sec,msec); | |
196 | mt.set(sec,msec); | |
197 | } // eo get_current_real_time | |
198 | ||
199 | ||
200 | /** | |
201 | * @brief gets the current time as MilliTime structure. | |
202 | * @param[out] mt reference to the MilliTime strcucture which is filled with the result. | |
203 | */ | |
204 | void get_current_monotonic_time(MilliTime& mt) | |
205 | { | |
206 | long sec, msec; | |
207 | get_current_monotonic_time(sec,msec); | |
208 | mt.set(sec,msec); | |
209 | } // eo get_current_monotonic_time | |
210 | ||
211 | ||
212 | ||
213 | ||
214 | ||
215 | } // eo namespace Utils | |
216 | } // eo namespace AsyncIo |