Changeset 2a84d06d for src/libcfa/time
- Timestamp:
- Mar 27, 2018, 5:22:58 PM (6 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:
- 43725bd
- Parents:
- af1ed1ad
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/time
raf1ed1ad r2a84d06d 1 1 // 2 // Cforall Version 1.0.0 Copyright (C) 201 6University of Waterloo2 // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo 3 3 // 4 4 // The contents of this file are covered under the licence agreement in the 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // calendar--7 // time -- 8 8 // 9 9 // Author : Peter A. Buhr 10 10 // Created On : Wed Mar 14 23:18:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Mar 22 17:11:19201813 // Update Count : 49512 // Last Modified On : Tue Mar 27 16:37:37 2018 13 // Update Count : 564 14 14 // 15 15 … … 19 19 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc 20 20 21 #include <time.h> 21 #include <time.h> // timespec 22 22 extern "C" { 23 #include <sys/time.h> 24 int snprintf( char * buf, size_t size, const char * fmt, ... ); 23 #include <sys/time.h> // timeval 25 24 } 26 #include <fstream> 27 28 enum { 29 CLOCKGRAN = 15_000_000L, // ALWAYS in nanoseconds, MUST BE less than 1 second 30 TIMEGRAN = 1_000_000_000L // nanosecond granularity, except for timeval 31 }; 32 33 34 #if defined( REALTIME_POSIX ) 35 #define tv_XSEC tv_nsec 36 #else 37 #define tv_XSEC tv_usec 38 #endif 39 40 41 #if defined( __linux__ ) 42 // fake a few things 43 #define CLOCK_REALTIME 0 // real (clock on the wall) time 44 #endif 45 46 // conversions for existing time types 25 #include <iostream> // istype/ostype 26 27 enum { TIMEGRAN = 1_000_000_000L }; // nanosecond granularity, except for timeval 28 29 30 //######################### timeval ######################### 31 47 32 static inline void ?{}( timeval & t ) {} 48 33 static inline void ?{}( timeval & t, time_t sec ) { t.tv_sec = sec; t.tv_usec = 0; } 49 34 static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; } 35 static inline void ?{}( timeval & t, zero_t ) { t.tv_sec = 0; t.tv_usec = 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 ######################### 50 44 51 45 static inline void ?{}( timespec & t ) {} 52 46 static inline void ?{}( timespec & t, time_t sec ) { t.tv_sec = sec; t.tv_nsec = 0; } 53 47 static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; } 48 static inline void ?{}( timespec & t, zero_t ) { t.tv_sec = 0; t.tv_nsec = 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 ######################### 54 57 55 58 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; } … … 60 63 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); } 61 64 62 63 65 //######################### Duration ######################### 64 66 … … 69 71 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; } 70 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 }; } 71 75 72 76 static inline void ?{}( Duration & dur, timeval t ) with( dur ) { … … 88 92 } // ?=? timespec 89 93 94 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; } 95 96 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } 97 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } 98 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } 99 100 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } 101 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } 102 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } 103 104 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } 105 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } 106 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } 107 108 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } 109 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } 110 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } 111 112 static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } 113 114 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } 115 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } 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 121 static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } 122 static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; } 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 128 static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; } 129 130 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 131 132 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } 133 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000L) }; } 134 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000L) }; } 135 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } 136 static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } 137 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; } 138 static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; } 139 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; } 140 static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; } 141 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; } 142 static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; } 143 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; } 144 static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; } 145 146 static inline int64_t ?`ns ( Duration dur ) { return dur.tv; } 147 static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000L); } 148 static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000L); } 149 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; } 150 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); } 151 static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); } 152 static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); } 153 static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); } 154 static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); } 155 156 //------------------------- timeval (cont) ------------------------- 157 90 158 static inline void ?{}( timeval & t, Duration dur ) with( dur ) { 91 159 t.tv_sec = tv / TIMEGRAN; // seconds 92 t.tv_usec = tv % TIMEGRAN / ( TIMEGRAN / 1000000L); // microseconds160 t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000L); // microseconds 93 161 } // ?{} 162 163 //------------------------- timespec (cont) ------------------------- 94 164 95 165 static inline void ?{}( timespec & t, Duration dur ) with( dur ) { … … 98 168 } // Timespec 99 169 100 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }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 rhs ) with( rhs ) { return (Duration)@{ -tv }; }107 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }108 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }109 110 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }111 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }112 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }113 114 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }115 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }116 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }117 118 static inline Duration ?%?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv % rhs }; }119 static inline int64_t ?%?( int64_t lhs, Duration rhs ) { return lhs % (rhs.tv / TIMEGRAN); }120 static inline int64_t ?%?( Duration lhs, Duration rhs ) { return lhs.tv % rhs.tv; }121 122 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }123 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }124 static inline _Bool ?<?( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; }125 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }126 static inline _Bool ?>?( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; }127 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }128 129 static inline Duration abs( Duration lhs ) {130 return lhs.tv >= 0 ? lhs : -lhs;131 } // abs132 133 static inline forall( dtype ostype | ostream( ostype ) )134 ostype & ?|?( ostype & os, Duration dur ) with( dur ) {135 os | tv / TIMEGRAN;136 char buf[16];137 snprintf( buf, 16, "%09ld", ((tv < 0 ? -tv : tv) % TIMEGRAN) );138 int i;139 for ( i = 8; i >= 0 && buf[i] == '0' ; i -= 1 ); // find least significant digit140 if ( i != -1 ) { buf[i + 1] = '\0'; os | '.' | buf; }141 return os;142 }143 144 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }145 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000l) }; }146 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000_000l) }; }147 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }148 static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }149 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }150 static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }151 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }152 static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }153 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }154 static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }155 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; }156 static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; }157 158 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; }159 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); }160 static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); }161 static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); }162 static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); }163 static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); }164 165 170 166 171 //######################### Time ######################### … … 171 176 }; 172 177 173 #ifdef __CFA_DEBUG__ 174 #define CreateFmt "Attempt to create Time( year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d, nsec=%d ), " \ 175 "which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038." 176 #endif // __CFA_DEBUG__ 177 178 void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) { 179 tm tm; 180 181 // tzset(); // initialize time global variables 182 tm.tm_isdst = -1; // let mktime determine if alternate timezone is in effect 183 tm.tm_year = year - 1900; // mktime uses 1900 as its starting point 184 tm.tm_mon = month - 1; 185 tm.tm_mday = day; // mktime uses range 1-31 186 tm.tm_hour = hour; 187 tm.tm_min = min; 188 tm.tm_sec = sec; 189 time_t epochsec = mktime( &tm ); 190 #ifdef __CFA_DEBUG__ 191 if ( epochsec == (time_t)-1 ) { 192 abort( CreateFmt, year, month, day, hour, min, sec, nsec ); 193 } // if 194 #endif // __CFA_DEBUG__ 195 tv = (int64_t)(epochsec) * TIMEGRAN + nsec; // convert to nanoseconds 196 #ifdef __CFA_DEBUG__ 197 if ( tv > 2147483647LL * TIMEGRAN ) { // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038. 198 abort( CreateFmt, year, month, day, hour, min, sec, nsec ); 199 } // if 200 #endif // __CFA_DEBUG__ 201 } // mktime 178 void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ); 202 179 203 180 static inline void ?{}( Time & t ) with( t ) { … … 205 182 } // Time 206 183 207 // These two constructors must not call mktime because it calls malloc. The malloc calls lead to recursion problems 208 // because Time values are created from the sigalrm handler in composing the next context switch event. 209 210 static inline void ?{}( Time & t, int sec ) with( t ) { 211 #ifdef __CFA_DEBUG__ 212 if ( tv < 0 || tv > 2147483647LL ) { // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038. 213 abort( CreateFmt, 1970, 0, 0, 0, 0, sec, 0 ); 214 } // if 215 #endif // __CFA_DEBUG__ 216 tv = (int64_t)sec * TIMEGRAN; 217 } // Time 218 219 static inline void ?{}( Time & t, int sec, int nsec ) with( t ) { 220 #ifdef __U_DEBUG__ 221 if ( tv < 0 || tv > 2147483647LL || nsec < 0 ) { // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038. 222 abort( CreateFmt, 1970, 0, 0, 0, 0, sec, nsec ); 223 } // if 224 #endif // __U_DEBUG__ 225 tv = (int64_t)sec * TIMEGRAN + nsec; 226 } // Time 227 228 static inline void ?{}( Time & time, int min, int sec, long int nsec ) { 229 mktime( time, 1970, 1, 1, 0, min, sec, nsec ); 230 } // Time 231 232 static inline void ?{}( Time & time, int hour, int min, int sec, long int nsec ) { 233 mktime( time, 1970, 1, 1, hour, min, sec, nsec ); 234 } // Time 235 236 static inline void ?{}( Time & time, int day, int hour, int min, int sec, long int nsec ) { 237 mktime( time, 1970, 1, day, hour, min, sec, nsec ); 238 } // Time 239 240 static inline void ?{}( Time & time, int month, int day, int hour, int min, int sec, long int nsec ) { 241 mktime( time, 1970, month, day, hour, min, sec, nsec ); 242 } // Time 243 244 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, long int nsec ) { 184 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) { 245 185 mktime( time, year, month, day, hour, min, sec, nsec ); 186 } // Time 187 188 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) { 189 mktime( time, year, month, day, hour, min, sec, 0 ); 190 } // Time 191 192 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) { 193 mktime( time, year, month, day, hour, min, 0, 0 ); 194 } // Time 195 196 static inline void ?{}( Time & time, int year, int month, int day, int hour ) { 197 mktime( time, year, month, day, hour, 0, 0, 0 ); 198 } // Time 199 200 static inline void ?{}( Time & time, int year, int month, int day ) { 201 mktime( time, year, month, day, 0, 0, 0, 0 ); 202 } // Time 203 204 static inline void ?{}( Time & time, int year, int month ) { 205 mktime( time, year, month, 0, 0, 0, 0, 0 ); 206 } // Time 207 208 static inline void ?{}( Time & time, int year ) { 209 mktime( time, year, 0, 0, 0, 0, 0, 0 ); 246 210 } // Time 247 211 … … 254 218 } // Time 255 219 220 static inline void ?{}( Time & t, zero_t ) { t.tv = 0; } 221 static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; } 222 256 223 static inline Time ?=?( Time & time, timeval t ) with( time ) { 257 tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;224 tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000L); 258 225 return time; 259 226 } // ?=? … … 273 240 t.tv_nsec = tv % TIMEGRAN; // nanoseconds 274 241 } // ?{} 275 276 static inline int64_t nsec( Time time ) with( time ) { return tv; }277 242 278 243 static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } … … 290 255 static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } 291 256 292 static inline char * yymmd( Time time, char * buf ) with( time ) { 293 tm tm; 294 time_t s = tv / TIMEGRAN; 295 gmtime_r( &s, &tm ); 296 snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon, tm.tm_mday ); 297 return buf; 298 } // yymmd 299 300 static inline char * mmyyd( Time time, char * buf ) with( time ) { 301 tm tm; 302 time_t s = tv / TIMEGRAN; 303 gmtime_r( &s, &tm ); 304 snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon, tm.tm_year % 99, tm.tm_mday ); 305 return buf; 306 } // yymmd 307 308 static inline char * dmmyy( Time time, char * buf ) with( time ) { 309 tm tm; 310 time_t s = tv / TIMEGRAN; 311 gmtime_r( &s, &tm ); 312 snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon, tm.tm_year % 99 ); 313 return buf; 314 } // yymmd 315 316 static inline forall( dtype ostype | ostream( ostype ) ) 317 ostype & ?|?( ostype & os, Time time ) with( time ) { 318 char buf[32]; // at least 26 319 time_t s = tv / TIMEGRAN; 320 tm tm; 321 gmtime_r( &s, &tm ); // ctime_r adjusts for timezone 322 asctime_r( &tm, (char *)&buf ); 323 buf[24] = '\0'; // remove trailing '\n' 324 long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN; 325 if ( ns == 0 ) { 326 os | buf; 327 } else { 328 buf[19] = '\0'; 329 os | buf; 330 char buf2[16]; 331 snprintf( buf2, 16, "%09ld", ns ); 332 int i; 333 for ( i = 8; i >= 0 && buf2[i] == '0' ; i -= 1 ); // find least significant digit 334 if ( i != -1 ) { buf2[i + 1] = '\0'; os | '.' | buf2; } 335 os | ' ' | &buf[20]; 336 } // if 337 return os; 338 } // ?|? 257 char * yy_mm_dd( Time time, char * buf ); 258 static inline char * ?`ymd( Time time, char * buf ) { 259 return yy_mm_dd( time, buf ); 260 } // ymd 261 262 char * mm_dd_yy( Time time, char * buf ); 263 static inline char * ?`mdy( Time time, char * buf ) { 264 return mm_dd_yy( time, buf ); 265 } // mdy 266 267 char * dd_mm_yy( Time time, char * buf ); 268 static inline char * ?`dmy( Time time, char * buf ) { 269 return dd_mm_yy( time, buf );; 270 } // dmy 271 272 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 273 274 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 275 339 276 340 277 //######################### Clock ######################### … … 346 283 }; 347 284 348 void resetClock( Clock & clk ) with( clk ) {349 clocktype = CLOCK_REALTIME ;285 static inline void resetClock( Clock & clk ) with( clk ) { 286 clocktype = CLOCK_REALTIME_COARSE; 350 287 } // Clock::resetClock 351 288 352 void resetClock( Clock & clk, Duration adj ) with( clk ) {289 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { 353 290 clocktype = -1; 354 291 Duration tz = (timeval){ timezone, 0 }; … … 356 293 } // resetClock 357 294 358 void ?{}( Clock & clk ) {295 static inline void ?{}( Clock & clk ) { 359 296 resetClock( clk ); 360 297 } // Clock 361 298 362 void ?{}( Clock & clk, Duration adj ) {299 static inline void ?{}( Clock & clk, Duration adj ) { 363 300 resetClock( clk, adj ); 364 301 } // Clock 365 302 366 Duration getRes() {303 static inline Duration getRes() { 367 304 struct timespec res; 368 clock_getres( CLOCK_REALTIME , &res );305 clock_getres( CLOCK_REALTIME_COARSE, &res ); 369 306 return (Duration){ res }; 370 307 } // getRes 371 308 372 Time getTime() { 309 static inline Time getTimeNsec() { // with nanoseconds 373 310 timespec curr; 374 311 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); … … 376 313 } // getTime 377 314 378 Time getTime( Clock & clk ) with( clk ) { 315 static inline Time getTime() { // without nanoseconds 316 timespec curr; 317 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); 318 curr.tv_nsec = 0; 319 return (Time){ curr }; 320 } // getTime 321 322 static inline Time getTime( Clock & clk ) with( clk ) { 379 323 return getTime() + offset; 380 324 } // getTime 381 325 382 Time ?()( Clock & clk ) with( clk ) {// alternative syntax326 static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax 383 327 return getTime() + offset; 384 328 } // getTime 385 329 386 timeval getTime( Clock & clk ) {330 static inline timeval getTime( Clock & clk ) { 387 331 return (timeval){ clk() }; 388 332 } // getTime 389 333 390 tm getTime( Clock & clk ) with( clk ) {334 static inline tm getTime( Clock & clk ) with( clk ) { 391 335 tm ret; 392 336 localtime_r( getTime( clk ).tv_sec, &ret ); … … 398 342 // tab-width: 4 // 399 343 // End: // 400
Note: See TracChangeset
for help on using the changeset viewer.