Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 /**
0003 s_time.h 
0004 ==========================================================
0005 
0006 This follows the approach of plog. 
0007 Good for string time stamps, but not convenient for
0008 measuring durations. See schrono.h or stimer.h for that. 
0009 
0010 **/
0011 
0012 #include <sys/time.h>
0013 #include <string>
0014 #include <iomanip>
0015 #include <sstream>
0016 
0017 namespace s_time
0018 {
0019     inline int EpochSeconds()
0020     {
0021         time_t now = time(0);
0022         return now ; 
0023     }
0024 
0025     constexpr const char* FMT = "%Y%m%d_%H%M%S" ; 
0026  
0027     inline std::string Format(int epochseconds=0, const char* fmt=nullptr)
0028     {
0029         const char* ufmt = fmt == nullptr ? FMT : fmt ;  
0030 
0031         int t = epochseconds == 0 ? EpochSeconds() : epochseconds ; 
0032         time_t now(t) ;  
0033         struct tm  tstruct;
0034         char       buf[80];
0035         tstruct = *localtime(&now);
0036         strftime(buf, sizeof(buf), ufmt, &tstruct);
0037         return buf ;
0038     }
0039 
0040     inline std::string Stamp()
0041     {
0042         return Format(0, nullptr); 
0043     }
0044     inline std::string Now()
0045     {
0046         return Format(0, "%c"); 
0047     }
0048 
0049 
0050     inline void localtime_s(struct tm* t, const time_t* time)
0051     {
0052         ::localtime_r(time, t); 
0053     }
0054 
0055     struct Time
0056     {   
0057         time_t time;
0058         unsigned short millitm;
0059     };  
0060 
0061     inline void init(Time* stamp)  // ftime in plog
0062     {   
0063         timeval tv; 
0064         ::gettimeofday(&tv, NULL); // 2nd arg is obsolete timezone
0065 
0066         stamp->time = tv.tv_sec;
0067         stamp->millitm = static_cast<unsigned short>(tv.tv_usec / 1000);
0068     }   
0069 
0070     inline std::string Desc(Time* stamp)
0071     {
0072          tm t ; 
0073          localtime_s(&t, &stamp->time) ; 
0074 
0075          std::stringstream ss ; 
0076          ss 
0077             << t.tm_year + 1900  
0078             << "-"
0079             << std::setfill('0')
0080             << std::setw(2) << t.tm_mon + 1
0081             << "-"
0082             << std::setfill('0')
0083             << std::setw(2) << t.tm_mday 
0084             << " "
0085             << std::setfill('0')
0086             << std::setw(2) << t.tm_hour
0087             << ":"
0088             << std::setfill('0')
0089             << std::setw(2) << t.tm_min 
0090             << ":"
0091             << std::setfill('0')
0092             << std::setw(2) << t.tm_sec
0093             << "."
0094             << std::setfill('0')
0095             << std::setw(3) << static_cast<int> (stamp->millitm)
0096             << " "
0097             << std::endl 
0098             ; 
0099          std::string s = ss.str(); 
0100          return s ; 
0101      }
0102 }; 
0103 
0104 
0105