[10a97adb] | 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 | // clock -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Apr 12 14:36:06 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Apr 12 16:53:31 2018 |
---|
| 13 | // Update Count : 3 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <time> |
---|
| 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 | |
---|
| 36 | static inline void resetClock( Clock & clk ) with( clk ) { |
---|
| 37 | clocktype = CLOCK_REALTIME_COARSE; |
---|
| 38 | } // Clock::resetClock |
---|
| 39 | |
---|
| 40 | static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { |
---|
| 41 | clocktype = -1; |
---|
| 42 | offset = adj + timezone`s; // timezone (global) is (UTC - local time) in seconds |
---|
| 43 | } // resetClock |
---|
| 44 | |
---|
| 45 | static inline void ?{}( Clock & clk ) { resetClock( clk ); } |
---|
| 46 | static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); } |
---|
| 47 | |
---|
| 48 | static inline Duration getRes() { |
---|
| 49 | struct timespec res; |
---|
| 50 | clock_getres( CLOCK_REALTIME_COARSE, &res ); |
---|
| 51 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
| 52 | } // getRes |
---|
| 53 | |
---|
| 54 | static inline Time getTimeNsec() { // with nanoseconds |
---|
| 55 | timespec curr; |
---|
| 56 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
| 57 | return (Time){ curr }; |
---|
| 58 | } // getTime |
---|
| 59 | |
---|
| 60 | static inline Time getTime() { // without nanoseconds |
---|
| 61 | timespec curr; |
---|
| 62 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
| 63 | curr.tv_nsec = 0; |
---|
| 64 | return (Time){ curr }; |
---|
| 65 | } // getTime |
---|
| 66 | |
---|
| 67 | static inline Time getTime( Clock & clk ) with( clk ) { |
---|
| 68 | return getTime() + offset; |
---|
| 69 | } // getTime |
---|
| 70 | |
---|
| 71 | static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax |
---|
| 72 | return getTime() + offset; |
---|
| 73 | } // getTime |
---|
| 74 | |
---|
| 75 | static inline timeval getTime( Clock & clk ) { |
---|
| 76 | return (timeval){ clk() }; |
---|
| 77 | } // getTime |
---|
| 78 | |
---|
| 79 | static inline tm getTime( Clock & clk ) with( clk ) { |
---|
| 80 | tm ret; |
---|
| 81 | localtime_r( getTime( clk ).tv_sec, &ret ); |
---|
| 82 | return ret; |
---|
| 83 | } // getTime |
---|
| 84 | |
---|
| 85 | // Local Variables: // |
---|
| 86 | // mode: c // |
---|
| 87 | // tab-width: 4 // |
---|
| 88 | // End: // |
---|