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+ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 5 | * |
| 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 | |
| 29 | #include "Timeval.h" |
| 30 | |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 31 | extern "C" { |
| 32 | #include <osmocom/core/timer.h> |
| 33 | } |
| 34 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | using namespace std; |
| 36 | |
Pau Espin Pedrol | 4703140 | 2018-12-12 17:06:06 +0100 | [diff] [blame] | 37 | void Timeval::now() |
| 38 | { |
| 39 | osmo_clock_gettime(CLOCK_REALTIME, &mTimespec); |
| 40 | } |
| 41 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 42 | void Timeval::future(unsigned offset) |
| 43 | { |
| 44 | now(); |
| 45 | unsigned sec = offset/1000; |
| 46 | unsigned msec = offset%1000; |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 47 | mTimespec.tv_nsec += msec*1000*1000; |
| 48 | mTimespec.tv_sec += sec; |
| 49 | if (mTimespec.tv_nsec > 1000*1000*1000) { |
| 50 | mTimespec.tv_nsec -= 1000*1000*1000; |
| 51 | mTimespec.tv_sec += 1; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | struct timespec Timeval::timespec() const |
| 57 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 58 | return mTimespec; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | bool Timeval::passed() const |
| 63 | { |
| 64 | Timeval nowTime; |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 65 | if (nowTime.mTimespec.tv_sec < mTimespec.tv_sec) return false; |
| 66 | if (nowTime.mTimespec.tv_sec > mTimespec.tv_sec) return true; |
| 67 | if (nowTime.mTimespec.tv_nsec >= mTimespec.tv_nsec) return true; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | double Timeval::seconds() const |
| 72 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 73 | return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | long Timeval::delta(const Timeval& other) const |
| 79 | { |
| 80 | // 2^31 milliseconds is just over 4 years. |
kurtis.heimerl | 4395f4b | 2011-12-17 00:03:24 +0000 | [diff] [blame] | 81 | int32_t deltaS = other.sec() - sec(); |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 82 | int32_t deltaNs = other.nsec() - nsec(); |
| 83 | return 1000*deltaS + deltaNs/1000000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 84 | } |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 85 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | |
| 89 | ostream& operator<<(ostream& os, const Timeval& tv) |
| 90 | { |
Harald Welte | 2a3d8ba | 2019-07-21 11:42:13 +0200 | [diff] [blame] | 91 | ios_base::fmtflags flags_backup = os.setf( ios::fixed, ios::floatfield ); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 92 | os << tv.seconds(); |
Harald Welte | 2a3d8ba | 2019-07-21 11:42:13 +0200 | [diff] [blame] | 93 | os.flags( flags_backup ); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 94 | return os; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | ostream& operator<<(ostream& os, const struct timespec& ts) |
| 99 | { |
Pau Espin Pedrol | 0646b3c | 2018-12-12 15:58:24 +0100 | [diff] [blame] | 100 | os << ts.tv_sec << "," << ts.tv_nsec/1000; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 101 | return os; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | // vim: ts=4 sw=4 |