blob: 7c6a6f697d7530fd4b3d7d3285cf9bff5853696f [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under the terms of the GNU Affero Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
25
26#ifndef TIMEVAL_H
27#define TIMEVAL_H
28
29#include <stdint.h>
30#include "sys/time.h"
31#include <iostream>
kurtis.heimerl91232742012-07-22 05:09:00 +000032#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000033
34
kurtis.heimerl5a872472013-05-31 21:47:25 +000035
dburgess82c46ff2011-10-07 02:40:51 +000036/** A wrapper on usleep to sleep for milliseconds. */
37inline void msleep(long v) { usleep(v*1000); }
38
39
40/** A C++ wrapper for struct timeval. */
41class Timeval {
42
43 private:
44
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010045 struct timespec mTimespec;
dburgess82c46ff2011-10-07 02:40:51 +000046
47 public:
48
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010049 /** Set the value to current time. */
Pau Espin Pedrol47031402018-12-12 17:06:06 +010050 void now();
dburgess82c46ff2011-10-07 02:40:51 +000051
52 /** Set the value to gettimeofday plus an offset. */
53 void future(unsigned ms);
54
55 //@{
56 Timeval(unsigned sec, unsigned usec)
57 {
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010058 mTimespec.tv_sec = sec;
59 mTimespec.tv_nsec = usec*1000;
dburgess82c46ff2011-10-07 02:40:51 +000060 }
61
62 Timeval(const struct timeval& wTimeval)
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010063 {
64 mTimespec.tv_sec = wTimeval.tv_sec;
65 mTimespec.tv_nsec = wTimeval.tv_sec*1000;
66 }
dburgess82c46ff2011-10-07 02:40:51 +000067
68 /**
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010069 Create a Timespec offset into the future.
dburgess82c46ff2011-10-07 02:40:51 +000070 @param offset milliseconds
71 */
72 Timeval(unsigned offset=0) { future(offset); }
73 //@}
74
75 /** Convert to a struct timespec. */
76 struct timespec timespec() const;
77
78 /** Return total seconds. */
79 double seconds() const;
80
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010081 uint32_t sec() const { return mTimespec.tv_sec; }
82 uint32_t usec() const { return mTimespec.tv_nsec / 1000; }
83 uint32_t nsec() const { return mTimespec.tv_nsec; }
dburgess82c46ff2011-10-07 02:40:51 +000084
85 /** Return differnce from other (other-self), in ms. */
86 long delta(const Timeval& other) const;
87
88 /** Elapsed time in ms. */
89 long elapsed() const { return delta(Timeval()); }
90
91 /** Remaining time in ms. */
92 long remaining() const { return -elapsed(); }
93
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010094 /** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */
dburgess82c46ff2011-10-07 02:40:51 +000095 bool passed() const;
96
97 /** Add a given number of minutes to the time. */
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010098 void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; }
dburgess82c46ff2011-10-07 02:40:51 +000099
100};
101
102std::ostream& operator<<(std::ostream& os, const Timeval&);
103
104std::ostream& operator<<(std::ostream& os, const struct timespec&);
105
106
107#endif
108// vim: ts=4 sw=4