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"
46 #include "sim/serialize.hh"
54 * Internal time set function
56 void _set(bool monotonic);
59 static const long NSEC_PER_SEC = 1000 * 1000 * 1000;
60 static const long NSEC_PER_MSEC = 1000 * 1000;
61 static const long NSEC_PER_USEC = 1000;
64 explicit Time() { clear(); }
65 explicit Time(double sec) { operator=(sec); }
66 Time(const Time &val) : _time(val._time) { }
67 Time(uint64_t sec, uint64_t nsec) { set(sec, nsec); }
68 Time(const timeval &tv) { operator=(tv); }
69 Time(const timespec &ts) { operator=(ts); }
72 * Accessors for getting and setting the current clock
74 time_t sec() const { return _time.tv_sec; }
75 long msec() const { return _time.tv_nsec / NSEC_PER_MSEC; }
76 long usec() const { return _time.tv_nsec / NSEC_PER_USEC; }
77 long nsec() const { return _time.tv_nsec; }
79 void sec(time_t sec) { _time.tv_sec = sec; }
80 void msec(long msec) { _time.tv_nsec = msec * NSEC_PER_MSEC; }
81 void usec(long usec) { _time.tv_nsec = usec * NSEC_PER_USEC; }
82 void nsec(long nsec) { _time.tv_nsec = nsec; }
87 void clear() { memset(&_time, 0, sizeof(_time)); }
90 * Use this to set time for the purposes of time measurement (use
91 * a monotonic clock if it is available
93 void setTimer() { _set(true); }
96 * Use this to set the time to the actual current time
98 void setWallclock() { _set(false); }
101 * Set the current time
103 void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
106 * Set the current time from a value measured in Ticks
107 * @param ticks Number of ticks to convert into a time.
109 void setTick(Tick ticks);
112 * Get the current time from a value measured in Ticks
113 * @return Time value measured in Ticks.
115 Tick getTick() const;
118 operator=(const Time &other)
126 operator=(double new_time)
128 double seconds = floor(new_time);
129 sec((time_t)seconds);
130 nsec((long)((seconds - new_time) * 1e9));
135 operator=(const timeval &tv)
138 nsec(tv.tv_usec * 1000);
143 operator=(const timespec &ts)
151 * Get the time in floating point seconds
153 operator double() const
155 return (double)sec() + ((double)nsec()) * 1e-9;
159 * operators for time conversion
161 operator timespec() const { return _time; }
162 operator timeval() const
171 operator+=(const Time &other)
174 _time.tv_sec += other.sec();
175 _time.tv_nsec += other.nsec();
176 if (_time.tv_nsec > NSEC_PER_SEC) {
178 _time.tv_nsec -= NSEC_PER_SEC;
185 operator-=(const Time &other)
187 _time.tv_sec -= other.sec();
188 _time.tv_nsec -= other.nsec();
189 if (_time.tv_nsec < 0) {
191 _time.tv_nsec += NSEC_PER_SEC;
197 std::string date(const std::string &format = "") const;
198 std::string time() const;
200 void serialize(const std::string &base, CheckpointOut &cp) const;
201 void unserialize(const std::string &base, CheckpointIn &cp);
204 void sleep(const Time &time);
207 operator==(const Time &l, const Time &r)
209 return l.sec() == r.sec() && l.nsec() == r.nsec();
213 operator!=(const Time &l, const Time &r)
215 return l.sec() != r.sec() || l.nsec() != r.nsec();
219 operator<(const Time &l, const Time &r)
221 return (l.sec() < r.sec()) ||
222 (l.sec() == r.sec() && l.nsec() < r.nsec());
226 operator<=(const Time &l, const Time &r)
228 return (l.sec() < r.sec()) ||
229 (l.sec() == r.sec() && l.nsec() <= r.nsec());
233 operator>(const Time &l, const Time &r)
235 return (l.sec() > r.sec()) ||
236 (l.sec() == r.sec() && l.nsec() > r.nsec());
240 operator>=(const Time &l, const Time &r)
242 return (l.sec() > r.sec()) ||
243 (l.sec() == r.sec() && l.nsec() >= r.nsec());
247 operator+(const Time &l, const Time &r)
255 operator-(const Time &l, const Time &r)
262 inline std::ostream &
263 operator<<(std::ostream &out, const Time &time)
269 time_t mkutctime(struct tm *time);
271 #endif // __BASE_TIME_HH__