blob: c497864225bcdd0e71110670ed008f28d3afc98b [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under the terms of the GNU Affero Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
25
26#ifndef TIMEVAL_H
27#define TIMEVAL_H
28
29#include <stdint.h>
30#include "sys/time.h"
31#include <iostream>
kurtis.heimerl91232742012-07-22 05:09:00 +000032#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000033
34
kurtis.heimerl5a872472013-05-31 21:47:25 +000035
dburgess82c46ff2011-10-07 02:40:51 +000036/** A wrapper on usleep to sleep for milliseconds. */
37inline void msleep(long v) { usleep(v*1000); }
38
39
40/** A C++ wrapper for struct timeval. */
41class Timeval {
42
43 private:
44
45 struct timeval mTimeval;
46
47 public:
48
49 /** Set the value to gettimeofday. */
50 void now() { gettimeofday(&mTimeval,NULL); }
51
52 /** Set the value to gettimeofday plus an offset. */
53 void future(unsigned ms);
54
55 //@{
56 Timeval(unsigned sec, unsigned usec)
57 {
58 mTimeval.tv_sec = sec;
59 mTimeval.tv_usec = usec;
60 }
61
62 Timeval(const struct timeval& wTimeval)
63 :mTimeval(wTimeval)
64 {}
65
66 /**
67 Create a Timeval offset into the future.
68 @param offset milliseconds
69 */
70 Timeval(unsigned offset=0) { future(offset); }
71 //@}
72
73 /** Convert to a struct timespec. */
74 struct timespec timespec() const;
75
76 /** Return total seconds. */
77 double seconds() const;
78
79 uint32_t sec() const { return mTimeval.tv_sec; }
80 uint32_t usec() const { return mTimeval.tv_usec; }
81
82 /** Return differnce from other (other-self), in ms. */
83 long delta(const Timeval& other) const;
84
85 /** Elapsed time in ms. */
86 long elapsed() const { return delta(Timeval()); }
87
88 /** Remaining time in ms. */
89 long remaining() const { return -elapsed(); }
90
91 /** Return true if the time has passed, as per gettimeofday. */
92 bool passed() const;
93
94 /** Add a given number of minutes to the time. */
95 void addMinutes(unsigned minutes) { mTimeval.tv_sec += minutes*60; }
96
97};
98
99std::ostream& operator<<(std::ostream& os, const Timeval&);
100
101std::ostream& operator<<(std::ostream& os, const struct timespec&);
102
103
104#endif
105// vim: ts=4 sw=4