2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
24 * @author Reinhard Pfau \<reinhard.pfau@gmx.de\>
26 * @copyright © Copyright 2008-2009 Intra2Net AG
27 * @contact Intra2net Opensource team \<opensource@intra2net.com\>
30 #include "asyncio_time_tools.hpp"
34 #include <sys/timeb.h>
35 #include <sys/syscall.h>
38 // define missing POSIX.1b constants...
40 #ifndef CLOCK_REALTIME
41 #define CLOCK_REALTIME 0
43 #ifndef CLOCK_MONOTONIC
44 #define CLOCK_MONOTONIC 1
58 * @brief fetches the value from the monotonic clock source.
59 * @param[out] seconds the seconds.
60 * @param[out] nano_seconds the nano seconds.
61 * @return @a true if the clock was successfully read.
63 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
65 struct timespec tp[1];
66 int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
70 nano_seconds= tp->tv_nsec;
73 } // eo monotonic_clock_gettime(long int&,long int&)
78 void get_current_real_time(long& current_sec, long& current_msec)
81 gettimeofday(&tv,NULL);
82 current_sec= tv.tv_sec;
83 current_msec= (tv.tv_usec / 1000);
84 if (current_msec >= 1000)
86 current_sec += (current_msec / 1000);
89 } // eo get_current_real_time
92 void get_current_monotonic_time(long& current_sec, long& current_msec)
95 if (monotonic_clock_gettime(current_sec,nsec))
97 current_msec= nsec / 1000000L;
102 get_current_real_time(current_sec,current_msec);
104 } // eo get_current_monotonic_time
108 } // eo anonymous namespace
110 /**************************************************************************\
111 \**************************************************************************/
115 * implementation of MilliTime
118 MilliTime::MilliTime(long sec, long msec)
119 : mt_sec(sec), mt_msec(msec)
122 } // eo MilliTime::MilliTime
125 void MilliTime::set(long sec, long msec)
130 } // eo MilliTime::set
134 * normalizes the values, so that mt_msec has a value between 0 and 999.
136 void MilliTime::normalize()
140 mt_sec += (mt_msec / 1000) - 1;
141 mt_msec = (mt_msec % 1000) + 1000;
143 else if (mt_msec>=1000)
145 mt_sec+= (mt_msec / 1000);
148 } // eo MilliTime::normalize
152 * determine if the represented point in time is before another one.
153 * @param other the other point in time.
154 * @return true if the own point in time is before the other one.
156 bool MilliTime::operator < (MilliTime& other)
161 (mt_sec < other.mt_sec)
162 || (( mt_sec == other.mt_sec) && (mt_msec < other.mt_msec));
163 } // eo MilliTime::operator <
167 * determine if two point in times are equal.
168 * @param other the point in time to compare with.
169 * @return true if the represented times are equal.
171 bool MilliTime::operator == (MilliTime& other)
175 return (( mt_sec == other.mt_sec) && (mt_msec == other.mt_msec));
176 } // eo MilliTime::operator <
179 * @brief subtracts a time delta from the object.
180 * @param lhs the time delta to subtract.
181 * @return reference to the object itself.
183 MilliTime& MilliTime::operator -= (const MilliTime& lhs)
185 mt_sec -= lhs.mt_sec;
186 mt_msec -= lhs.mt_msec;
192 * @brief adds a time delta from the object.
193 * @param lhs the time delta to add.
194 * @return reference to the object itself.
196 MilliTime& MilliTime::operator += (const MilliTime& lhs)
198 mt_sec += lhs.mt_sec;
199 mt_msec += lhs.mt_msec;
206 * @brief gets the current time as MilliTime structure.
207 * @param[out] mt reference to the MilliTime structure which is filled with the result.
209 void get_current_real_time(MilliTime& mt)
212 get_current_real_time(sec,msec);
214 } // eo get_current_real_time
218 * @brief gets the current time as MilliTime structure.
219 * @param[out] mt reference to the MilliTime structure which is filled with the result.
221 void get_current_monotonic_time(MilliTime& mt)
224 get_current_monotonic_time(sec,msec);
226 } // eo get_current_monotonic_time
232 } // eo namespace Utils
233 } // eo namespace AsyncIo