source: src/libcfa/time @ 9f652a1

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 9f652a1 was 9f652a1, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting and add ?%=? for duration

  • Property mode set to 100644
File size: 14.7 KB
RevLine 
[6ecc079]1//
[2a84d06d]2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
[6ecc079]3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[2a84d06d]7// time --
[6ecc079]8//
9// Author           : Peter A. Buhr
10// Created On       : Wed Mar 14 23:18:57 2018
11// Last Modified By : Peter A. Buhr
[9f652a1]12// Last Modified On : Tue Apr  3 08:46:08 2018
13// Update Count     : 585
[6ecc079]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
[2a84d06d]21#include <time.h>                                                                               // timespec
[6ecc079]22extern "C" {
[2a84d06d]23#include <sys/time.h>                                                                   // timeval
[6ecc079]24}
[2a84d06d]25#include <iostream>                                                                             // istype/ostype
[6ecc079]26
[273cde6]27enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
[6ecc079]28
29
[2a84d06d]30//######################### timeval #########################
[6ecc079]31
32static inline void ?{}( timeval & t ) {}
33static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
[8eb2018]34static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
35static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; }
[2a84d06d]36static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
37static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
38static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
39static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
40static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
41
42
43//######################### timespec #########################
[6ecc079]44
45static inline void ?{}( timespec & t ) {}
46static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
[8eb2018]47static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
48static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; }
[2a84d06d]49static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
50static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
51static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
52static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
53static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
54
55
56//######################### C time #########################
[6ecc079]57
58static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
59static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
60static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
61static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
62static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
63static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
64
65//######################### Duration #########################
66
67struct Duration {
68        int64_t tv;
69};
70
71static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
72static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
[2a84d06d]73static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
74static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
[6ecc079]75
[8eb2018]76#if 0
[6ecc079]77static inline void ?{}( Duration & dur, timeval t ) with( dur ) {
78        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
79} // Duration
80
81static inline void ?{}( Duration & dur, timespec t ) with( dur ) {
82        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
83} // Duration
84
85static inline Duration ?=?( Duration & dur, timeval t ) with( dur ) {
86        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
87        return dur;
88} // ?=?
89
90static inline Duration ?=?( Duration & dur, timespec t ) with( dur ) {
91        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
92        return dur;
93} // ?=? timespec
[8eb2018]94#endif
[6ecc079]95
[8eb2018]96//static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
[6ecc079]97
98static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
99static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
100static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
101
102static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
103static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
104static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
105
106static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
107static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
108static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
109
110static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
[8eb2018]111static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
[6ecc079]112static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
113
[2a84d06d]114static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
[9f652a1]115static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
[6ecc079]116
117static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
118static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
[2a84d06d]119static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
[6ecc079]120static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
[2a84d06d]121static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
[6ecc079]122static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
123
[2a84d06d]124static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
125static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
126static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
127static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
128static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
129static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
[6ecc079]130
[2a84d06d]131static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; }
132
133forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
134
135static inline Duration ?`ns( int64_t nsec  ) { return (Duration)@{ nsec }; }
[273cde6]136static inline Duration ?`us( int64_t usec  ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
137static inline Duration ?`ms( int64_t msec  ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
[2a84d06d]138static inline Duration ?`s ( int64_t sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
139static inline Duration ?`s ( double  sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
[273cde6]140static inline Duration ?`m ( int64_t min   ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
141static inline Duration ?`m ( double  min   ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
[9f652a1]142static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
143static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
144static inline Duration ?`d ( int64_t days  ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
145static inline Duration ?`d ( double  days  ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
146static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
147static inline Duration ?`w ( double  weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
148//static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
149//static inline Duration ?`f ( double  fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
[6ecc079]150
[2a84d06d]151static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
[273cde6]152static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
153static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
[2a84d06d]154static inline int64_t ?`s  ( Duration dur ) { return dur.tv / TIMEGRAN; }
[273cde6]155static inline int64_t ?`m  ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
[9f652a1]156static inline int64_t ?`h  ( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
157static inline int64_t ?`d  ( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
158static inline int64_t ?`w  ( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
159static inline int64_t ?`f  ( Duration dur ) { return dur.tv / (14LL * 24LL * 60LL * 60LL * TIMEGRAN); }
[2a84d06d]160
161//------------------------- timeval (cont) -------------------------
162
163static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
164        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
[273cde6]165        t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
[2a84d06d]166} // ?{}
167
168//------------------------- timespec (cont) -------------------------
169
170static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
171        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
172        t.tv_nsec = tv % TIMEGRAN;                                                      // nanoseconds
173} // Timespec
[6ecc079]174
175
176//######################### Time #########################
177
178
179struct Time {
180        uint64_t tv;
181};
182
183static inline void ?{}( Time & t ) with( t ) {
184        tv = 0;
185} // Time
186
[8eb2018]187void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
[6ecc079]188
[2a84d06d]189static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
[8eb2018]190        time{ year, month, day, hour, min, sec, 0 };
[6ecc079]191} // Time
192
[2a84d06d]193static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
[8eb2018]194        time{ year, month, day, hour, min, 0, 0 };
[6ecc079]195} // Time
196
[2a84d06d]197static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
[8eb2018]198        time{ year, month, day, hour, 0, 0, 0 };
[6ecc079]199} // Time
200
[2a84d06d]201static inline void ?{}( Time & time, int year, int month, int day ) {
[8eb2018]202        time{ year, month, day, 0, 0, 0, 0 };
[6ecc079]203} // Time
204
[2a84d06d]205static inline void ?{}( Time & time, int year, int month ) {
[8eb2018]206        time{ year, month, 0, 0, 0, 0, 0 };
[6ecc079]207} // Time
208
[2a84d06d]209static inline void ?{}( Time & time, int year ) {
[8eb2018]210        time{ year, 0, 0, 0, 0, 0, 0 };
[6ecc079]211} // Time
212
213static inline void ?{}( Time & time, timeval t ) with( time ) {
214        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
215} // Time
216
217static inline void ?{}( Time & time, timespec t ) with( time ) {
218        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
219} // Time
220
[2a84d06d]221static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
222static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
223
[6ecc079]224static inline Time ?=?( Time & time, timeval t ) with( time ) {
[273cde6]225        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
[6ecc079]226        return time;
227} // ?=?
228
229static inline Time ?=?( Time & time, timespec t ) with( time ) {
230        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
231        return time;
232} // ?=?
233
234static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
235static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
236static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
237
238static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
239static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; }
240static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
241static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; }
242static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; }
243static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; }
244static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; }
245static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; }
246static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
247
[2a84d06d]248char * yy_mm_dd( Time time, char * buf );
249static inline char * ?`ymd( Time time, char * buf ) {
250        return yy_mm_dd( time, buf );
251} // ymd
252
253char * mm_dd_yy( Time time, char * buf );
254static inline char * ?`mdy( Time time, char * buf ) {
255        return mm_dd_yy( time, buf );
256} // mdy
257
258char * dd_mm_yy( Time time, char * buf );
259static inline char * ?`dmy( Time time, char * buf ) {
260        return dd_mm_yy( time, buf );;
261} // dmy
262
263size_t strftime( char * buf, size_t size, const char * fmt, Time time );
264
265forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
266
[273cde6]267//------------------------- timeval (cont) -------------------------
268
269static inline void ?{}( timeval & t, Time time ) with( t, time ) {
270        tv_sec = tv / TIMEGRAN;                                                         // seconds
271        tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
272} // ?{}
273
274//------------------------- timespec (cont) -------------------------
275
276static inline void ?{}( timespec & t, Time time ) with( t, time ) {
277        tv_sec = tv / TIMEGRAN;                                                         // seconds
278        tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds
279} // ?{}
280
[6ecc079]281
282//######################### Clock #########################
283
284
285struct Clock {
286        Duration offset;                                                                        // for virtual clock: contains offset from real-time
287        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
288};
289
[2a84d06d]290static inline void resetClock( Clock & clk ) with( clk ) {
291        clocktype = CLOCK_REALTIME_COARSE;
[6ecc079]292} // Clock::resetClock
293
[2a84d06d]294static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
[6ecc079]295        clocktype = -1;
[8eb2018]296        offset = adj + timezone`s;                                                      // timezone is (UTC - local time) in seconds
[6ecc079]297} // resetClock
298
[2a84d06d]299static inline void ?{}( Clock & clk ) {
[6ecc079]300        resetClock( clk );
301} // Clock
302
[2a84d06d]303static inline void ?{}( Clock & clk, Duration adj ) {
[6ecc079]304        resetClock( clk, adj );
305} // Clock
306
[2a84d06d]307static inline Duration getRes() {
[6ecc079]308        struct timespec res;
[2a84d06d]309        clock_getres( CLOCK_REALTIME_COARSE, &res );
[8eb2018]310        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
[6ecc079]311} // getRes
312
[2a84d06d]313static inline Time getTimeNsec() {                                              // with nanoseconds
[6ecc079]314        timespec curr;
315        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
316        return (Time){ curr };
317} // getTime
318
[2a84d06d]319static inline Time getTime() {                                                  // without nanoseconds
320        timespec curr;
321        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
322        curr.tv_nsec = 0;
323        return (Time){ curr };
324} // getTime
325
326static inline Time getTime( Clock & clk ) with( clk ) {
[6ecc079]327        return getTime() + offset;
328} // getTime
329
[2a84d06d]330static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
[6ecc079]331        return getTime() + offset;
332} // getTime
333
[2a84d06d]334static inline timeval getTime( Clock & clk ) {
[6ecc079]335        return (timeval){ clk() };
336} // getTime
337
[2a84d06d]338static inline tm getTime( Clock & clk ) with( clk ) {
[6ecc079]339        tm ret;
340        localtime_r( getTime( clk ).tv_sec, &ret );
341        return ret;
342} // getTime
343
344// Local Variables: //
345// mode: c //
346// tab-width: 4 //
347// End: //
Note: See TracBrowser for help on using the repository browser.