time.cc revision 3918
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#include <sys/types.h>
322SN/A#include <sys/time.h>
332SN/A#include <time.h>
342SN/A#include <iostream>
35400SN/A#include <string>
362SN/A
37400SN/A#include "base/time.hh"
382SN/A
392SN/Ausing namespace std;
402SN/A
41400SN/Astruct _timeval
422SN/A{
43400SN/A    timeval tv;
44400SN/A};
452SN/A
46400SN/Adouble
47400SN/Aconvert(const timeval &tv)
48400SN/A{
49400SN/A    return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
50400SN/A}
51400SN/A
52658SN/ATime::Time(bool set_now)
53400SN/A{
54400SN/A    time = new _timeval;
55658SN/A    if (set_now)
56658SN/A        set();
57400SN/A}
58400SN/A
59400SN/ATime::Time(const timeval &val)
60400SN/A{
61400SN/A    time = new _timeval;
62400SN/A    set(val);
63400SN/A}
64400SN/A
65400SN/ATime::Time(const Time &val)
66400SN/A{
67400SN/A    time = new _timeval;
68400SN/A    set(val.get());
69400SN/A}
70400SN/A
71400SN/ATime::~Time()
72400SN/A{
73400SN/A    delete time;
74400SN/A}
75400SN/A
76400SN/Aconst timeval &
77400SN/ATime::get() const
78400SN/A{
79400SN/A    return time->tv;
80400SN/A}
81400SN/A
82400SN/Avoid
83658SN/ATime::set()
84658SN/A{
85658SN/A    ::gettimeofday(&time->tv, NULL);
86658SN/A}
87658SN/A
88658SN/Avoid
89400SN/ATime::set(const timeval &tv)
90400SN/A{
91400SN/A    memcpy(&time->tv, &tv, sizeof(timeval));
92400SN/A}
93400SN/A
94400SN/Adouble
95400SN/ATime::operator()() const
96400SN/A{
97400SN/A    return convert(get());
98400SN/A}
99400SN/A
100400SN/Astring
101400SN/ATime::date(string format) const
102400SN/A{
103400SN/A    const timeval &tv = get();
104400SN/A    time_t sec = tv.tv_sec;
105400SN/A    char buf[256];
106400SN/A
107400SN/A    if (format.empty()) {
1083918Ssaidi@eecs.umich.edu#ifdef __SUNPRO_CC
1093918Ssaidi@eecs.umich.edu        ctime_r(&sec, buf, 256);
1103918Ssaidi@eecs.umich.edu#else
111400SN/A        ctime_r(&sec, buf);
1123918Ssaidi@eecs.umich.edu#endif
113400SN/A        buf[24] = '\0';
114400SN/A        return buf;
1152SN/A    }
1162SN/A
117400SN/A    struct tm *tm = localtime(&sec);
118400SN/A    strftime(buf, sizeof(buf), format.c_str(), tm);
119400SN/A    return buf;
120400SN/A}
1212SN/A
122400SN/Aostream &
123400SN/Aoperator<<(ostream &out, const Time &start)
124400SN/A{
125400SN/A    out << start.date();
126400SN/A    return out;
127400SN/A}
1282SN/A
129400SN/ATime
130400SN/Aoperator-(const Time &l, const Time &r)
131400SN/A{
132400SN/A    timeval tv;
133400SN/A    timersub(&l.get(), &r.get(), &tv);
134400SN/A    return tv;
135400SN/A}
1362SN/A
137658SN/Aconst Time Time::start(true);
138