| 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 : Tue Apr 3 08:46:08 2018
|
|---|
| 13 | // Update Count : 585
|
|---|
| 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 | static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
|
|---|
| 116 |
|
|---|
| 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 | static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
|
|---|
| 123 |
|
|---|
| 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 | static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
|
|---|
| 130 |
|
|---|
| 131 | static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; }
|
|---|
| 132 |
|
|---|
| 133 | forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
|
|---|
| 134 |
|
|---|
| 135 | static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
|
|---|
| 136 | static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
|
|---|
| 137 | static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
|
|---|
| 138 | static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
|---|
| 139 | static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
|---|
| 140 | static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
|---|
| 141 | static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
|---|
| 142 | static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 143 | static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 144 | static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 145 | static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 146 | static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 147 | static inline Duration ?`w ( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 148 | //static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 149 | //static inline Duration ?`f ( double fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
|---|
| 150 |
|
|---|
| 151 | static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
|
|---|
| 152 | static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
|
|---|
| 153 | static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
|
|---|
| 154 | static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; }
|
|---|
| 155 | static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
|
|---|
| 156 | static inline int64_t ?`h ( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
|
|---|
| 157 | static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
|
|---|
| 158 | static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
|
|---|
| 159 | static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14LL * 24LL * 60LL * 60LL * TIMEGRAN); }
|
|---|
| 160 |
|
|---|
| 161 | //------------------------- timeval (cont) -------------------------
|
|---|
| 162 |
|
|---|
| 163 | static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
|
|---|
| 164 | t.tv_sec = tv / TIMEGRAN; // seconds
|
|---|
| 165 | t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
|
|---|
| 166 | } // ?{}
|
|---|
| 167 |
|
|---|
| 168 | //------------------------- timespec (cont) -------------------------
|
|---|
| 169 |
|
|---|
| 170 | static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
|
|---|
| 171 | t.tv_sec = tv / TIMEGRAN; // seconds
|
|---|
| 172 | t.tv_nsec = tv % TIMEGRAN; // nanoseconds
|
|---|
| 173 | } // Timespec
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | //######################### Time #########################
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | struct Time {
|
|---|
| 180 | uint64_t tv;
|
|---|
| 181 | };
|
|---|
| 182 |
|
|---|
| 183 | static inline void ?{}( Time & t ) with( t ) {
|
|---|
| 184 | tv = 0;
|
|---|
| 185 | } // Time
|
|---|
| 186 |
|
|---|
| 187 | void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
|
|---|
| 188 |
|
|---|
| 189 | static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
|
|---|
| 190 | time{ year, month, day, hour, min, sec, 0 };
|
|---|
| 191 | } // Time
|
|---|
| 192 |
|
|---|
| 193 | static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
|
|---|
| 194 | time{ year, month, day, hour, min, 0, 0 };
|
|---|
| 195 | } // Time
|
|---|
| 196 |
|
|---|
| 197 | static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
|
|---|
| 198 | time{ year, month, day, hour, 0, 0, 0 };
|
|---|
| 199 | } // Time
|
|---|
| 200 |
|
|---|
| 201 | static inline void ?{}( Time & time, int year, int month, int day ) {
|
|---|
| 202 | time{ year, month, day, 0, 0, 0, 0 };
|
|---|
| 203 | } // Time
|
|---|
| 204 |
|
|---|
| 205 | static inline void ?{}( Time & time, int year, int month ) {
|
|---|
| 206 | time{ year, month, 0, 0, 0, 0, 0 };
|
|---|
| 207 | } // Time
|
|---|
| 208 |
|
|---|
| 209 | static inline void ?{}( Time & time, int year ) {
|
|---|
| 210 | time{ year, 0, 0, 0, 0, 0, 0 };
|
|---|
| 211 | } // Time
|
|---|
| 212 |
|
|---|
| 213 | static inline void ?{}( Time & time, timeval t ) with( time ) {
|
|---|
| 214 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
|
|---|
| 215 | } // Time
|
|---|
| 216 |
|
|---|
| 217 | static inline void ?{}( Time & time, timespec t ) with( time ) {
|
|---|
| 218 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
|
|---|
| 219 | } // Time
|
|---|
| 220 |
|
|---|
| 221 | static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
|
|---|
| 222 | static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
|
|---|
| 223 |
|
|---|
| 224 | static inline Time ?=?( Time & time, timeval t ) with( time ) {
|
|---|
| 225 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
|
|---|
| 226 | return time;
|
|---|
| 227 | } // ?=?
|
|---|
| 228 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 281 |
|
|---|
| 282 | //######################### Clock #########################
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | struct Clock {
|
|---|
| 286 | Duration offset; // for virtual clock: contains offset from real-time
|
|---|
| 287 | int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME
|
|---|
| 288 | };
|
|---|
| 289 |
|
|---|
| 290 | static inline void resetClock( Clock & clk ) with( clk ) {
|
|---|
| 291 | clocktype = CLOCK_REALTIME_COARSE;
|
|---|
| 292 | } // Clock::resetClock
|
|---|
| 293 |
|
|---|
| 294 | static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
|
|---|
| 295 | clocktype = -1;
|
|---|
| 296 | offset = adj + timezone`s; // timezone is (UTC - local time) in seconds
|
|---|
| 297 | } // resetClock
|
|---|
| 298 |
|
|---|
| 299 | static inline void ?{}( Clock & clk ) {
|
|---|
| 300 | resetClock( clk );
|
|---|
| 301 | } // Clock
|
|---|
| 302 |
|
|---|
| 303 | static inline void ?{}( Clock & clk, Duration adj ) {
|
|---|
| 304 | resetClock( clk, adj );
|
|---|
| 305 | } // Clock
|
|---|
| 306 |
|
|---|
| 307 | static inline Duration getRes() {
|
|---|
| 308 | struct timespec res;
|
|---|
| 309 | clock_getres( CLOCK_REALTIME_COARSE, &res );
|
|---|
| 310 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
|
|---|
| 311 | } // getRes
|
|---|
| 312 |
|
|---|
| 313 | static inline Time getTimeNsec() { // with nanoseconds
|
|---|
| 314 | timespec curr;
|
|---|
| 315 | clock_gettime( CLOCK_REALTIME_COARSE, &curr );
|
|---|
| 316 | return (Time){ curr };
|
|---|
| 317 | } // getTime
|
|---|
| 318 |
|
|---|
| 319 | static inline Time getTime() { // without nanoseconds
|
|---|
| 320 | timespec curr;
|
|---|
| 321 | clock_gettime( CLOCK_REALTIME_COARSE, &curr );
|
|---|
| 322 | curr.tv_nsec = 0;
|
|---|
| 323 | return (Time){ curr };
|
|---|
| 324 | } // getTime
|
|---|
| 325 |
|
|---|
| 326 | static inline Time getTime( Clock & clk ) with( clk ) {
|
|---|
| 327 | return getTime() + offset;
|
|---|
| 328 | } // getTime
|
|---|
| 329 |
|
|---|
| 330 | static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax
|
|---|
| 331 | return getTime() + offset;
|
|---|
| 332 | } // getTime
|
|---|
| 333 |
|
|---|
| 334 | static inline timeval getTime( Clock & clk ) {
|
|---|
| 335 | return (timeval){ clk() };
|
|---|
| 336 | } // getTime
|
|---|
| 337 |
|
|---|
| 338 | static inline tm getTime( Clock & clk ) with( clk ) {
|
|---|
| 339 | tm ret;
|
|---|
| 340 | localtime_r( getTime( clk ).tv_sec, &ret );
|
|---|
| 341 | return ret;
|
|---|
| 342 | } // getTime
|
|---|
| 343 |
|
|---|
| 344 | // Local Variables: //
|
|---|
| 345 | // mode: c //
|
|---|
| 346 | // tab-width: 4 //
|
|---|
| 347 | // End: //
|
|---|