[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 |
---|
[0f56058] | 12 | // Last Modified On : Tue Apr 10 17:25:34 2018 |
---|
| 13 | // Update Count : 622 |
---|
[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 | |
---|
[0f56058] | 29 | #include <time_t.h> // Duration (constructors) / Time (constructors) |
---|
[6ecc079] | 30 | |
---|
| 31 | //######################### Duration ######################### |
---|
| 32 | |
---|
[2a84d06d] | 33 | static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } |
---|
[6ecc079] | 34 | |
---|
| 35 | static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } |
---|
| 36 | static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } |
---|
| 37 | static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } |
---|
| 38 | |
---|
| 39 | static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } |
---|
| 40 | static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } |
---|
| 41 | static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } |
---|
| 42 | |
---|
| 43 | static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } |
---|
| 44 | static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } |
---|
| 45 | static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } |
---|
| 46 | |
---|
| 47 | static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } |
---|
[8eb2018] | 48 | static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } |
---|
[6ecc079] | 49 | static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } |
---|
[643c6b9] | 50 | static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; } |
---|
[6ecc079] | 51 | |
---|
[2a84d06d] | 52 | static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } |
---|
[9f652a1] | 53 | static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; } |
---|
[6ecc079] | 54 | |
---|
| 55 | static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } |
---|
| 56 | static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } |
---|
[2a84d06d] | 57 | static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; } |
---|
[6ecc079] | 58 | static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; } |
---|
[2a84d06d] | 59 | static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; } |
---|
[6ecc079] | 60 | static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; } |
---|
| 61 | |
---|
[2a84d06d] | 62 | static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } |
---|
| 63 | static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; } |
---|
| 64 | static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv < 0; } |
---|
| 65 | static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; } |
---|
| 66 | static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv > 0; } |
---|
| 67 | static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; } |
---|
[6ecc079] | 68 | |
---|
[bc03be3] | 69 | static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; } |
---|
[2a84d06d] | 70 | |
---|
| 71 | forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); |
---|
| 72 | |
---|
[bc03be3] | 73 | static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } |
---|
| 74 | static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; } |
---|
| 75 | static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; } |
---|
[643c6b9] | 76 | static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } |
---|
| 77 | static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } |
---|
| 78 | static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } |
---|
| 79 | static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } |
---|
| 80 | static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } |
---|
| 81 | static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } |
---|
| 82 | static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
| 83 | static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
| 84 | static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
| 85 | static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } |
---|
| 86 | |
---|
| 87 | static inline int64_t ?`ns( Duration dur ) { return dur.tv; } |
---|
| 88 | static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); } |
---|
| 89 | static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); } |
---|
| 90 | static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; } |
---|
| 91 | static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); } |
---|
| 92 | static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); } |
---|
| 93 | static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); } |
---|
| 94 | static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | //######################### C timeval ######################### |
---|
[2a84d06d] | 98 | |
---|
[643c6b9] | 99 | static inline void ?{}( timeval & t ) {} |
---|
| 100 | static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; } |
---|
| 101 | static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; } |
---|
| 102 | static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; } |
---|
| 103 | static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; } |
---|
| 104 | static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; } |
---|
| 105 | static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; } |
---|
| 106 | static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; } |
---|
| 107 | static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; } |
---|
[6ecc079] | 108 | |
---|
| 109 | |
---|
[643c6b9] | 110 | //######################### C timespec ######################### |
---|
[6ecc079] | 111 | |
---|
[643c6b9] | 112 | static inline void ?{}( timespec & t ) {} |
---|
| 113 | static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; } |
---|
| 114 | static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; } |
---|
| 115 | static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; } |
---|
| 116 | static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; } |
---|
| 117 | static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; } |
---|
| 118 | static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; } |
---|
| 119 | static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; } |
---|
| 120 | static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; } |
---|
[6ecc079] | 121 | |
---|
| 122 | |
---|
[8ad6533] | 123 | //######################### C itimerval ######################### |
---|
| 124 | |
---|
| 125 | static inline void ?{}( itimerval & itv, Duration alarm ) with( itv ) { |
---|
| 126 | // itimerval contains durations but but uses time data-structure timeval. |
---|
| 127 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds |
---|
| 128 | it_interval{ 0 }; // 0 seconds, 0 microseconds |
---|
| 129 | } // itimerval |
---|
| 130 | |
---|
| 131 | static inline void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) { |
---|
| 132 | // itimerval contains durations but but uses time data-structure timeval. |
---|
| 133 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds |
---|
| 134 | it_interval{ interval`s, interval`us }; // seconds, microseconds |
---|
| 135 | } // itimerval |
---|
| 136 | |
---|
| 137 | |
---|
[643c6b9] | 138 | //######################### C time ######################### |
---|
[6ecc079] | 139 | |
---|
[643c6b9] | 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 ); } |
---|
[6ecc079] | 146 | |
---|
| 147 | |
---|
[643c6b9] | 148 | //######################### Time ######################### |
---|
[6ecc079] | 149 | |
---|
[bc03be3] | 150 | static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; } |
---|
| 151 | |
---|
[6ecc079] | 152 | static inline void ?{}( Time & time, timeval t ) with( time ) { |
---|
| 153 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; |
---|
| 154 | } // Time |
---|
| 155 | |
---|
| 156 | static inline Time ?=?( Time & time, timeval t ) with( time ) { |
---|
[273cde6] | 157 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL); |
---|
[6ecc079] | 158 | return time; |
---|
| 159 | } // ?=? |
---|
| 160 | |
---|
[bc03be3] | 161 | static inline void ?{}( Time & time, timespec t ) with( time ) { |
---|
| 162 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; |
---|
| 163 | } // Time |
---|
| 164 | |
---|
[6ecc079] | 165 | static inline Time ?=?( Time & time, timespec t ) with( time ) { |
---|
| 166 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; |
---|
| 167 | return time; |
---|
| 168 | } // ?=? |
---|
| 169 | |
---|
| 170 | static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } |
---|
| 171 | static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } |
---|
| 172 | static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } |
---|
| 173 | |
---|
| 174 | static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } |
---|
| 175 | static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; } |
---|
| 176 | static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } |
---|
| 177 | static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; } |
---|
| 178 | static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; } |
---|
| 179 | static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; } |
---|
| 180 | static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; } |
---|
| 181 | static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; } |
---|
| 182 | static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } |
---|
| 183 | |
---|
[2a84d06d] | 184 | char * yy_mm_dd( Time time, char * buf ); |
---|
[643c6b9] | 185 | static inline char * ?`ymd( Time time, char * buf ) { // short form |
---|
[2a84d06d] | 186 | return yy_mm_dd( time, buf ); |
---|
| 187 | } // ymd |
---|
| 188 | |
---|
| 189 | char * mm_dd_yy( Time time, char * buf ); |
---|
[643c6b9] | 190 | static inline char * ?`mdy( Time time, char * buf ) { // short form |
---|
[2a84d06d] | 191 | return mm_dd_yy( time, buf ); |
---|
| 192 | } // mdy |
---|
| 193 | |
---|
| 194 | char * dd_mm_yy( Time time, char * buf ); |
---|
[643c6b9] | 195 | static inline char * ?`dmy( Time time, char * buf ) { // short form |
---|
[2a84d06d] | 196 | return dd_mm_yy( time, buf );; |
---|
| 197 | } // dmy |
---|
| 198 | |
---|
| 199 | size_t strftime( char * buf, size_t size, const char * fmt, Time time ); |
---|
| 200 | |
---|
| 201 | forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); |
---|
| 202 | |
---|
[273cde6] | 203 | //------------------------- timeval (cont) ------------------------- |
---|
| 204 | |
---|
| 205 | static inline void ?{}( timeval & t, Time time ) with( t, time ) { |
---|
| 206 | tv_sec = tv / TIMEGRAN; // seconds |
---|
| 207 | tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds |
---|
| 208 | } // ?{} |
---|
| 209 | |
---|
| 210 | //------------------------- timespec (cont) ------------------------- |
---|
| 211 | |
---|
| 212 | static inline void ?{}( timespec & t, Time time ) with( t, time ) { |
---|
| 213 | tv_sec = tv / TIMEGRAN; // seconds |
---|
| 214 | tv_nsec = tv % TIMEGRAN; // nanoseconds |
---|
| 215 | } // ?{} |
---|
| 216 | |
---|
[6ecc079] | 217 | |
---|
| 218 | //######################### Clock ######################### |
---|
| 219 | |
---|
[643c6b9] | 220 | struct Clock { // private |
---|
[6ecc079] | 221 | Duration offset; // for virtual clock: contains offset from real-time |
---|
| 222 | int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME |
---|
| 223 | }; |
---|
| 224 | |
---|
[2a84d06d] | 225 | static inline void resetClock( Clock & clk ) with( clk ) { |
---|
| 226 | clocktype = CLOCK_REALTIME_COARSE; |
---|
[6ecc079] | 227 | } // Clock::resetClock |
---|
| 228 | |
---|
[2a84d06d] | 229 | static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { |
---|
[6ecc079] | 230 | clocktype = -1; |
---|
[643c6b9] | 231 | offset = adj + timezone`s; // timezone (global) is (UTC - local time) in seconds |
---|
[6ecc079] | 232 | } // resetClock |
---|
| 233 | |
---|
[0f56058] | 234 | static inline void ?{}( Clock & clk ) { resetClock( clk ); } |
---|
| 235 | static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); } |
---|
[6ecc079] | 236 | |
---|
[2a84d06d] | 237 | static inline Duration getRes() { |
---|
[6ecc079] | 238 | struct timespec res; |
---|
[2a84d06d] | 239 | clock_getres( CLOCK_REALTIME_COARSE, &res ); |
---|
[8eb2018] | 240 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
[6ecc079] | 241 | } // getRes |
---|
| 242 | |
---|
[2a84d06d] | 243 | static inline Time getTimeNsec() { // with nanoseconds |
---|
[6ecc079] | 244 | timespec curr; |
---|
| 245 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
| 246 | return (Time){ curr }; |
---|
| 247 | } // getTime |
---|
| 248 | |
---|
[2a84d06d] | 249 | static inline Time getTime() { // without nanoseconds |
---|
| 250 | timespec curr; |
---|
| 251 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
| 252 | curr.tv_nsec = 0; |
---|
| 253 | return (Time){ curr }; |
---|
| 254 | } // getTime |
---|
| 255 | |
---|
| 256 | static inline Time getTime( Clock & clk ) with( clk ) { |
---|
[6ecc079] | 257 | return getTime() + offset; |
---|
| 258 | } // getTime |
---|
| 259 | |
---|
[2a84d06d] | 260 | static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax |
---|
[6ecc079] | 261 | return getTime() + offset; |
---|
| 262 | } // getTime |
---|
| 263 | |
---|
[2a84d06d] | 264 | static inline timeval getTime( Clock & clk ) { |
---|
[6ecc079] | 265 | return (timeval){ clk() }; |
---|
| 266 | } // getTime |
---|
| 267 | |
---|
[2a84d06d] | 268 | static inline tm getTime( Clock & clk ) with( clk ) { |
---|
[6ecc079] | 269 | tm ret; |
---|
| 270 | localtime_r( getTime( clk ).tv_sec, &ret ); |
---|
| 271 | return ret; |
---|
| 272 | } // getTime |
---|
| 273 | |
---|
| 274 | // Local Variables: // |
---|
| 275 | // mode: c // |
---|
| 276 | // tab-width: 4 // |
---|
| 277 | // End: // |
---|