Choreonoid  1.8
Timeval.h
Go to the documentation of this file.
1 
2 #ifndef CNOID_UTIL_TIMEVAL_H
3 #define CNOID_UTIL_TIMEVAL_H
4 
5 namespace cnoid {
6 
12 class Timeval
13 {
14  long sec;
15  long usec;
16 
17  void fixup() {
18  if (usec < 0) {
19  usec += 1000000;
20  --sec;
21  }
22  }
23 
24 public:
25 
26  Timeval() {
27  sec = 0;
28  usec = 0;
29  }
30 
31  explicit Timeval(long sec, long usec) {
32  sec = sec;
33  usec = usec;
34  if (usec < 0 || usec >= 1000000) {
35  sec += usec / 1000000;
36  usec %= 1000000;
37  fixup();
38  }
39  }
40 
41  explicit Timeval(long long usec)
42  {
43  usec = static_cast<long>(usec % 1000000);
44  sec = static_cast<long>(usec / 1000000);
45  fixup();
46  }
47 
48  // explicit
49  operator long long() const { return static_cast<long long>(sec) * 1000000 + usec; }
50 
52  sec += rhs.sec;
53  usec += rhs.usec;
54  if (usec >= 1000000) {
55  usec -= 1000000;
56  ++sec;
57  }
58  return *this;
59  }
60 
62  sec -= rhs.sec;
63  usec -= rhs.usec;
64  fixup();
65  return *this;
66  }
67 
69  long long t = sec * 1000000 + usec;
70  long long r = rhs.sec * 1000000 + rhs.usec;
71  t %= r;
72  sec = static_cast<long>(t / 1000000);
73  usec = static_cast<long>(t % 1000000);
74  fixup();
75  return *this;
76  }
77 
78  friend Timeval operator+(Timeval x, Timeval y) {return x += y;}
79  friend Timeval operator-(Timeval x, Timeval y) {return x -= y;}
80  friend Timeval operator%(Timeval x, Timeval y) {return x %= y;}
81 
82  friend bool operator==(Timeval x, Timeval y) {
83  return (x.sec == y.sec && x.usec == y.usec);
84  }
85 
86  friend bool operator<(Timeval x, Timeval y) {
87  if (x.sec == y.sec)
88  return (x.usec < y.usec);
89  return (x.sec < y.sec);
90  }
91 
92  friend bool operator!=(Timeval x, Timeval y) { return !(x == y); }
93  friend bool operator> (Timeval x, Timeval y) { return y < x; }
94  friend bool operator<=(Timeval x, Timeval y) { return !(y < x); }
95  friend bool operator>=(Timeval x, Timeval y) { return !(x < y); }
96 
97  friend std::ostream& operator<<(std::ostream& os, Timeval x) {
98  return os << '{' << x.sec << ',' << x.usec << '}';
99  }
100 };
101 
102 }
103 
104 #endif
cnoid::Timeval::Timeval
Timeval()
Definition: Timeval.h:26
cnoid::Timeval::operator+=
Timeval & operator+=(Timeval rhs)
Definition: Timeval.h:51
cnoid::Timeval::operator<=
friend bool operator<=(Timeval x, Timeval y)
Definition: Timeval.h:94
cnoid::Timeval::Timeval
Timeval(long long usec)
Definition: Timeval.h:41
cnoid::Timeval::operator==
friend bool operator==(Timeval x, Timeval y)
Definition: Timeval.h:82
cnoid::Timeval::operator>
friend bool operator>(Timeval x, Timeval y)
Definition: Timeval.h:93
cnoid::Timeval::operator-
friend Timeval operator-(Timeval x, Timeval y)
Definition: Timeval.h:79
cnoid::Timeval::operator+
friend Timeval operator+(Timeval x, Timeval y)
Definition: Timeval.h:78
cnoid::Timeval::operator<
friend bool operator<(Timeval x, Timeval y)
Definition: Timeval.h:86
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Timeval::Timeval
Timeval(long sec, long usec)
Definition: Timeval.h:31
cnoid::Timeval::operator%
friend Timeval operator%(Timeval x, Timeval y)
Definition: Timeval.h:80
cnoid::Timeval::operator>=
friend bool operator>=(Timeval x, Timeval y)
Definition: Timeval.h:95
cnoid::Timeval::operator<<
friend std::ostream & operator<<(std::ostream &os, Timeval x)
Definition: Timeval.h:97
cnoid::Timeval
Definition: Timeval.h:12
cnoid::Timeval::operator!=
friend bool operator!=(Timeval x, Timeval y)
Definition: Timeval.h:92
cnoid::Timeval::operator%=
Timeval & operator%=(Timeval rhs)
Definition: Timeval.h:68
cnoid::Timeval::operator-=
Timeval & operator-=(Timeval rhs)
Definition: Timeval.h:61