cheshirekow  v0.1.0
timeval.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (josh.bialkowski@gmail.com)
3  *
4  * This file is part of cpp-nix.
5  *
6  * cpp-nix is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * cpp-nix is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cpp-nix. If not, see <http://www.gnu.org/licenses/>.
18  */
26 #ifndef CPP_NIX_TIMEVAL_H_
27 #define CPP_NIX_TIMEVAL_H_
28 
29 #include <sys/time.h>
30 
31 timeval operator+(const timeval& a, const timeval& b);
32 timeval operator-(const timeval& a, const timeval& b);
33 
34 namespace nix {
35 
36 class Timeval;
37 
38 class Timeval : public timeval {
39  public:
40  Timeval(const timeval& other)
41  : timeval(other) {
42  }
43 
44  Timeval(int sec = 0, int usec = 0)
45  : timeval { sec, usec } {
46  }
47 
49  return (*this) = (*this) + other;
50  }
51 
53  return (*this) = (*this) - other;
54  }
55 };
56 
57 } // namespace nix
58 
59 inline bool operator==(const timeval& a, const timeval& b) {
60  return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec;
61 }
62 
63 inline bool operator!=(const timeval& a, const timeval& b) {
64  return !(a == b);
65 }
66 
67 inline bool operator<(const timeval& a, const timeval& b) {
68  if (a.tv_sec < b.tv_sec) {
69  return true;
70  } else if (a.tv_sec > b.tv_sec) {
71  return false;
72  } else {
73  return a.tv_usec < b.tv_usec;
74  }
75 }
76 
77 inline bool operator>(const timeval& a, const timeval& b) {
78  if (a.tv_sec > b.tv_sec) {
79  return true;
80  } else if (a.tv_sec < b.tv_sec) {
81  return false;
82  } else {
83  return a.tv_usec > b.tv_usec;
84  }
85 }
86 
87 inline bool operator<=(const timeval& a, const timeval& b) {
88  if (a.tv_sec < b.tv_sec) {
89  return true;
90  } else if (a.tv_sec > b.tv_sec) {
91  return false;
92  } else {
93  return a.tv_usec <= b.tv_usec;
94  }
95 }
96 
97 inline bool operator>=(const timeval& a, const timeval& b) {
98  if (a.tv_sec > b.tv_sec) {
99  return true;
100  } else if (a.tv_sec < b.tv_sec) {
101  return false;
102  } else {
103  return a.tv_usec >= b.tv_usec;
104  }
105 }
106 
107 #endif // TIMEVAL_H_
Timeval(const timeval &other)
Definition: timeval.h:40
Timeval & operator+=(const Timeval &other)
Definition: timeval.h:48
bool operator!=(const timeval &a, const timeval &b)
Definition: timeval.h:63
bool operator==(const timeval &a, const timeval &b)
Definition: timeval.h:59
timeval operator-(const timeval &a, const timeval &b)
bool operator<(const timeval &a, const timeval &b)
Definition: timeval.h:67
bool operator>=(const timeval &a, const timeval &b)
Definition: timeval.h:97
Timeval & operator-=(const Timeval &other)
Definition: timeval.h:52
bool operator<=(const timeval &a, const timeval &b)
Definition: timeval.h:87
bool operator>(const timeval &a, const timeval &b)
Definition: timeval.h:77
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
timeval operator+(const timeval &a, const timeval &b)
Timeval(int sec=0, int usec=0)
Definition: timeval.h:44