PixelTimeFormatter

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*************************************************************************
 * XDAQ Components for Pixel Online Software                             *
 * Authors: Dario Menasce, Marco Rovere                                  *
 ************************************************************************/

#ifndef PixelTimeFormatter_h
#define PixelTimeFormatter_h
/**
* \file CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h
* \brief This class provides utility methods to manipulate ASCII formatted timestamps
*
*   A longer explanation will be placed here later
*/

#include "CalibFormats/SiPixelObjects/interface/PixelConfigKey.h"

#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <sys/time.h>
#include <cstdlib>

#define USE_TIMER_ 0

namespace pos {
  class PixelTimeFormatter {
  public:
    //---------------------------------------------------------------------------------
    PixelTimeFormatter(std::string source) {
      if (!USE_TIMER_)
        return;
      origin_ = source;
      std::cout << "[PixelTimeFormatter::PixelTimeFormatter()]\t\t    Time counter started for " << origin_
                << std::endl;
      startTime_ = getImSecTime();
    }

    virtual ~PixelTimeFormatter() = default;

    void stopTimer(void) {
      if (!USE_TIMER_)
        return;
      endTime_ = getImSecTime();
      double start = startTime_.tv_sec + startTime_.tv_usec / 1000000.;
      double stop = endTime_.tv_sec + endTime_.tv_usec / 1000000.;
      std::cout << "[PixelTimeFormatter::stopTimer()]\t\t\t    Elapsed time: " << stop - start << " seconds for "
                << origin_ << std::endl;
    }

    virtual void writeXMLHeader(pos::PixelConfigKey key,
                                int version,
                                std::string path,
                                std::ofstream *out,
                                std::ofstream *out1 = nullptr,
                                std::ofstream *out2 = nullptr) const {
      ;
    }

    //---------------------------------------------------------------------------------
    static std::string getTime(void) {
      constexpr size_t kBufferLength = 72;
      char theDate[kBufferLength];
      struct tm *thisTime;
      time_t aclock;
      std::string date;
      time(&aclock);
      thisTime = localtime(&aclock);

      snprintf(theDate,
               kBufferLength,
               "%d-%02d-%02d %02d:%02d:%02d",
               thisTime->tm_year + 1900,
               thisTime->tm_mon + 1,
               thisTime->tm_mday,
               thisTime->tm_hour,
               thisTime->tm_min,
               thisTime->tm_sec);
      date = theDate;
      //std::cout << "[PixelTimeFormatter::getTime()]\t\t\t\t    Time: " << date << std::endl ;
      return date;
    }

    //---------------------------------------------------------------------------------
    struct tm *getITime(void) {
      struct tm *thisTime;
      time_t aclock;
      time(&aclock);
      thisTime = localtime(&aclock);
      return thisTime;
    }

    //---------------------------------------------------------------------------------
    static std::string getmSecTime(void) {
      constexpr size_t kBufferSize = 20;
      char theDate[kBufferSize];
      struct timeval msecTime;
      gettimeofday(&msecTime, (struct timezone *)nullptr);

      snprintf(theDate, kBufferSize, "%d-%d", (unsigned int)msecTime.tv_sec, (unsigned int)msecTime.tv_usec);
      return std::string(theDate);
    }

    //---------------------------------------------------------------------------------
    struct timeval getImSecTime(void) {
      struct timeval msecTime;
      gettimeofday(&msecTime, (struct timezone *)nullptr);

      return msecTime;
    }
    /*    
    //---------------------------------------------------------------------------------
    static double timeDiff(std::string firstTime, std::string secondTime)
    {
      time_t rawTime;
      struct tm * rawTimeInfo;

      int firstMonth  = atoi( firstTime.substr(0,2).c_str()) ;
      int firstDay    = atoi( firstTime.substr(3,2).c_str()) ;
      int firstYear   = atoi( firstTime.substr(6,4).c_str()) ;
      int secondMonth = atoi(secondTime.substr(0,2).c_str()) ;
      int secondDay   = atoi(secondTime.substr(3,2).c_str()) ;
      int secondYear  = atoi(secondTime.substr(6,4).c_str()) ;
  
      time(&rawTime);
      rawTimeInfo = localtime(&rawTime);
      rawTimeInfo->tm_mon  = firstMonth - 1    ;
      rawTimeInfo->tm_mday = firstDay	       ;
      rawTimeInfo->tm_year = firstYear  - 1900 ;

      time_t ft = mktime( rawTimeInfo ) ;

      rawTimeInfo = localtime(&rawTime);
      rawTimeInfo->tm_mon  = secondMonth - 1	;
      rawTimeInfo->tm_mday = secondDay  	;
      rawTimeInfo->tm_year = secondYear  - 1900 ;

      time_t st = mktime( rawTimeInfo ) ;
  
      return difftime(ft, st) ;
    }
*/
    //=================================================================================

  private:
    struct timeval startTime_;
    struct timeval endTime_;
    std::string origin_;
    bool verbose_;
  };
}  // namespace pos

#endif