otsdaq  v1_01_02
 All Classes Namespaces Functions
TimeFormatter.cc
1 #include "otsdaq-core/ConfigurationInterface/TimeFormatter.h"
2 
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <cstdlib>
11 #include <cstdio>
12 
13 #define USE_TIMER 0
14 
15 using namespace ots;
16 
17 //==============================================================================
18 TimeFormatter::TimeFormatter(std::string source )
19 {
20  if( !USE_TIMER)
21  return ;
22  origin_ = source ;
23  std::cout << __COUT_HDR_FL__ << "[TimeFormatter::TimeFormatter()]\t\t Time counter started for " << origin_ << std::endl << std::endl;
24  startTime_ = getImSecTime() ;
25 }
26 
27 //==============================================================================
28 void TimeFormatter::stopTimer(void)
29 {
30  if( !USE_TIMER )
31  return ;
32  endTime_ = getImSecTime() ;
33  double start = startTime_.tv_sec + startTime_.tv_usec/1000000. ;
34  double stop = endTime_.tv_sec + endTime_.tv_usec/1000000. ;
35  std::cout << __COUT_HDR_FL__ << "[TimeFormatter::stopTimer()]\t\t\t Elapsed time: " << stop-start << " seconds for " << origin_ << std::endl << std::endl;
36 }
37 
38 //==============================================================================
39 std::string TimeFormatter::getTime(void)
40 {
41  char theDate[20] ;
42  struct tm *thisTime;
43  time_t aclock;
44  std::string date ;
45  time( &aclock );
46  thisTime = localtime( &aclock );
47 
48  sprintf(theDate,
49  "%d-%02d-%02d %02d:%02d:%02d", thisTime->tm_year+1900,
50  thisTime->tm_mon+1,
51  thisTime->tm_mday,
52  thisTime->tm_hour,
53  thisTime->tm_min,
54  thisTime->tm_sec );
55  date = theDate ;
56  //std::cout << __COUT_HDR_FL__ << "[TimeFormatter::getTime()]\t\t\t\t Time: " << date << std::endl << std::endl;
57  return date ;
58 }
59 
60 //==============================================================================
61 struct tm * TimeFormatter::getITime(void)
62 {
63  struct tm *thisTime;
64  time_t aclock;
65  time( &aclock );
66  thisTime = localtime( &aclock );
67  return thisTime ;
68 }
69 
70 //==============================================================================
71 std::string getmSecTime(void)
72 {
73  char theDate[20] ;
74  struct timeval msecTime;
75  gettimeofday(&msecTime, (struct timezone *)0) ;
76 
77  sprintf(theDate,
78  "%d-%d",
79  (unsigned int)msecTime.tv_sec,
80  (unsigned int)msecTime.tv_usec );
81  return std::string(theDate) ;
82 }
83 
84 //==============================================================================
85 struct timeval TimeFormatter::getImSecTime(void)
86 {
87  struct timeval msecTime;
88  gettimeofday(&msecTime, (struct timezone *)0) ;
89 
90  return msecTime ;
91 }