00001 #include "otsdaq-core/ConfigurationInterface/TimeFormatter.h"
00002
00003 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00004 #include "otsdaq-core/Macros/CoutMacros.h"
00005 #include <iostream>
00006 #include <sstream>
00007 #include <string>
00008 #include <time.h>
00009 #include <sys/time.h>
00010 #include <cstdlib>
00011 #include <cstdio>
00012
00013 #define USE_TIMER 0
00014
00015 using namespace ots;
00016
00017
00018 TimeFormatter::TimeFormatter(std::string source )
00019 {
00020 if( !USE_TIMER)
00021 return ;
00022 origin_ = source ;
00023 std::cout << __COUT_HDR_FL__ << "[TimeFormatter::TimeFormatter()]\t\t Time counter started for " << origin_ << std::endl << std::endl;
00024 startTime_ = getImSecTime() ;
00025 }
00026
00027
00028 void TimeFormatter::stopTimer(void)
00029 {
00030 if( !USE_TIMER )
00031 return ;
00032 endTime_ = getImSecTime() ;
00033 double start = startTime_.tv_sec + startTime_.tv_usec/1000000. ;
00034 double stop = endTime_.tv_sec + endTime_.tv_usec/1000000. ;
00035 std::cout << __COUT_HDR_FL__ << "[TimeFormatter::stopTimer()]\t\t\t Elapsed time: " << stop-start << " seconds for " << origin_ << std::endl << std::endl;
00036 }
00037
00038
00039 std::string TimeFormatter::getTime(void)
00040 {
00041 char theDate[20] ;
00042 struct tm *thisTime;
00043 time_t aclock;
00044 std::string date ;
00045 time( &aclock );
00046 thisTime = localtime( &aclock );
00047
00048 sprintf(theDate,
00049 "%d-%02d-%02d %02d:%02d:%02d", thisTime->tm_year+1900,
00050 thisTime->tm_mon+1,
00051 thisTime->tm_mday,
00052 thisTime->tm_hour,
00053 thisTime->tm_min,
00054 thisTime->tm_sec );
00055 date = theDate ;
00056
00057 return date ;
00058 }
00059
00060
00061 struct tm * TimeFormatter::getITime(void)
00062 {
00063 struct tm *thisTime;
00064 time_t aclock;
00065 time( &aclock );
00066 thisTime = localtime( &aclock );
00067 return thisTime ;
00068 }
00069
00070
00071 std::string getmSecTime(void)
00072 {
00073 char theDate[20] ;
00074 struct timeval msecTime;
00075 gettimeofday(&msecTime, (struct timezone *)0) ;
00076
00077 sprintf(theDate,
00078 "%d-%d",
00079 (unsigned int)msecTime.tv_sec,
00080 (unsigned int)msecTime.tv_usec );
00081 return std::string(theDate) ;
00082 }
00083
00084
00085 struct timeval TimeFormatter::getImSecTime(void)
00086 {
00087 struct timeval msecTime;
00088 gettimeofday(&msecTime, (struct timezone *)0) ;
00089
00090 return msecTime ;
00091 }