blob: cfd01cf2e937f5c91098d9b01ab1b590662a0a79 [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+
dburgess82c46ff2011-10-07 02:40:51 +00005*
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 Pedrol47031402018-12-12 17:06:06 +010031extern "C" {
32#include <osmocom/core/timer.h>
33}
34
dburgess82c46ff2011-10-07 02:40:51 +000035using namespace std;
36
Pau Espin Pedrol47031402018-12-12 17:06:06 +010037void Timeval::now()
38{
39 osmo_clock_gettime(CLOCK_REALTIME, &mTimespec);
40}
41
dburgess82c46ff2011-10-07 02:40:51 +000042void Timeval::future(unsigned offset)
43{
44 now();
45 unsigned sec = offset/1000;
46 unsigned msec = offset%1000;
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010047 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;
dburgess82c46ff2011-10-07 02:40:51 +000052 }
53}
54
55
56struct timespec Timeval::timespec() const
57{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010058 return mTimespec;
dburgess82c46ff2011-10-07 02:40:51 +000059}
60
61
62bool Timeval::passed() const
63{
64 Timeval nowTime;
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010065 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;
dburgess82c46ff2011-10-07 02:40:51 +000068 return false;
69}
70
71double Timeval::seconds() const
72{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010073 return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec);
dburgess82c46ff2011-10-07 02:40:51 +000074}
75
76
77
78long Timeval::delta(const Timeval& other) const
79{
80 // 2^31 milliseconds is just over 4 years.
kurtis.heimerl4395f4b2011-12-17 00:03:24 +000081 int32_t deltaS = other.sec() - sec();
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010082 int32_t deltaNs = other.nsec() - nsec();
83 return 1000*deltaS + deltaNs/1000000;
dburgess82c46ff2011-10-07 02:40:51 +000084}
Pau Espin Pedrolbdb970e2019-07-22 12:03:39 +020085
dburgess82c46ff2011-10-07 02:40:51 +000086
87
88
89ostream& operator<<(ostream& os, const Timeval& tv)
90{
Harald Welte2a3d8ba2019-07-21 11:42:13 +020091 ios_base::fmtflags flags_backup = os.setf( ios::fixed, ios::floatfield );
dburgess82c46ff2011-10-07 02:40:51 +000092 os << tv.seconds();
Harald Welte2a3d8ba2019-07-21 11:42:13 +020093 os.flags( flags_backup );
dburgess82c46ff2011-10-07 02:40:51 +000094 return os;
95}
96
97
98ostream& operator<<(ostream& os, const struct timespec& ts)
99{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +0100100 os << ts.tv_sec << "," << ts.tv_nsec/1000;
dburgess82c46ff2011-10-07 02:40:51 +0000101 return os;
102}
103
104
105
106// vim: ts=4 sw=4