time.cc revision 2665
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()) { 108400SN/A ctime_r(&sec, buf); 109400SN/A buf[24] = '\0'; 110400SN/A return buf; 1112SN/A } 1122SN/A 113400SN/A struct tm *tm = localtime(&sec); 114400SN/A strftime(buf, sizeof(buf), format.c_str(), tm); 115400SN/A return buf; 116400SN/A} 1172SN/A 118400SN/Aostream & 119400SN/Aoperator<<(ostream &out, const Time &start) 120400SN/A{ 121400SN/A out << start.date(); 122400SN/A return out; 123400SN/A} 1242SN/A 125400SN/ATime 126400SN/Aoperator-(const Time &l, const Time &r) 127400SN/A{ 128400SN/A timeval tv; 129400SN/A timersub(&l.get(), &r.get(), &tv); 130400SN/A return tv; 131400SN/A} 1322SN/A 133658SN/Aconst Time Time::start(true); 134