Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:31

0001 #pragma once
0002 /**
0003 schrono.h
0004 ===========
0005 
0006 Good for measuring durations, but complicated to 
0007 extract string timestamps : see stime.h for that. 
0008 
0009 **/
0010 
0011 #include <cstring>
0012 #include <thread>
0013 #include <chrono>
0014 #include <ctime>
0015 
0016 namespace schrono
0017 {
0018     typedef std::chrono::time_point<std::chrono::high_resolution_clock> TP ; 
0019     typedef std::chrono::duration<double> DT ; 
0020 
0021     inline std::chrono::time_point<std::chrono::high_resolution_clock> stamp()
0022     {
0023         TP t = std::chrono::high_resolution_clock::now();
0024         return t ; 
0025     }
0026     inline double duration( 
0027         std::chrono::time_point<std::chrono::high_resolution_clock>& t0, 
0028         std::chrono::time_point<std::chrono::high_resolution_clock>& t1 )
0029     {
0030         DT _dt = t1 - t0;
0031         double dt = _dt.count() ;
0032         return dt ; 
0033     }
0034 
0035     inline double delta( std::chrono::time_point<std::chrono::high_resolution_clock>& t0 )
0036     {
0037         DT _dt = t0.time_since_epoch() ; // HMM: no standard epoch ? so this might be non-absolute
0038         double dt = _dt.count() ;
0039         return dt ; 
0040     }
0041 
0042     inline double delta_stamp()
0043     {
0044         TP t0 = stamp(); 
0045         return delta(t0); 
0046     }
0047 
0048     inline void sleep(int seconds)
0049     {
0050         std::chrono::seconds dura(seconds);
0051         std::this_thread::sleep_for( dura );
0052     }
0053 
0054     inline std::time_t approx_time_t(std::chrono::time_point<std::chrono::high_resolution_clock>& t0 )
0055     {
0056         auto highResNow = std::chrono::high_resolution_clock::now();
0057         auto systemNow = std::chrono::system_clock::now();
0058         auto offset = std::chrono::duration_cast<std::chrono::system_clock::duration>( highResNow - t0 ); 
0059         auto output = systemNow + offset  ;
0060         std::time_t tt =  std::chrono::system_clock::to_time_t( output );
0061         return tt ; 
0062     }
0063 
0064     inline const char* format( std::time_t tt )
0065     {
0066         std::tm* tm = std::localtime(&tt);
0067         char buffer[32];
0068         // Format: Mo, 15.06.2009 20:20:00
0069         std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", tm);  
0070         return strdup(buffer) ; 
0071      }
0072 
0073     inline const char* format(std::chrono::time_point<std::chrono::high_resolution_clock>& t0)
0074     {
0075         std::time_t tt = approx_time_t(t0); 
0076         return format(tt) ;   
0077     }
0078 
0079 
0080 }
0081 
0082 
0083