[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
|
---|
[030653a] | 12 | // Last Modified On : Wed Jun 17 16:13:00 2020
|
---|
| 13 | // Update Count : 663
|
---|
[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
|
---|
| 22 | #include <sys/time.h> // timeval
|
---|
[461eed2] | 23 | #include <time_t.hfa> // Duration/Time types
|
---|
[6ecc079] | 24 |
|
---|
[273cde6] | 25 | enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval
|
---|
[6ecc079] | 26 |
|
---|
| 27 | //######################### Duration #########################
|
---|
| 28 |
|
---|
[bbe1a87] | 29 | static inline {
|
---|
[ffe2fad] | 30 | Duration ?=?( Duration & dur, __attribute__((unused)) zero_t ) { return dur{ 0 }; }
|
---|
[bbe1a87] | 31 |
|
---|
[e0c235c] | 32 | Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tn }; }
|
---|
| 33 | Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn + rhs.tn }; }
|
---|
[bbe1a87] | 34 | Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
|
---|
| 35 |
|
---|
[e0c235c] | 36 | Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tn }; }
|
---|
| 37 | Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
|
---|
[bbe1a87] | 38 | Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
|
---|
| 39 |
|
---|
[e0c235c] | 40 | Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn * rhs }; }
|
---|
| 41 | Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tn }; }
|
---|
[bbe1a87] | 42 | Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
|
---|
| 43 |
|
---|
[e0c235c] | 44 | int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tn / rhs.tn; }
|
---|
| 45 | Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn / rhs }; }
|
---|
[bbe1a87] | 46 | Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
|
---|
[e0c235c] | 47 | double div( Duration lhs, Duration rhs ) { return (double)lhs.tn / (double)rhs.tn; }
|
---|
[bbe1a87] | 48 |
|
---|
[e0c235c] | 49 | Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tn % rhs.tn }; }
|
---|
[bbe1a87] | 50 | Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
|
---|
| 51 |
|
---|
[e0c235c] | 52 | bool ?==?( Duration lhs, Duration rhs ) { return lhs.tn == rhs.tn; }
|
---|
| 53 | bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tn != rhs.tn; }
|
---|
| 54 | bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tn < rhs.tn; }
|
---|
| 55 | bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tn <= rhs.tn; }
|
---|
| 56 | bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tn > rhs.tn; }
|
---|
| 57 | bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tn >= rhs.tn; }
|
---|
[bbe1a87] | 58 |
|
---|
[e0c235c] | 59 | bool ?==?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn == 0; }
|
---|
| 60 | bool ?!=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn != 0; }
|
---|
| 61 | bool ?<? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn < 0; }
|
---|
| 62 | bool ?<=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn <= 0; }
|
---|
| 63 | bool ?>? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn > 0; }
|
---|
| 64 | bool ?>=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn >= 0; }
|
---|
[bbe1a87] | 65 |
|
---|
[e0c235c] | 66 | Duration abs( Duration rhs ) { return rhs.tn >= 0 ? rhs : -rhs; }
|
---|
[bbe1a87] | 67 |
|
---|
| 68 | Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
|
---|
| 69 | Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
|
---|
| 70 | Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
|
---|
| 71 | Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
---|
| 72 | Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
---|
| 73 | Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
---|
| 74 | Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
---|
| 75 | Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
---|
| 76 | Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
---|
| 77 | Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
| 78 | Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
| 79 | Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
| 80 | Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
| 81 |
|
---|
[e0c235c] | 82 | int64_t ?`ns( Duration dur ) { return dur.tn; }
|
---|
| 83 | int64_t ?`us( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000_000LL); }
|
---|
| 84 | int64_t ?`ms( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000LL); }
|
---|
| 85 | int64_t ?`s( Duration dur ) { return dur.tn / TIMEGRAN; }
|
---|
| 86 | int64_t ?`m( Duration dur ) { return dur.tn / (60LL * TIMEGRAN); }
|
---|
| 87 | int64_t ?`h( Duration dur ) { return dur.tn / (60LL * 60LL * TIMEGRAN); }
|
---|
| 88 | int64_t ?`d( Duration dur ) { return dur.tn / (24LL * 60LL * 60LL * TIMEGRAN); }
|
---|
| 89 | int64_t ?`w( Duration dur ) { return dur.tn / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
|
---|
| 90 |
|
---|
[030653a] | 91 | double ?`dns( Duration dur ) { return dur.tn; }
|
---|
| 92 | double ?`dus( Duration dur ) { return dur.tn / ((double)TIMEGRAN / 1_000_000.); }
|
---|
| 93 | double ?`dms( Duration dur ) { return dur.tn / ((double)TIMEGRAN / 1_000.); }
|
---|
| 94 | double ?`ds( Duration dur ) { return dur.tn / (double)TIMEGRAN; }
|
---|
| 95 | double ?`dm( Duration dur ) { return dur.tn / (60. * TIMEGRAN); }
|
---|
| 96 | double ?`dh( Duration dur ) { return dur.tn / (60. * 60. * (double)TIMEGRAN); }
|
---|
| 97 | double ?`dd( Duration dur ) { return dur.tn / (24. * 60. * 60. * (double)TIMEGRAN); }
|
---|
| 98 | double ?`dw( Duration dur ) { return dur.tn / (7. * 24. * 60. * 60. * (double)TIMEGRAN); }
|
---|
| 99 |
|
---|
[e0c235c] | 100 | Duration max( Duration lhs, Duration rhs ) { return (lhs.tn < rhs.tn) ? rhs : lhs;}
|
---|
| 101 | Duration min( Duration lhs, Duration rhs ) { return !(rhs.tn < lhs.tn) ? lhs : rhs;}
|
---|
[bbe1a87] | 102 | } // distribution
|
---|
[643c6b9] | 103 |
|
---|
| 104 | //######################### C timeval #########################
|
---|
[2a84d06d] | 105 |
|
---|
[bbe1a87] | 106 | static inline {
|
---|
| 107 | void ?{}( timeval & t ) {}
|
---|
| 108 | void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
|
---|
| 109 | void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
|
---|
[ffe2fad] | 110 | void ?{}( timeval & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
|
---|
[6ecc079] | 111 |
|
---|
[ffe2fad] | 112 | timeval ?=?( timeval & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
|
---|
[461eed2] | 113 | timeval ?+?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
|
---|
| 114 | timeval ?-?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
|
---|
[93c2e0a] | 115 | bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
|
---|
| 116 | bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
|
---|
[bbe1a87] | 117 | } // distribution
|
---|
[6ecc079] | 118 |
|
---|
[643c6b9] | 119 | //######################### C timespec #########################
|
---|
[6ecc079] | 120 |
|
---|
[bbe1a87] | 121 | static inline {
|
---|
| 122 | void ?{}( timespec & t ) {}
|
---|
| 123 | void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
|
---|
| 124 | void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
|
---|
[ffe2fad] | 125 | void ?{}( timespec & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
|
---|
[6ecc079] | 126 |
|
---|
[ffe2fad] | 127 | timespec ?=?( timespec & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
|
---|
[461eed2] | 128 | timespec ?+?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
|
---|
| 129 | timespec ?-?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
|
---|
[93c2e0a] | 130 | bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
|
---|
| 131 | bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
|
---|
[bbe1a87] | 132 | } // distribution
|
---|
[6ecc079] | 133 |
|
---|
[8ad6533] | 134 | //######################### C itimerval #########################
|
---|
| 135 |
|
---|
[bbe1a87] | 136 | static inline {
|
---|
| 137 | void ?{}( itimerval & itv, Duration alarm ) with( itv ) {
|
---|
| 138 | // itimerval contains durations but but uses time data-structure timeval.
|
---|
| 139 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds
|
---|
| 140 | it_interval{ 0 }; // 0 seconds, 0 microseconds
|
---|
| 141 | } // itimerval
|
---|
[8ad6533] | 142 |
|
---|
[bbe1a87] | 143 | void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) {
|
---|
| 144 | // itimerval contains durations but but uses time data-structure timeval.
|
---|
| 145 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds
|
---|
| 146 | it_interval{ interval`s, interval`us }; // seconds, microseconds
|
---|
| 147 | } // itimerval
|
---|
| 148 | } // distribution
|
---|
[8ad6533] | 149 |
|
---|
[643c6b9] | 150 | //######################### Time #########################
|
---|
[6ecc079] | 151 |
|
---|
[e0c235c] | 152 | void ?{}( Time & time, int year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0, int64_t nsec = 0 );
|
---|
[bbe1a87] | 153 | static inline {
|
---|
[ffe2fad] | 154 | Time ?=?( Time & time, __attribute__((unused)) zero_t ) { return time{ 0 }; }
|
---|
[bbe1a87] | 155 |
|
---|
[e0c235c] | 156 | void ?{}( Time & time, timeval t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
|
---|
[bbe1a87] | 157 | Time ?=?( Time & time, timeval t ) with( time ) {
|
---|
[e0c235c] | 158 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
|
---|
[bbe1a87] | 159 | return time;
|
---|
| 160 | } // ?=?
|
---|
| 161 |
|
---|
[e0c235c] | 162 | void ?{}( Time & time, timespec t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; }
|
---|
[bbe1a87] | 163 | Time ?=?( Time & time, timespec t ) with( time ) {
|
---|
[e0c235c] | 164 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
|
---|
[bbe1a87] | 165 | return time;
|
---|
| 166 | } // ?=?
|
---|
| 167 |
|
---|
[e0c235c] | 168 | Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tn + rhs.tn }; }
|
---|
[bbe1a87] | 169 | Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
|
---|
| 170 | Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
|
---|
| 171 |
|
---|
[e0c235c] | 172 | Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
|
---|
| 173 | Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tn - rhs.tn }; }
|
---|
[bbe1a87] | 174 | Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
|
---|
[e0c235c] | 175 | bool ?==?( Time lhs, Time rhs ) { return lhs.tn == rhs.tn; }
|
---|
| 176 | bool ?!=?( Time lhs, Time rhs ) { return lhs.tn != rhs.tn; }
|
---|
| 177 | bool ?<?( Time lhs, Time rhs ) { return lhs.tn < rhs.tn; }
|
---|
| 178 | bool ?<=?( Time lhs, Time rhs ) { return lhs.tn <= rhs.tn; }
|
---|
| 179 | bool ?>?( Time lhs, Time rhs ) { return lhs.tn > rhs.tn; }
|
---|
| 180 | bool ?>=?( Time lhs, Time rhs ) { return lhs.tn >= rhs.tn; }
|
---|
| 181 |
|
---|
| 182 | int64_t ?`ns( Time t ) { return t.tn; }
|
---|
[bbe1a87] | 183 | } // distribution
|
---|
[6ecc079] | 184 |
|
---|
[2a84d06d] | 185 | char * yy_mm_dd( Time time, char * buf );
|
---|
[643c6b9] | 186 | static inline char * ?`ymd( Time time, char * buf ) { // short form
|
---|
[2a84d06d] | 187 | return yy_mm_dd( time, buf );
|
---|
| 188 | } // ymd
|
---|
| 189 |
|
---|
| 190 | char * mm_dd_yy( Time time, char * buf );
|
---|
[643c6b9] | 191 | static inline char * ?`mdy( Time time, char * buf ) { // short form
|
---|
[2a84d06d] | 192 | return mm_dd_yy( time, buf );
|
---|
| 193 | } // mdy
|
---|
| 194 |
|
---|
| 195 | char * dd_mm_yy( Time time, char * buf );
|
---|
[643c6b9] | 196 | static inline char * ?`dmy( Time time, char * buf ) { // short form
|
---|
[2a84d06d] | 197 | return dd_mm_yy( time, buf );;
|
---|
| 198 | } // dmy
|
---|
| 199 |
|
---|
[e3fea42] | 200 | size_t strftime( char buf[], size_t size, const char fmt[], Time time );
|
---|
[2a84d06d] | 201 |
|
---|
[273cde6] | 202 | //------------------------- timeval (cont) -------------------------
|
---|
| 203 |
|
---|
| 204 | static inline void ?{}( timeval & t, Time time ) with( t, time ) {
|
---|
[e0c235c] | 205 | tv_sec = tn / TIMEGRAN; // seconds
|
---|
| 206 | tv_usec = tn % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
|
---|
[273cde6] | 207 | } // ?{}
|
---|
| 208 |
|
---|
| 209 | //------------------------- timespec (cont) -------------------------
|
---|
| 210 |
|
---|
| 211 | static inline void ?{}( timespec & t, Time time ) with( t, time ) {
|
---|
[e0c235c] | 212 | tv_sec = tn / TIMEGRAN; // seconds
|
---|
| 213 | tv_nsec = tn % TIMEGRAN; // nanoseconds
|
---|
[273cde6] | 214 | } // ?{}
|
---|
| 215 |
|
---|
[6ecc079] | 216 | // Local Variables: //
|
---|
| 217 | // mode: c //
|
---|
| 218 | // tab-width: 4 //
|
---|
| 219 | // End: //
|
---|