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 | |
| 30 | using namespace std; |
| 31 | |
| 32 | void Timeval::future(unsigned offset) |
| 33 | { |
| 34 | now(); |
| 35 | unsigned sec = offset/1000; |
| 36 | unsigned msec = offset%1000; |
| 37 | mTimeval.tv_usec += msec*1000; |
| 38 | mTimeval.tv_sec += sec; |
| 39 | if (mTimeval.tv_usec>1000000) { |
| 40 | mTimeval.tv_usec -= 1000000; |
| 41 | mTimeval.tv_sec += 1; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | struct timespec Timeval::timespec() const |
| 47 | { |
| 48 | struct timespec retVal; |
| 49 | retVal.tv_sec = mTimeval.tv_sec; |
| 50 | retVal.tv_nsec = 1000 * (long)mTimeval.tv_usec; |
| 51 | return retVal; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | bool Timeval::passed() const |
| 56 | { |
| 57 | Timeval nowTime; |
| 58 | if (nowTime.mTimeval.tv_sec < mTimeval.tv_sec) return false; |
| 59 | if (nowTime.mTimeval.tv_sec > mTimeval.tv_sec) return true; |
| 60 | if (nowTime.mTimeval.tv_usec > mTimeval.tv_usec) return true; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | double Timeval::seconds() const |
| 65 | { |
| 66 | return ((double)mTimeval.tv_sec) + 1e-6*((double)mTimeval.tv_usec); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | long Timeval::delta(const Timeval& other) const |
| 72 | { |
| 73 | // 2^31 milliseconds is just over 4 years. |
kurtis.heimerl | 4395f4b | 2011-12-17 00:03:24 +0000 | [diff] [blame^] | 74 | int32_t deltaS = other.sec() - sec(); |
| 75 | int32_t deltaUs = other.usec() - usec(); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 76 | return 1000*deltaS + deltaUs/1000; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | ostream& operator<<(ostream& os, const Timeval& tv) |
| 83 | { |
| 84 | os.setf( ios::fixed, ios::floatfield ); |
| 85 | os << tv.seconds(); |
| 86 | return os; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | ostream& operator<<(ostream& os, const struct timespec& ts) |
| 91 | { |
| 92 | os << ts.tv_sec << "," << ts.tv_nsec; |
| 93 | return os; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | // vim: ts=4 sw=4 |