| [73abe95] | 1 | //
 | 
|---|
| [10a97adb] | 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.
 | 
|---|
| [73abe95] | 6 | //
 | 
|---|
 | 7 | // clock --
 | 
|---|
 | 8 | //
 | 
|---|
| [10a97adb] | 9 | // Author           : Peter A. Buhr
 | 
|---|
 | 10 | // Created On       : Thu Apr 12 14:36:06 2018
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [54b3fde] | 12 | // Last Modified On : Thu Jun 13 21:21:13 2019
 | 
|---|
 | 13 | // Update Count     : 8
 | 
|---|
| [73abe95] | 14 | //
 | 
|---|
| [10a97adb] | 15 | 
 | 
|---|
| [73abe95] | 16 | #include <time.hfa>
 | 
|---|
| [10a97adb] | 17 | 
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | //######################### C time #########################
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
 | 
|---|
 | 22 | static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
 | 
|---|
 | 23 | static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
 | 
|---|
 | 24 | static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
 | 
|---|
 | 25 | static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
 | 
|---|
 | 26 | static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | //######################### Clock #########################
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | struct Clock {                                                                                  // private
 | 
|---|
 | 32 |         Duration offset;                                                                        // for virtual clock: contains offset from real-time
 | 
|---|
 | 33 |         int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
 | 
|---|
 | 34 | };
 | 
|---|
 | 35 | 
 | 
|---|
| [bbe1a87] | 36 | static inline {
 | 
|---|
 | 37 |         void resetClock( Clock & clk ) with( clk ) {
 | 
|---|
 | 38 |                 clocktype = CLOCK_REALTIME_COARSE;
 | 
|---|
 | 39 |         } // Clock::resetClock
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |         void resetClock( Clock & clk, Duration adj ) with( clk ) {
 | 
|---|
 | 42 |                 clocktype = -1;
 | 
|---|
 | 43 |                 offset = adj + __timezone`s;                                    // timezone (global) is (UTC - local time) in seconds
 | 
|---|
 | 44 |         } // resetClock
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |         void ?{}( Clock & clk ) { resetClock( clk ); }
 | 
|---|
 | 47 |         void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |         Duration getResNsec() {
 | 
|---|
 | 50 |                 struct timespec res;
 | 
|---|
 | 51 |                 clock_getres( CLOCK_REALTIME, &res );
 | 
|---|
 | 52 |                 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
 | 
|---|
 | 53 |         } // getRes
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |         Duration getRes() {
 | 
|---|
 | 56 |                 struct timespec res;
 | 
|---|
 | 57 |                 clock_getres( CLOCK_REALTIME_COARSE, &res );
 | 
|---|
 | 58 |                 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
 | 
|---|
 | 59 |         } // getRes
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |         Time getTimeNsec() {                                                            // with nanoseconds
 | 
|---|
 | 62 |                 timespec curr;
 | 
|---|
 | 63 |                 clock_gettime( CLOCK_REALTIME, &curr );
 | 
|---|
 | 64 |                 return (Time){ curr };
 | 
|---|
| [54b3fde] | 65 |         } // getTimeNsec
 | 
|---|
| [bbe1a87] | 66 | 
 | 
|---|
 | 67 |         Time getTime() {                                                                        // without nanoseconds
 | 
|---|
 | 68 |                 timespec curr;
 | 
|---|
 | 69 |                 clock_gettime( CLOCK_REALTIME_COARSE, &curr );
 | 
|---|
 | 70 |                 curr.tv_nsec = 0;
 | 
|---|
 | 71 |                 return (Time){ curr };
 | 
|---|
 | 72 |         } // getTime
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |         Time getTime( Clock & clk ) with( clk ) {
 | 
|---|
 | 75 |                 return getTime() + offset;
 | 
|---|
 | 76 |         } // getTime
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |         Time ?()( Clock & clk ) with( clk ) {                           // alternative syntax
 | 
|---|
 | 79 |                 return getTime() + offset;
 | 
|---|
 | 80 |         } // getTime
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |         timeval getTime( Clock & clk ) {
 | 
|---|
 | 83 |                 return (timeval){ clk() };
 | 
|---|
 | 84 |         } // getTime
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |         tm getTime( Clock & clk ) with( clk ) {
 | 
|---|
 | 87 |                 tm ret;
 | 
|---|
 | 88 |                 localtime_r( getTime( clk ).tv_sec, &ret );
 | 
|---|
 | 89 |                 return ret;
 | 
|---|
 | 90 |         } // getTime
 | 
|---|
 | 91 | } // distribution
 | 
|---|
| [10a97adb] | 92 | 
 | 
|---|
 | 93 | // Local Variables: //
 | 
|---|
 | 94 | // mode: c //
 | 
|---|
 | 95 | // tab-width: 4 //
 | 
|---|
 | 96 | // End: //
 | 
|---|