| [b1a4300] | 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. | 
|---|
| [b1a4300] | 6 | // | 
|---|
|  | 7 | // time -- | 
|---|
|  | 8 | // | 
|---|
| [6ecc079] | 9 | // Author           : Peter A. Buhr | 
|---|
|  | 10 | // Created On       : Wed Mar 14 23:18:57 2018 | 
|---|
|  | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [82df430] | 12 | // Last Modified On : Sat Apr 14 17:48:23 2018 | 
|---|
|  | 13 | // Update Count     : 636 | 
|---|
| [b1a4300] | 14 | // | 
|---|
| [6ecc079] | 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 | } | 
|---|
| [10a97adb] | 25 | #include <time_t.h>                                                                             // Duration/Time types | 
|---|
| [6ecc079] | 26 |  | 
|---|
| [273cde6] | 27 | enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval | 
|---|
| [6ecc079] | 28 |  | 
|---|
|  | 29 |  | 
|---|
|  | 30 | //######################### Duration ######################### | 
|---|
|  | 31 |  | 
|---|
| [2a84d06d] | 32 | static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } | 
|---|
| [6ecc079] | 33 |  | 
|---|
|  | 34 | static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } | 
|---|
|  | 35 | static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } | 
|---|
|  | 36 | static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } | 
|---|
|  | 37 |  | 
|---|
|  | 38 | static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } | 
|---|
|  | 39 | static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } | 
|---|
|  | 40 | static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } | 
|---|
|  | 43 | static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } | 
|---|
|  | 44 | static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } | 
|---|
|  | 45 |  | 
|---|
|  | 46 | static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } | 
|---|
| [8eb2018] | 47 | static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } | 
|---|
| [6ecc079] | 48 | static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } | 
|---|
| [643c6b9] | 49 | static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; } | 
|---|
| [6ecc079] | 50 |  | 
|---|
| [2a84d06d] | 51 | static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } | 
|---|
| [9f652a1] | 52 | static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; } | 
|---|
| [6ecc079] | 53 |  | 
|---|
|  | 54 | static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } | 
|---|
|  | 55 | static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } | 
|---|
| [2a84d06d] | 56 | static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; } | 
|---|
| [6ecc079] | 57 | static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; } | 
|---|
| [2a84d06d] | 58 | static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; } | 
|---|
| [6ecc079] | 59 | static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; } | 
|---|
|  | 60 |  | 
|---|
| [2a84d06d] | 61 | static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } | 
|---|
|  | 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; } | 
|---|
| [6ecc079] | 67 |  | 
|---|
| [bc03be3] | 68 | static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; } | 
|---|
| [2a84d06d] | 69 |  | 
|---|
| [bc03be3] | 70 | static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } | 
|---|
|  | 71 | static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; } | 
|---|
|  | 72 | static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; } | 
|---|
| [643c6b9] | 73 | static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } | 
|---|
|  | 74 | static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } | 
|---|
|  | 75 | static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } | 
|---|
|  | 76 | static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } | 
|---|
|  | 77 | static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 78 | static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 79 | static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 80 | static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 81 | static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 82 | static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | static inline int64_t ?`ns( Duration dur ) { return dur.tv; } | 
|---|
|  | 85 | static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); } | 
|---|
|  | 86 | static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); } | 
|---|
|  | 87 | static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; } | 
|---|
|  | 88 | static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); } | 
|---|
|  | 89 | static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); } | 
|---|
|  | 90 | static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); } | 
|---|
|  | 91 | static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); } | 
|---|
|  | 92 |  | 
|---|
| [b1a4300] | 93 | static inline Duration max( Duration lhs, Duration rhs ) { return  (lhs.tv < rhs.tv) ? rhs : lhs;} | 
|---|
|  | 94 | static inline Duration min( Duration lhs, Duration rhs ) { return !(rhs.tv < lhs.tv) ? lhs : rhs;} | 
|---|
|  | 95 |  | 
|---|
| [643c6b9] | 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 | //######################### Time ######################### | 
|---|
| [6ecc079] | 139 |  | 
|---|
| [07b8001] | 140 | void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); | 
|---|
|  | 141 | static inline Time ?=?( Time & time, zero_t ) { return time{ 0 }; } | 
|---|
| [6ecc079] | 142 |  | 
|---|
| [82df430] | 143 | static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; } | 
|---|
| [6ecc079] | 144 | static inline Time ?=?( Time & time, timeval t ) with( time ) { | 
|---|
| [273cde6] | 145 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL); | 
|---|
| [6ecc079] | 146 | return time; | 
|---|
|  | 147 | } // ?=? | 
|---|
|  | 148 |  | 
|---|
| [82df430] | 149 | static inline void ?{}( Time & time, timespec t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; } | 
|---|
| [6ecc079] | 150 | static inline Time ?=?( Time & time, timespec t ) with( time ) { | 
|---|
|  | 151 | tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; | 
|---|
|  | 152 | return time; | 
|---|
|  | 153 | } // ?=? | 
|---|
|  | 154 |  | 
|---|
|  | 155 | static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } | 
|---|
|  | 156 | static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } | 
|---|
|  | 157 | static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } | 
|---|
|  | 160 | static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; } | 
|---|
|  | 161 | static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } | 
|---|
|  | 162 | static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; } | 
|---|
|  | 163 | static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; } | 
|---|
|  | 164 | static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; } | 
|---|
|  | 165 | static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; } | 
|---|
|  | 166 | static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; } | 
|---|
|  | 167 | static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } | 
|---|
|  | 168 |  | 
|---|
| [2a84d06d] | 169 | char * yy_mm_dd( Time time, char * buf ); | 
|---|
| [643c6b9] | 170 | static inline char * ?`ymd( Time time, char * buf ) {   // short form | 
|---|
| [2a84d06d] | 171 | return yy_mm_dd( time, buf ); | 
|---|
|  | 172 | } // ymd | 
|---|
|  | 173 |  | 
|---|
|  | 174 | char * mm_dd_yy( Time time, char * buf ); | 
|---|
| [643c6b9] | 175 | static inline char * ?`mdy( Time time, char * buf ) {   // short form | 
|---|
| [2a84d06d] | 176 | return mm_dd_yy( time, buf ); | 
|---|
|  | 177 | } // mdy | 
|---|
|  | 178 |  | 
|---|
|  | 179 | char * dd_mm_yy( Time time, char * buf ); | 
|---|
| [643c6b9] | 180 | static inline char * ?`dmy( Time time, char * buf ) {   // short form | 
|---|
| [2a84d06d] | 181 | return dd_mm_yy( time, buf );; | 
|---|
|  | 182 | } // dmy | 
|---|
|  | 183 |  | 
|---|
|  | 184 | size_t strftime( char * buf, size_t size, const char * fmt, Time time ); | 
|---|
|  | 185 |  | 
|---|
| [273cde6] | 186 | //------------------------- timeval (cont) ------------------------- | 
|---|
|  | 187 |  | 
|---|
|  | 188 | static inline void ?{}( timeval & t, Time time ) with( t, time ) { | 
|---|
|  | 189 | tv_sec = tv / TIMEGRAN;                                                         // seconds | 
|---|
|  | 190 | tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds | 
|---|
|  | 191 | } // ?{} | 
|---|
|  | 192 |  | 
|---|
|  | 193 | //------------------------- timespec (cont) ------------------------- | 
|---|
|  | 194 |  | 
|---|
|  | 195 | static inline void ?{}( timespec & t, Time time ) with( t, time ) { | 
|---|
|  | 196 | tv_sec = tv / TIMEGRAN;                                                         // seconds | 
|---|
|  | 197 | tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds | 
|---|
|  | 198 | } // ?{} | 
|---|
|  | 199 |  | 
|---|
| [6ecc079] | 200 | // Local Variables: // | 
|---|
|  | 201 | // mode: c // | 
|---|
|  | 202 | // tab-width: 4 // | 
|---|
|  | 203 | // End: // | 
|---|