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@intra2net.com\>
26 * @copyright © Copyright 2008 Intra2Net AG
29 #include "asyncio_time_tools.hpp"
33 #include <sys/timeb.h>
34 #include <sys/syscall.h>
37 // define missing POSIX.1b constants...
39 #ifndef CLOCK_REALTIME
40 #define CLOCK_REALTIME 0
42 #ifndef CLOCK_MONOTONIC
43 #define CLOCK_MONOTONIC 1
57 * @brief fetches the value from the monotonic clock source.
58 * @param[out] seconds the seconds.
59 * @param[out] nano_seconds the nano seconds.
60 * @return @a true if the clock was successfully read.
62 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
64 struct timespec tp[1];
65 int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
69 nano_seconds= tp->tv_nsec;
72 } // eo monotonic_clock_gettime(long int&,long int&)
77 void get_current_real_time(long& current_sec, long& current_msec)
80 gettimeofday(&tv,NULL);
81 current_sec= tv.tv_sec;
82 current_msec= (tv.tv_usec / 1000);
83 if (current_msec >= 1000)
85 current_sec += (current_msec / 1000);
88 } // eo get_current_real_time
91 void get_current_monotonic_time(long& current_sec, long& current_msec)
94 if (monotonic_clock_gettime(current_sec,nsec))
96 current_msec= nsec / 1000000L;
101 get_current_real_time(current_sec,current_msec);
103 } // eo get_current_monotonic_time
107 } // eo anonymous namespace
109 /**************************************************************************\
110 \**************************************************************************/
114 * implementation of MilliTime
117 MilliTime::MilliTime(long sec, long msec)
118 : mt_sec(sec), mt_msec(msec)
121 } // eo MilliTime::MilliTime
124 void MilliTime::set(long sec, long msec)
129 } // eo MilliTime::set
133 * normalizes the values, so that mt_msec has a value between 0 and 999.
135 void MilliTime::normalize()
139 mt_sec += (mt_msec / 1000) - 1;
140 mt_msec = (mt_msec % 1000) + 1000;
142 else if (mt_msec>=1000)
144 mt_sec+= (mt_msec / 1000);
147 } // eo MilliTime::normalize
151 * determine if the represented point in time is before another one.
152 * @param other the other point in time.
153 * @return true if the own point in time is before the other one.
155 bool MilliTime::operator < (MilliTime& other)
160 (mt_sec < other.mt_sec)
161 || (( mt_sec == other.mt_sec) && (mt_msec < other.mt_msec));
162 } // eo MilliTime::operator <
166 * determine if two point in times are equal.
167 * @param other the point in time to compare with.
168 * @return true if the represented times are equal.
170 bool MilliTime::operator == (MilliTime& other)
174 return (( mt_sec == other.mt_sec) && (mt_msec == other.mt_msec));
175 } // eo MilliTime::operator <
178 * @brief subtracts a time delta from the object.
179 * @param lhs the time delta to subtract.
180 * @return reference to the object itself.
182 MilliTime& MilliTime::operator -= (const MilliTime& lhs)
184 mt_sec -= lhs.mt_sec;
185 mt_msec -= lhs.mt_msec;
191 * @brief adds a time delta from the object.
192 * @param lhs the time delta to add.
193 * @return reference to the object itself.
195 MilliTime& MilliTime::operator += (const MilliTime& lhs)
197 mt_sec += lhs.mt_sec;
198 mt_msec += lhs.mt_msec;
205 * @brief gets the current time as MilliTime structure.
206 * @param[out] mt reference to the MilliTime strcucture which is filled with the result.
208 void get_current_real_time(MilliTime& mt)
211 get_current_real_time(sec,msec);
213 } // eo get_current_real_time
217 * @brief gets the current time as MilliTime structure.
218 * @param[out] mt reference to the MilliTime strcucture which is filled with the result.
220 void get_current_monotonic_time(MilliTime& mt)
223 get_current_monotonic_time(sec,msec);
225 } // eo get_current_monotonic_time
231 } // eo namespace Utils
232 } // eo namespace AsyncIo