CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TimeOfDay.cc
Go to the documentation of this file.
1 #include <ctime>
2 #include <iomanip>
3 #include <locale>
4 #include <ostream>
5 
7 
8 namespace {
9  int const power[] = {1000 * 1000, 100 * 1000, 10 * 1000, 1000, 100, 10, 1};
10 }
11 
12 namespace edm {
13 
14  TimeOfDay::TimeOfDay() : tv_(TimeOfDay::setTime_()) {}
15 
16  TimeOfDay::TimeOfDay(struct timeval const& tv) : tv_(tv) {}
17 
18  TimeOfDay::TimeOfDay(std::chrono::system_clock::time_point const& tp) {
19  auto us = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();
20  tv_.tv_sec = us / 1000000;
21  tv_.tv_usec = us % 1000000;
22  }
23 
24  timeval TimeOfDay::setTime_() {
25  timeval tv;
26  gettimeofday(&tv, nullptr);
27  return tv;
28  }
29 
30  std::ostream& operator<<(std::ostream& os, TimeOfDay const& tod) {
31  auto oldflags = os.flags(); // save the stream format flags so they can be restored
32  auto oldfill = os.fill(); // save the stream fill character so it can be restored
33  struct tm timebuf;
34  localtime_r(&tod.tv_.tv_sec, &timebuf);
35  typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter;
36  std::time_put<char, Iter> const& tp = std::use_facet<std::time_put<char, Iter> >(std::locale());
37  int precision = os.precision();
38  Iter begin(os);
39  if (precision == 0) {
40  char const pattern[] = "%d-%b-%Y %H:%M:%S %Z";
41  tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
42  } else {
43  char const pattern[] = "%d-%b-%Y %H:%M:%S.";
44  tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
45  precision = std::min(precision, 6);
46  os << std::setfill('0') << std::setw(precision) << tod.tv_.tv_usec / power[precision] << ' ';
47  tp.put(begin, os, ' ', &timebuf, 'Z');
48  }
49  os.flags(oldflags);
50  os.fill(oldfill);
51  return os;
52  }
53 
54 } // namespace edm
static struct timeval setTime_()
Definition: TimeOfDay.cc:24
T min(T a, T b)
Definition: MathUtil.h:58
struct timeval tv_
Definition: TimeOfDay.h:14
std::ostream & operator<<(std::ostream &ost, const HLTGlobalStatus &hlt)
Formatted printout of trigger tbale.