Changes in src/libcfa/time [07b8001:8ad6533]
- File:
-
- 1 edited
-
src/libcfa/time (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/time
r07b8001 r8ad6533 10 10 // Created On : Wed Mar 14 23:18:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Apr 13 07:51:52201813 // Update Count : 6 3412 // Last Modified On : Mon Apr 9 13:10:23 2018 13 // Update Count : 616 14 14 // 15 15 … … 23 23 #include <sys/time.h> // timeval 24 24 } 25 #include < time_t.h> // Duration/Time types25 #include <iostream> // istype/ostype 26 26 27 27 enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval … … 30 30 //######################### Duration ######################### 31 31 32 struct Duration { // private 33 int64_t tv; // nanoseconds 34 }; // Duration 35 36 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; } 32 37 static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; } 33 38 39 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; } 34 40 static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } 35 41 … … 69 75 70 76 static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; } 77 78 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 71 79 72 80 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } … … 135 143 136 144 145 //######################### C time ######################### 146 147 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; } 148 static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; } 149 static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); } 150 static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); } 151 static inline tm * localtime( time_t tp ) { return localtime( &tp ); } 152 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); } 153 154 137 155 //######################### Time ######################### 138 156 139 static inline void ?{}( Time & time, Time t ) with( time ) { tv = t.tv; } 140 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); 141 static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; } 142 143 static inline Time ?=?( Time & time, zero_t ) { return time{ 0 }; } 157 struct Time { // private 158 uint64_t tv; // nanoseconds since UNIX epoch 159 }; // Time 160 161 static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast 162 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow 163 164 static inline void ?{}( Time & t, zero_t ) { t.tv = 0; } 165 static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; } 166 167 static inline void ?{}( Time & time, timeval t ) with( time ) { 168 tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; 169 } // Time 144 170 145 171 static inline Time ?=?( Time & time, timeval t ) with( time ) { … … 188 214 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 189 215 216 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 217 190 218 //------------------------- timeval (cont) ------------------------- 191 219 … … 202 230 } // ?{} 203 231 232 233 //######################### Clock ######################### 234 235 struct Clock { // private 236 Duration offset; // for virtual clock: contains offset from real-time 237 int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME 238 }; 239 240 static inline void resetClock( Clock & clk ) with( clk ) { 241 clocktype = CLOCK_REALTIME_COARSE; 242 } // Clock::resetClock 243 244 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { 245 clocktype = -1; 246 offset = adj + timezone`s; // timezone (global) is (UTC - local time) in seconds 247 } // resetClock 248 249 static inline void ?{}( Clock & clk ) { 250 resetClock( clk ); 251 } // Clock 252 253 static inline void ?{}( Clock & clk, Duration adj ) { 254 resetClock( clk, adj ); 255 } // Clock 256 257 static inline Duration getRes() { 258 struct timespec res; 259 clock_getres( CLOCK_REALTIME_COARSE, &res ); 260 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; 261 } // getRes 262 263 static inline Time getTimeNsec() { // with nanoseconds 264 timespec curr; 265 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); 266 return (Time){ curr }; 267 } // getTime 268 269 static inline Time getTime() { // without nanoseconds 270 timespec curr; 271 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); 272 curr.tv_nsec = 0; 273 return (Time){ curr }; 274 } // getTime 275 276 static inline Time getTime( Clock & clk ) with( clk ) { 277 return getTime() + offset; 278 } // getTime 279 280 static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax 281 return getTime() + offset; 282 } // getTime 283 284 static inline timeval getTime( Clock & clk ) { 285 return (timeval){ clk() }; 286 } // getTime 287 288 static inline tm getTime( Clock & clk ) with( clk ) { 289 tm ret; 290 localtime_r( getTime( clk ).tv_sec, &ret ); 291 return ret; 292 } // getTime 293 204 294 // Local Variables: // 205 295 // mode: c //
Note:
See TracChangeset
for help on using the changeset viewer.