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