Aleph-w 7.10.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 <cassert>
48# include <sys/time.h>
49# include <stdio.h>
50
51typedef struct timespec Time;
52
53
54# define MSEC 1000
55# define USEC 1000000
56# define NSEC 1000000000
57
58
59# define EVENT_TIME(e) ((e)->get_key())
60
61
62# define EVENT_SEC(e) (EVENT_TIME(e).tv_sec)
63# define EVENT_NSEC(e) (EVENT_TIME(e).tv_nsec)
64
65
66inline long msec_to_nsec(const long & msec)
67{
68 assert(msec >= 0 and msec < MSEC);
69
70 return msec*USEC;
71}
72
73
74inline long usec_to_nsec(const long & usec)
75{
76 assert(usec >= 0 and usec < USEC);
77
78 return usec*MSEC;
79}
80
81
82inline char * time_to_char(const Time & t, char * str, size_t str_size = 64)
83{
84 snprintf(str, str_size, "(%ld sec, %ld nsec)", t.tv_sec, t.tv_nsec);
85
86 return str;
87}
88
89
90inline Time timeval_to_time(const struct timeval & current_time)
91{
92 assert(current_time.tv_usec >= 0 and current_time.tv_usec < USEC);
93
94 Time ret_val;
95 ret_val.tv_sec = current_time.tv_sec;
96 ret_val.tv_nsec = usec_to_nsec(current_time.tv_usec);
97
98 assert(ret_val.tv_nsec >= 0 and ret_val.tv_nsec < NSEC);
99
100 return ret_val;
101}
102
103
105{
106 struct timeval current_time;
107 gettimeofday(&current_time, nullptr);
108
109 return timeval_to_time(current_time);
110}
111
112
113inline Time time_plus_msec(const Time & current_time,
114 const int & msec)
115{
116 assert(current_time.tv_nsec >= 0 and current_time.tv_nsec < NSEC);
117
118 const long sec = msec/MSEC; /* compute seconds inside msec */
119
120 const long remain = msec % MSEC;
121
122 const long total_nsec = current_time.tv_nsec + msec_to_nsec(remain);
123
124 Time _t;
125 _t.tv_sec = current_time.tv_sec + sec + total_nsec/NSEC;
126 _t.tv_nsec = total_nsec%NSEC;
127
128 assert(_t.tv_nsec >= 0 and _t.tv_nsec < NSEC);
129
130 return _t;
131}
132
133
134inline bool operator == (const Time & l, const Time & r)
135{
136 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
137 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
138
139 return l.tv_sec == r.tv_sec and l.tv_nsec == r.tv_nsec;
140}
141
142
143inline bool operator < (const Time & l, const Time & r)
144{
145 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
146 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
147
148 if (l.tv_sec not_eq r.tv_sec)
149 return l.tv_sec < r.tv_sec;
150
151 return l.tv_nsec < r.tv_nsec;
152}
153
154
155inline bool operator <= (const Time & l, const Time & r)
156{
157 assert(l.tv_nsec >= 0 and l.tv_nsec < NSEC);
158 assert(r.tv_nsec >= 0 and r.tv_nsec < NSEC);
159
160 if (l.tv_sec not_eq r.tv_sec)
161 return l.tv_sec < r.tv_sec;
162
163 return l.tv_nsec <= r.tv_nsec;
164}
165
166
167inline bool operator > (const Time & l, const Time & r)
168{
169 return not (l <= r);
170}
171
172
173inline bool operator >= (const Time& l, const Time& r)
174{
175 return not (l < r);
176}
177
178
179# endif // AH_TIME_H
bool operator>=(const Time &l, const Time &r)
Definition ah-time.H:173
#define MSEC
Definition ah-time.H:54
bool operator==(const Time &l, const Time &r)
Definition ah-time.H:134
long usec_to_nsec(const long &usec)
Definition ah-time.H:74
bool operator>(const Time &l, const Time &r)
Definition ah-time.H:167
Time time_plus_msec(const Time &current_time, const int &msec)
Definition ah-time.H:113
Time read_current_time()
Definition ah-time.H:104
Time timeval_to_time(const struct timeval &current_time)
Definition ah-time.H:90
char * time_to_char(const Time &t, char *str, size_t str_size=64)
Definition ah-time.H:82
#define NSEC
Definition ah-time.H:56
struct timespec Time
Definition ah-time.H:51
#define USEC
Definition ah-time.H:55
long msec_to_nsec(const long &msec)
Definition ah-time.H:66
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:143
bool operator<=(const Time &l, const Time &r)
Definition ah-time.H:155
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sec_function > > sec(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4150
Gsl_Compatible_Rng r
DynList< int > l