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 36 of file Timeval.h.

Constructor & Destructor Documentation

◆ Timeval() [1/5]

utils::Timeval::Timeval ( )
noexcept

Definition at line 33 of file Timeval.cpp.

34 : timeVal({}) {
35 }
timeval timeVal
Definition Timeval.h:77

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 37 of file Timeval.cpp.

38 : timeVal({}) {
39 timeVal.tv_sec = *initList.begin();
40 timeVal.tv_usec = static_cast<suseconds_t>(*(initList.begin() + 1));
41 }

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 43 of file Timeval.cpp.

43 {
44 timeVal.tv_sec = static_cast<time_t>(time);
45 timeVal.tv_usec = static_cast<suseconds_t>((time - static_cast<double>(timeVal.tv_sec)) * 1'000'000.0);
46
47 normalize();
48 }
const Timeval & normalize()
Definition Timeval.cpp:182
time_t time(time_t *tloc)
Definition time.cpp:30

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 50 of file Timeval.cpp.

51 : timeVal(timeVal) {
52 }

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 54 of file Timeval.cpp.

54 {
57
58 return currentTime;
59 }
static Timeval currentTime()
Definition Timeval.cpp:54
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition time.cpp:35

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 155 of file Timeval.cpp.

155 {
156 int ms = 0;
157
158 if (timeVal.tv_sec > INT_MAX - 1) {
159 ms = INT_MAX;
160 } else {
161 ms = static_cast<int>(static_cast<double>(timeVal.tv_sec) * 1'000. + static_cast<double>(timeVal.tv_usec) / 1'000. + 0.5);
162 }
163
164 return ms;
165 }

References timeVal.

◆ getMsd()

double utils::Timeval::getMsd ( ) const

Definition at line 167 of file Timeval.cpp.

167 {
168 double msd = 0;
169 if (timeVal.tv_sec > INT_MAX - 1) {
170 msd = INT_MAX;
171 } else {
172 msd = static_cast<double>(timeVal.tv_sec) * 1'000. + static_cast<double>(timeVal.tv_usec) / 1'000.;
173 }
174
175 return msd;
176 }

References timeVal.

◆ getTimespec()

timespec utils::Timeval::getTimespec ( ) const

Definition at line 151 of file Timeval.cpp.

151 {
152 return timespec{timeVal.tv_sec, static_cast<int32_t>(timeVal.tv_usec * 1000)};
153 }

References timeVal.

◆ normalize()

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

Definition at line 182 of file Timeval.cpp.

182 {
183 while (timeVal.tv_usec > 999'999 || timeVal.tv_usec < 0) {
184 if (timeVal.tv_usec > 999'999) {
185 timeVal.tv_usec -= 1'000'000;
186 timeVal.tv_sec++;
187 } else if (timeVal.tv_usec < 0) {
188 timeVal.tv_usec += 1'000'000;
189 timeVal.tv_sec--;
190 }
191 }
192
193 return *this;
194 }

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 143 of file Timeval.cpp.

143 {
144 return !(*this == timeVal);
145 }

References operator==().

Here is the call graph for this function:

◆ operator&() [1/2]

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

Definition at line 147 of file Timeval.cpp.

147 {
148 return &timeVal;
149 }

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 178 of file Timeval.cpp.

178 {
179 return &timeVal;
180 }

References timeVal.

Referenced by operator=().

Here is the caller graph for this function:

◆ operator*()

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

Definition at line 94 of file Timeval.cpp.

94 {
95 utils::Timeval help;
96
97 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(this->timeVal.tv_sec) * mul);
98 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(this->timeVal.tv_usec) * mul);
99
100 return help.normalize();
101 }

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 113 of file Timeval.cpp.

113 {
114 *this = (*this * mul).normalize();
115 return *this;
116 }

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 76 of file Timeval.cpp.

76 {
77 utils::Timeval help;
78
79 help.timeVal.tv_sec = this->timeVal.tv_sec + timeVal.timeVal.tv_sec;
80 help.timeVal.tv_usec = this->timeVal.tv_usec + timeVal.timeVal.tv_usec;
81
82 return help.normalize();
83 }

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 103 of file Timeval.cpp.

103 {
104 *this = (*this + timeVal).normalize();
105 return *this;
106 }

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

Here is the call graph for this function:

◆ operator-() [1/2]

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

Definition at line 118 of file Timeval.cpp.

118 {
119 return Timeval({-this->timeVal.tv_sec, -this->timeVal.tv_usec}).normalize();
120 }
Timeval() noexcept
Definition Timeval.cpp:33

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 85 of file Timeval.cpp.

85 {
86 utils::Timeval help;
87
88 help.timeVal.tv_sec = this->timeVal.tv_sec - timeVal.timeVal.tv_sec;
89 help.timeVal.tv_usec = this->timeVal.tv_usec - timeVal.timeVal.tv_usec;
90
91 return help.normalize();
92 }

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 108 of file Timeval.cpp.

108 {
109 *this = (*this - timeVal).normalize();
110 return *this;
111 }

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 122 of file Timeval.cpp.

122 {
123 return (this->timeVal.tv_sec < timeVal.timeVal.tv_sec) ||
124 ((this->timeVal.tv_sec == timeVal.timeVal.tv_sec) && (this->timeVal.tv_usec < timeVal.timeVal.tv_usec));
125 }

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 131 of file Timeval.cpp.

131 {
132 return !(*this > timeVal);
133 }

References operator>().

Here is the call graph for this function:

◆ operator=() [1/2]

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

Definition at line 61 of file Timeval.cpp.

61 { // NOLINT
62 if (&this->timeVal != &timeVal) {
63 this->timeVal.tv_sec = timeVal.timeVal.tv_sec;
64 this->timeVal.tv_usec = timeVal.timeVal.tv_usec;
65 }
66
67 return *this;
68 }

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 70 of file Timeval.cpp.

70 {
71 this->timeVal = timeVal;
72
73 return *this;
74 }

References timeVal.

◆ operator==()

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

Definition at line 139 of file Timeval.cpp.

139 {
140 return !(*this < timeVal) && !(*this > timeVal);
141 }

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 127 of file Timeval.cpp.

127 {
128 return timeVal < *this;
129 }

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 135 of file Timeval.cpp.

135 {
136 return !(*this < timeVal);
137 }

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 201 of file Timeval.cpp.

201 {
202 utils::Timeval help;
203
204 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(timeVal.timeVal.tv_sec) * mul);
205 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(timeVal.timeVal.tv_usec) * mul);
206
207 return help.normalize();
208 }

◆ operator<<

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

Definition at line 196 of file Timeval.cpp.

196 {
197 return ostream << std::string("{") + std::to_string(timeVal.timeVal.tv_sec) + std::string(":") +
198 std::to_string(timeVal.timeVal.tv_usec) + std::string("}");
199 }

Member Data Documentation

◆ timeVal

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

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