time.cc revision 7840
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; 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. 15 * 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. 27 * 28 * Authors: Nathan Binkert 29 */ 30 31#include <iostream> 32#include <sstream> 33 34#include "base/time.hh" 35#include "config/use_posix_clock.hh" 36 37using namespace std; 38 39void 40Time::_set(bool monotonic) 41{ 42#if USE_POSIX_CLOCK 43 ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time); 44#else 45 timeval tv; 46 ::gettimeofday(&tv, NULL); 47 operator=(tv); 48#endif 49} 50 51string 52Time::date(const string &format) const 53{ 54 time_t sec = this->sec(); 55 char buf[256]; 56 57 if (format.empty()) { 58#ifdef __SUNPRO_CC 59 ctime_r(&sec, buf, sizeof(buf)); 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 72string 73Time::time() const 74{ 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(); 100} 101 102void 103sleep(const Time &time) 104{ 105 timespec ts = time; 106 107#if USE_POSIX_CLOCK 108 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); 109#else 110 nanosleep(&ts, NULL); 111#endif 112} 113