dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
| 5 | * |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 6 | * 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.heimerl | 9123274 | 2012-07-22 05:09:00 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | |
| 36 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 37 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | /** A wrapper on usleep to sleep for milliseconds. */ |
| 39 | inline void msleep(long v) { usleep(v*1000); } |
| 40 | |
| 41 | |
| 42 | /** A C++ wrapper for struct timeval. */ |
| 43 | class Timeval { |
| 44 | |
| 45 | private: |
| 46 | |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 47 | struct timespec mTimespec; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 51 | /** Set the value to current time. */ |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 52 | void now(); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | |
| 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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 60 | mTimespec.tv_sec = sec; |
| 61 | mTimespec.tv_nsec = usec*1000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Timeval(const struct timeval& wTimeval) |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 65 | { |
| 66 | mTimespec.tv_sec = wTimeval.tv_sec; |
| 67 | mTimespec.tv_nsec = wTimeval.tv_sec*1000; |
| 68 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 71 | Create a Timespec offset into the future. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 72 | @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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 83 | 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; } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 87 | /** Return difference from other (other-self), in ms. */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 88 | 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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 96 | /** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 97 | bool passed() const; |
| 98 | |
| 99 | /** Add a given number of minutes to the time. */ |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 100 | void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 101 | |
| 102 | }; |
| 103 | |
| 104 | std::ostream& operator<<(std::ostream& os, const Timeval&); |
| 105 | |
| 106 | std::ostream& operator<<(std::ostream& os, const struct timespec&); |
| 107 | |
| 108 | |
| 109 | #endif |
| 110 | // vim: ts=4 sw=4 |