Changeset 643c6b9 for src/libcfa/time
- Timestamp:
- Apr 6, 2018, 1:24:31 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- d35abc2
- Parents:
- bc03be3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/time
rbc03be3 r643c6b9 10 10 // Created On : Wed Mar 14 23:18:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Apr 4 16:28:48201813 // Update Count : 59512 // Last Modified On : Fri Apr 6 11:15:49 2018 13 // Update Count : 610 14 14 // 15 15 … … 28 28 29 29 30 //######################### timeval ######################### 30 //######################### Duration ######################### 31 32 struct Duration { // private 33 int64_t tv; // nanoseconds 34 }; // Duration 35 36 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; } 37 static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; } 38 39 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; } 40 static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } 41 42 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } 43 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } 44 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } 45 46 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } 47 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } 48 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } 49 50 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } 51 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } 52 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } 53 54 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } 55 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } 56 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } 57 static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; } 58 59 static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } 60 static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; } 61 62 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } 63 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } 64 static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; } 65 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; } 66 static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; } 67 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; } 68 69 static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } 70 static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; } 71 static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv < 0; } 72 static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; } 73 static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv > 0; } 74 static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; } 75 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 ); 79 80 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } 81 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; } 82 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; } 83 static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } 84 static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } 85 static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } 86 static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } 87 static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } 88 static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } 89 static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } 90 static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } 91 static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } 92 static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } 93 94 static inline int64_t ?`ns( Duration dur ) { return dur.tv; } 95 static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); } 96 static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); } 97 static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; } 98 static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); } 99 static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); } 100 static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); } 101 static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); } 102 103 104 //######################### C timeval ######################### 31 105 32 106 static inline void ?{}( timeval & t ) {} … … 41 115 42 116 43 //######################### timespec #########################117 //######################### C timespec ######################### 44 118 45 119 static inline void ?{}( timespec & t ) {} … … 64 138 65 139 66 //######################### Duration #########################67 68 struct Duration {69 int64_t tv; // nanoseconds70 }; // Duration71 72 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }73 static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }74 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }75 static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }76 77 #if 078 static inline void ?{}( Duration & dur, timeval t ) with( dur ) {79 tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;80 } // Duration81 82 static inline void ?{}( Duration & dur, timespec t ) with( dur ) {83 tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;84 } // Duration85 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 } // ?=? timespec95 96 //------------------------- timeval (cont) -------------------------97 98 static inline void ?{}( timeval & t, Duration dur ) with( dur ) {99 t.tv_sec = tv / TIMEGRAN; // seconds100 t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds101 } // ?{}102 103 //------------------------- timespec (cont) -------------------------104 105 static inline void ?{}( timespec & t, Duration dur ) with( dur ) {106 t.tv_sec = tv / TIMEGRAN; // seconds107 t.tv_nsec = tv % TIMEGRAN; // nanoseconds108 } // Timespec109 110 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }111 #endif112 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; }126 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }127 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }128 129 static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }130 static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }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; }134 static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; }135 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }136 static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; }137 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }138 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; }145 146 static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }147 148 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );149 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) }; }157 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }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) }; }161 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }162 static inline Duration ?`w ( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }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) }; }165 166 static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }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); }169 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; }170 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }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); }175 176 177 140 //######################### Time ######################### 178 141 179 struct Time { 142 struct Time { // private 180 143 uint64_t tv; // nanoseconds since UNIX epoch 181 144 }; 182 145 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 146 static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast 147 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow 212 148 213 149 static inline void ?{}( Time & t, zero_t ) { t.tv = 0; } … … 247 183 248 184 char * yy_mm_dd( Time time, char * buf ); 249 static inline char * ?`ymd( Time time, char * buf ) { 185 static inline char * ?`ymd( Time time, char * buf ) { // short form 250 186 return yy_mm_dd( time, buf ); 251 187 } // ymd 252 188 253 189 char * mm_dd_yy( Time time, char * buf ); 254 static inline char * ?`mdy( Time time, char * buf ) { 190 static inline char * ?`mdy( Time time, char * buf ) { // short form 255 191 return mm_dd_yy( time, buf ); 256 192 } // mdy 257 193 258 194 char * dd_mm_yy( Time time, char * buf ); 259 static inline char * ?`dmy( Time time, char * buf ) { 195 static inline char * ?`dmy( Time time, char * buf ) { // short form 260 196 return dd_mm_yy( time, buf );; 261 197 } // dmy … … 282 218 //######################### Clock ######################### 283 219 284 struct Clock { 220 struct Clock { // private 285 221 Duration offset; // for virtual clock: contains offset from real-time 286 222 int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME … … 293 229 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { 294 230 clocktype = -1; 295 offset = adj + timezone`s; // timezone is (UTC - local time) in seconds231 offset = adj + timezone`s; // timezone (global) is (UTC - local time) in seconds 296 232 } // resetClock 297 233
Note: See TracChangeset
for help on using the changeset viewer.