blob: 18ea4b4e7b630551950ad1f47f4fff9433410da5 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
5*
dburgess82c46ff2011-10-07 02:40:51 +00006* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
28#ifndef TIMEVAL_H
29#define TIMEVAL_H
30
31#include <stdint.h>
32#include "sys/time.h"
33#include <iostream>
kurtis.heimerl91232742012-07-22 05:09:00 +000034#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000035
36
kurtis.heimerl5a872472013-05-31 21:47:25 +000037
dburgess82c46ff2011-10-07 02:40:51 +000038/** A wrapper on usleep to sleep for milliseconds. */
39inline void msleep(long v) { usleep(v*1000); }
40
41
42/** A C++ wrapper for struct timeval. */
43class Timeval {
44
45 private:
46
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010047 struct timespec mTimespec;
dburgess82c46ff2011-10-07 02:40:51 +000048
49 public:
50
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010051 /** Set the value to current time. */
Pau Espin Pedrol47031402018-12-12 17:06:06 +010052 void now();
dburgess82c46ff2011-10-07 02:40:51 +000053
54 /** Set the value to gettimeofday plus an offset. */
55 void future(unsigned ms);
56
57 //@{
58 Timeval(unsigned sec, unsigned usec)
59 {
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010060 mTimespec.tv_sec = sec;
61 mTimespec.tv_nsec = usec*1000;
dburgess82c46ff2011-10-07 02:40:51 +000062 }
63
64 Timeval(const struct timeval& wTimeval)
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010065 {
66 mTimespec.tv_sec = wTimeval.tv_sec;
67 mTimespec.tv_nsec = wTimeval.tv_sec*1000;
68 }
dburgess82c46ff2011-10-07 02:40:51 +000069
70 /**
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010071 Create a Timespec offset into the future.
dburgess82c46ff2011-10-07 02:40:51 +000072 @param offset milliseconds
73 */
74 Timeval(unsigned offset=0) { future(offset); }
75 //@}
76
77 /** Convert to a struct timespec. */
78 struct timespec timespec() const;
79
80 /** Return total seconds. */
81 double seconds() const;
82
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010083 uint32_t sec() const { return mTimespec.tv_sec; }
84 uint32_t usec() const { return mTimespec.tv_nsec / 1000; }
85 uint32_t nsec() const { return mTimespec.tv_nsec; }
dburgess82c46ff2011-10-07 02:40:51 +000086
Martin Hauke066fd042019-10-13 19:08:00 +020087 /** Return difference from other (other-self), in ms. */
dburgess82c46ff2011-10-07 02:40:51 +000088 long delta(const Timeval& other) const;
89
90 /** Elapsed time in ms. */
91 long elapsed() const { return delta(Timeval()); }
92
93 /** Remaining time in ms. */
94 long remaining() const { return -elapsed(); }
95
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010096 /** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */
dburgess82c46ff2011-10-07 02:40:51 +000097 bool passed() const;
98
99 /** Add a given number of minutes to the time. */
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +0100100 void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; }
dburgess82c46ff2011-10-07 02:40:51 +0000101
102};
103
104std::ostream& operator<<(std::ostream& os, const Timeval&);
105
106std::ostream& operator<<(std::ostream& os, const struct timespec&);
107
108
109#endif
110// vim: ts=4 sw=4