2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Reinhardt
32 #ifndef __BASE_TIME_HH__
33 #define __BASE_TIME_HH__
45 #include "base/types.hh"
55 * Internal time set function
57 void _set(bool monotonic);
60 static const long NSEC_PER_SEC = 1000 * 1000 * 1000;
61 static const long NSEC_PER_MSEC = 1000 * 1000;
62 static const long NSEC_PER_USEC = 1000;
65 explicit Time() { clear(); }
66 explicit Time(double sec) { operator=(sec); }
67 Time(const Time &val) : _time(val._time) { }
68 Time(uint64_t sec, uint64_t nsec) { set(sec, nsec); }
69 Time(const timeval &tv) { operator=(tv); }
70 Time(const timespec &ts) { operator=(ts); }
73 * Accessors for getting and setting the current clock
75 time_t sec() const { return _time.tv_sec; }
76 long msec() const { return _time.tv_nsec / NSEC_PER_MSEC; }
77 long usec() const { return _time.tv_nsec / NSEC_PER_USEC; }
78 long nsec() const { return _time.tv_nsec; }
80 void sec(time_t sec) { _time.tv_sec = sec; }
81 void msec(long msec) { _time.tv_nsec = msec * NSEC_PER_MSEC; }
82 void usec(long usec) { _time.tv_nsec = usec * NSEC_PER_USEC; }
83 void nsec(long nsec) { _time.tv_nsec = nsec; }
88 void clear() { memset(&_time, 0, sizeof(_time)); }
91 * Use this to set time for the purposes of time measurement (use
92 * a monotonic clock if it is available
94 void setTimer() { _set(true); }
97 * Use this to set the time to the actual current time
99 void setWallclock() { _set(false); }
102 * Set the current time
104 void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
107 * Set the current time from a value measured in Ticks
108 * @param ticks Number of ticks to convert into a time.
110 void setTick(Tick ticks);
113 * Get the current time from a value measured in Ticks
114 * @return Time value measured in Ticks.
116 Tick getTick() const;
119 operator=(const Time &other)
127 operator=(double new_time)
129 double seconds = floor(new_time);
130 sec((time_t)seconds);
131 nsec((long)((seconds - new_time) * 1e9));
136 operator=(const timeval &tv)
139 nsec(tv.tv_usec * 1000);
144 operator=(const timespec &ts)
152 * Get the time in floating point seconds
154 operator double() const
156 return (double)sec() + ((double)nsec()) * 1e-9;
160 * operators for time conversion
162 operator timespec() const { return _time; }
163 operator timeval() const
172 operator+=(const Time &other)
175 _time.tv_sec += other.sec();
176 _time.tv_nsec += other.nsec();
177 if (_time.tv_nsec > NSEC_PER_SEC) {
179 _time.tv_nsec -= NSEC_PER_SEC;
186 operator-=(const Time &other)
188 _time.tv_sec -= other.sec();
189 _time.tv_nsec -= other.nsec();
190 if (_time.tv_nsec < 0) {
192 _time.tv_nsec += NSEC_PER_SEC;
198 std::string date(const std::string &format = "") const;
199 std::string time() const;
201 void serialize(const std::string &base, std::ostream &os);
202 void unserialize(const std::string &base, Checkpoint *cp,
203 const std::string §ion);
206 void sleep(const Time &time);
209 operator==(const Time &l, const Time &r)
211 return l.sec() == r.sec() && l.nsec() == r.nsec();
215 operator!=(const Time &l, const Time &r)
217 return l.sec() != r.sec() || l.nsec() != r.nsec();
221 operator<(const Time &l, const Time &r)
223 return (l.sec() < r.sec()) ||
224 (l.sec() == r.sec() && l.nsec() < r.nsec());
228 operator<=(const Time &l, const Time &r)
230 return (l.sec() < r.sec()) ||
231 (l.sec() == r.sec() && l.nsec() <= r.nsec());
235 operator>(const Time &l, const Time &r)
237 return (l.sec() > r.sec()) ||
238 (l.sec() == r.sec() && l.nsec() > r.nsec());
242 operator>=(const Time &l, const Time &r)
244 return (l.sec() > r.sec()) ||
245 (l.sec() == r.sec() && l.nsec() >= r.nsec());
249 operator+(const Time &l, const Time &r)
257 operator-(const Time &l, const Time &r)
264 inline std::ostream &
265 operator<<(std::ostream &out, const Time &time)
271 time_t mkutctime(struct tm *time);
273 #endif // __BASE_TIME_HH__