CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h

Go to the documentation of this file.
00001 /*************************************************************************
00002  * XDAQ Components for Pixel Online Software                             *
00003  * Copyright (C) 2007, I.N.F.N. Milano Bicocca                           *
00004  * All rights reserved.                                                  *
00005  * Authors: Dario Menasce, Marco Rovere                                  *
00006  ************************************************************************/
00007 
00008 #ifndef PixelTimeFormatter_h
00009 #define PixelTimeFormatter_h
00010 
00017 #include <iostream>
00018 #include <sstream>
00019 #include <string>
00020 #include <time.h>
00021 #include <sys/time.h>
00022 #include <cstdlib>
00023 
00024 #define USE_TIMER_ 0
00025 
00026 namespace pos{
00027   class PixelTimeFormatter
00028   {
00029   public:
00030 
00031     //---------------------------------------------------------------------------------
00032     PixelTimeFormatter(std::string source ) 
00033     {
00034      if( !USE_TIMER_) return ;
00035      origin_ = source ;
00036      std::cout << "[PixelTimeFormatter::PixelTimeFormatter()]\t\t    Time counter started for " << origin_ << std::endl ;
00037      startTime_ = getImSecTime() ;
00038     }
00039     
00040     void stopTimer(void) 
00041     {
00042      if( !USE_TIMER_ ) return ;
00043      endTime_ = getImSecTime() ;
00044      double start = startTime_.tv_sec + startTime_.tv_usec/1000000. ;  
00045      double stop  = endTime_.tv_sec   + endTime_.tv_usec/1000000. ;  
00046      std::cout << "[PixelTimeFormatter::stopTimer()]\t\t\t    Elapsed time: " << stop-start << " seconds for " << origin_ << std::endl ;
00047     }
00048 
00049     //---------------------------------------------------------------------------------
00050     static std::string getTime(void) 
00051     {
00052       char theDate[20] ;
00053       struct tm *thisTime;
00054       time_t aclock;
00055       std::string date ;
00056       time( &aclock );            
00057       thisTime = localtime( &aclock ); 
00058        
00059       sprintf(theDate,
00060               "%d-%02d-%02d %02d:%02d:%02d", thisTime->tm_year+1900,
00061               thisTime->tm_mon+1,
00062               thisTime->tm_mday,
00063               thisTime->tm_hour,
00064               thisTime->tm_min,
00065               thisTime->tm_sec ); 
00066       date = theDate ;
00067       //std::cout << "[PixelTimeFormatter::getTime()]\t\t\t\t    Time: " << date << std::endl ;                                   
00068       return date ;
00069     }
00070 
00071     //---------------------------------------------------------------------------------
00072     struct tm * getITime(void) 
00073     {
00074       struct tm *thisTime;
00075       time_t aclock;
00076       time( &aclock );            
00077       thisTime = localtime( &aclock ); 
00078       return thisTime ;
00079     }
00080 
00081     //---------------------------------------------------------------------------------
00082     static std::string getmSecTime(void) 
00083     {
00084       char theDate[20] ;
00085       struct timeval msecTime;
00086       gettimeofday(&msecTime, (struct timezone *)0) ;
00087       
00088       sprintf(theDate,
00089               "%d-%d", 
00090               (unsigned int)msecTime.tv_sec,
00091               (unsigned int)msecTime.tv_usec ); 
00092       return std::string(theDate) ;
00093     }
00094 
00095     //---------------------------------------------------------------------------------
00096     struct timeval getImSecTime(void) 
00097     {
00098       struct timeval msecTime;
00099       gettimeofday(&msecTime, (struct timezone *)0) ;
00100       
00101       return msecTime ;
00102     }
00103 /*    
00104     //---------------------------------------------------------------------------------
00105     static double timeDiff(std::string firstTime, std::string secondTime)
00106     {
00107       time_t rawTime;
00108       struct tm * rawTimeInfo;
00109 
00110       int firstMonth  = atoi( firstTime.substr(0,2).c_str()) ;
00111       int firstDay    = atoi( firstTime.substr(3,2).c_str()) ;
00112       int firstYear   = atoi( firstTime.substr(6,4).c_str()) ;
00113       int secondMonth = atoi(secondTime.substr(0,2).c_str()) ;
00114       int secondDay   = atoi(secondTime.substr(3,2).c_str()) ;
00115       int secondYear  = atoi(secondTime.substr(6,4).c_str()) ;
00116   
00117       time(&rawTime);
00118       rawTimeInfo = localtime(&rawTime);
00119       rawTimeInfo->tm_mon  = firstMonth - 1    ;
00120       rawTimeInfo->tm_mday = firstDay          ;
00121       rawTimeInfo->tm_year = firstYear  - 1900 ;
00122 
00123       time_t ft = mktime( rawTimeInfo ) ;
00124 
00125       rawTimeInfo = localtime(&rawTime);
00126       rawTimeInfo->tm_mon  = secondMonth - 1    ;
00127       rawTimeInfo->tm_mday = secondDay          ;
00128       rawTimeInfo->tm_year = secondYear  - 1900 ;
00129 
00130       time_t st = mktime( rawTimeInfo ) ;
00131   
00132       return difftime(ft, st) ;
00133     }
00134 */    
00135     //=================================================================================
00136     
00137     private:
00138     
00139      struct timeval startTime_      ;
00140      struct timeval endTime_        ;
00141      std::string    origin_         ;
00142      bool           verbose_        ;
00143   } ;
00144 }
00145 
00146 #endif