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