Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:23

0001 /*************************************************************************
0002  * XDAQ Components for Pixel Online Software                             *
0003  * Authors: Dario Menasce, Marco Rovere                                  *
0004  ************************************************************************/
0005 
0006 #ifndef PixelTimeFormatter_h
0007 #define PixelTimeFormatter_h
0008 /**
0009 * \file CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h
0010 * \brief This class provides utility methods to manipulate ASCII formatted timestamps
0011 *
0012 *   A longer explanation will be placed here later
0013 */
0014 
0015 #include "CalibFormats/SiPixelObjects/interface/PixelConfigKey.h"
0016 
0017 #include <iostream>
0018 #include <sstream>
0019 #include <string>
0020 #include <ctime>
0021 #include <sys/time.h>
0022 #include <cstdlib>
0023 
0024 #define USE_TIMER_ 0
0025 
0026 namespace pos {
0027   class PixelTimeFormatter {
0028   public:
0029     //---------------------------------------------------------------------------------
0030     PixelTimeFormatter(std::string source) {
0031       if (!USE_TIMER_)
0032         return;
0033       origin_ = source;
0034       std::cout << "[PixelTimeFormatter::PixelTimeFormatter()]\t\t    Time counter started for " << origin_
0035                 << std::endl;
0036       startTime_ = getImSecTime();
0037     }
0038 
0039     virtual ~PixelTimeFormatter() = default;
0040 
0041     void stopTimer(void) {
0042       if (!USE_TIMER_)
0043         return;
0044       endTime_ = getImSecTime();
0045       double start = startTime_.tv_sec + startTime_.tv_usec / 1000000.;
0046       double stop = endTime_.tv_sec + endTime_.tv_usec / 1000000.;
0047       std::cout << "[PixelTimeFormatter::stopTimer()]\t\t\t    Elapsed time: " << stop - start << " seconds for "
0048                 << origin_ << std::endl;
0049     }
0050 
0051     virtual void writeXMLHeader(pos::PixelConfigKey key,
0052                                 int version,
0053                                 std::string path,
0054                                 std::ofstream *out,
0055                                 std::ofstream *out1 = nullptr,
0056                                 std::ofstream *out2 = nullptr) const {
0057       ;
0058     }
0059 
0060     //---------------------------------------------------------------------------------
0061     static std::string getTime(void) {
0062       constexpr size_t kBufferLength = 72;
0063       char theDate[kBufferLength];
0064       struct tm *thisTime;
0065       time_t aclock;
0066       std::string date;
0067       time(&aclock);
0068       thisTime = localtime(&aclock);
0069 
0070       snprintf(theDate,
0071                kBufferLength,
0072                "%d-%02d-%02d %02d:%02d:%02d",
0073                thisTime->tm_year + 1900,
0074                thisTime->tm_mon + 1,
0075                thisTime->tm_mday,
0076                thisTime->tm_hour,
0077                thisTime->tm_min,
0078                thisTime->tm_sec);
0079       date = theDate;
0080       //std::cout << "[PixelTimeFormatter::getTime()]\t\t\t\t    Time: " << date << std::endl ;
0081       return date;
0082     }
0083 
0084     //---------------------------------------------------------------------------------
0085     struct tm *getITime(void) {
0086       struct tm *thisTime;
0087       time_t aclock;
0088       time(&aclock);
0089       thisTime = localtime(&aclock);
0090       return thisTime;
0091     }
0092 
0093     //---------------------------------------------------------------------------------
0094     static std::string getmSecTime(void) {
0095       constexpr size_t kBufferSize = 20;
0096       char theDate[kBufferSize];
0097       struct timeval msecTime;
0098       gettimeofday(&msecTime, (struct timezone *)nullptr);
0099 
0100       snprintf(theDate, kBufferSize, "%d-%d", (unsigned int)msecTime.tv_sec, (unsigned int)msecTime.tv_usec);
0101       return std::string(theDate);
0102     }
0103 
0104     //---------------------------------------------------------------------------------
0105     struct timeval getImSecTime(void) {
0106       struct timeval msecTime;
0107       gettimeofday(&msecTime, (struct timezone *)nullptr);
0108 
0109       return msecTime;
0110     }
0111     /*    
0112     //---------------------------------------------------------------------------------
0113     static double timeDiff(std::string firstTime, std::string secondTime)
0114     {
0115       time_t rawTime;
0116       struct tm * rawTimeInfo;
0117 
0118       int firstMonth  = atoi( firstTime.substr(0,2).c_str()) ;
0119       int firstDay    = atoi( firstTime.substr(3,2).c_str()) ;
0120       int firstYear   = atoi( firstTime.substr(6,4).c_str()) ;
0121       int secondMonth = atoi(secondTime.substr(0,2).c_str()) ;
0122       int secondDay   = atoi(secondTime.substr(3,2).c_str()) ;
0123       int secondYear  = atoi(secondTime.substr(6,4).c_str()) ;
0124   
0125       time(&rawTime);
0126       rawTimeInfo = localtime(&rawTime);
0127       rawTimeInfo->tm_mon  = firstMonth - 1    ;
0128       rawTimeInfo->tm_mday = firstDay          ;
0129       rawTimeInfo->tm_year = firstYear  - 1900 ;
0130 
0131       time_t ft = mktime( rawTimeInfo ) ;
0132 
0133       rawTimeInfo = localtime(&rawTime);
0134       rawTimeInfo->tm_mon  = secondMonth - 1    ;
0135       rawTimeInfo->tm_mday = secondDay      ;
0136       rawTimeInfo->tm_year = secondYear  - 1900 ;
0137 
0138       time_t st = mktime( rawTimeInfo ) ;
0139   
0140       return difftime(ft, st) ;
0141     }
0142 */
0143     //=================================================================================
0144 
0145   private:
0146     struct timeval startTime_;
0147     struct timeval endTime_;
0148     std::string origin_;
0149     bool verbose_;
0150   };
0151 }  // namespace pos
0152 
0153 #endif