time.cc (5546:4ffc3cafba9b) time.cc (7840:ed75cee5c793)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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;

--- 14 unchanged lines hidden (view full) ---

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.
27 *
28 * Authors: Nathan Binkert
29 */
30
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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;

--- 14 unchanged lines hidden (view full) ---

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.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#include <cctype>
32#include <cstring>
33#include <ctime>
34#include <iostream>
31#include <iostream>
35#include <string>
32#include <sstream>
36
37#include "base/time.hh"
33
34#include "base/time.hh"
35#include "config/use_posix_clock.hh"
38
39using namespace std;
40
36
37using namespace std;
38
41struct _timeval
39void
40Time::_set(bool monotonic)
42{
41{
42#if USE_POSIX_CLOCK
43 ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
44#else
43 timeval tv;
45 timeval tv;
44};
45
46double
47convert(const timeval &tv)
48{
49 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
46 ::gettimeofday(&tv, NULL);
47 operator=(tv);
48#endif
50}
51
49}
50
52Time::Time(bool set_now)
53{
54 time = new _timeval;
55 if (set_now)
56 set();
57}
58
59Time::Time(const timeval &val)
60{
61 time = new _timeval;
62 set(val);
63}
64
65Time::Time(const Time &val)
66{
67 time = new _timeval;
68 set(val.get());
69}
70
71Time::~Time()
72{
73 delete time;
74}
75
76const timeval &
77Time::get() const
78{
79 return time->tv;
80}
81
82void
83Time::set()
84{
85 ::gettimeofday(&time->tv, NULL);
86}
87
88void
89Time::set(const timeval &tv)
90{
91 memcpy(&time->tv, &tv, sizeof(timeval));
92}
93
94double
95Time::operator()() const
96{
97 return convert(get());
98}
99
100string
51string
101Time::date(string format) const
52Time::date(const string &format) const
102{
53{
103 const timeval &tv = get();
104 time_t sec = tv.tv_sec;
54 time_t sec = this->sec();
105 char buf[256];
106
107 if (format.empty()) {
108#ifdef __SUNPRO_CC
55 char buf[256];
56
57 if (format.empty()) {
58#ifdef __SUNPRO_CC
109 ctime_r(&sec, buf, 256);
59 ctime_r(&sec, buf, sizeof(buf));
110#else
111 ctime_r(&sec, buf);
112#endif
113 buf[24] = '\0';
114 return buf;
115 }
116
117 struct tm *tm = localtime(&sec);
118 strftime(buf, sizeof(buf), format.c_str(), tm);
119 return buf;
120}
121
60#else
61 ctime_r(&sec, buf);
62#endif
63 buf[24] = '\0';
64 return buf;
65 }
66
67 struct tm *tm = localtime(&sec);
68 strftime(buf, sizeof(buf), format.c_str(), tm);
69 return buf;
70}
71
122ostream &
123operator<<(ostream &out, const Time &start)
72string
73Time::time() const
124{
74{
125 out << start.date();
126 return out;
75 double time = double(*this);
76 double secs = fmod(time, 60.0);
77 double all_mins = floor(time / 60.0);
78 double mins = fmod(all_mins, 60.0);
79 double hours = floor(all_mins / 60.0);
80
81 stringstream str;
82
83 if (hours > 0.0) {
84 if (hours < 10.0)
85 str << '0';
86 str << hours << ':';
87 }
88
89 if (mins > 0.0) {
90 if (mins < 10.0)
91 str << '0';
92 str << mins << ':';
93 }
94
95 if (secs < 10.0 && !str.str().empty())
96 str << '0';
97 str << secs;
98
99 return str.str();
127}
128
100}
101
129Time
130operator-(const Time &l, const Time &r)
102void
103sleep(const Time &time)
131{
104{
132 timeval tv;
133 timersub(&l.get(), &r.get(), &tv);
134 return tv;
135}
105 timespec ts = time;
136
106
137const Time Time::start(true);
107#if USE_POSIX_CLOCK
108 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
109#else
110 nanosleep(&ts, NULL);
111#endif
112}