blob: fd1bcda70cb15b76f6653b16cbef6522e9f1d6fe [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
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 Pedrol47031402018-12-12 17:06:06 +010030extern "C" {
31#include <osmocom/core/timer.h>
32}
33
dburgess82c46ff2011-10-07 02:40:51 +000034using namespace std;
35
Pau Espin Pedrol47031402018-12-12 17:06:06 +010036void Timeval::now()
37{
38 osmo_clock_gettime(CLOCK_REALTIME, &mTimespec);
39}
40
dburgess82c46ff2011-10-07 02:40:51 +000041void Timeval::future(unsigned offset)
42{
43 now();
44 unsigned sec = offset/1000;
45 unsigned msec = offset%1000;
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010046 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;
dburgess82c46ff2011-10-07 02:40:51 +000051 }
52}
53
54
55struct timespec Timeval::timespec() const
56{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010057 return mTimespec;
dburgess82c46ff2011-10-07 02:40:51 +000058}
59
60
61bool Timeval::passed() const
62{
63 Timeval nowTime;
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010064 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;
dburgess82c46ff2011-10-07 02:40:51 +000067 return false;
68}
69
70double Timeval::seconds() const
71{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010072 return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec);
dburgess82c46ff2011-10-07 02:40:51 +000073}
74
75
76
77long Timeval::delta(const Timeval& other) const
78{
79 // 2^31 milliseconds is just over 4 years.
kurtis.heimerl4395f4b2011-12-17 00:03:24 +000080 int32_t deltaS = other.sec() - sec();
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010081 int32_t deltaNs = other.nsec() - nsec();
82 return 1000*deltaS + deltaNs/1000000;
dburgess82c46ff2011-10-07 02:40:51 +000083}
84
85
86
87
88ostream& operator<<(ostream& os, const Timeval& tv)
89{
90 os.setf( ios::fixed, ios::floatfield );
91 os << tv.seconds();
92 return os;
93}
94
95
96ostream& operator<<(ostream& os, const struct timespec& ts)
97{
Pau Espin Pedrol0646b3c2018-12-12 15:58:24 +010098 os << ts.tv_sec << "," << ts.tv_nsec/1000;
dburgess82c46ff2011-10-07 02:40:51 +000099 return os;
100}
101
102
103
104// vim: ts=4 sw=4