source: src/libcfa/time @ 643c6b9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 643c6b9 was 643c6b9, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

cleanup, add "div" for Duration

  • Property mode set to 100644
File size: 12.7 KB
Line 
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 : Fri Apr  6 11:15:49 2018
13// Update Count     : 610
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
22extern "C" {
23#include <sys/time.h>                                                                   // timeval
24}
25#include <iostream>                                                                             // istype/ostype
26
27enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
28
29
30//######################### Duration #########################
31
32struct Duration {                                                                               // private
33        int64_t tv;                                                                                     // nanoseconds
34}; // Duration
35
36static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
37static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
38
39static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
40static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
41
42static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
43static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
44static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
45
46static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
47static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
48static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
49
50static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
51static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
52static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
53
54static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
55static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
56static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
57static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; }
58
59static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
60static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
61
62static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
63static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
64static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
65static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
66static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
67static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
68
69static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
70static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
71static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
72static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
73static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
74static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
75
76static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
77
78forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
79
80static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
81static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
82static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
83static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
84static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
85static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
86static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
87static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
88static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
89static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
90static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
91static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
92static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
93
94static inline int64_t ?`ns( Duration dur ) { return dur.tv; }
95static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
96static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
97static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; }
98static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
99static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
100static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
101static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
102
103
104//######################### C timeval #########################
105
106static inline void ?{}( timeval & t ) {}
107static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
108static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
109static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; }
110static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
111static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
112static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
113static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
114static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
115
116
117//######################### C timespec #########################
118
119static inline void ?{}( timespec & t ) {}
120static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
121static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
122static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; }
123static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
124static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
125static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
126static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
127static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
128
129
130//######################### C time #########################
131
132static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
133static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
134static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
135static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
136static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
137static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
138
139
140//######################### Time #########################
141
142struct Time {                                                                                   // private
143        uint64_t tv;                                                                            // nanoseconds since UNIX epoch
144};
145
146static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast
147void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow
148
149static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
150static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
151
152static inline void ?{}( Time & time, timeval t ) with( time ) {
153        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
154} // Time
155
156static inline Time ?=?( Time & time, timeval t ) with( time ) {
157        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
158        return time;
159} // ?=?
160
161static inline void ?{}( Time & time, timespec t ) with( time ) {
162        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
163} // Time
164
165static inline Time ?=?( Time & time, timespec t ) with( time ) {
166        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
167        return time;
168} // ?=?
169
170static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
171static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
172static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
173
174static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
175static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; }
176static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
177static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; }
178static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; }
179static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; }
180static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; }
181static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; }
182static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
183
184char * yy_mm_dd( Time time, char * buf );
185static inline char * ?`ymd( Time time, char * buf ) {   // short form
186        return yy_mm_dd( time, buf );
187} // ymd
188
189char * mm_dd_yy( Time time, char * buf );
190static inline char * ?`mdy( Time time, char * buf ) {   // short form
191        return mm_dd_yy( time, buf );
192} // mdy
193
194char * dd_mm_yy( Time time, char * buf );
195static inline char * ?`dmy( Time time, char * buf ) {   // short form
196        return dd_mm_yy( time, buf );;
197} // dmy
198
199size_t strftime( char * buf, size_t size, const char * fmt, Time time );
200
201forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
202
203//------------------------- timeval (cont) -------------------------
204
205static inline void ?{}( timeval & t, Time time ) with( t, time ) {
206        tv_sec = tv / TIMEGRAN;                                                         // seconds
207        tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
208} // ?{}
209
210//------------------------- timespec (cont) -------------------------
211
212static inline void ?{}( timespec & t, Time time ) with( t, time ) {
213        tv_sec = tv / TIMEGRAN;                                                         // seconds
214        tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds
215} // ?{}
216
217
218//######################### Clock #########################
219
220struct Clock {                                                                                  // private
221        Duration offset;                                                                        // for virtual clock: contains offset from real-time
222        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
223};
224
225static inline void resetClock( Clock & clk ) with( clk ) {
226        clocktype = CLOCK_REALTIME_COARSE;
227} // Clock::resetClock
228
229static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
230        clocktype = -1;
231        offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
232} // resetClock
233
234static inline void ?{}( Clock & clk ) {
235        resetClock( clk );
236} // Clock
237
238static inline void ?{}( Clock & clk, Duration adj ) {
239        resetClock( clk, adj );
240} // Clock
241
242static inline Duration getRes() {
243        struct timespec res;
244        clock_getres( CLOCK_REALTIME_COARSE, &res );
245        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
246} // getRes
247
248static inline Time getTimeNsec() {                                              // with nanoseconds
249        timespec curr;
250        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
251        return (Time){ curr };
252} // getTime
253
254static inline Time getTime() {                                                  // without nanoseconds
255        timespec curr;
256        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
257        curr.tv_nsec = 0;
258        return (Time){ curr };
259} // getTime
260
261static inline Time getTime( Clock & clk ) with( clk ) {
262        return getTime() + offset;
263} // getTime
264
265static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
266        return getTime() + offset;
267} // getTime
268
269static inline timeval getTime( Clock & clk ) {
270        return (timeval){ clk() };
271} // getTime
272
273static inline tm getTime( Clock & clk ) with( clk ) {
274        tm ret;
275        localtime_r( getTime( clk ).tv_sec, &ret );
276        return ret;
277} // getTime
278
279// Local Variables: //
280// mode: c //
281// tab-width: 4 //
282// End: //
Note: See TracBrowser for help on using the repository browser.