// // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // time -- // // Author : Peter A. Buhr // Created On : Wed Mar 14 23:18:57 2018 // Last Modified By : Peter A. Buhr // Last Modified On : Sun Apr 1 20:03:16 2018 // Update Count : 581 // #pragma once // http://en.cppreference.com/w/cpp/header/chrono // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc #include // timespec extern "C" { #include // timeval } #include // istype/ostype enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval //######################### timeval ######################### static inline void ?{}( timeval & t ) {} static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; } static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; } static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; } static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; } static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; } static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; } static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; } static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; } //######################### timespec ######################### static inline void ?{}( timespec & t ) {} static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; } static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; } static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; } static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; } static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; } static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; } static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; } static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; } //######################### C time ######################### static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; } static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; } static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); } static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); } static inline tm * localtime( time_t tp ) { return localtime( &tp ); } static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); } //######################### Duration ######################### struct Duration { int64_t tv; }; static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; } static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; } static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; } static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } #if 0 static inline void ?{}( Duration & dur, timeval t ) with( dur ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; } // Duration static inline void ?{}( Duration & dur, timespec t ) with( dur ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; } // Duration static inline Duration ?=?( Duration & dur, timeval t ) with( dur ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; return dur; } // ?=? static inline Duration ?=?( Duration & dur, timespec t ) with( dur ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; return dur; } // ?=? timespec #endif //static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; } static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } static inline _Bool ?? ( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; } static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; } static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; } static inline _Bool ?? ( Duration lhs, zero_t ) { return lhs.tv > 0; } static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; } static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; } forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; } static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; } static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; } static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600LL * TIMEGRAN) }; } static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (3600LL * TIMEGRAN) }; } static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24LL * 3600LL * TIMEGRAN) }; } static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24LL * 3600LL * TIMEGRAN) }; } static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 3600LL * TIMEGRAN) }; } //static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 3600LL * TIMEGRAN) }; } static inline int64_t ?`ns ( Duration dur ) { return dur.tv; } static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); } static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); } static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; } static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); } static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600LL * TIMEGRAN); } static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24LL * 3600LL * TIMEGRAN); } static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7LL * 24LL * 3600LL * TIMEGRAN); } static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14LL * 24LL * 3600LL * TIMEGRAN); } //------------------------- timeval (cont) ------------------------- static inline void ?{}( timeval & t, Duration dur ) with( dur ) { t.tv_sec = tv / TIMEGRAN; // seconds t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds } // ?{} //------------------------- timespec (cont) ------------------------- static inline void ?{}( timespec & t, Duration dur ) with( dur ) { t.tv_sec = tv / TIMEGRAN; // seconds t.tv_nsec = tv % TIMEGRAN; // nanoseconds } // Timespec //######################### Time ######################### struct Time { uint64_t tv; }; static inline void ?{}( Time & t ) with( t ) { tv = 0; } // Time void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ); static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) { time{ year, month, day, hour, min, sec, 0 }; } // Time static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) { time{ year, month, day, hour, min, 0, 0 }; } // Time static inline void ?{}( Time & time, int year, int month, int day, int hour ) { time{ year, month, day, hour, 0, 0, 0 }; } // Time static inline void ?{}( Time & time, int year, int month, int day ) { time{ year, month, day, 0, 0, 0, 0 }; } // Time static inline void ?{}( Time & time, int year, int month ) { time{ year, month, 0, 0, 0, 0, 0 }; } // Time static inline void ?{}( Time & time, int year ) { time{ year, 0, 0, 0, 0, 0, 0 }; } // Time static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; } // Time static inline void ?{}( Time & time, timespec t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; } // Time static inline void ?{}( Time & t, zero_t ) { t.tv = 0; } static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; } static inline Time ?=?( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL); return time; } // ?=? static inline Time ?=?( Time & time, timespec t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; return time; } // ?=? static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; } static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; } static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; } static inline _Bool ??( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; } static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } char * yy_mm_dd( Time time, char * buf ); static inline char * ?`ymd( Time time, char * buf ) { return yy_mm_dd( time, buf ); } // ymd char * mm_dd_yy( Time time, char * buf ); static inline char * ?`mdy( Time time, char * buf ) { return mm_dd_yy( time, buf ); } // mdy char * dd_mm_yy( Time time, char * buf ); static inline char * ?`dmy( Time time, char * buf ) { return dd_mm_yy( time, buf );; } // dmy size_t strftime( char * buf, size_t size, const char * fmt, Time time ); forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); //------------------------- timeval (cont) ------------------------- static inline void ?{}( timeval & t, Time time ) with( t, time ) { tv_sec = tv / TIMEGRAN; // seconds tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds } // ?{} //------------------------- timespec (cont) ------------------------- static inline void ?{}( timespec & t, Time time ) with( t, time ) { tv_sec = tv / TIMEGRAN; // seconds tv_nsec = tv % TIMEGRAN; // nanoseconds } // ?{} //######################### Clock ######################### struct Clock { Duration offset; // for virtual clock: contains offset from real-time int clocktype; // implementation only -1 (virtual), CLOCK_REALTIME }; static inline void resetClock( Clock & clk ) with( clk ) { clocktype = CLOCK_REALTIME_COARSE; } // Clock::resetClock static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { clocktype = -1; offset = adj + timezone`s; // timezone is (UTC - local time) in seconds } // resetClock static inline void ?{}( Clock & clk ) { resetClock( clk ); } // Clock static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); } // Clock static inline Duration getRes() { struct timespec res; clock_getres( CLOCK_REALTIME_COARSE, &res ); return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; } // getRes static inline Time getTimeNsec() { // with nanoseconds timespec curr; clock_gettime( CLOCK_REALTIME_COARSE, &curr ); return (Time){ curr }; } // getTime static inline Time getTime() { // without nanoseconds timespec curr; clock_gettime( CLOCK_REALTIME_COARSE, &curr ); curr.tv_nsec = 0; return (Time){ curr }; } // getTime static inline Time getTime( Clock & clk ) with( clk ) { return getTime() + offset; } // getTime static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax return getTime() + offset; } // getTime static inline timeval getTime( Clock & clk ) { return (timeval){ clk() }; } // getTime static inline tm getTime( Clock & clk ) with( clk ) { tm ret; localtime_r( getTime( clk ).tv_sec, &ret ); return ret; } // getTime // Local Variables: // // mode: c // // tab-width: 4 // // End: //