Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-time.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef AH_TIME_H
45# define AH_TIME_H
46
47# include <sys/time.h>
48# include <stdio.h>
49
50typedef struct timespec Time;
51
52
53# define MSEC 1000
54# define USEC 1000000
55# define NSEC 1000000000
56
57
58# define EVENT_TIME(e) ((e)->get_key())
59
60
61# define EVENT_SEC(e) (EVENT_TIME(e).tv_sec)
62# define EVENT_NSEC(e) (EVENT_TIME(e).tv_nsec)
63
64
65inline long msec_to_nsec(const long & msec)
66{
67 assert(msec >= 0 and msec < MSEC);
68
69 return msec*USEC;
70}
71
72
73inline long usec_to_nsec(const long & usec)
74{
75 assert(usec >= 0 and usec < USEC);
76
77 return usec*MSEC;
78}
79
80
81inline char * time_to_char(const Time & t, char * str, size_t str_size = 64)
82{
83 snprintf(str, str_size, "(%ld sec, %ld nsec)", t.tv_sec, t.tv_nsec);
84
85 return str;
86}
87
88
89inline Time timeval_to_time(const struct timeval & current_time)
90{
91 assert(current_time.tv_usec >= 0 and current_time.tv_usec < USEC);
92
93 Time ret_val;
94 ret_val.tv_sec = current_time.tv_sec;
95 ret_val.tv_nsec = usec_to_nsec(current_time.tv_usec);
96
97 assert(ret_val.tv_nsec >= 0 and ret_val.tv_nsec < NSEC);
98
99 return ret_val;
100}
101
102
104{
105 struct timeval current_time;
106 gettimeofday(&current_time, nullptr);
107
108 return timeval_to_time(current_time);
109}
110
111
112inline Time time_plus_msec(const Time & current_time,
113 const int & msec)
114{
115 assert(current_time.tv_nsec >= 0 and current_time.tv_nsec < NSEC);
116
117 const long sec = msec/MSEC; /* compute seconds inside msec */
118
119 const long remain = msec % MSEC;
120
121 const long total_nsec = current_time.tv_nsec + msec_to_nsec(remain);
122
123 Time _t;
124 _t.tv_sec = current_time.tv_sec + sec + total_nsec/NSEC;
125 _t.tv_nsec = total_nsec%NSEC;
126
127 assert(_t.tv_nsec >= 0 and _t.tv_nsec < NSEC);
128
129 return _t;
130}
131
132
133inline bool operator == (const Time & l, const Time & r)
134{
135 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
136 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
137
138 return l.tv_sec == r.tv_sec and l.tv_nsec == r.tv_nsec;
139}
140
141
142inline bool operator < (const Time & l, const Time & r)
143{
144 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
145 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
146
147 if (l.tv_sec not_eq r.tv_sec)
148 return l.tv_sec < r.tv_sec;
149
150 return l.tv_nsec < r.tv_nsec;
151}
152
153
154inline bool operator <= (const Time & l, const Time & r)
155{
156 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
157 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
158
159 if (l.tv_sec not_eq r.tv_sec)
160 return l.tv_sec < r.tv_sec;
161
162 return l.tv_nsec <= r.tv_nsec;
163}
164
165
166inline bool operator > (const Time & l, const Time & r)
167{
168 return not (l <= r);
169}
170
171
172inline bool operator >= (const Time& l, const Time& r)
173{
174 return not (l < r);
175}
176
177
178# endif // AH_TIME_H
bool operator>=(const Time &l, const Time &r)
Definition ah-time.H:172
#define MSEC
Definition ah-time.H:53
bool operator==(const Time &l, const Time &r)
Definition ah-time.H:133
long usec_to_nsec(const long &usec)
Definition ah-time.H:73
bool operator>(const Time &l, const Time &r)
Definition ah-time.H:166
Time time_plus_msec(const Time &current_time, const int &msec)
Definition ah-time.H:112
Time read_current_time()
Definition ah-time.H:103
Time timeval_to_time(const struct timeval &current_time)
Definition ah-time.H:89
char * time_to_char(const Time &t, char *str, size_t str_size=64)
Definition ah-time.H:81
#define NSEC
Definition ah-time.H:55
struct timespec Time
Definition ah-time.H:50
#define USEC
Definition ah-time.H:54
long msec_to_nsec(const long &msec)
Definition ah-time.H:65
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:142
bool operator<=(const Time &l, const Time &r)
Definition ah-time.H:154
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sec_function > > sec(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4072
DynList< int > l