tdaq-develop-2025-02-12
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__
24  << "[TimeFormatter::TimeFormatter()]\t\t Time counter started for "
25  << origin_ << std::endl
26  << std::endl;
27  startTime_ = getImSecTime();
28 }
29 
30 //==============================================================================
31 void TimeFormatter::stopTimer(void)
32 {
33  if(!USE_TIMER)
34  return;
35  endTime_ = getImSecTime();
36  double start = startTime_.tv_sec + startTime_.tv_usec / 1000000.;
37  double stop = endTime_.tv_sec + endTime_.tv_usec / 1000000.;
38  std::cout << __COUT_HDR_FL__
39  << "[TimeFormatter::stopTimer()]\t\t\t Elapsed time: " << stop - start
40  << " seconds for " << origin_ << std::endl
41  << std::endl;
42 }
43 
44 //==============================================================================
45 std::string TimeFormatter::getTime(void)
46 {
47  char theDate[72];
48  struct tm* thisTime;
49  time_t aclock;
50  std::string date;
51  time(&aclock);
52  thisTime = localtime(&aclock);
53 
54  sprintf(theDate,
55  "%d-%02d-%02d %02d:%02d:%02d",
56  thisTime->tm_year + 1900,
57  thisTime->tm_mon + 1,
58  thisTime->tm_mday,
59  thisTime->tm_hour,
60  thisTime->tm_min,
61  thisTime->tm_sec);
62  date = theDate;
63  // std::cout << __COUT_HDR_FL__ << "[TimeFormatter::getTime()]\t\t\t\t Time: " <<
64  // date << std::endl << std::endl;
65  return date;
66 }
67 
68 //==============================================================================
69 struct tm* TimeFormatter::getITime(void)
70 {
71  struct tm* thisTime;
72  time_t aclock;
73  time(&aclock);
74  thisTime = localtime(&aclock);
75  return thisTime;
76 }
77 
78 //==============================================================================
79 std::string getmSecTime(void)
80 {
81  char theDate[20];
82  struct timeval msecTime;
83  gettimeofday(&msecTime, (struct timezone*)0);
84 
85  sprintf(
86  theDate, "%d-%d", (unsigned int)msecTime.tv_sec, (unsigned int)msecTime.tv_usec);
87  return std::string(theDate);
88 }
89 
90 //==============================================================================
91 struct timeval TimeFormatter::getImSecTime(void)
92 {
93  struct timeval msecTime;
94  gettimeofday(&msecTime, (struct timezone*)0);
95 
96  return msecTime;
97 }
static std::string getTime(void)
Static memebers.