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