MLIB
Loading...
Searching...
No Matches
tvops.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include "safe_winsock.h"
15#include <assert.h>
16#include <ostream>
17#include <chrono>
18
21
22timeval operator+ (const timeval& t1, const timeval& t2);
23timeval operator- (const timeval& t1, const timeval& t2);
24timeval& operator+= (timeval& lhs, const timeval& rhs);
25timeval& operator-= (timeval& lhs, const timeval& rhs);
26timeval operator* (const timeval& op1, int op2);
27timeval operator* (int op1, const timeval& op2);
28timeval operator/ (const timeval& op1, int op2);
29
31inline bool operator== (const timeval& t1, const timeval& t2)
32{
33 if (t1.tv_sec == t2.tv_sec)
34 return (t1.tv_usec == t2.tv_usec);
35 return 0;
36}
37
38// MSVC - make sure you compile with /Zc:__cplusplus option
39#if __cplusplus < 202002L
40// C++11 or C++17
41
43inline bool operator< (const timeval& t1, const timeval& t2)
44{
45 if (t1.tv_sec != t2.tv_sec)
46 return (t1.tv_sec < t2.tv_sec);
47 else
48 return (t1.tv_usec < t2.tv_usec);
49}
50
52inline bool operator> (const timeval& t1, const timeval& t2)
53{
54 if (t1.tv_sec != t2.tv_sec)
55 return (t1.tv_sec > t2.tv_sec);
56 else
57 return (t1.tv_usec > t2.tv_usec);
58}
59
61inline bool operator>= (const timeval& t1, const timeval& t2)
62{
63 return !(t1 < t2);
64}
65
67inline bool operator<= (const timeval& t1, const timeval& t2)
68{
69 return !(t1 > t2);
70}
71
73inline bool operator!= (const timeval& t1, const timeval& t2)
74{
75 return !(t1 == t2);
76}
77#else
78// for C++20 use spaceship operator
79
81inline auto operator<=> (const timeval& t1, const timeval& t2)
82{
83 if (t1.tv_sec == t2.tv_sec)
84 return (t1.tv_usec <=> t2.tv_usec);
85 else
86 return (t1.tv_sec <=> t2.tv_sec);
87}
88
89#endif
90
91// conversion to/from microseconds
92long long usec64 (const timeval& tv);
93timeval fromusec (long long us);
94
95// conversion to seconds
96double secd (const timeval& tv);
97
98#ifdef _WIN32
99timeval fromsystime (const SYSTEMTIME& st);
100void tosystime (const timeval& tv, SYSTEMTIME* st);
101void tolocaltime (const timeval& tv, SYSTEMTIME* st);
102timeval zone_bias ();
103#endif
104
105void normalize (timeval& tv);
106
107timeval fromdouble (double d);
108
109// Inline implementations ----------------------------------------------------
110
112inline
113timeval operator+ (const timeval& t1, const timeval& t2)
114{
115 timeval ans;
116 ans.tv_usec = t1.tv_usec + t2.tv_usec;
117 ans.tv_sec = t1.tv_sec + t2.tv_sec;
118 normalize (ans);
119 return ans;
120}
121
123inline
124timeval operator- (const timeval& t1, const timeval& t2)
125{
126 timeval ans;
127
128 ans.tv_usec = t1.tv_usec - t2.tv_usec;
129 ans.tv_sec = t1.tv_sec - t2.tv_sec;
130 normalize (ans);
131 return ans;
132}
133
135inline
136timeval& operator+= (timeval& lhs, const timeval& rhs)
137{
138 lhs.tv_usec += rhs.tv_usec;
139 lhs.tv_sec += rhs.tv_sec;
140 normalize (lhs);
141 return lhs;
142}
143
145inline
146timeval& operator-= (timeval& lhs, const timeval& rhs)
147{
148 lhs.tv_usec -= rhs.tv_usec;
149 lhs.tv_sec -= rhs.tv_sec;
150 normalize (lhs);
151 return lhs;
152}
153
155inline
156double secd (const timeval& tv)
157{
158 return tv.tv_sec + (double)tv.tv_usec / 1000000;
159}
160
162inline
163long long usec64 (const timeval& tv)
164{
165 return (long long)tv.tv_sec * 1000000L + (long long)tv.tv_usec;
166}
167
169inline
170timeval fromusec (long long us)
171{
172 timeval tv;
173 tv.tv_sec = (int)(us / 1000000L);
174 tv.tv_usec = (int)(us % 1000000L);
175 return tv;
176}
177
179inline
180timeval fromdouble (double d)
181{
182 timeval tv;
183 tv.tv_sec = (long)d;
184 tv.tv_usec = (long)((d - tv.tv_sec) * 1000000);
185 return tv;
186}
187
189template <typename T>
190timeval from_chrono (const T& dur)
191{
192 using namespace std::chrono;
193 auto sec = duration_cast<seconds>(dur);
194 auto us = duration_cast<microseconds> (dur - sec);
195 return timeval{(long)sec.count (), (long)us.count ()};
196}
197
199inline
200std::chrono::microseconds to_chrono (const timeval& tv)
201{
202 using namespace std::chrono;
203 return seconds (tv.tv_sec) + microseconds (tv.tv_usec);
204}
205
206
208inline
209timeval operator* (const timeval& op1, int op2)
210{
211 timeval tv;
212
213 tv.tv_usec = op1.tv_usec * op2;
214 tv.tv_sec = op1.tv_sec * op2;
215 normalize (tv);
216 return tv;
217}
218
220inline
221timeval operator* (int op1, const timeval& op2)
222{
223 return op2 * op1;
224}
225
227inline
228timeval operator/ (const timeval& op1, int op2)
229{
230 timeval tv;
231 tv.tv_usec = op1.tv_usec / op2;
232 tv.tv_sec = op1.tv_sec / op2;
233 normalize (tv);
234 return tv;
235}
236
238inline ::std::ostream& operator<< (::std::ostream& os, const timeval& tv)
239{
240 os << "{ tv_sec: " << tv.tv_sec << ", tv_usec: " << tv.tv_usec << '}';
241 return os;
242}
243
bool operator<=(const timeval &t1, const timeval &t2)
"Less or equal than" operator
Definition tvops.h:67
bool operator!=(const timeval &t1, const timeval &t2)
"Not equal" operator
Definition tvops.h:73
timeval operator/(const timeval &op1, int op2)
Division by an integer.
Definition tvops.h:228
timeval operator-(const timeval &t1, const timeval &t2)
Subtraction operator.
Definition tvops.h:124
long long usec64(const timeval &tv)
Conversion to 64-bit microseconds.
Definition tvops.h:163
timeval operator+(const timeval &t1, const timeval &t2)
Addition operator.
Definition tvops.h:113
bool operator>(const timeval &t1, const timeval &t2)
"Greater than" operator
Definition tvops.h:52
void normalize(timeval &tv)
Following an arithmetic operation, brings timeval structure to a canonical form where tv_usec is less...
Definition tvops.cpp:74
std::chrono::microseconds to_chrono(const timeval &tv)
Conversion to a chrono duration in microseconds.
Definition tvops.h:200
timeval operator*(const timeval &op1, int op2)
Multiplication by an integer.
Definition tvops.h:209
inline ::std::ostream & operator<<(::std::ostream &os, const timeval &tv)
Stream extraction operator.
Definition tvops.h:238
timeval fromdouble(double d)
Conversion from floating-point seconds.
Definition tvops.h:180
double secd(const timeval &tv)
Conversion to floating-point seconds.
Definition tvops.h:156
timeval fromusec(long long us)
Conversion from 64-bit microseconds.
Definition tvops.h:170
timeval & operator-=(timeval &lhs, const timeval &rhs)
Subtraction assignment.
Definition tvops.h:146
timeval from_chrono(const T &dur)
Conversion from a chrono duration.
Definition tvops.h:190
bool operator>=(const timeval &t1, const timeval &t2)
"Greater or equal than" operator
Definition tvops.h:61
bool operator==(const timeval &t1, const timeval &t2)
Equality operator.
Definition tvops.h:31
bool operator<(const timeval &t1, const timeval &t2)
"Less than" operator
Definition tvops.h:43
timeval & operator+=(timeval &lhs, const timeval &rhs)
Addition assignment operator.
Definition tvops.h:136