MLIB
Loading...
Searching...
No Matches
tvops.h
Go to the documentation of this file.
1
7#pragma once
8
9#if __has_include("defs.h")
10#include "defs.h"
11#endif
12
13#include <winsock2.h>
14#include <assert.h>
15#include <ostream>
16
19
20timeval operator+ (const timeval& t1, const timeval& t2);
21timeval operator- (const timeval& t1, const timeval& t2);
22timeval& operator+= (timeval& lhs, const timeval& rhs);
23timeval& operator-= (timeval& lhs, const timeval& rhs);
24timeval operator* (const timeval& op1, int op2);
25timeval operator* (int op1, const timeval& op2);
26timeval operator/ (const timeval& op1, int op2);
27
29inline bool operator== (const timeval& t1, const timeval& t2)
30{
31 if (t1.tv_sec == t2.tv_sec)
32 return (t1.tv_usec == t2.tv_usec);
33 return 0;
34}
35
36// MSVC - make sure you compile with /Zc:__cplusplus option
37#if __cplusplus < 202002L
38// C++11 or C++17
39
41inline bool operator< (const timeval& t1, const timeval& t2)
42{
43 if (t1.tv_sec != t2.tv_sec)
44 return (t1.tv_sec < t2.tv_sec);
45 else
46 return (t1.tv_usec < t2.tv_usec);
47}
48
50inline bool operator> (const timeval& t1, const timeval& t2)
51{
52 if (t1.tv_sec != t2.tv_sec)
53 return (t1.tv_sec > t2.tv_sec);
54 else
55 return (t1.tv_usec > t2.tv_usec);
56}
57
59inline bool operator>= (const timeval& t1, const timeval& t2)
60{
61 return !(t1 < t2);
62}
63
65inline bool operator<= (const timeval& t1, const timeval& t2)
66{
67 return !(t1 > t2);
68}
69
71inline bool operator!= (const timeval& t1, const timeval& t2)
72{
73 return !(t1 == t2);
74}
75#else
76// for C++20 use spaceship operator
77
79inline auto operator<=> (const timeval& t1, const timeval& t2)
80{
81 if (t1.tv_sec == t2.tv_sec)
82 return (t1.tv_usec <=> t2.tv_usec);
83 else
84 return (t1.tv_sec <=> t2.tv_sec);
85}
86
87#endif
88
89// conversion to/from microseconds
90LONGLONG
91usec64 (const timeval& tv);
92timeval fromusec (LONGLONG us);
93
94// conversion to seconds
95double secd (const timeval& tv);
96
97timeval fromsystime (const SYSTEMTIME& st);
98void tosystime (const timeval& tv, SYSTEMTIME* st);
99void tolocaltime (const timeval& tv, SYSTEMTIME* st);
100timeval zone_bias ();
101
102void normalize (timeval& tv);
103
104timeval fromdouble (double d);
105
106// Inline implementations ----------------------------------------------------
107
109inline timeval operator+ (const timeval& t1, const timeval& t2)
110{
111 timeval ans;
112 ans.tv_usec = t1.tv_usec + t2.tv_usec;
113 ans.tv_sec = t1.tv_sec + t2.tv_sec;
114 normalize (ans);
115 return ans;
116}
117
119inline timeval operator- (const timeval& t1, const timeval& t2)
120{
121 timeval ans;
122
123 ans.tv_usec = t1.tv_usec - t2.tv_usec;
124 ans.tv_sec = t1.tv_sec - t2.tv_sec;
125 normalize (ans);
126 return ans;
127}
128
130inline timeval& operator+= (timeval& lhs, const timeval& rhs)
131{
132 lhs.tv_usec += rhs.tv_usec;
133 lhs.tv_sec += rhs.tv_sec;
134 normalize (lhs);
135 return lhs;
136}
137
139inline timeval& operator-= (timeval& lhs, const timeval& rhs)
140{
141 lhs.tv_usec -= rhs.tv_usec;
142 lhs.tv_sec -= rhs.tv_sec;
143 normalize (lhs);
144 return lhs;
145}
146
148inline double secd (const timeval& tv)
149{
150 return tv.tv_sec + (double)tv.tv_usec / 1000000;
151}
152
154inline LONGLONG usec64 (const timeval& tv)
155{
156 return (LONGLONG)tv.tv_sec * 1000000L + (LONGLONG)tv.tv_usec;
157}
158
160inline timeval fromusec (LONGLONG us)
161{
162 timeval tv;
163 tv.tv_sec = (int)(us / 1000000L);
164 tv.tv_usec = (int)(us % 1000000L);
165 return tv;
166}
167
169inline timeval fromdouble (double d)
170{
171 timeval tv;
172 tv.tv_sec = (long)d;
173 tv.tv_usec = (long)((d - tv.tv_sec) * 1000000);
174 return tv;
175}
176
178inline timeval operator* (const timeval& op1, int op2)
179{
180 timeval tv;
181
182 tv.tv_usec = op1.tv_usec * op2;
183 tv.tv_sec = op1.tv_sec * op2;
184 normalize (tv);
185 return tv;
186}
187
189inline timeval operator* (int op1, const timeval& op2)
190{
191 return op2 * op1;
192}
193
195inline timeval operator/ (const timeval& op1, int op2)
196{
197 timeval tv;
198 tv.tv_usec = op1.tv_usec / op2;
199 tv.tv_sec = op1.tv_sec / op2;
200 normalize (tv);
201 return tv;
202}
203
204inline ::std::ostream& operator<< (::std::ostream& os, const timeval& tv)
205{
206 os << "{ tv_sec: " << tv.tv_sec << ", tv_usec: " << tv.tv_usec << '}';
207 return os;
208}
209
bool operator<=(const timeval &t1, const timeval &t2)
"Less or equal than" operator
Definition tvops.h:65
bool operator!=(const timeval &t1, const timeval &t2)
"Not equal" operator
Definition tvops.h:71
void tosystime(const timeval &tv, SYSTEMTIME *st)
Convert from timeval format to SYSTEMTIME format.
Definition tvops.cpp:33
void tolocaltime(const timeval &tv, SYSTEMTIME *st)
Convert to local time (in a SYSTEMTIME structure)
Definition tvops.cpp:61
timeval operator/(const timeval &op1, int op2)
Division by an integer.
Definition tvops.h:195
timeval operator-(const timeval &t1, const timeval &t2)
Subtraction operator.
Definition tvops.h:119
timeval operator+(const timeval &t1, const timeval &t2)
Addition operator.
Definition tvops.h:109
timeval fromusec(LONGLONG us)
Conversion from 64-bit microseconds.
Definition tvops.h:160
bool operator>(const timeval &t1, const timeval &t2)
"Greater than" operator
Definition tvops.h:50
void normalize(timeval &tv)
Following an arithmetic operation, brings timeval structure to a canonical form where tv_usec is less...
Definition tvops.cpp:74
timeval zone_bias()
Return time zone bias.
Definition tvops.cpp:42
timeval operator*(const timeval &op1, int op2)
Multiplication by an integer.
Definition tvops.h:178
LONGLONG usec64(const timeval &tv)
Conversion to 64-bit microseconds.
Definition tvops.h:154
timeval fromsystime(const SYSTEMTIME &st)
Convert from UTC system time to Unix time scale (UTC form 01/01/70)
Definition tvops.cpp:20
timeval fromdouble(double d)
Conversion from floating-point seconds.
Definition tvops.h:169
double secd(const timeval &tv)
Conversion to floating-point seconds.
Definition tvops.h:148
timeval & operator-=(timeval &lhs, const timeval &rhs)
Subtraction assignment.
Definition tvops.h:139
bool operator>=(const timeval &t1, const timeval &t2)
"Greater or equal than" operator
Definition tvops.h:59
bool operator==(const timeval &t1, const timeval &t2)
Equality operator.
Definition tvops.h:29
bool operator<(const timeval &t1, const timeval &t2)
"Less than" operator
Definition tvops.h:41
timeval & operator+=(timeval &lhs, const timeval &rhs)
Addition assignment operator.
Definition tvops.h:130