SNode.C
Loading...
Searching...
No Matches
Timeval.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef DOXYGEN_SHOULD_SKIP_THIS
43
44#include "utils/Timeval.h"
45
46#include <climits>
47#include <cstdint>
48#include <string>
49#include <sys/types.h>
50
51#endif /* DOXYGEN_SHOULD_SKIP_THIS */
52
53namespace utils {
54
55 Timeval::Timeval() noexcept
56 : timeVal({}) {
57 }
58
59 Timeval::Timeval(const std::initializer_list<time_t>& initList) noexcept
60 : timeVal({}) {
61 timeVal.tv_sec = *initList.begin();
62 timeVal.tv_usec = static_cast<suseconds_t>(*(initList.begin() + 1));
63 }
64
65 Timeval::Timeval(double time) noexcept {
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
70 }
71
72 Timeval::Timeval(const timeval& timeVal) noexcept
73 : timeVal(timeVal) {
74 }
75
77 utils::Timeval currentTime;
78 utils::system::gettimeofday(&currentTime, nullptr);
79
80 return currentTime;
81 }
82
83 Timeval& Timeval::operator=(const Timeval& timeVal) { // 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 }
91
92 Timeval& Timeval::operator=(const timeval& timeVal) {
93 this->timeVal = timeVal;
94
95 return *this;
96 }
97
98 Timeval Timeval::operator+(const Timeval& timeVal) const {
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 }
106
107 Timeval Timeval::operator-(const Timeval& timeVal) const {
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 }
115
116 Timeval Timeval::operator*(double mul) const {
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 }
124
125 Timeval& Timeval::operator+=(const Timeval& timeVal) {
126 *this = (*this + timeVal).normalize();
127 return *this;
128 }
129
130 Timeval& Timeval::operator-=(const Timeval& timeVal) {
131 *this = (*this - timeVal).normalize();
132 return *this;
133 }
134
135 Timeval& Timeval::operator*=(double mul) {
136 *this = (*this * mul).normalize();
137 return *this;
138 }
139
141 return Timeval({-this->timeVal.tv_sec, -this->timeVal.tv_usec}).normalize();
142 }
143
144 bool Timeval::operator<(const Timeval& timeVal) const {
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 }
148
149 bool Timeval::operator>(const Timeval& timeVal) const {
150 return timeVal < *this;
151 }
152
153 bool Timeval::operator<=(const Timeval& timeVal) const {
154 return !(*this > timeVal);
155 }
156
157 bool Timeval::operator>=(const Timeval& timeVal) const {
158 return !(*this < timeVal);
159 }
160
161 bool Timeval::operator==(const Timeval& timeVal) const {
162 return !(*this < timeVal) && !(*this > timeVal);
163 }
164
165 bool Timeval::operator!=(const Timeval& timeVal) const {
166 return !(*this == timeVal);
167 }
168
169 timeval* Timeval::operator&() {
170 return &timeVal;
171 }
172
173 timespec Timeval::getTimespec() const {
174 return timespec{timeVal.tv_sec, static_cast<int32_t>(timeVal.tv_usec * 1000)};
175 }
176
177 int Timeval::getMs() const {
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 }
188
189 double Timeval::getMsd() const {
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 }
199
200 const timeval* Timeval::operator&() const {
201 return &timeVal;
202 }
203
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 }
217
218 std::ostream& operator<<(std::ostream& ostream, const utils::Timeval& timeVal) {
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 }
222
223 Timeval operator*(double mul, const utils::Timeval& timeVal) {
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 }
231
232} // namespace utils
double getMsd() const
Definition Timeval.cpp:189
const timeval * operator&() const
Definition Timeval.cpp:200
bool operator>(const Timeval &timeVal) const
Definition Timeval.cpp:149
bool operator<(const Timeval &timeVal) const
Definition Timeval.cpp:144
Timeval operator*(double mul) const
Definition Timeval.cpp:116
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:59
timeval * operator&()
Definition Timeval.cpp:169
Timeval operator-() const
Definition Timeval.cpp:140
static Timeval currentTime()
Definition Timeval.cpp:76
timeval timeVal
Definition Timeval.h:99
bool operator<=(const Timeval &timeVal) const
Definition Timeval.cpp:153
int getMs() const
Definition Timeval.cpp:177
bool operator!=(const Timeval &timeVal) const
Definition Timeval.cpp:165
timespec getTimespec() const
Definition Timeval.cpp:173
Timeval operator-(const Timeval &timeVal) const
Definition Timeval.cpp:107
Timeval & operator=(const timeval &timeVal)
Definition Timeval.cpp:92
Timeval & operator+=(const Timeval &timeVal)
Definition Timeval.cpp:125
bool operator==(const Timeval &timeVal) const
Definition Timeval.cpp:161
Timeval & operator-=(const Timeval &timeVal)
Definition Timeval.cpp:130
Timeval() noexcept
Definition Timeval.cpp:55
Timeval operator+(const Timeval &timeVal) const
Definition Timeval.cpp:98
Timeval & operator=(const Timeval &timeVal)
Definition Timeval.cpp:83
Timeval & operator*=(double mul)
Definition Timeval.cpp:135
bool operator>=(const Timeval &timeVal) const
Definition Timeval.cpp:157
Timeval(double time) noexcept
Definition Timeval.cpp:65
Timeval(const timeval &timeVal) noexcept
Definition Timeval.cpp:72
const Timeval & normalize()
Definition Timeval.cpp:204
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition time.cpp:57
Timeval operator*(double mul, const utils::Timeval &timeVal)
Definition Timeval.cpp:223