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#ifndef DOXYGEN_SHOULD_SKIP_THIS
21
22#include "utils/Timeval.h"
23
24#include <climits>
25#include <cstdint>
26#include <string>
27#include <sys/types.h>
28
29#endif /* DOXYGEN_SHOULD_SKIP_THIS */
30
31namespace utils {
32
33 Timeval::Timeval() noexcept
34 : timeVal({}) {
35 }
36
37 Timeval::Timeval(const std::initializer_list<time_t>& initList) noexcept
38 : timeVal({}) {
39 timeVal.tv_sec = *initList.begin();
40 timeVal.tv_usec = static_cast<suseconds_t>(*(initList.begin() + 1));
41 }
42
43 Timeval::Timeval(double time) noexcept {
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
48 }
49
50 Timeval::Timeval(const timeval& timeVal) noexcept
51 : timeVal(timeVal) {
52 }
53
55 utils::Timeval currentTime;
56 utils::system::gettimeofday(&currentTime, nullptr);
57
58 return currentTime;
59 }
60
61 Timeval& Timeval::operator=(const Timeval& timeVal) { // 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 }
69
70 Timeval& Timeval::operator=(const timeval& timeVal) {
71 this->timeVal = timeVal;
72
73 return *this;
74 }
75
76 Timeval Timeval::operator+(const Timeval& timeVal) const {
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 }
84
85 Timeval Timeval::operator-(const Timeval& timeVal) const {
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 }
93
94 Timeval Timeval::operator*(double mul) const {
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 }
102
103 Timeval& Timeval::operator+=(const Timeval& timeVal) {
104 *this = (*this + timeVal).normalize();
105 return *this;
106 }
107
108 Timeval& Timeval::operator-=(const Timeval& timeVal) {
109 *this = (*this - timeVal).normalize();
110 return *this;
111 }
112
113 Timeval& Timeval::operator*=(double mul) {
114 *this = (*this * mul).normalize();
115 return *this;
116 }
117
119 return Timeval({-this->timeVal.tv_sec, -this->timeVal.tv_usec}).normalize();
120 }
121
122 bool Timeval::operator<(const Timeval& timeVal) const {
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 }
126
127 bool Timeval::operator>(const Timeval& timeVal) const {
128 return timeVal < *this;
129 }
130
131 bool Timeval::operator<=(const Timeval& timeVal) const {
132 return !(*this > timeVal);
133 }
134
135 bool Timeval::operator>=(const Timeval& timeVal) const {
136 return !(*this < timeVal);
137 }
138
139 bool Timeval::operator==(const Timeval& timeVal) const {
140 return !(*this < timeVal) && !(*this > timeVal);
141 }
142
143 bool Timeval::operator!=(const Timeval& timeVal) const {
144 return !(*this == timeVal);
145 }
146
147 timeval* Timeval::operator&() {
148 return &timeVal;
149 }
150
151 timespec Timeval::getTimespec() const {
152 return timespec{timeVal.tv_sec, static_cast<int32_t>(timeVal.tv_usec * 1000)};
153 }
154
155 int Timeval::getMs() const {
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 }
166
167 double Timeval::getMsd() const {
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 }
177
178 const timeval* Timeval::operator&() const {
179 return &timeVal;
180 }
181
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 }
195
196 std::ostream& operator<<(std::ostream& ostream, const utils::Timeval& timeVal) {
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 }
200
201 Timeval operator*(double mul, const utils::Timeval& timeVal) {
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 }
209
210} // namespace utils
double getMsd() const
Definition Timeval.cpp:167
const timeval * operator&() const
Definition Timeval.cpp:178
bool operator>(const Timeval &timeVal) const
Definition Timeval.cpp:127
friend Timeval operator*(double mul, const Timeval &timeVal)
Definition Timeval.cpp:201
bool operator<(const Timeval &timeVal) const
Definition Timeval.cpp:122
Timeval operator*(double mul) const
Definition Timeval.cpp:94
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:37
timeval * operator&()
Definition Timeval.cpp:147
Timeval operator-() const
Definition Timeval.cpp:118
static Timeval currentTime()
Definition Timeval.cpp:54
timeval timeVal
Definition Timeval.h:77
bool operator<=(const Timeval &timeVal) const
Definition Timeval.cpp:131
int getMs() const
Definition Timeval.cpp:155
bool operator!=(const Timeval &timeVal) const
Definition Timeval.cpp:143
timespec getTimespec() const
Definition Timeval.cpp:151
Timeval operator-(const Timeval &timeVal) const
Definition Timeval.cpp:85
Timeval & operator=(const timeval &timeVal)
Definition Timeval.cpp:70
Timeval & operator+=(const Timeval &timeVal)
Definition Timeval.cpp:103
bool operator==(const Timeval &timeVal) const
Definition Timeval.cpp:139
Timeval & operator-=(const Timeval &timeVal)
Definition Timeval.cpp:108
Timeval() noexcept
Definition Timeval.cpp:33
Timeval operator+(const Timeval &timeVal) const
Definition Timeval.cpp:76
Timeval & operator=(const Timeval &timeVal)
Definition Timeval.cpp:61
Timeval & operator*=(double mul)
Definition Timeval.cpp:113
bool operator>=(const Timeval &timeVal) const
Definition Timeval.cpp:135
Timeval(double time) noexcept
Definition Timeval.cpp:43
Timeval(const timeval &timeVal) noexcept
Definition Timeval.cpp:50
const Timeval & normalize()
Definition Timeval.cpp:182
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition time.cpp:35