otsdaq  v2_05_02_indev
TimeFormatter.cc
1 #include "otsdaq/ConfigurationInterface/TimeFormatter.h"
2 
3 #include <sys/time.h>
4 #include <time.h>
5 #include <cstdio>
6 #include <cstdlib>
7 #include <iostream>
8 #include <sstream>
9 #include <string>
10 #include "otsdaq/Macros/CoutMacros.h"
11 #include "otsdaq/MessageFacility/MessageFacility.h"
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
36  << std::endl;
37 }
38 
39 //==============================================================================
40 std::string TimeFormatter::getTime(void)
41 {
42  char theDate[72];
43  struct tm* thisTime;
44  time_t aclock;
45  std::string date;
46  time(&aclock);
47  thisTime = localtime(&aclock);
48 
49  sprintf(theDate,
50  "%d-%02d-%02d %02d:%02d:%02d",
51  thisTime->tm_year + 1900,
52  thisTime->tm_mon + 1,
53  thisTime->tm_mday,
54  thisTime->tm_hour,
55  thisTime->tm_min,
56  thisTime->tm_sec);
57  date = theDate;
58  // std::cout << __COUT_HDR_FL__ << "[TimeFormatter::getTime()]\t\t\t\t Time: " <<
59  // date << std::endl << std::endl;
60  return date;
61 }
62 
63 //==============================================================================
64 struct tm* TimeFormatter::getITime(void)
65 {
66  struct tm* thisTime;
67  time_t aclock;
68  time(&aclock);
69  thisTime = localtime(&aclock);
70  return thisTime;
71 }
72 
73 //==============================================================================
74 std::string getmSecTime(void)
75 {
76  char theDate[20];
77  struct timeval msecTime;
78  gettimeofday(&msecTime, (struct timezone*)0);
79 
80  sprintf(theDate, "%d-%d", (unsigned int)msecTime.tv_sec, (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 }