cheshirekow  v0.1.0
timespec.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks 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  * mpblocks 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 mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
25 #ifndef MPBLOCKS_UTIL_TIMESPEC_H_
26 #define MPBLOCKS_UTIL_TIMESPEC_H_
27 
28 #include <ctime>
29 
30 timespec operator+(const timespec& a, const timespec& b);
31 timespec operator-(const timespec& a, const timespec& b);
32 
33 namespace mpblocks {
34 namespace utility {
35 
37 class Timespec : public timespec {
38  public:
39  Timespec(const timespec& other) : timespec(other) {}
40 
41  Timespec(int sec = 0, long int nsec = 0) : timespec{sec, nsec} {}
42 
44  return (*this) = (*this) + other;
45  }
46 
48  return (*this) = (*this) - other;
49  }
50 
51  double Milliseconds() const{
52  return tv_sec + tv_nsec / 1e6;
53  }
54 };
55 
56 inline bool operator==(const timespec& a, const timespec& b) {
57  return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
58 }
59 
60 inline bool operator!=(const timespec& a, const timespec& b) {
61  return !(a == b);
62 }
63 
64 inline bool operator<(const timespec& a, const timespec& b) {
65  if (a.tv_sec < b.tv_sec) {
66  return true;
67  } else if (a.tv_sec > b.tv_sec) {
68  return false;
69  } else {
70  return a.tv_nsec < b.tv_nsec;
71  }
72 }
73 
74 inline bool operator>(const timespec& a, const timespec& b) {
75  if (a.tv_sec > b.tv_sec) {
76  return true;
77  } else if (a.tv_sec < b.tv_sec) {
78  return false;
79  } else {
80  return a.tv_nsec > b.tv_nsec;
81  }
82 }
83 
84 inline bool operator<=(const timespec& a, const timespec& b) {
85  if (a.tv_sec < b.tv_sec) {
86  return true;
87  } else if (a.tv_sec > b.tv_sec) {
88  return false;
89  } else {
90  return a.tv_nsec <= b.tv_nsec;
91  }
92 }
93 
94 inline bool operator>=(const timespec& a, const timespec& b) {
95  if (a.tv_sec > b.tv_sec) {
96  return true;
97  } else if (a.tv_sec < b.tv_sec) {
98  return false;
99  } else {
100  return a.tv_nsec >= b.tv_nsec;
101  }
102 }
103 
104 Timespec operator+(const Timespec& a, const Timespec& b);
105 Timespec operator-(const Timespec& a, const Timespec& b);
106 
107 } // namespace utility
108 } // namespace mpblocks
109 
110 #endif // INCLUDE_MPBLOCKS_UTIL_TIMESPEC_H_
bool operator<(const timespec &a, const timespec &b)
Definition: timespec.h:64
Timespec operator-(const Timespec &a, const Timespec &b)
bool operator!=(const timespec &a, const timespec &b)
Definition: timespec.h:60
timespec operator+(const timespec &a, const timespec &b)
bool operator<=(const timespec &a, const timespec &b)
Definition: timespec.h:84
bool operator>=(const timespec &a, const timespec &b)
Definition: timespec.h:94
bool operator>(const timespec &a, const timespec &b)
Definition: timespec.h:74
timespec operator-(const timespec &a, const timespec &b)
Timespec(int sec=0, long int nsec=0)
Definition: timespec.h:41
Timespec(const timespec &other)
Definition: timespec.h:39
Timespec operator+(const Timespec &a, const Timespec &b)
Timespec & operator+=(const Timespec &other)
Definition: timespec.h:43
Timespec & operator-=(const Timespec &other)
Definition: timespec.h:47
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
bool operator==(const timespec &a, const timespec &b)
Definition: timespec.h:56
double Milliseconds() const
Definition: timespec.h:51
just a c timespec with arithmetic operators
Definition: timespec.h:37