dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | #include "Timeval.h" |
| 29 | |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 30 | extern "C" { |
| 31 | #include <osmocom/core/timer.h> |
| 32 | } |
| 33 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 34 | using namespace std; |
| 35 | |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 36 | void Timeval::now() |
| 37 | { |
| 38 | osmo_clock_gettime(CLOCK_REALTIME, &mTimespec); |
| 39 | } |
| 40 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 41 | void Timeval::future(unsigned offset) |
| 42 | { |
| 43 | now(); |
| 44 | unsigned sec = offset/1000; |
| 45 | unsigned msec = offset%1000; |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 46 | mTimespec.tv_nsec += msec*1000*1000; |
| 47 | mTimespec.tv_sec += sec; |
| 48 | if (mTimespec.tv_nsec > 1000*1000*1000) { |
| 49 | mTimespec.tv_nsec -= 1000*1000*1000; |
| 50 | mTimespec.tv_sec += 1; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | struct timespec Timeval::timespec() const |
| 56 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 57 | return mTimespec; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | bool Timeval::passed() const |
| 62 | { |
| 63 | Timeval nowTime; |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 64 | if (nowTime.mTimespec.tv_sec < mTimespec.tv_sec) return false; |
| 65 | if (nowTime.mTimespec.tv_sec > mTimespec.tv_sec) return true; |
| 66 | if (nowTime.mTimespec.tv_nsec >= mTimespec.tv_nsec) return true; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | double Timeval::seconds() const |
| 71 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 72 | return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | long Timeval::delta(const Timeval& other) const |
| 78 | { |
| 79 | // 2^31 milliseconds is just over 4 years. |
kurtis.heimerl | 4395f4b | 2011-12-17 00:03:24 +0000 | [diff] [blame] | 80 | int32_t deltaS = other.sec() - sec(); |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 81 | int32_t deltaNs = other.nsec() - nsec(); |
| 82 | return 1000*deltaS + deltaNs/1000000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | ostream& operator<<(ostream& os, const Timeval& tv) |
| 89 | { |
| 90 | os.setf( ios::fixed, ios::floatfield ); |
| 91 | os << tv.seconds(); |
| 92 | return os; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | ostream& operator<<(ostream& os, const struct timespec& ts) |
| 97 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 98 | os << ts.tv_sec << "," << ts.tv_nsec/1000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 99 | return os; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | // vim: ts=4 sw=4 |