SNode.C
Loading...
Searching...
No Matches
utils::Timeval Class Reference

#include <Timeval.h>

Collaboration diagram for utils::Timeval:

Public Member Functions

 Timeval () noexcept
 
 Timeval (const std::initializer_list< time_t > &initList) noexcept
 
 Timeval (const Timeval &timeVal) noexcept=default
 
 Timeval (double time) noexcept
 
 Timeval (const timeval &timeVal) noexcept
 
Timevaloperator= (const Timeval &timeVal)
 
Timevaloperator= (const timeval &timeVal)
 
Timeval operator+ (const Timeval &timeVal) const
 
Timeval operator- (const Timeval &timeVal) const
 
Timeval operator* (double mul) const
 
Timevaloperator+= (const Timeval &timeVal)
 
Timevaloperator-= (const Timeval &timeVal)
 
Timevaloperator*= (double mul)
 
Timeval operator- () const
 
bool operator< (const Timeval &timeVal) const
 
bool operator> (const Timeval &timeVal) const
 
bool operator<= (const Timeval &timeVal) const
 
bool operator>= (const Timeval &timeVal) const
 
bool operator== (const Timeval &timeVal) const
 
bool operator!= (const Timeval &timeVal) const
 
timeval * operator& ()
 
const timeval * operator& () const
 
timespec getTimespec () const
 
int getMs () const
 
double getMsd () const
 

Static Public Member Functions

static Timeval currentTime ()
 

Private Member Functions

const Timevalnormalize ()
 

Private Attributes

timeval timeVal = {0, 0}
 

Friends

std::ostream & operator<< (std::ostream &ostream, const utils::Timeval &timeVal)
 
Timeval operator* (double mul, const Timeval &timeVal)
 

Detailed Description

Definition at line 58 of file Timeval.h.

Constructor & Destructor Documentation

◆ Timeval() [1/5]

utils::Timeval::Timeval ( )
noexcept

Definition at line 55 of file Timeval.cpp.

56 : timeVal({}) {
57 }
timeval timeVal
Definition Timeval.h:99

References timeVal.

Referenced by core::EventMultiplexer::getNextTimeout().

Here is the caller graph for this function:

◆ Timeval() [2/5]

utils::Timeval::Timeval ( const std::initializer_list< time_t > & initList)
noexcept

Definition at line 59 of file Timeval.cpp.

60 : timeVal({}) {
61 timeVal.tv_sec = *initList.begin();
62 timeVal.tv_usec = static_cast<suseconds_t>(*(initList.begin() + 1));
63 }

References timeVal.

◆ Timeval() [3/5]

utils::Timeval::Timeval ( const Timeval & timeVal)
defaultnoexcept

Referenced by core::socket::stream::SocketReader::SocketReader().

Here is the caller graph for this function:

◆ Timeval() [4/5]

utils::Timeval::Timeval ( double time)
noexcept

Definition at line 65 of file Timeval.cpp.

65 {
66 timeVal.tv_sec = static_cast<time_t>(time);
67 timeVal.tv_usec = static_cast<suseconds_t>((time - static_cast<double>(timeVal.tv_sec)) * 1'000'000.0);
68
69 normalize();
70 }
const Timeval & normalize()
Definition Timeval.cpp:204
time_t time(time_t *tloc)
Definition time.cpp:52

References normalize(), and timeVal.

Here is the call graph for this function:

◆ Timeval() [5/5]

utils::Timeval::Timeval ( const timeval & timeVal)
noexcept

Definition at line 72 of file Timeval.cpp.

73 : timeVal(timeVal) {
74 }

References timeVal.

Referenced by operator-().

Here is the caller graph for this function:

Member Function Documentation

◆ currentTime()

Timeval utils::Timeval::currentTime ( )
static

Definition at line 76 of file Timeval.cpp.

76 {
79
80 return currentTime;
81 }
static Timeval currentTime()
Definition Timeval.cpp:76
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition time.cpp:57

References utils::system::gettimeofday(), and operator&().

Referenced by core::DescriptorEventPublisher::enable(), core::EventMultiplexer::signal(), core::EventMultiplexer::terminate(), and core::EventMultiplexer::tick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMs()

int utils::Timeval::getMs ( ) const

Definition at line 177 of file Timeval.cpp.

