dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 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.heimerl | 9123274 | 2012-07-22 05:09:00 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 33 | |
| 34 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 35 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 36 | /** A wrapper on usleep to sleep for milliseconds. */ |
| 37 | inline void msleep(long v) { usleep(v*1000); } |
| 38 | |
| 39 | |
| 40 | /** A C++ wrapper for struct timeval. */ |
| 41 | class Timeval { |
| 42 | |
| 43 | private: |
| 44 | |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 45 | struct timespec mTimespec; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 49 | /** Set the value to current time. */ |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 50 | void now(); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 51 | |
| 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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 58 | mTimespec.tv_sec = sec; |
| 59 | mTimespec.tv_nsec = usec*1000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Timeval(const struct timeval& wTimeval) |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 63 | { |
| 64 | mTimespec.tv_sec = wTimeval.tv_sec; |
| 65 | mTimespec.tv_nsec = wTimeval.tv_sec*1000; |
| 66 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 69 | Create a Timespec offset into the future. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 70 | @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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 81 | 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; } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 84 | |
| 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 Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 94 | /** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 95 | bool passed() const; |
| 96 | |
| 97 | /** Add a given number of minutes to the time. */ |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 98 | void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 99 | |
| 100 | }; |
| 101 | |
| 102 | std::ostream& operator<<(std::ostream& os, const Timeval&); |
| 103 | |
| 104 | std::ostream& operator<<(std::ostream& os, const struct timespec&); |
| 105 | |
| 106 | |
| 107 | #endif |
| 108 | // vim: ts=4 sw=4 |