EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Time.hh
1 #ifndef EUDAQ_INCLUDED_Time
2 #define EUDAQ_INCLUDED_Time
3 
4 #include "eudaq/Platform.hh"
5 #include <errno.h>
6 #include <ostream>
7 #include <iomanip>
8 #include <string>
9 #include <cstring>
10 
11 #if ((defined WIN32) && (defined __CINT__))
12 typedef unsigned long long uint64_t typedef long long
13  int64_t typedef unsigned int uint32_t typedef int int32_t
14 #else
15 #include <cstdint>
16 #endif
17 
18 #ifdef WIN32
19 #ifndef __CINT__
20 #include <winsock.h>
21 #endif
22 #else
23 #include <sys/time.h>
24 #endif
25 
26 #define TIME_DEFAULT_FORMAT \
27  "%Y-%m-%d %H:%M:%S.%3" // I thing it is impossible to export static variables
28  // from a dll. so i made it to a macro this works fine.
29 
30  namespace eudaq {
31 
32  class DLLEXPORT Time {
33  public:
34  explicit Time(int32_t sec, int32_t usec = 0) {
35  tv_usec = usec % 1000000;
36  tv_sec = sec + usec / 1000000;
37  }
38  Time(int year, int month, int date, int hour = 0, int minute = 0,
39  int sec = 0, int usec = 0);
40  Time(timeval tv) {
41  tv_usec = tv.tv_usec % 1000000;
42  tv_sec = tv.tv_sec + tv.tv_usec / 1000000;
43  }
44  double Seconds() const { return tv_sec + tv_usec / 1e6; }
45  timeval GetTimeval() const {
46  timeval tv;
47  tv.tv_sec = tv_sec;
48  tv.tv_usec = tv_usec;
49  return tv;
50  }
51  Time &operator+=(const timeval &other) {
52  tv_usec += other.tv_usec;
53  tv_sec += other.tv_sec + tv_usec / 1000000;
54  tv_usec %= 1000000;
55  return *this;
56  }
57  Time &operator-=(const timeval &other) {
58  if (tv_usec < other.tv_usec) {
59  tv_usec += 1000000 - other.tv_usec;
60  tv_sec -= other.tv_sec + 1;
61  } else {
62  tv_usec -= other.tv_usec;
63  tv_sec -= other.tv_sec;
64  }
65  return *this;
66  }
67  bool operator<(const timeval &other) {
68  return tv_sec < other.tv_sec ||
69  (tv_sec == other.tv_sec && tv_usec < other.tv_usec);
70  }
71  bool operator>(const timeval &other) {
72  return tv_sec > other.tv_sec ||
73  (tv_sec == other.tv_sec && tv_usec > other.tv_usec);
74  }
75  operator const timeval() const { return GetTimeval(); }
76  std::string
77  Formatted(const std::string &format = TIME_DEFAULT_FORMAT) const;
78  static Time Current();
79 
80  private:
81  int32_t tv_sec;
82  int32_t tv_usec;
83  };
84 
85  inline Time operator+(const timeval &lhs, const timeval rhs) {
86  Time t(lhs);
87  t += rhs;
88  return t;
89  }
90 
91  inline Time operator-(const timeval &lhs, const timeval rhs) {
92  Time t(lhs);
93  t -= rhs;
94  return t;
95  }
96 
97  inline bool operator==(const timeval &lhs, const timeval rhs) {
98  return (lhs.tv_sec == rhs.tv_sec) && (lhs.tv_usec == rhs.tv_usec);
99  }
100 
101  inline std::ostream &operator<<(std::ostream & os, const timeval &tv) {
102  if (tv.tv_sec < 0) {
103  return os << '-' << Time(-tv.tv_sec - 1, 1000000 - tv.tv_usec);
104  }
105  return os << tv.tv_sec << '.' << std::setw(6) << std::setfill('0')
106  << tv.tv_usec << std::setfill(' ');
107  }
108 }
109 
110 #endif // EUDAQ_INCLUDED_Time
Definition: Time.hh:32