177 {
178 int ms = 0;
179
180 if (timeVal.tv_sec > INT_MAX - 1) {
181 ms = INT_MAX;
182 } else {
183 ms = static_cast<int>(static_cast<double>(timeVal.tv_sec) * 1'000. + static_cast<double>(timeVal.tv_usec) / 1'000. + 0.5);
184 }
185
186 return ms;
187 }

References timeVal.

◆ getMsd()

double utils::Timeval::getMsd ( ) const

Definition at line 189 of file Timeval.cpp.

189 {
190 double msd = 0;
191 if (timeVal.tv_sec > INT_MAX - 1) {
192 msd = INT_MAX;
193 } else {
194 msd = static_cast<double>(timeVal.tv_sec) * 1'000. + static_cast<double>(timeVal.tv_usec) / 1'000.;
195 }
196
197 return msd;
198 }

References timeVal.

◆ getTimespec()

timespec utils::Timeval::getTimespec ( ) const

Definition at line 173 of file Timeval.cpp.

173 {
174 return timespec{timeVal.tv_sec, static_cast<int32_t>(timeVal.tv_usec * 1000)};
175 }

References timeVal.

◆ normalize()

const Timeval & utils::Timeval::normalize ( )
private

Definition at line 204 of file Timeval.cpp.

204 {
205 while (timeVal.tv_usec > 999'999 || timeVal.tv_usec < 0) {
206 if (timeVal.tv_usec > 999'999) {
207 timeVal.tv_usec -= 1'000'000;
208 timeVal.tv_sec++;
209 } else if (timeVal.tv_usec < 0) {
210 timeVal.tv_usec += 1'000'000;
211 timeVal.tv_sec--;
212 }
213 }
214
215 return *this;
216 }

References timeVal.

Referenced by operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-(), operator-=(), and Timeval().

Here is the caller graph for this function:

◆ operator!=()

bool utils::Timeval::operator!= ( const Timeval & timeVal) const

Definition at line 165 of file Timeval.cpp.

165 {
166 return !(*this == timeVal);
167 }

References operator==().

Here is the call graph for this function:

◆ operator&() [1/2]

timeval * utils::Timeval::operator& ( )

Definition at line 169 of file Timeval.cpp.

169 {
170 return &timeVal;
171 }

References timeVal.

Referenced by currentTime().

Here is the caller graph for this function:

◆ operator&() [2/2]

const timeval * utils::Timeval::operator& ( ) const

Definition at line 200 of file Timeval.cpp.

200 {
201 return &timeVal;
202 }

References timeVal.

Referenced by operator=().

Here is the caller graph for this function:

◆ operator*()

Timeval utils::Timeval::operator* ( double mul) const

Definition at line 116 of file Timeval.cpp.

116 {
117 utils::Timeval help;
118
119 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(this->timeVal.tv_sec) * mul);
120 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(this->timeVal.tv_usec) * mul);
121
122 return help.normalize();
123 }

References normalize(), and timeVal.

Referenced by operator*=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator*=()

Timeval & utils::Timeval::operator*= ( double mul)

Definition at line 135 of file Timeval.cpp.

135 {
136 *this = (*this * mul).normalize();
137 return *this;
138 }

References normalize(), operator*(), and operator=().

Here is the call graph for this function:

◆ operator+()

Timeval utils::Timeval::operator+ ( const Timeval & timeVal) const

Definition at line 98 of file Timeval.cpp.

98 {
99 utils::Timeval help;
100
101 help.timeVal.tv_sec = this->timeVal.tv_sec + timeVal.timeVal.tv_sec;
102 help.timeVal.tv_usec = this->timeVal.tv_usec + timeVal.timeVal.tv_usec;
103
104 return help.normalize();
105 }

References normalize(), and timeVal.

Referenced by operator+=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator+=()

Timeval & utils::Timeval::operator+= ( const Timeval & timeVal)

Definition at line 125 of file Timeval.cpp.

125 {
126 *this = (*this + timeVal).normalize();
127 return *this;
128 }

References normalize(), operator+(), and operator=().

Here is the call graph for this function:

◆ operator-() [1/2]

Timeval utils::Timeval::operator- ( ) const

Definition at line 140 of file Timeval.cpp.

140 {
141 return Timeval({-this->timeVal.tv_sec, -this->timeVal.tv_usec}).normalize();
142 }
Timeval() noexcept
Definition Timeval.cpp:55

