Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /**
0002 ssystime.h
0003 ===========
0004 
0005 Following time mechanics of plog
0006 
0007 The sstamp.h methods based on uint64_t states and std::chrono
0008 are more flexible than this old school approach, see::
0009 
0010     sstamp::FormatLog  (aka U::FormatLog from np)
0011     sstamp::Format     (aka U:Formatr from np)
0012 
0013 **/
0014 
0015 #include <string>
0016 #include <sstream>
0017 #include <iostream>
0018 #include <iomanip>
0019 
0020 #include <cstddef>
0021 #include <sys/time.h>
0022 
0023 struct ssystime
0024 {
0025     time_t time ;
0026     unsigned short millitm;
0027     // for ease of persisting the sstamp.h uint64_t approach is better
0028 
0029     ssystime();
0030     void now();
0031 
0032     template<bool UTC> std::string fmt() const ;
0033 
0034     static std::string utc();
0035     static std::string local();
0036 };
0037 
0038 inline ssystime::ssystime()
0039    :
0040    time(0),
0041    millitm(0)
0042 {
0043 }
0044 
0045 inline void ssystime::now()
0046 {
0047     timeval tv;
0048     ::gettimeofday(&tv, NULL);
0049 
0050     time = tv.tv_sec ;
0051     millitm = static_cast<unsigned short>(tv.tv_usec / 1000);
0052 }
0053 
0054 template<bool UTC=false>
0055 inline std::string ssystime::fmt() const
0056 {
0057     tm t;
0058     if(UTC) ::gmtime_r(&time, &t);
0059     else    ::localtime_r(&time, &t);
0060 
0061     std::stringstream ss ;
0062     ss
0063        << t.tm_year + 1900 << "-"
0064        << std::setfill('0') << std::setw(2) << t.tm_mon + 1 << "-"
0065        << std::setfill('0') << std::setw(2) << t.tm_mday    << " "
0066        << std::setfill('0') << std::setw(2) << t.tm_hour    << ":"
0067        << std::setfill('0') << std::setw(2) << t.tm_min     << ":"
0068        << std::setfill('0') << std::setw(2) << t.tm_sec     << "."
0069        << std::setfill('0') << std::setw(3) << static_cast<int> (millitm)
0070        << " "
0071        ;
0072 
0073     std::string str = ss.str() ;
0074     return str ;
0075 }
0076 
0077 inline std::string ssystime::utc()
0078 {
0079     ssystime t ;
0080     t.now();
0081     return t.fmt<true>() ;
0082 }
0083 
0084 inline std::string ssystime::local()
0085 {
0086     ssystime t ;
0087     t.now();
0088     return t.fmt<false>() ;
0089 }
0090 
0091 inline std::ostream& operator<<(std::ostream& os, const ssystime& t)
0092 {
0093     os << t.fmt<false>() ;
0094     return os;
0095 }
0096