cheshirekow  v0.1.0
timespec.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_TIMESPEC_H_
27 #define CPP_NIX_TIMESPEC_H_
28 
29 #include <sys/time.h>
30 
31 timespec operator+(const timespec& a, const timespec& b);
32 timespec operator-(const timespec& a, const timespec& b);
33 
34 namespace nix {
35 
36 class Timespec;
37 
39 class Timespec : public timespec {
40  public:
41  Timespec(const timespec& other)
42  : timespec(other) {
43  }
44 
45  Timespec(int sec = 0, long int nsec = 0)
46  : timespec { sec, nsec } {
47  }
48 
50  return (*this) = (*this) + other;
51  }
52 
54  return (*this) = (*this) - other;
55  }
56 };
57 
58 } // namespace nix
59 
60 inline bool operator==(const timespec& a, const timespec& b) {
61  return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
62 }
63 
64 inline bool operator!=(const timespec& a, const timespec& b) {
65  return !(a == b);
66 }
67 
68 inline bool operator<(const timespec& a, const timespec& b) {
69  if (a.tv_sec < b.tv_sec) {
70  return true;
71  } else if (a.tv_sec > b.tv_sec) {
72  return false;
73  } else {
74  return a.tv_nsec < b.tv_nsec;
75  }
76 }
77 
78 inline bool operator>(const timespec& a, const timespec& b) {
79  if (a.tv_sec > b.tv_sec) {
80  return true;
81  } else if (a.tv_sec < b.tv_sec) {
82  return false;
83  } else {
84  return a.tv_nsec > b.tv_nsec;
85  }
86 }
87 
88 inline bool operator<=(const timespec& a, const timespec& b) {
89  if (a.tv_sec < b.tv_sec) {
90  return true;
91  } else if (a.tv_sec > b.tv_sec) {
92  return false;
93  } else {
94  return a.tv_nsec <= b.tv_nsec;
95  }
96 }
97 
98 inline bool operator>=(const timespec& a, const timespec& b) {
99  if (a.tv_sec > b.tv_sec) {
100  return true;
101  } else if (a.tv_sec < b.tv_sec) {
102  return false;
103  } else {
104  return a.tv_nsec >= b.tv_nsec;
105  }
106 }
107 
108 
109 #endif // TIMESPEC_H_
just a c timeval
Definition: timespec.h:39
Timespec & operator+=(const Timespec &other)
Definition: timespec.h:49
Timespec & operator-=(const Timespec &other)
Definition: timespec.h:53
Timespec(int sec=0, long int nsec=0)
Definition: timespec.h:45
timespec operator+(const timespec &a, const timespec &b)
timespec operator-(const timespec &a, const timespec &b)
bool operator>(const timespec &a, const timespec &b)
Definition: timespec.h:78
bool operator>=(const timespec &a, const timespec &b)
Definition: timespec.h:98
bool operator<(const timespec &a, const timespec &b)
Definition: timespec.h:68
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
bool operator<=(const timespec &a, const timespec &b)
Definition: timespec.h:88
bool operator!=(const timespec &a, const timespec &b)
Definition: timespec.h:64
Timespec(const timespec &other)
Definition: timespec.h:41
bool operator==(const timespec &a, const timespec &b)
Definition: timespec.h:60