References normalize(), Timeval(), and timeVal.

Here is the call graph for this function:

◆ operator-() [2/2]

Timeval utils::Timeval::operator- ( const Timeval & timeVal) const

Definition at line 107 of file Timeval.cpp.

107 {
108 utils::Timeval help;
109
110 help.timeVal.tv_sec = this->timeVal.tv_sec - timeVal.timeVal.tv_sec;
111 help.timeVal.tv_usec = this->timeVal.tv_usec - timeVal.timeVal.tv_usec;
112
113 return help.normalize();
114 }

References normalize(), and timeVal.

Referenced by operator-=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator-=()

Timeval & utils::Timeval::operator-= ( const Timeval & timeVal)

Definition at line 130 of file Timeval.cpp.

130 {
131 *this = (*this - timeVal).normalize();
132 return *this;
133 }

References normalize(), operator-(), and operator=().

Here is the call graph for this function:

◆ operator<()

bool utils::Timeval::operator< ( const Timeval & timeVal) const

Definition at line 144 of file Timeval.cpp.

144 {
145 return (this->timeVal.tv_sec < timeVal.timeVal.tv_sec) ||
146 ((this->timeVal.tv_sec == timeVal.timeVal.tv_sec) && (this->timeVal.tv_usec < timeVal.timeVal.tv_usec));
147 }

References timeVal.

Referenced by core::TimerEventPublisher::timernode_lt::operator()(), operator==(), operator>(), and operator>=().

Here is the caller graph for this function:

◆ operator<=()

bool utils::Timeval::operator<= ( const Timeval & timeVal) const

Definition at line 153 of file Timeval.cpp.

153 {
154 return !(*this > timeVal);
155 }

References operator>().

Here is the call graph for this function:

◆ operator=() [1/2]

Timeval & utils::Timeval::operator= ( const Timeval & timeVal)

Definition at line 83 of file Timeval.cpp.

83 { // NOLINT
84 if (&this->timeVal != &timeVal) {
85 this->timeVal.tv_sec = timeVal.timeVal.tv_sec;
86 this->timeVal.tv_usec = timeVal.timeVal.tv_usec;
87 }
88
89 return *this;
90 }

References operator&(), and timeVal.

Referenced by database::mariadb::MariaDBCommand::commandStart(), core::DescriptorEventPublisher::getNextTimeout(), core::EventMultiplexer::getNextTimeout(), operator*=(), operator+=(), and operator-=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [2/2]

Timeval & utils::Timeval::operator= ( const timeval & timeVal)

Definition at line 92 of file Timeval.cpp.

92 {
93 this->timeVal = timeVal;
94
95 return *this;
96 }

References timeVal.

◆ operator==()

bool utils::Timeval::operator== ( const Timeval & timeVal) const

Definition at line 161 of file Timeval.cpp.

161 {
162 return !(*this < timeVal) && !(*this > timeVal);
163 }

References operator<(), and operator>().

Referenced by operator!=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator>()

bool utils::Timeval::operator> ( const Timeval & timeVal) const

Definition at line 149 of file Timeval.cpp.

149 {
150 return timeVal < *this;
151 }

References operator<().

Referenced by operator<=(), and operator==().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator>=()

bool utils::Timeval::operator>= ( const Timeval & timeVal) const

Definition at line 157 of file Timeval.cpp.

157 {
158 return !(*this < timeVal);
159 }

References operator<().

Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ operator*

Timeval operator* ( double mul,
const Timeval & timeVal )
friend

Definition at line 223 of file Timeval.cpp.

223 {
224 utils::Timeval help;
225
226 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(timeVal.timeVal.tv_sec) * mul);
227 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(timeVal.timeVal.tv_usec) * mul);
228
229 return help.normalize();
230 }

◆ operator<<

std::ostream & operator<< ( std::ostream & ostream,
const utils::Timeval & timeVal )
friend

Definition at line 218 of file Timeval.cpp.

218 {
219 return ostream << std::string("{") + std::to_string(timeVal.timeVal.tv_sec) + std::string(":") +
220 std::to_string(timeVal.timeVal.tv_usec) + std::string("}");
221 }

Member Data Documentation

◆ timeVal

timeval utils::Timeval::timeVal = {0, 0}
private

The documentation for this class was generated from the following files: