Changeset 10a97adb for src/libcfa/time
- Timestamp:
- Apr 12, 2018, 8:53:12 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- 07b8001
- Parents:
- 2ae8507a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/time
r2ae8507a r10a97adb 10 10 // Created On : Wed Mar 14 23:18:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Apr 10 17:25:34201813 // Update Count : 62 212 // Last Modified On : Thu Apr 12 16:53:35 2018 13 // Update Count : 629 14 14 // 15 15 … … 23 23 #include <sys/time.h> // timeval 24 24 } 25 #include < iostream> // istype/ostype25 #include <time_t.h> // Duration/Time types 26 26 27 27 enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval 28 28 29 #include <time_t.h> // Duration (constructors) / Time (constructors)30 29 31 30 //######################### Duration ######################### … … 68 67 69 68 static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; } 70 71 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );72 69 73 70 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } … … 136 133 137 134 138 //######################### C time #########################139 140 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }141 static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }142 static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }143 static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }144 static inline tm * localtime( time_t tp ) { return localtime( &tp ); }145 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }146 147 148 135 //######################### Time ######################### 149 136 … … 199 186 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 200 187 201 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );202 203 188 //------------------------- timeval (cont) ------------------------- 204 189 … … 215 200 } // ?{} 216 201 217 218 //######################### Clock #########################219 220 struct Clock { // private221 Duration offset; // for virtual clock: contains offset from real-time222 int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME223 };224 225 static inline void resetClock( Clock & clk ) with( clk ) {226 clocktype = CLOCK_REALTIME_COARSE;227 } // Clock::resetClock228 229 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {230 clocktype = -1;231 offset = adj + timezone`s; // timezone (global) is (UTC - local time) in seconds232 } // resetClock233 234 static inline void ?{}( Clock & clk ) { resetClock( clk ); }235 static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }236 237 static inline 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 } // getRes242 243 static inline Time getTimeNsec() { // with nanoseconds244 timespec curr;245 clock_gettime( CLOCK_REALTIME_COARSE, &curr );246 return (Time){ curr };247 } // getTime248 249 static inline Time getTime() { // without nanoseconds250 timespec curr;251 clock_gettime( CLOCK_REALTIME_COARSE, &curr );252 curr.tv_nsec = 0;253 return (Time){ curr };254 } // getTime255 256 static inline Time getTime( Clock & clk ) with( clk ) {257 return getTime() + offset;258 } // getTime259 260 static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax261 return getTime() + offset;262 } // getTime263 264 static inline timeval getTime( Clock & clk ) {265 return (timeval){ clk() };266 } // getTime267 268 static inline tm getTime( Clock & clk ) with( clk ) {269 tm ret;270 localtime_r( getTime( clk ).tv_sec, &ret );271 return ret;272 } // getTime273 274 202 // Local Variables: // 275 203 // mode: c //
Note: See TracChangeset
for help on using the changeset viewer.