1 #ifndef EUDAQ_INCLUDED_Time
2 #define EUDAQ_INCLUDED_Time
4 #include "eudaq/Platform.hh"
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
26 #define TIME_DEFAULT_FORMAT \
27 "%Y-%m-%d %H:%M:%S.%3" // I thing it is impossible to export static variables
34 explicit Time(int32_t sec, int32_t usec = 0) {
35 tv_usec = usec % 1000000;
36 tv_sec = sec + usec / 1000000;
38 Time(
int year,
int month,
int date,
int hour = 0,
int minute = 0,
39 int sec = 0,
int usec = 0);
41 tv_usec = tv.tv_usec % 1000000;
42 tv_sec = tv.tv_sec + tv.tv_usec / 1000000;
44 double Seconds()
const {
return tv_sec + tv_usec / 1e6; }
45 timeval GetTimeval()
const {
51 Time &operator+=(
const timeval &other) {
52 tv_usec += other.tv_usec;
53 tv_sec += other.tv_sec + tv_usec / 1000000;
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;
62 tv_usec -= other.tv_usec;
63 tv_sec -= other.tv_sec;
67 bool operator<(
const timeval &other) {
68 return tv_sec < other.tv_sec ||
69 (tv_sec == other.tv_sec && tv_usec < other.tv_usec);
71 bool operator>(
const timeval &other) {
72 return tv_sec > other.tv_sec ||
73 (tv_sec == other.tv_sec && tv_usec > other.tv_usec);
75 operator const timeval()
const {
return GetTimeval(); }
77 Formatted(
const std::string &format = TIME_DEFAULT_FORMAT)
const;
78 static Time Current();
85 inline Time operator+(
const timeval &lhs,
const timeval rhs) {
91 inline Time operator-(
const timeval &lhs,
const timeval rhs) {
97 inline bool operator==(
const timeval &lhs,
const timeval rhs) {
98 return (lhs.tv_sec == rhs.tv_sec) && (lhs.tv_usec == rhs.tv_usec);
101 inline std::ostream &operator<<(std::ostream & os,
const timeval &tv) {
103 return os <<
'-' << Time(-tv.tv_sec - 1, 1000000 - tv.tv_usec);
105 return os << tv.tv_sec <<
'.' << std::setw(6) << std::setfill(
'0')
106 << tv.tv_usec << std::setfill(
' ');
110 #endif // EUDAQ_INCLUDED_Time