Belle II Software  release-05-02-19
Time.cc
1 #include "daq/slc/system/Time.h"
2 
3 #include "daq/slc/base/Reader.h"
4 #include "daq/slc/base/Writer.h"
5 
6 #include <sys/time.h>
7 
8 #include <cmath>
9 #include <cstdlib>
10 #include <sstream>
11 
12 using namespace Belle2;
13 
14 #define MEGA 1000000
15 
16 Time::Time()
17 {
18  timeval tv;
19  gettimeofday(&tv, 0);
20  m_s = tv.tv_sec;
21  m_us = tv.tv_usec;
22 }
23 
24 Time::Time(const double t)
25 {
26  set(t);
27 }
28 
29 Time::Time(const long s, const long us)
30  : m_s(s), m_us(us)
31 {
32  adjust();
33 }
34 
35 Time::~Time()
36 {
37 }
38 
39 void Time::adjust()
40 {
41  if (MEGA <= labs(m_us)) {
42  m_s += m_us / MEGA;
43  m_us = m_us - (m_us / MEGA) * MEGA;
44  }
45  if (0 < m_s && m_us < 0) {
46  m_s--;
47  m_us += MEGA;
48  }
49  if (m_s < 0 && 0 < m_us) {
50  m_s++;
51  m_us -= MEGA;
52  }
53 }
54 
55 void Time::clear()
56 {
57  m_s = m_us = 0;
58 }
59 
60 void Time::set()
61 {
62  timeval tv;
63  gettimeofday(&tv, 0);
64  m_s = tv.tv_sec;
65  m_us = tv.tv_usec;
66 }
67 
68 void Time::set(const double t)
69 {
70  double s, us;
71  us = modf(t, &s);
72  m_s = (long)s;
73  m_us = (long)(us * 1000000);
74  adjust();
75 }
76 
77 void Time::set(const long s, const long us)
78 {
79  m_s = s;
80  m_us = us;
81  adjust();
82 }
83 
84 double Time::get() const
85 {
86  return (double)m_s + ((double)m_us) / 1000000.;
87 }
88 
89 long Time::getSecond() const
90 {
91  return m_s;
92 }
93 
94 long Time::getMicroSecond() const
95 {
96  return m_us;
97 }
98 
99 std::string Time::toString() const
100 {
101  std::stringstream ss;
102  ss << m_s << "." << m_us;
103  return ss.str();
104 }
105 
106 void Time::readObject(Reader& r)
107 {
108  m_s = r.readLong();
109  m_us = r.readLong();
110 }
111 
112 void Time::writeObject(Writer& w) const
113 {
114  w.writeLong(m_s);
115  w.writeLong(m_us);
116 }
117 
118 Time& Time::operator = (const Time& t)
119 {
120  m_s = t.m_s;
121  m_us = t.m_us;
122  return *this;
123 }
124 
125 bool Time::operator == (const Time& t) const
126 {
127  return (m_s == t.m_s) && (m_us == t.m_us);
128 }
129 
130 Time Time::operator + (const Time& t) const
131 {
132  Time tt = *this;
133  tt.m_s += t.m_s;
134  tt.m_us += t.m_us;
135  return tt;
136 }
137 
138 Time Time::operator - (const Time& t) const
139 {
140  Time tt = *this;
141  tt.m_s -= t.m_s;
142  tt.m_us -= t.m_us;
143  return tt;
144 }
145 
Belle2::Reader
Definition: Reader.h:15
Belle2::operator==
bool operator==(const DecayNode &node1, const DecayNode &node2)
Compare two Decay Nodes: They are equal if All daughter decay nodes are equal or one of the daughter ...
Definition: DecayNode.cc:50
Belle2::Writer
Definition: Writer.h:15
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::operator-
B2Vector3< DataType > operator-(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for substracting a TVector3 from a B2Vector3
Definition: B2Vector3.h:542
Belle2::operator+
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition: B2Vector3.h:535
Belle2::Time
Definition: Time.h:14