1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // time -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed Mar 14 23:18:57 2018 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sun Jan 5 19:01:07 2020 |
---|
13 | // Update Count : 652 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | // http://en.cppreference.com/w/cpp/header/chrono |
---|
19 | // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc |
---|
20 | |
---|
21 | #include <time.h> // timespec |
---|
22 | extern "C" { |
---|
23 | #include <sys/time.h> // timeval |
---|
24 | } |
---|
25 | #include <time_t.hfa> // Duration/Time types |
---|
26 | |
---|
27 | enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval |
---|
28 | |
---|
29 | //######################### Duration ######################### |
---|
30 | |
---|
31 | static inline { |
---|
32 | Duration ?=?( Duration & dur, __attribute__((unused)) zero_t ) { return dur{ 0 }; } |
---|
33 | |
---|
34 | Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tn }; } |
---|
35 | Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn + rhs.tn }; } |
---|
36 | Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } |
---|
37 | |
---|
38 | Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tn }; } |
---|
39 | Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; } |
---|
40 | Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } |
---|
41 | |
---|
42 | Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn * rhs }; } |
---|
43 | Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tn }; } |
---|
44 | Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } |
---|
45 | |
---|
46 | int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tn / rhs.tn; } |
---|
47 | Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn / rhs }; } |
---|
48 | Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } |
---|
49 | double div( Duration lhs, Duration rhs ) { return (double)lhs.tn / (double)rhs.tn; } |
---|
50 | |
---|
51 | Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tn % rhs.tn }; } |
---|
52 | Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; } |
---|
53 | |
---|
54 | bool ?==?( Duration lhs, Duration rhs ) { return lhs.tn == rhs.tn; } |
---|
55 | bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tn != rhs.tn; } |
---|
56 | bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tn < rhs.tn; } |
---|
57 | bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tn <= rhs.tn; } |
---|
58 | bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tn > rhs.tn; } |
---|
59 | bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tn >= rhs.tn; } |
---|
60 | |
---|
61 | bool ?==?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn == 0; } |
---|
62 | bool ?!=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn != 0; } |
---|
63 | bool ?<? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn < 0; } |
---|
64 | bool ?<=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn <= 0; } |
---|
65 | bool ?>? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn > 0; } |
---|
66 | bool ?>=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn >= 0; } |
---|
67 | |
---|
68 | Duration abs( Duration rhs ) { return rhs.tn >= 0 ? rhs : -rhs; } |
---|
69 | |
---|
70 | Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } |
---|
71 | Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; } |
---|
72 | Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; } |
---|
73 | Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } |
---|
74 | Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } |
---|
75 | Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } |
---|
76 | Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } |
---|
77 | Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } |
---|
78 | Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } |
---|
79 | Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
80 | Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
81 | Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
82 | Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
83 | |
---|
84 | int64_t ?`ns( Duration dur ) { return dur.tn; } |
---|
85 | int64_t ?`us( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000_000LL); } |
---|
86 | int64_t ?`ms( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000LL); } |
---|
87 | int64_t ?`s( Duration dur ) { return dur.tn / TIMEGRAN; } |
---|
88 | int64_t ?`m( Duration dur ) { return dur.tn / (60LL * TIMEGRAN); } |
---|
89 | int64_t ?`h( Duration dur ) { return dur.tn / (60LL * 60LL * TIMEGRAN); } |
---|
90 | int64_t ?`d( Duration dur ) { return dur.tn / (24LL * 60LL * 60LL * TIMEGRAN); } |
---|
91 | int64_t ?`w( Duration dur ) { return dur.tn / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); } |
---|
92 | |
---|
93 | Duration max( Duration lhs, Duration rhs ) { return (lhs.tn < rhs.tn) ? rhs : lhs;} |
---|
94 | Duration min( Duration lhs, Duration rhs ) { return !(rhs.tn < lhs.tn) ? lhs : rhs;} |
---|
95 | } // distribution |
---|
96 | |
---|
97 | //######################### C timeval ######################### |
---|
98 | |
---|
99 | static inline { |
---|
100 | void ?{}( timeval & t ) {} |
---|
101 | void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; } |
---|
102 | void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; } |
---|
103 | void ?{}( timeval & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; } |
---|
104 | |
---|
105 | timeval ?=?( timeval & t, __attribute__((unused)) zero_t ) { return t{ 0 }; } |
---|
106 | timeval ?+?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; } |
---|
107 | timeval ?-?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; } |
---|
108 | bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; } |
---|
109 | bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; } |
---|
110 | } // distribution |
---|
111 | |
---|
112 | //######################### C timespec ######################### |
---|
113 | |
---|
114 | static inline { |
---|
115 | void ?{}( timespec & t ) {} |
---|
116 | void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; } |
---|
117 | void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; } |
---|
118 | void ?{}( timespec & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; } |
---|
119 | |
---|
120 | timespec ?=?( timespec & t, __attribute__((unused)) zero_t ) { return t{ 0 }; } |
---|
121 | timespec ?+?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; } |
---|
122 | timespec ?-?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; } |
---|
123 | bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; } |
---|
124 | bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; } |
---|
125 | } // distribution |
---|
126 | |
---|
127 | //######################### C itimerval ######################### |
---|
128 | |
---|
129 | static inline { |
---|
130 | void ?{}( itimerval & itv, Duration alarm ) with( itv ) { |
---|
131 | // itimerval contains durations but but uses time data-structure timeval. |
---|
132 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds |
---|
133 | it_interval{ 0 }; // 0 seconds, 0 microseconds |
---|
134 | } // itimerval |
---|
135 | |
---|
136 | void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) { |
---|
137 | // itimerval contains durations but but uses time data-structure timeval. |
---|
138 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds |
---|
139 | it_interval{ interval`s, interval`us }; // seconds, microseconds |
---|
140 | } // itimerval |
---|
141 | } // distribution |
---|
142 | |
---|
143 | //######################### Time ######################### |
---|
144 | |
---|
145 | void ?{}( Time & time, int year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0, int64_t nsec = 0 ); |
---|
146 | static inline { |
---|
147 | Time ?=?( Time & time, __attribute__((unused)) zero_t ) { return time{ 0 }; } |
---|
148 | |
---|
149 | void ?{}( Time & time, timeval t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; } |
---|
150 | Time ?=?( Time & time, timeval t ) with( time ) { |
---|
151 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL); |
---|
152 | return time; |
---|
153 | } // ?=? |
---|
154 | |
---|
155 | void ?{}( Time & time, timespec t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; } |
---|
156 | Time ?=?( Time & time, timespec t ) with( time ) { |
---|
157 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; |
---|
158 | return time; |
---|
159 | } // ?=? |
---|
160 | |
---|
161 | Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tn + rhs.tn }; } |
---|
162 | Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } |
---|
163 | Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } |
---|
164 | |
---|
165 | Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; } |
---|
166 | Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tn - rhs.tn }; } |
---|
167 | Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } |
---|
168 | bool ?==?( Time lhs, Time rhs ) { return lhs.tn == rhs.tn; } |
---|
169 | bool ?!=?( Time lhs, Time rhs ) { return lhs.tn != rhs.tn; } |
---|
170 | bool ?<?( Time lhs, Time rhs ) { return lhs.tn < rhs.tn; } |
---|
171 | bool ?<=?( Time lhs, Time rhs ) { return lhs.tn <= rhs.tn; } |
---|
172 | bool ?>?( Time lhs, Time rhs ) { return lhs.tn > rhs.tn; } |
---|
173 | bool ?>=?( Time lhs, Time rhs ) { return lhs.tn >= rhs.tn; } |
---|
174 | |
---|
175 | int64_t ?`ns( Time t ) { return t.tn; } |
---|
176 | } // distribution |
---|
177 | |
---|
178 | char * yy_mm_dd( Time time, char * buf ); |
---|
179 | static inline char * ?`ymd( Time time, char * buf ) { // short form |
---|
180 | return yy_mm_dd( time, buf ); |
---|
181 | } // ymd |
---|
182 | |
---|
183 | char * mm_dd_yy( Time time, char * buf ); |
---|
184 | static inline char * ?`mdy( Time time, char * buf ) { // short form |
---|
185 | return mm_dd_yy( time, buf ); |
---|
186 | } // mdy |
---|
187 | |
---|
188 | char * dd_mm_yy( Time time, char * buf ); |
---|
189 | static inline char * ?`dmy( Time time, char * buf ) { // short form |
---|
190 | return dd_mm_yy( time, buf );; |
---|
191 | } // dmy |
---|
192 | |
---|
193 | size_t strftime( char * buf, size_t size, const char * fmt, Time time ); |
---|
194 | |
---|
195 | //------------------------- timeval (cont) ------------------------- |
---|
196 | |
---|
197 | static inline void ?{}( timeval & t, Time time ) with( t, time ) { |
---|
198 | tv_sec = tn / TIMEGRAN; // seconds |
---|
199 | tv_usec = tn % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds |
---|
200 | } // ?{} |
---|
201 | |
---|
202 | //------------------------- timespec (cont) ------------------------- |
---|
203 | |
---|
204 | static inline void ?{}( timespec & t, Time time ) with( t, time ) { |
---|
205 | tv_sec = tn / TIMEGRAN; // seconds |
---|
206 | tv_nsec = tn % TIMEGRAN; // nanoseconds |
---|
207 | } // ?{} |
---|
208 | |
---|
209 | //######################### C time ######################### |
---|
210 | |
---|
211 | static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; } |
---|
212 | static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; } |
---|
213 | static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); } |
---|
214 | static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); } |
---|
215 | static inline tm * localtime( time_t tp ) { return localtime( &tp ); } |
---|
216 | static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); } |
---|
217 | |
---|
218 | //######################### Clock ######################### |
---|
219 | |
---|
220 | struct Clock { // private |
---|
221 | Duration offset; // for virtual clock: contains offset from real-time |
---|
222 | }; |
---|
223 | |
---|
224 | static inline { |
---|
225 | void resetClock( Clock & clk, Duration adj ) with( clk ) { |
---|
226 | offset = adj + __timezone`s; // timezone (global) is (UTC - local time) in seconds |
---|
227 | } // resetClock |
---|
228 | |
---|
229 | void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); } |
---|
230 | |
---|
231 | Duration getResNsec() { |
---|
232 | struct timespec res; |
---|
233 | clock_getres( CLOCK_REALTIME, &res ); |
---|
234 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
235 | } // getRes |
---|
236 | |
---|
237 | Duration getRes() { |
---|
238 | struct timespec res; |
---|
239 | clock_getres( CLOCK_REALTIME_COARSE, &res ); |
---|
240 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
241 | } // getRes |
---|
242 | |
---|
243 | Time getTimeNsec() { // with nanoseconds |
---|
244 | timespec curr; |
---|
245 | clock_gettime( CLOCK_REALTIME, &curr ); |
---|
246 | return (Time){ curr }; |
---|
247 | } // getTimeNsec |
---|
248 | |
---|
249 | Time getTime() { // without nanoseconds |
---|
250 | timespec curr; |
---|
251 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
252 | curr.tv_nsec = 0; |
---|
253 | return (Time){ curr }; |
---|
254 | } // getTime |
---|
255 | |
---|
256 | Time getTime( Clock & clk ) with( clk ) { |
---|
257 | return getTime() + offset; |
---|
258 | } // getTime |
---|
259 | |
---|
260 | Time ?()( Clock & clk ) with( clk ) { // alternative syntax |
---|
261 | return getTime() + offset; |
---|
262 | } // getTime |
---|
263 | |
---|
264 | timeval getTime( Clock & clk ) { |
---|
265 | return (timeval){ clk() }; |
---|
266 | } // getTime |
---|
267 | |
---|
268 | tm getTime( Clock & clk ) with( clk ) { |
---|
269 | tm ret; |
---|
270 | localtime_r( getTime( clk ).tv_sec, &ret ); |
---|
271 | return ret; |
---|
272 | } // getTime |
---|
273 | |
---|
274 | Time getCPUTime() { |
---|
275 | timespec ts; |
---|
276 | clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ); |
---|
277 | return (Time){ ts }; |
---|
278 | } // getCPUTime |
---|
279 | } // distribution |
---|
280 | |
---|
281 | // Local Variables: // |
---|
282 | // mode: c // |
---|
283 | // tab-width: 4 // |
---|
284 | // End: // |
---|