5 * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\>
7 * @copyright © Copyright 2008 Intra2Net AG
9 * @contact info@intra2net.com
13 #include "asyncio_time_tools.hpp"
17 #include <sys/timeb.h>
18 #include <sys/syscall.h>
21 // define missing POSIX.1b constants...
23 #ifndef CLOCK_REALTIME
24 #define CLOCK_REALTIME 0
26 #ifndef CLOCK_MONOTONIC
27 #define CLOCK_MONOTONIC 1
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.
46 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
48 struct timespec tp[1];
49 int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
53 nano_seconds= tp->tv_nsec;
56 } // eo monotonic_clock_gettime(long int&,long int&)
61 void get_current_real_time(long& current_sec, long& current_msec)
64 gettimeofday(&tv,NULL);
65 current_sec= tv.tv_sec;
66 current_msec= (tv.tv_usec / 1000);
67 if (current_msec >= 1000)
69 current_sec += (current_msec / 1000);
72 } // eo get_current_real_time
75 void get_current_monotonic_time(long& current_sec, long& current_msec)
78 if (monotonic_clock_gettime(current_sec,nsec))
80 current_msec= nsec / 1000000L;
85 get_current_real_time(current_sec,current_msec);
87 } // eo get_current_monotonic_time
91 } // eo anonymous namespace
93 /**************************************************************************\
94 \**************************************************************************/
98 * implementation of MilliTime
101 MilliTime::MilliTime(long sec, long msec)
102 : mt_sec(sec), mt_msec(msec)
105 } // eo MilliTime::MilliTime
108 void MilliTime::set(long sec, long msec)
113 } // eo MilliTime::set
117 * normalizes the values, so that mt_msec has a value between 0 and 999.
119 void MilliTime::normalize()
123 mt_sec += (mt_msec / 1000) - 1;
124 mt_msec = (mt_msec % 1000) + 1000;
126 else if (mt_msec>=1000)
128 mt_sec+= (mt_msec / 1000);
131 } // eo MilliTime::normalize
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.
139 bool MilliTime::operator < (MilliTime& other)
144 (mt_sec < other.mt_sec)
145 || (( mt_sec == other.mt_sec) && (mt_msec < other.mt_msec));
146 } // eo MilliTime::operator <
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.
154 bool MilliTime::operator == (MilliTime& other)
158 return (( mt_sec == other.mt_sec) && (mt_msec == other.mt_msec));
159 } // eo MilliTime::operator <
162 * @brief subtracts a time delta from the object.
163 * @param lhs the time delta to subtract.
164 * @return reference to the object itself.
166 MilliTime& MilliTime::operator -= (const MilliTime& lhs)
168 mt_sec -= lhs.mt_sec;
169 mt_msec -= lhs.mt_msec;
175 * @brief adds a time delta from the object.
176 * @param lhs the time delta to add.
177 * @return reference to the object itself.
179 MilliTime& MilliTime::operator += (const MilliTime& lhs)
181 mt_sec += lhs.mt_sec;
182 mt_msec += lhs.mt_msec;
189 * @brief gets the current time as MilliTime structure.
190 * @param[out] mt reference to the MilliTime strcucture which is filled with the result.
192 void get_current_real_time(MilliTime& mt)
195 get_current_real_time(sec,msec);
197 } // eo get_current_real_time
201 * @brief gets the current time as MilliTime structure.
202 * @param[out] mt reference to the MilliTime strcucture which is filled with the result.
204 void get_current_monotonic_time(MilliTime& mt)
207 get_current_monotonic_time(sec,msec);
209 } // eo get_current_monotonic_time
215 } // eo namespace Utils
216 } // eo namespace AsyncIo